Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

tests/framework: Add GetTestFile #2136

Merged
merged 3 commits into from
Dec 3, 2020
Merged

Conversation

eduser25
Copy link
Contributor

@eduser25 eduser25 commented Dec 2, 2020

We might be interested in near future to save test results, data or logs on disk.
This commit introduces a helper for tests to prefix filenames and create test folder
upon use.

  • Added a random uin64 identifier for a test, which will be used
    to identify a certain test run. Note these are random, but not unique.

  • GetTestFile is fed with a filename and returns a path to a file
    inside test folder. If a folder for current test does not exist, it is
    created in the process.

  • Tests [X]

  • Does this change contain code from or inspired by another project? If so, did you notify the maintainers and provide attribution?
    No

- Added a random uin32 identifier for a test, which will be used
to identify a certain test run. Note these are random, but not unique.
- GetTestFile is fed with a filename and returns a path to a file
inside test folder. If a folder for current test does not exist, it is
created in the process.

Signed-off-by: Eduard Serra <eduser25@gmail.com>
@eduser25 eduser25 requested a review from a team as a code owner December 2, 2020 23:22
@@ -215,8 +242,14 @@ func (td *OsmTestData) AreRegistryCredsPresent() bool {
// Called by Gingkgo BeforeEach
func (td *OsmTestData) InitTestData(t GinkgoTInterface) error {
td.T = t
r, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
Copy link
Contributor

Choose a reason for hiding this comment

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

How come we're not using time to seed the random number generator here? https://stackoverflow.com/a/12321192

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rand.crypto is supposed to be the safest, the reader from crypto is fed entropy from various sources I think.
In fact I was forced to change it to rand.crypto as using math.rand actually triggers the linter....

Copy link
Member

Choose a reason for hiding this comment

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

For non crypto rand generations, using math.Rand is perfectly fine, you just need to silence the linter false alarms with // #nosec annotation

Copy link
Contributor

Choose a reason for hiding this comment

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

@eduser25 - what is the linter error that you're getting?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shashankram, I see. the linter doesn't actually tell you it is fine for use for non-crypto.
@michelleN :
In some test file...

import (
	"fmt"
	"math/rand"

...

a := rand.Float32()
fmt.Printf("%i", a)
...
eserra@edu-linux ~/src/osm/tests (metrics) $ golangci-lint run --out-format=github-actions --timeout 5m
::error file=framework/common_profile.go,line=120,col=7::G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec)

Copy link
Member

@shashankram shashankram Dec 3, 2020

Choose a reason for hiding this comment

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

@shashankram, I see. the linter doesn't actually tell you it is fine for use for non-crypto.

That's because the linter can't know whether you are using the api for crypto purpose. Doesn't mean you need to use crypto api for non crypto operations, you just need to suppress the false alarm, no other alternative with gosec linter enabled.

Copy link
Contributor

@draychev draychev left a comment

Choose a reason for hiding this comment

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

What's the use case for this?

@shashankram
Copy link
Member

shashankram commented Dec 3, 2020

What's the use case for this?

Although the PR description has a one liner Precursor to be able to save test data on disk., it would be nice to add a bit more detail on what exactly this means.

@eduser25
Copy link
Contributor Author

eduser25 commented Dec 3, 2020

What's the use case for this?

Although the PR description has a one liner Precursor to be able to save test data on disk., it would be nice to add more a bit more detail on what exactly this means.

Basically tests saving some results, logs or test data on file/disk. Honestly thought it'd be more obvious, I'll add more purpose to description.

@eduser25 eduser25 merged commit 16be519 into openservicemesh:main Dec 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants