From dc931b924226d938bad694d7a89dda39eb1bd532 Mon Sep 17 00:00:00 2001 From: lauk Date: Wed, 3 Jul 2024 11:13:57 +0800 Subject: [PATCH] add bootconfig test files --- .../bootconfig/ignition/ignition_test.go | 78 ++++++++ .../bootconfig/kickstart/kickstart_test.go | 78 ++++++++ pkg/osmanager/bootconfig/tools_test.go | 168 ++++++++++++++++++ 3 files changed, 324 insertions(+) create mode 100644 pkg/osmanager/bootconfig/ignition/ignition_test.go create mode 100644 pkg/osmanager/bootconfig/kickstart/kickstart_test.go create mode 100644 pkg/osmanager/bootconfig/tools_test.go diff --git a/pkg/osmanager/bootconfig/ignition/ignition_test.go b/pkg/osmanager/bootconfig/ignition/ignition_test.go new file mode 100644 index 0000000..3e94e90 --- /dev/null +++ b/pkg/osmanager/bootconfig/ignition/ignition_test.go @@ -0,0 +1,78 @@ +/* +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 ignition + +import ( + "nestos-kubernetes-deployer/pkg/configmanager" + "nestos-kubernetes-deployer/pkg/configmanager/asset" + "nestos-kubernetes-deployer/pkg/configmanager/globalconfig" + "nestos-kubernetes-deployer/pkg/constants" + "os" + "testing" +) + +func TestIgnition(t *testing.T) { + err := os.Chdir("../../../../data") + if err != nil { + t.Fatal(err) + } + clusterAsset := &asset.ClusterAsset{ + ClusterID: "cluster", + Platform: "libvirt", + Master: []asset.NodeAsset{ + {Hostname: "k8s-master01", IP: "192.168.1.1"}, + {Hostname: "k8s-master02", IP: "192.168.1.2"}, + }, + Worker: []asset.NodeAsset{ + {Hostname: "k8s-worker01", IP: "192.168.1.6"}, + }, + SSHKey: "./assets.go", + HookConf: asset.HookConf{ + ShellFiles: []asset.ShellFile{ + {Name: "sss"}, + {Name: "sss"}, + }, + }, + } + bootstrapBaseurl := "a/b" + configmanager.GlobalConfig = &globalconfig.GlobalConfig{ + PersistDir: "./", + BootstrapUrl: globalconfig.BootstrapUrl{ + BootstrapIgnHost: "127.0.0.1", + BootstrapIgnPort: "9080", + }, + } + + ci := NewIgnition(clusterAsset, bootstrapBaseurl) + + t.Run("GenerateBootConfig", func(t *testing.T) { + clusterAsset.Runtime = constants.Crio + err := ci.GenerateBootConfig() + if err != nil { + t.Error("test fail", err) + return + } + t.Log("success") + }) + + t.Run("GenerateBootConfig_fail", func(t *testing.T) { + clusterAsset.Runtime = "podman" + err := ci.GenerateBootConfig() + if err == nil { + t.Error("expected failure, got success") + } + }) +} diff --git a/pkg/osmanager/bootconfig/kickstart/kickstart_test.go b/pkg/osmanager/bootconfig/kickstart/kickstart_test.go new file mode 100644 index 0000000..d93f32a --- /dev/null +++ b/pkg/osmanager/bootconfig/kickstart/kickstart_test.go @@ -0,0 +1,78 @@ +/* +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 kickstart + +import ( + "nestos-kubernetes-deployer/pkg/configmanager" + "nestos-kubernetes-deployer/pkg/configmanager/asset" + "nestos-kubernetes-deployer/pkg/configmanager/globalconfig" + "nestos-kubernetes-deployer/pkg/constants" + "os" + "testing" +) + +func TestKickstart(t *testing.T) { + err := os.Chdir("../../../../data") + if err != nil { + t.Fatal(err) + } + clusterAsset := &asset.ClusterAsset{ + ClusterID: "cluster", + Platform: "libvirt", + Master: []asset.NodeAsset{ + {Hostname: "k8s-master01", IP: "192.168.1.1"}, + {Hostname: "k8s-master02", IP: "192.168.1.2"}, + }, + Worker: []asset.NodeAsset{ + {Hostname: "k8s-worker01", IP: "192.168.1.6"}, + }, + SSHKey: "./assets.go", + HookConf: asset.HookConf{ + ShellFiles: []asset.ShellFile{ + {Name: "sss"}, + {Name: "sss"}, + }, + }, + } + bootstrapBaseurl := "a/b" + configmanager.GlobalConfig = &globalconfig.GlobalConfig{ + PersistDir: "./", + BootstrapUrl: globalconfig.BootstrapUrl{ + BootstrapIgnHost: "127.0.0.1", + BootstrapIgnPort: "9080", + }, + } + + ci := NewKickstart(clusterAsset, bootstrapBaseurl) + + t.Run("GenerateBootConfig", func(t *testing.T) { + clusterAsset.Runtime = constants.Crio + err := ci.GenerateBootConfig() + if err != nil { + t.Error("test fail", err) + return + } + t.Log("success") + }) + + t.Run("GenerateBootConfig_fail", func(t *testing.T) { + clusterAsset.Runtime = "podman" + err := ci.GenerateBootConfig() + if err == nil { + t.Error("expected failure, got success") + } + }) +} diff --git a/pkg/osmanager/bootconfig/tools_test.go b/pkg/osmanager/bootconfig/tools_test.go new file mode 100644 index 0000000..6b81882 --- /dev/null +++ b/pkg/osmanager/bootconfig/tools_test.go @@ -0,0 +1,168 @@ +/* +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 bootconfig + +import ( + "nestos-kubernetes-deployer/pkg/configmanager" + "nestos-kubernetes-deployer/pkg/configmanager/asset" + "nestos-kubernetes-deployer/pkg/configmanager/globalconfig" + "nestos-kubernetes-deployer/pkg/constants" + "os" + "testing" +) + +func TestTool(t *testing.T) { + err := os.Chdir("../../../data") + if err != nil { + t.Fatal(err) + } + clusterAsset := &asset.ClusterAsset{ + ClusterID: "cluster", + Master: []asset.NodeAsset{ + {Hostname: "k8s-master01", IP: "192.168.1.1"}, + {Hostname: "k8s-master02", IP: "192.168.1.2"}, + }, + Worker: []asset.NodeAsset{ + {Hostname: "k8s-worker01", IP: "192.168.1.6"}, + }, + SSHKey: "./assets.go", + HookConf: asset.HookConf{ + ShellFiles: []asset.ShellFile{ + {Name: "sss"}, + {Name: "sss"}, + }, + }, + } + + configmanager.GlobalConfig = &globalconfig.GlobalConfig{ + PersistDir: "/", + BootstrapUrl: globalconfig.BootstrapUrl{ + BootstrapIgnHost: "127.0.0.1", + BootstrapIgnPort: "9080", + }, + } + + var tmplData *TmplData + t.Run("GetTmplData", func(t *testing.T) { + tmplData, err = GetTmplData(clusterAsset) + if err != nil { + t.Error("test fail", err) + return + } + t.Log("success") + }) + + t.Run("AppendStorageFiles Success", func(t *testing.T) { + var files []File + err := AppendStorageFiles(&files, "/", constants.BootConfigFilesPath, tmplData, []string{constants.InitClusterService}) + if err != nil { + t.Error("test fail", err) + return + } + t.Log("success") + }) + + t.Run("AppendStorageFiles Fail", func(t *testing.T) { + var files []File + err := AppendStorageFiles(&files, "/", "invalid/path", tmplData, []string{constants.InitClusterService}) + if err == nil { + t.Error("expected failure, got success") + } + }) + + t.Run("AppendSystemdUnits Success", func(t *testing.T) { + var systemd Systemd + err := AppendSystemdUnits(&systemd, constants.BootConfigSystemdPath, tmplData, []string{constants.InitClusterService}) + if err != nil { + t.Error("test fail", err) + return + } + t.Log("success") + }) + + t.Run("AppendSystemdUnits Fail", func(t *testing.T) { + var systemd Systemd + err := AppendSystemdUnits(&systemd, "invalid/path", tmplData, []string{constants.InitClusterService}) + if err == nil { + t.Error("expected failure, got success") + } + }) + + t.Run("GetSavePath Success", func(t *testing.T) { + GetSavePath("cluster") + t.Log("success") + }) + + t.Run("SaveYAML Success", func(t *testing.T) { + err := SaveYAML(clusterAsset, "/tmp", "test.yaml", "header") + if err != nil { + t.Error("test fail", err) + } + t.Log("success") + }) + + t.Run("SaveYAML Fail", func(t *testing.T) { + err := SaveYAML(clusterAsset, "/invalid/path", "", "header") + if err == nil { + t.Error("expected failure, got success") + } + }) + + t.Run("SaveJSON Success", func(t *testing.T) { + err := SaveJSON(clusterAsset, "/tmp", "test.json") + if err != nil { + t.Error("test fail", err) + } + t.Log("success") + }) + + t.Run("SaveJSON Fail", func(t *testing.T) { + err := SaveJSON("", "", "") + if err == nil { + t.Error("expected failure, got success") + } + }) + + t.Run("Marshal Success", func(t *testing.T) { + _, err := Marshal(clusterAsset) + if err != nil { + t.Error("test fail", err) + } + t.Log("success") + }) + + t.Run("SaveFile Success", func(t *testing.T) { + tbyte := []byte("This is a test content") + err := SaveFile(tbyte, "/tmp", "testfile") + if err != nil { + t.Error("test fail", err) + } + t.Log("success") + }) + + t.Run("SaveFile Fail", func(t *testing.T) { + err := SaveFile(nil, "/invalid/path", "testfile") + if err == nil { + t.Error("expected failure, got success") + } + }) + + t.Run("CreateSetHostnameUnit Success", func(t *testing.T) { + CreateSetHostnameUnit() + t.Log("success") + }) + +} -- Gitee