Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3. Random ID generation #3

Open
wants to merge 3 commits into
base: 2-test-context
Choose a base branch
from
Open

3. Random ID generation #3

wants to merge 3 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Jul 17, 2024

This PR is based on #2 and adds the functions for random number generation from opentofu/opentofu#1731

@ghost ghost force-pushed the 3-test-random branch from 98e173e to ce16151 Compare August 12, 2024 12:07
@ghost ghost changed the title Random ID generation 3. Random ID generation Aug 12, 2024
@ghost ghost changed the base branch from main to 2-test-context August 21, 2024 18:36

// IDPrefix returns a random identifier with a given prefix. The prefix length does not count towards the
// length.
func IDPrefix(prefix string, length uint, characterSpace CharacterRange) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like extra cruft that's overkill.

// randomness. This function guarantees that when queried in order, the values are always the same as long as the name
// of the test doesn't change.
func DeterministicID(t *testing.T, length uint, characterSpace CharacterRange) string {
return IDFromSource(DeterministicSource(t), length, characterSpace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have existing scenarios that this would be used for?


// DeterministicSource produces a rand.Rand that is deterministic based on the provided test name. It will always
// supply the same values as long as the test name doesn't change.
func DeterministicSource(t *testing.T) *rand.Rand {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm honestly not a fan of this function and don't see where it would be useful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be useful for some tests, but it may lead to confusion when you change the function name. The test will not pass and it could be hard to say why from the first glance.

Also, code under test should provide consistent results so it may be not as universal as we think. I had hard times changing all the iterations over maps in my mock implementation for tofu test.

Do we have a real-world example outside of this PR?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main use case is that if something fails in CI with a random number, it can be harder to reproduce locally if said random number wasn't deterministic. The certificate generation PR (#5) makes use of this function. (This still needs some work to be fully deterministic.)

@cam72cam
Copy link
Member

Overall, I think that the charset idea is useful, but a lot of the other stuff introduced is overkill.

@ghost
Copy link
Author

ghost commented Aug 22, 2024

@cam72cam much of this is based on existing randomness needs in OpenTofu, see the corresponding refactoring PR opentofu/opentofu#1731

@cam72cam
Copy link
Member

Looking at the linked PR does not change my above comments. The deterministic from t.Name is not used and IMO not a good idea. The Prefix case is used, but does not buy us anything for 2 simple use cases.

@abstractionfactory abstractionfactory marked this pull request as draft January 22, 2025 10:51
@abstractionfactory abstractionfactory force-pushed the 2-test-context branch 3 times, most recently from f9cbaec to 579f79e Compare January 22, 2025 11:51
@abstractionfactory abstractionfactory marked this pull request as ready for review January 22, 2025 12:54
Janos Bonic and others added 3 commits January 22, 2025 14:05
Note: this commit contains code based on OpenTofu code and therefore, changes the license headers

Signed-off-by: Janos Bonic <[email protected]>
Co-authored-by: AbstractionFactory <[email protected]>
Signed-off-by: Janos <[email protected]>
Co-authored-by: AbstractionFactory <[email protected]>
Signed-off-by: AbstractionFactory <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants