containerd/platforms/cpuinfo_linux_test.go
Sebastiaan van Stijn 381442945b
platforms: remove errdefs dependency
This cleans up the platforms package from dependencies that are not strictly
needed. This is in preparation of making this package a separate module, which
can be shared by plugins, and containerd versions (as well as external consumers),

- Remove dependency on the errdefs package: most uses of these error
  definitions were used internally, and other errors may not be useful
  for external consumers as sentinel errors.
- ErrInvalidArgument may be a potential exception, although a look at
  current uses of this package shows that there's no special handling of
  invalid parameters vs other errors (all would boil down to "the passed
  platform is invalid" (either the format, or parsing is not implemented
  on a specific platform)
- Remove uses of the convenience "Platform" alias in favor of using the
  upstream (from OCI spec). Consumers of this package can still use the
  convenience alias, but make sure that function signatures do not imply
  that it's a different type (which can cause confusion).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-09-13 16:44:48 +02:00

140 lines
3.1 KiB
Go

/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package platforms
import (
"errors"
"runtime"
"testing"
)
func TestCPUVariant(t *testing.T) {
if !isArmArch(runtime.GOARCH) {
t.Skip("only relevant on linux/arm")
}
variants := []string{"v8", "v7", "v6", "v5", "v4", "v3"}
p, err := getCPUVariant()
if err != nil {
t.Fatalf("Error getting CPU variant: %v", err)
return
}
for _, variant := range variants {
if p == variant {
t.Logf("got valid variant as expected: %#v = %#v", p, variant)
return
}
}
t.Fatalf("could not get valid variant as expected: %v", variants)
}
func TestGetCPUVariantFromArch(t *testing.T) {
for _, testcase := range []struct {
name string
input string
output string
expectedErr error
}{
{
name: "Test aarch64",
input: "aarch64",
output: "8",
expectedErr: nil,
},
{
name: "Test Armv8 with capital",
input: "Armv8",
output: "8",
expectedErr: nil,
},
{
name: "Test armv7",
input: "armv7",
output: "7",
expectedErr: nil,
},
{
name: "Test armv6",
input: "armv6",
output: "6",
expectedErr: nil,
},
{
name: "Test armv5",
input: "armv5",
output: "5",
expectedErr: nil,
},
{
name: "Test armv4",
input: "armv4",
output: "4",
expectedErr: nil,
},
{
name: "Test armv3",
input: "armv3",
output: "3",
expectedErr: nil,
},
{
name: "Test unknown input",
input: "armv9",
output: "unknown",
expectedErr: nil,
},
{
name: "Test invalid input which doesn't start with armv",
input: "armxxxx",
output: "",
expectedErr: errInvalidArgument,
},
{
name: "Test invalid input whose length is less than 5",
input: "armv",
output: "",
expectedErr: errInvalidArgument,
},
} {
t.Run(testcase.name, func(t *testing.T) {
t.Logf("input: %v", testcase.input)
variant, err := getCPUVariantFromArch(testcase.input)
if err == nil {
if testcase.expectedErr != nil {
t.Fatalf("Expect to get error: %v, however no error got", testcase.expectedErr)
} else {
if variant != testcase.output {
t.Fatalf("Expect to get variant: %v, however %v returned", testcase.output, variant)
}
}
} else {
if !errors.Is(err, testcase.expectedErr) {
t.Fatalf("Expect to get error: %v, however error %v returned", testcase.expectedErr, err)
}
}
})
}
}