diff --git a/pkg/configmanager/runtime/contained.go b/pkg/configmanager/runtime/contained.go index 268a0481bc4655448e4bbf335c92731d3c466f92..287298e32f74b52905f5324afff1d44d05d8de80 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 0000000000000000000000000000000000000000..edccadd883ccb2465d6ebf1974347dcb742282df --- /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 0a8c79eeb3957bbd909c29d143a6b0e3958472e9..98920af6dbf01d713cc6ed11c35c3603e53e0334 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 0000000000000000000000000000000000000000..935c5ebb7e1a05386106f27a742600bf47daa5eb --- /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 f6b26d13e07d577997aae7d0788c92348d9c650e..bae2f3300f5d3bc599b61cef1a9aa8675c72fcdd 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 0000000000000000000000000000000000000000..dd2808bd0cb5f7f82358f6cf74bb7c0244865d47 --- /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 f83a2cf77331f8e6caf03f7ab235519c9c43f893..92c9f17bef139db610d5eb0ed0f21ed9069549a8 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 0000000000000000000000000000000000000000..fb92b83e1a34469444f8815d434f262269e7fe59 --- /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 869b2fe5a011ea845d0135f65cbf53a88116a654..f8bfb5db18732a7de6a0b08bda062237d7285c92 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 2a9da81dd622ea4bd8e8edc3d14aaf3602dd9777..1b909df70d892aa691dcbe683cad474755eb923e 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) + } + } + }) } }