Skip to content

Commit 6e55234

Browse files
committed
Add unit test to function GetCPUVariantFromArch
Add unit test to function GetCPUVariantFromArch Fix import issue on non-linux platforms Fix some style issue Signed-off-by: Tony Fang <[email protected]>
1 parent 8d5c045 commit 6e55234

File tree

4 files changed

+104
-13
lines changed

4 files changed

+104
-13
lines changed

platforms/cpuinfo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func cpuVariant() string {
3535
var err error
3636
cpuVariantValue, err = getCPUVariant()
3737
if err != nil {
38-
log.L.Errorf("Error getCPUVariant for OS %s : %v", runtime.GOOS, err)
38+
log.L.Errorf("Error getCPUVariant for OS %s: %v", runtime.GOOS, err)
3939
}
4040
}
4141
})

platforms/cpuinfo_linux.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func getCPUInfo(pattern string) (info string, err error) {
7070
return "", err
7171
}
7272

73-
return "", fmt.Errorf("getCPUInfo for pattern: %s: %w", pattern, errdefs.ErrNotFound)
73+
return "", fmt.Errorf("getCPUInfo for pattern %s: %w", pattern, errdefs.ErrNotFound)
7474
}
7575

7676
// getCPUVariantFromArch get CPU variant from arch through a system call
@@ -82,7 +82,7 @@ func getCPUVariantFromArch(arch string) (string, error) {
8282

8383
if arch == "aarch64" {
8484
variant = "8"
85-
} else if len(arch) >= 5 {
85+
} else if arch[0:4] == "armv" && len(arch) >= 5 {
8686
//Valid arch format is in form of armvXx
8787
switch arch[3:5] {
8888
case "v8":
@@ -101,7 +101,7 @@ func getCPUVariantFromArch(arch string) (string, error) {
101101
variant = "unknown"
102102
}
103103
} else {
104-
return "", fmt.Errorf("getCPUVariantFromArch invalid arch : %s, %v", arch, errdefs.ErrInvalidArgument)
104+
return "", fmt.Errorf("getCPUVariantFromArch invalid arch: %s, %w", arch, errdefs.ErrInvalidArgument)
105105
}
106106
return variant, nil
107107
}
@@ -119,15 +119,15 @@ func getCPUVariant() (string, error) {
119119
//Let's try getting CPU variant from machine architecture
120120
arch, err := getMachineArch()
121121
if err != nil {
122-
return "", fmt.Errorf("failure getting machine architecture : %v", err)
122+
return "", fmt.Errorf("failure getting machine architecture: %v", err)
123123
}
124124

125125
variant, err = getCPUVariantFromArch(arch)
126126
if err != nil {
127-
return "", fmt.Errorf("failure getting CPU variant from machine architecture : %v", err)
127+
return "", fmt.Errorf("failure getting CPU variant from machine architecture: %v", err)
128128
}
129129
} else {
130-
return "", fmt.Errorf("failure getting CPU variant : %v", err)
130+
return "", fmt.Errorf("failure getting CPU variant: %v", err)
131131
}
132132
}
133133

platforms/cpuinfo_linux_test.go

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
package platforms
1818

1919
import (
20+
"errors"
2021
"runtime"
2122
"testing"
23+
24+
"github.com/containerd/containerd/errdefs"
2225
)
2326

2427
func TestCPUVariant(t *testing.T) {
@@ -30,24 +33,109 @@ func TestCPUVariant(t *testing.T) {
3033

3134
p, err := getCPUVariant()
3235
if err != nil {
33-
t.Fatalf("Error getting CPU variant: %v\n", err)
36+
t.Fatalf("Error getting CPU variant: %v", err)
3437
return
3538
}
3639

3740
for _, variant := range variants {
3841
if p == variant {
39-
t.Logf("got valid variant as expected: %#v = %#v\n", p, variant)
42+
t.Logf("got valid variant as expected: %#v = %#v", p, variant)
4043
return
4144
}
4245
}
4346

44-
t.Fatalf("could not get valid variant as expected: %v\n", variants)
47+
t.Fatalf("could not get valid variant as expected: %v", variants)
4548
}
4649

4750
func TestGetCPUVariantFromArch(t *testing.T) {
4851

49-
if !isArmArch(runtime.GOARCH) {
50-
t.Skip("only relevant on linux/arm")
51-
}
52+
for _, testcase := range []struct {
53+
name string
54+
input string
55+
output string
56+
expectedErr error
57+
}{
58+
{
59+
name: "Test aarch64",
60+
input: "aarch64",
61+
output: "8",
62+
expectedErr: nil,
63+
},
64+
{
65+
name: "Test Armv8 with capital",
66+
input: "Armv8",
67+
output: "8",
68+
expectedErr: nil,
69+
},
70+
{
71+
name: "Test armv7",
72+
input: "armv7",
73+
output: "7",
74+
expectedErr: nil,
75+
},
76+
{
77+
name: "Test armv6",
78+
input: "armv6",
79+
output: "6",
80+
expectedErr: nil,
81+
},
82+
{
83+
name: "Test armv5",
84+
input: "armv5",
85+
output: "5",
86+
expectedErr: nil,
87+
},
88+
{
89+
name: "Test armv4",
90+
input: "armv4",
91+
output: "4",
92+
expectedErr: nil,
93+
},
94+
{
95+
name: "Test armv3",
96+
input: "armv3",
97+
output: "3",
98+
expectedErr: nil,
99+
},
100+
{
101+
name: "Test unknown input",
102+
input: "armv9",
103+
output: "unknown",
104+
expectedErr: nil,
105+
},
106+
{
107+
name: "Test invalid input which doesn't start with armv",
108+
input: "armxxxx",
109+
output: "",
110+
expectedErr: errdefs.ErrInvalidArgument,
111+
},
112+
{
113+
name: "Test invalid input whose length is less than 5",
114+
input: "armv",
115+
output: "",
116+
expectedErr: errdefs.ErrInvalidArgument,
117+
},
118+
} {
119+
t.Run(testcase.name, func(t *testing.T) {
120+
t.Logf("input: %v", testcase.input)
52121

122+
variant, err := getCPUVariantFromArch(testcase.input)
123+
124+
if err == nil {
125+
if testcase.expectedErr != nil {
126+
t.Fatalf("Expect to get error: %v, however no error got", testcase.expectedErr)
127+
} else {
128+
if variant != testcase.output {
129+
t.Fatalf("Expect to get variant: %v, however %v returned", testcase.output, variant)
130+
}
131+
}
132+
133+
} else {
134+
if !errors.Is(err, testcase.expectedErr) {
135+
t.Fatalf("Expect to get error: %v, however error %v returned", testcase.expectedErr, err)
136+
}
137+
}
138+
})
139+
140+
}
53141
}

platforms/cpuinfo_other.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
package platforms
2121

2222
import (
23+
"fmt"
2324
"runtime"
25+
26+
"github.com/containerd/containerd/errdefs"
2427
)
2528

2629
func getCPUVariant() (string, error) {

0 commit comments

Comments
 (0)