Skip to content

Commit

Permalink
chore(pkg): let KernelRelease type implement Stringer interface.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP authored and poiana committed Nov 8, 2023
1 parent 344fd25 commit 1babd00
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
40 changes: 14 additions & 26 deletions pkg/driverbuilder/builder/ubuntu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import (
)

var tests = []struct {
config kernelrelease.KernelRelease
kernelversion string
expected struct {
config kernelrelease.KernelRelease
expected struct {
headersURLs []string
urls []string
gccVersion semver.Version
Expand All @@ -46,8 +45,8 @@ var tests = []struct {
Extraversion: "188",
FullExtraversion: "-188",
Architecture: kernelrelease.ArchitectureAmd64,
KernelVersion: "199",
},
kernelversion: "199",
expected: struct {
headersURLs []string
urls []string
Expand Down Expand Up @@ -77,8 +76,8 @@ var tests = []struct {
Extraversion: "1140-aws",
FullExtraversion: "-1140-aws",
Architecture: kernelrelease.ArchitectureArm64,
KernelVersion: "151",
},
kernelversion: "151",
expected: struct {
headersURLs []string
urls []string
Expand Down Expand Up @@ -108,8 +107,8 @@ var tests = []struct {
Extraversion: "1004-intel-iotg",
FullExtraversion: "-1004-intel-iotg",
Architecture: kernelrelease.ArchitectureAmd64,
KernelVersion: "6",
},
kernelversion: "6",
expected: struct {
headersURLs []string
urls []string
Expand Down Expand Up @@ -139,8 +138,8 @@ var tests = []struct {
Extraversion: "24-lowlatency-hwe-5.15",
FullExtraversion: "-24-lowlatency-hwe-5.15",
Architecture: kernelrelease.ArchitectureAmd64,
KernelVersion: "24~20.04.3",
},
kernelversion: "24~20.04.3",
expected: struct {
headersURLs []string
urls []string
Expand Down Expand Up @@ -170,8 +169,8 @@ var tests = []struct {
Extraversion: "9",
FullExtraversion: "-9",
Architecture: kernelrelease.ArchitectureAmd64,
KernelVersion: "9",
},
kernelversion: "9",
expected: struct {
headersURLs []string
urls []string
Expand Down Expand Up @@ -221,8 +220,8 @@ var tests = []struct {
Extraversion: "38-lts-utopic",
FullExtraversion: "-38-lts-utopic",
Architecture: kernelrelease.ArchitectureAmd64,
KernelVersion: "52~14.04.1",
},
kernelversion: "52~14.04.1",
expected: struct {
headersURLs []string
urls []string
Expand Down Expand Up @@ -252,8 +251,8 @@ var tests = []struct {
Extraversion: "1006-kvm",
FullExtraversion: "-1006-kvm",
Architecture: kernelrelease.ArchitectureAmd64,
KernelVersion: "6",
},
kernelversion: "6",
expected: struct {
headersURLs []string
urls []string
Expand All @@ -278,33 +277,24 @@ func TestUbuntuHeadersURLFromRelease(t *testing.T) {
for _, test := range tests {
expected := test.expected.headersURLs

// setup input
input := struct {
config kernelrelease.KernelRelease
kv string
}{
test.config,
test.kernelversion,
}

// call function
gotURLs, err := ubuntuHeadersURLFromRelease(input.config, input.kv)
gotURLs, err := ubuntuHeadersURLFromRelease(test.config)
// compare errors
// there are no official errors, so comparing fmt.Errorf() doesn't really work
// compare error message text instead
if err != nil && test.expected.err != nil && err.Error() != test.expected.err.Error() {
t.Fatalf("Unexpected error encountered with Test Input: '%v' | Error: '%s'", input, err)
t.Fatalf("Unexpected error encountered with Test Input: '%v' | Error: '%s'", test.config, err)
}

// check length of URL slice returned
if len(gotURLs) != len(expected) {
t.Fatalf("Slice sizes don't match! Test Input: '%v' | Got: '%v' / Want: '%v'", input, gotURLs, expected)
t.Fatalf("Slice sizes don't match! Test Input: '%v' | Got: '%v' / Want: '%v'", test.config, gotURLs, expected)
}

// check values are exact match
for i, v := range gotURLs {
if v != expected[i] {
t.Fatalf("Slice values don't match! Test Input: '%v' | Got: '%v' / Want: '%v'", input, gotURLs, expected)
t.Fatalf("Slice values don't match! Test Input: '%v' | Got: '%v' / Want: '%v'", test.config, gotURLs, expected)
}
}
}
Expand Down Expand Up @@ -332,15 +322,13 @@ func TestFetchUbuntuKernelURL(t *testing.T) {
input := struct {
baseURL string
config kernelrelease.KernelRelease
kv string
}{
url,
test.config,
test.kernelversion,
}

// call function
gotURLs, err := fetchUbuntuKernelURL(input.baseURL, input.config, input.kv)
gotURLs, err := fetchUbuntuKernelURL(input.baseURL, input.config)
if err != nil {
t.Fatalf("Unexpected error encountered with Test Input: '%v' | Error: '%s'", input, err)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/kernelrelease/kernelrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,7 @@ func (k *KernelRelease) SupportsModule() bool {
func (k *KernelRelease) SupportsProbe() bool {
return k.GTE(probeMinKernelVersion[k.Architecture])
}

func (k *KernelRelease) String() string {
return fmt.Sprintf("%s%s", k.Fullversion, k.FullExtraversion)
}

0 comments on commit 1babd00

Please sign in to comment.