From fb98be2cc6f4839cebd697e5089e38182ce93fb2 Mon Sep 17 00:00:00 2001 From: lauk001 Date: Mon, 1 Jul 2024 09:26:00 +0800 Subject: [PATCH] Add container runtime test code --- pkg/configmanager/runtime/contained.go | 8 +- pkg/configmanager/runtime/contained_test.go | 51 ++++++++++ pkg/configmanager/runtime/crio.go | 8 +- pkg/configmanager/runtime/crio_test.go | 44 ++++++++ pkg/configmanager/runtime/docker.go | 8 +- pkg/configmanager/runtime/docker_test.go | 44 ++++++++ pkg/configmanager/runtime/isulad.go | 8 +- pkg/configmanager/runtime/isulad_test.go | 44 ++++++++ pkg/configmanager/runtime/runtime.go | 8 +- pkg/configmanager/runtime/runtime_test.go | 105 +++++++++++++------- 10 files changed, 274 insertions(+), 54 deletions(-) create mode 100644 pkg/configmanager/runtime/contained_test.go create mode 100644 pkg/configmanager/runtime/crio_test.go create mode 100644 pkg/configmanager/runtime/docker_test.go create mode 100644 pkg/configmanager/runtime/isulad_test.go diff --git a/pkg/configmanager/runtime/contained.go b/pkg/configmanager/runtime/contained.go index 268a048..287298e 100644 --- a/pkg/configmanager/runtime/contained.go +++ b/pkg/configmanager/runtime/contained.go @@ -5,7 +5,7 @@ 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 + 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, @@ -19,14 +19,14 @@ import ( "nestos-kubernetes-deployer/pkg/api" ) -type containerdRuntime struct { +type ContainerdRuntime struct { } -func (ir *containerdRuntime) GetRuntimeCriSocket() string { +func (ir *ContainerdRuntime) GetRuntimeCriSocket() string { return "unix:///var/run/containerd/containerd.sock" } func IsContainerd(rt api.Runtime) bool { - _, ok := rt.(*containerdRuntime) + _, ok := rt.(*ContainerdRuntime) return ok } diff --git a/pkg/configmanager/runtime/contained_test.go b/pkg/configmanager/runtime/contained_test.go new file mode 100644 index 0000000..edccadd --- /dev/null +++ b/pkg/configmanager/runtime/contained_test.go @@ -0,0 +1,51 @@ +/* +Copyright 2024 KylinSoft Co., Ltd. + +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 runtime_test + +import ( + "nestos-kubernetes-deployer/pkg/configmanager/runtime" + "testing" +) + +// Define mockRuntime for testing purposes +type mockRuntime struct{} + +func (mr *mockRuntime) GetRuntimeCriSocket() string { + return "/var/run/mock.sock" +} + +func TestContainerdRuntime_GetRuntimeCriSocket(t *testing.T) { + cr := &runtime.ContainerdRuntime{} + expectedSocket := "unix:///var/run/containerd/containerd.sock" + if cr.GetRuntimeCriSocket() != expectedSocket { + t.Errorf("Expected socket path %s, but got %s", expectedSocket, cr.GetRuntimeCriSocket()) + } +} + +func TestIsContainerd(t *testing.T) { + // Test case 1: Check if containerdRuntime returns true for IsContainerd + cr := &runtime.ContainerdRuntime{} + if !runtime.IsContainerd(cr) { + t.Errorf("Expected IsContainerd to return true for containerdRuntime, but got false") + } + + // Test case 2: Check if mockRuntime returns false for IsContainerd + mr := &mockRuntime{} + if runtime.IsContainerd(mr) { + t.Errorf("Expected IsContainerd to return false for mockRuntime, but got true") + } +} diff --git a/pkg/configmanager/runtime/crio.go b/pkg/configmanager/runtime/crio.go index 0a8c79e..98920af 100644 --- a/pkg/configmanager/runtime/crio.go +++ b/pkg/configmanager/runtime/crio.go @@ -5,7 +5,7 @@ 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 + 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, @@ -17,13 +17,13 @@ package runtime import "nestos-kubernetes-deployer/pkg/api" -type crioRuntime struct{} +type CrioRuntime struct{} -func (cr *crioRuntime) GetRuntimeCriSocket() string { +func (cr *CrioRuntime) GetRuntimeCriSocket() string { return "unix:///var/run/crio/crio.sock" } func IsCrio(rt api.Runtime) bool { - _, ok := rt.(*crioRuntime) + _, ok := rt.(*CrioRuntime) return ok } diff --git a/pkg/configmanager/runtime/crio_test.go b/pkg/configmanager/runtime/crio_test.go new file mode 100644 index 0000000..935c5eb --- /dev/null +++ b/pkg/configmanager/runtime/crio_test.go @@ -0,0 +1,44 @@ +/* +Copyright 2024 KylinSoft Co., Ltd. + +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 runtime_test + +import ( + "nestos-kubernetes-deployer/pkg/configmanager/runtime" + "testing" +) + +func TestCrioRuntime_GetRuntimeCriSocket(t *testing.T) { + cr := &runtime.CrioRuntime{} + expectedSocket := "unix:///var/run/crio/crio.sock" + if cr.GetRuntimeCriSocket() != expectedSocket { + t.Errorf("Expected socket path %s, but got %s", expectedSocket, cr.GetRuntimeCriSocket()) + } +} + +func TestIsCrio(t *testing.T) { + // Test case 1: Check if crioRuntime returns true for IsCrio + cr := &runtime.CrioRuntime{} + if !runtime.IsCrio(cr) { + t.Errorf("Expected IsCrio to return true for crioRuntime, but got false") + } + + // Test case 2: Check if mockRuntime returns false for IsCrio + mr := &mockRuntime{} + if runtime.IsCrio(mr) { + t.Errorf("Expected IsCrio to return false for mockRuntime, but got true") + } +} diff --git a/pkg/configmanager/runtime/docker.go b/pkg/configmanager/runtime/docker.go index f6b26d1..bae2f33 100644 --- a/pkg/configmanager/runtime/docker.go +++ b/pkg/configmanager/runtime/docker.go @@ -5,7 +5,7 @@ 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 + 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, @@ -17,13 +17,13 @@ package runtime import "nestos-kubernetes-deployer/pkg/api" -type dockerRuntime struct{} +type DockerRuntime struct{} -func (dr *dockerRuntime) GetRuntimeCriSocket() string { +func (dr *DockerRuntime) GetRuntimeCriSocket() string { return "/var/run/dockershim.sock" } func IsDocker(rt api.Runtime) bool { - _, ok := rt.(*dockerRuntime) + _, ok := rt.(*DockerRuntime) return ok } diff --git a/pkg/configmanager/runtime/docker_test.go b/pkg/configmanager/runtime/docker_test.go new file mode 100644 index 0000000..dd2808b --- /dev/null +++ b/pkg/configmanager/runtime/docker_test.go @@ -0,0 +1,44 @@ +/* +Copyright 2024 KylinSoft Co., Ltd. + +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 runtime_test + +import ( + "nestos-kubernetes-deployer/pkg/configmanager/runtime" + "testing" +) + +func TestDockerRuntime_GetRuntimeCriSocket(t *testing.T) { + dr := &runtime.DockerRuntime{} + expectedSocket := "/var/run/dockershim.sock" + if dr.GetRuntimeCriSocket() != expectedSocket { + t.Errorf("Expected socket path %s, but got %s", expectedSocket, dr.GetRuntimeCriSocket()) + } +} + +func TestIsDocker(t *testing.T) { + // Test case 1: Check if dockerRuntime returns true for IsDocker + cr := &runtime.DockerRuntime{} + if !runtime.IsDocker(cr) { + t.Errorf("Expected Isdocker to return true for dockerRuntime, but got false") + } + + // Test case 2: Check if mockRuntime returns false for IsDocker + mr := &mockRuntime{} + if runtime.IsDocker(mr) { + t.Errorf("Expected IsDocker to return false for mockRuntime, but got true") + } +} diff --git a/pkg/configmanager/runtime/isulad.go b/pkg/configmanager/runtime/isulad.go index f83a2cf..92c9f17 100644 --- a/pkg/configmanager/runtime/isulad.go +++ b/pkg/configmanager/runtime/isulad.go @@ -5,7 +5,7 @@ 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 + 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, @@ -19,14 +19,14 @@ import ( "nestos-kubernetes-deployer/pkg/api" ) -type isuladRuntime struct { +type IsuladRuntime struct { } -func (ir *isuladRuntime) GetRuntimeCriSocket() string { +func (ir *IsuladRuntime) GetRuntimeCriSocket() string { return "/var/run/isulad.sock" } func IsIsulad(rt api.Runtime) bool { - _, ok := rt.(*isuladRuntime) + _, ok := rt.(*IsuladRuntime) return ok } diff --git a/pkg/configmanager/runtime/isulad_test.go b/pkg/configmanager/runtime/isulad_test.go new file mode 100644 index 0000000..fb92b83 --- /dev/null +++ b/pkg/configmanager/runtime/isulad_test.go @@ -0,0 +1,44 @@ +/* +Copyright 2024 KylinSoft Co., Ltd. + +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 runtime_test + +import ( + "nestos-kubernetes-deployer/pkg/configmanager/runtime" + "testing" +) + +func TestIsuladRuntime_GetRuntimeCriSocket(t *testing.T) { + ir := &runtime.IsuladRuntime{} + expectedSocket := "/var/run/isulad.sock" + if ir.GetRuntimeCriSocket() != expectedSocket { + t.Errorf("Expected socket path %s, but got %s", expectedSocket, ir.GetRuntimeCriSocket()) + } +} + +func TestIsIsulad(t *testing.T) { + // Test case 1: Check if isuladRuntime returns true for IsIsulad + cr := &runtime.IsuladRuntime{} + if !runtime.IsIsulad(cr) { + t.Errorf("Expected IsIsulad to return true for isuladRuntime, but got false") + } + + // Test case 2: Check if mockRuntime returns false for IsIsulad + mr := &mockRuntime{} + if runtime.IsIsulad(mr) { + t.Errorf("Expected IsIsulad to return false for mockRuntime, but got true") + } +} diff --git a/pkg/configmanager/runtime/runtime.go b/pkg/configmanager/runtime/runtime.go index 869b2fe..f8bfb5d 100644 --- a/pkg/configmanager/runtime/runtime.go +++ b/pkg/configmanager/runtime/runtime.go @@ -26,10 +26,10 @@ import ( var ( mapRuntime = map[string]api.Runtime{ - constants.Isulad: &isuladRuntime{}, - constants.Docker: &dockerRuntime{}, - constants.Crio: &crioRuntime{}, - constants.Containerd: &containerdRuntime{}, + constants.Isulad: &IsuladRuntime{}, + constants.Docker: &DockerRuntime{}, + constants.Crio: &CrioRuntime{}, + constants.Containerd: &ContainerdRuntime{}, } ) diff --git a/pkg/configmanager/runtime/runtime_test.go b/pkg/configmanager/runtime/runtime_test.go index 2a9da81..1b909df 100644 --- a/pkg/configmanager/runtime/runtime_test.go +++ b/pkg/configmanager/runtime/runtime_test.go @@ -1,47 +1,84 @@ -/* -Copyright 2024 KylinSoft Co., Ltd. - -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 runtime_test import ( + "nestos-kubernetes-deployer/pkg/api" "nestos-kubernetes-deployer/pkg/configmanager/runtime" "nestos-kubernetes-deployer/pkg/constants" "testing" + + "github.com/pkg/errors" ) func TestGetRuntime(t *testing.T) { - for _, runtimeName := range []string{constants.Isulad, constants.Docker, constants.Crio} { - rt, err := runtime.GetRuntime(runtimeName) - if err != nil { - t.Errorf("Unexpected error for supported runtime %s: %v", runtimeName, err) - } - if rt == nil { - t.Errorf("Unexpected nil runtime for supported runtime %s", runtimeName) - } + tests := []struct { + name string + runtime string + expected api.Runtime + err error + }{ + { + name: "Empty string defaults to Isulad", + runtime: "", + expected: &runtime.IsuladRuntime{}, + err: nil, + }, + { + name: "Isulad", + runtime: constants.Isulad, + expected: &runtime.IsuladRuntime{}, + err: nil, + }, + { + name: "Docker", + runtime: constants.Docker, + expected: &runtime.DockerRuntime{}, + err: nil, + }, + { + name: "Crio", + runtime: constants.Crio, + expected: &runtime.CrioRuntime{}, + err: nil, + }, + { + name: "Containerd", + runtime: constants.Containerd, + expected: &runtime.ContainerdRuntime{}, + err: nil, + }, + { + name: "Unsupported", + runtime: "unsupported", + expected: nil, + err: errors.New("unsupported runtime"), + }, } - unknownRuntime := "unknown" - rt, err := runtime.GetRuntime(unknownRuntime) - if err == nil { - t.Errorf("Expected error for unsupported runtime %s", unknownRuntime) - } - if rt != nil { - t.Errorf("Expected nil runtime for unsupported runtime %s", unknownRuntime) - } - expectedErrorMessage := "unsupported runtime" - if err != nil && err.Error() != expectedErrorMessage { - t.Errorf("Expected error message '%s' for unsupported runtime, got '%s'", expectedErrorMessage, err.Error()) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := runtime.GetRuntime(tt.runtime) + + if tt.err != nil { + if err == nil || err.Error() != tt.err.Error() { + t.Errorf("expected error %v, got %v", tt.err, err) + } + } else { + if err != nil { + t.Errorf("expected no error, got %v", err) + } + if _, ok := got.(*runtime.IsuladRuntime); tt.runtime == constants.Isulad && !ok { + t.Errorf("expected IsuladRuntime, got %T", got) + } + if _, ok := got.(*runtime.DockerRuntime); tt.runtime == constants.Docker && !ok { + t.Errorf("expected DockerRuntime, got %T", got) + } + if _, ok := got.(*runtime.CrioRuntime); tt.runtime == constants.Crio && !ok { + t.Errorf("expected CrioRuntime, got %T", got) + } + if _, ok := got.(*runtime.ContainerdRuntime); tt.runtime == constants.Containerd && !ok { + t.Errorf("expected ContainerdRuntime, got %T", got) + } + } + }) } } -- Gitee