Skip to content

Commit

Permalink
cmdutil: simplify SeedArgFor to take a string instead of a namer
Browse files Browse the repository at this point in the history
Simplify the previous commit so that SeedArgFor() takes strings
for the image type and distribution instead of the `namer`
interface. This makes the code a little bit easier to follow but
now the `distro.Distro` and `distro.ImageType` cannot be directly
passed into the helper anymore so it's a tradeoff.

Ideally it would just take `distro.{Distro,ImageType}` but for
that we would need an easy way to construct fake objects of
these types in our tests which there (currently) is not (AFAICT).

So settle with this approach as a compromise for now.
  • Loading branch information
mvo5 committed May 14, 2024
1 parent 4df0926 commit e53c3c1
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func makeManifest(
if config.Blueprint != nil {
bp = blueprint.Blueprint(*config.Blueprint)
}
seedArg, err := cmdutil.SeedArgFor(config, imgType, distribution, archName)
seedArg, err := cmdutil.SeedArgFor(config, imgType.Name(), distribution.Name(), archName)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gen-manifests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func makeManifestJob(
cacheDir := filepath.Join(cacheRoot, archName+distribution.Name())

// ensure that each file has a unique seed based on filename
seedArg, err := cmdutil.SeedArgFor(bc, imgType, distribution, archName)
seedArg, err := cmdutil.SeedArgFor(bc, imgType.Name(), distribution.Name(), archName)
if err != nil {
panic(err)
}
Expand Down
10 changes: 3 additions & 7 deletions internal/cmdutil/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,16 @@ func newRNGSeed() (int64, error) {
return randSeed.Int64(), nil
}

type namer interface {
Name() string
}

func SeedArgFor(bc *buildconfig.BuildConfig, imgType namer, distribution namer, archName string) (int64, error) {
func SeedArgFor(bc *buildconfig.BuildConfig, imgTypeName, distributionName, archName string) (int64, error) {
rngSeed, err := newRNGSeed()
if err != nil {
return 0, err
}

h := fnv.New64()
h.Write([]byte(distribution.Name()))
h.Write([]byte(distributionName))
h.Write([]byte(archName))
h.Write([]byte(imgType.Name()))
h.Write([]byte(imgTypeName))
h.Write([]byte(bc.Name))

return rngSeed + int64(h.Sum64()), nil
Expand Down
6 changes: 3 additions & 3 deletions internal/cmdutil/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func TestSeedArgFor(t *testing.T) {
t.Setenv(cmdutil.RNG_SEED_ENV_KEY, "1234")

for _, tc := range []struct {
bcName, imgType, distro, archName string
expectedSeed int64
bcName, imgTypeName, distroName, archName string
expectedSeed int64
}{
{"bcName", "fakeImgType", "fakeDistro", "x86_64", 9170052743323116054},
{"bcName1", "fakeImgType", "fakeDistro", "x86_64", -7134826073208782961},
Expand All @@ -62,7 +62,7 @@ func TestSeedArgFor(t *testing.T) {
{"bcName", "fakeImgType", "fakeDistro1", "aarch64", 47752167762999679},
} {
bc := &buildconfig.BuildConfig{Name: tc.bcName}
seedArg, err := cmdutil.SeedArgFor(bc, fakeNamer{tc.imgType}, fakeNamer{tc.distro}, tc.archName)
seedArg, err := cmdutil.SeedArgFor(bc, tc.imgTypeName, tc.distroName, tc.archName)
assert.NoError(t, err)
assert.Equal(t, tc.expectedSeed, seedArg)
}
Expand Down

0 comments on commit e53c3c1

Please sign in to comment.