From b6122a413ba8f3f2bf136b7325caf9f68b76778c Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Tue, 2 Jun 2020 20:48:15 -0400 Subject: [PATCH] add distro version tests --- cmd/root.go | 2 +- imgbom/distro/distro.go | 18 +++++--- imgbom/distro/distro_test.go | 90 ++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 imgbom/distro/distro_test.go diff --git a/cmd/root.go b/cmd/root.go index 1ece97dec..3677be8d8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -45,7 +45,7 @@ func runCmdWrapper(cmd *cobra.Command, args []string) { os.Exit(doRunCmd(cmd, args)) } -func doRunCmd(cmd *cobra.Command, args []string) int { +func doRunCmd(_ *cobra.Command, args []string) int { userImageStr := args[0] log.Infof("Fetching image '%s'", userImageStr) img, err := stereoscope.GetImage(userImageStr) diff --git a/imgbom/distro/distro.go b/imgbom/distro/distro.go index e9e23364d..cdd32fa51 100644 --- a/imgbom/distro/distro.go +++ b/imgbom/distro/distro.go @@ -7,8 +7,9 @@ import ( ) type Distro struct { - Type Type - Version *hashiVer.Version + Type Type + Version *hashiVer.Version + RawVersion string } func NewDistro(t Type, ver string) (Distro, error) { @@ -17,13 +18,18 @@ func NewDistro(t Type, ver string) (Distro, error) { return Distro{}, fmt.Errorf("could not create distro version: %w", err) } return Distro{ - Type: t, - Version: verObj, + Type: t, + Version: verObj, + RawVersion: ver, }, nil } -func (d Distro) MajorVersion() int { - return d.Version.Segments()[0] +func (d Distro) MajorVersion() string { + return fmt.Sprintf("%d", d.Version.Segments()[0]) +} + +func (d Distro) FullVersion() string { + return d.RawVersion } func (d Distro) String() string { diff --git a/imgbom/distro/distro_test.go b/imgbom/distro/distro_test.go new file mode 100644 index 000000000..bf5b6e7c0 --- /dev/null +++ b/imgbom/distro/distro_test.go @@ -0,0 +1,90 @@ +package distro + +import ( + "fmt" + "testing" +) + +func TestDistro_FullVersion(t *testing.T) { + + tests := []struct { + dist Type + version string + expected string + }{ + { + version: "8", + expected: "8", + }, + { + version: "18.04", + expected: "18.04", + }, + { + version: "0", + expected: "0", + }, + { + version: "18.1.2", + expected: "18.1.2", + }, + } + + for _, test := range tests { + name := fmt.Sprintf("%s:%s", test.dist, test.version) + t.Run(name, func(t *testing.T) { + d, err := NewDistro(test.dist, test.version) + if err != nil { + t.Errorf("could not create distro='%+v:%+v': %+v", test.dist, test.version, err) + } + + actual := d.FullVersion() + if actual != test.expected { + t.Errorf("mismatched distro raw version: '%s'!='%s'", actual, test.expected) + } + }) + } + +} + +func TestDistro_MajorVersion(t *testing.T) { + + tests := []struct { + dist Type + version string + expected string + }{ + { + version: "8", + expected: "8", + }, + { + version: "18.04", + expected: "18", + }, + { + version: "0", + expected: "0", + }, + { + version: "18.1.2", + expected: "18", + }, + } + + for _, test := range tests { + name := fmt.Sprintf("%s:%s", test.dist, test.version) + t.Run(name, func(t *testing.T) { + d, err := NewDistro(test.dist, test.version) + if err != nil { + t.Errorf("could not create distro='%+v:%+v': %+v", test.dist, test.version, err) + } + + actual := d.MajorVersion() + if actual != test.expected { + t.Errorf("mismatched major version: '%s'!='%s'", actual, test.expected) + } + }) + } + +}