From 10bd1f156ca19eb6e43c5d028797baeabf921438 Mon Sep 17 00:00:00 2001 From: lauk001 Date: Thu, 4 Jul 2024 16:27:41 +0800 Subject: [PATCH] add asset test files --- pkg/configmanager/asset/clusterasset_test.go | 121 +++++++++++++++++++ pkg/configmanager/asset/hookconfig_test.go | 59 +++++++++ 2 files changed, 180 insertions(+) create mode 100644 pkg/configmanager/asset/clusterasset_test.go create mode 100644 pkg/configmanager/asset/hookconfig_test.go diff --git a/pkg/configmanager/asset/clusterasset_test.go b/pkg/configmanager/asset/clusterasset_test.go new file mode 100644 index 0000000..2ea03c7 --- /dev/null +++ b/pkg/configmanager/asset/clusterasset_test.go @@ -0,0 +1,121 @@ +/* +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 asset + +import ( + "nestos-kubernetes-deployer/cmd/command/opts" + "testing" +) + +func TestClusterasset(t *testing.T) { + opts := &opts.OptionsList{ + RootOptDir: "/tmp", + InfraPlatform: opts.InfraPlatform{ + Libvirt: opts.Libvirt{ + URI: "qemu:///system", + OSPath: "/etc/qcow2.qcow2", + CIDR: "192.168.132.0/24", + Gateway: "192.168.132.1", + }, + }, + } + + cc := &ClusterAsset{ + ClusterID: "cluster", + Architecture: "amd64", + Platform: "libvirt", + OSImage: OSImage{Type: "nestos"}, + UserName: "root", + Password: "123", + SSHKey: "./assets.go", + Master: []NodeAsset{ + { + Hostname: "k8s-master01", + IP: "192.168.132.11", + HardwareInfo: HardwareInfo{ + CPU: 2, + RAM: 2048, + Disk: 30, + }, + }, + }, + Worker: []NodeAsset{ + { + Hostname: "k8s-worker01", + IP: "192.168.132.12", + HardwareInfo: HardwareInfo{ + CPU: 2, + RAM: 2048, + Disk: 30, + }, + }, + }, + Runtime: "crio", + Kubernetes: Kubernetes{ + KubernetesVersion: "v1.29.1", + KubernetesAPIVersion: "v1beta3", + ApiServerEndpoint: "192.168.132.11:6443", + ImageRegistry: "registry.k8s.io", + PauseImage: "pause:3.9", + Network: Network{ + ServiceSubnet: "10.96.0.0/16", + PodSubnet: "10.244.0.0/16", + }, + }, + HookConf: HookConf{ + ShellFiles: []ShellFile{ + {Name: "sss"}, + {Name: "sss"}, + }, + }, + } + + t.Run("CheckStringValue Success", func(t *testing.T) { + err := CheckStringValue(&cc.ClusterID, opts.ClusterID, "cluster") + if err != nil { + t.Errorf("CheckStringValue failed: %v", err) + } + }) + + t.Run("InitClusterAsset Success", func(t *testing.T) { + clusterConfig, err := cc.InitClusterAsset(opts) + if err != nil || clusterConfig == nil { + t.Errorf("InitClusterAsset failed: %v", err) + } + }) + + t.Run("Delete Success", func(t *testing.T) { + err := cc.Delete("sss") + if err != nil { + t.Errorf("Delete failed: %v", err) + } + }) + + t.Run("Persist Success", func(t *testing.T) { + err := cc.Persist("/tmp") + if err != nil { + t.Errorf("Persist failed: %v", err) + } + }) + + t.Run("InitClusterAsset KubernetesAPIVersion Fail", func(t *testing.T) { + opts.KubernetesAPIVersion = 21 + clusterConfig, err := cc.InitClusterAsset(opts) + if err == nil || clusterConfig != nil { + t.Error("Expected error, got nil") + } + }) +} diff --git a/pkg/configmanager/asset/hookconfig_test.go b/pkg/configmanager/asset/hookconfig_test.go new file mode 100644 index 0000000..e3e194e --- /dev/null +++ b/pkg/configmanager/asset/hookconfig_test.go @@ -0,0 +1,59 @@ +/* +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 asset + +import "testing" + +func TestHookconfig(t *testing.T) { + + hookconfig := &HookConf{ + PreHookScript: "../../../hack/build.sh", + PostHookYaml: "", + ShellFiles: []ShellFile{ + { + Name: "test.sh", + Mode: 0644, + Content: nil, + }, + }, + PostHookFiles: []string{"sss", "sss"}, + } + + t.Run("GetCmdHooks Success", func(t *testing.T) { + err := GetCmdHooks(hookconfig) + if err != nil { + t.Errorf("GetCmdHooks failed: %v", err) + } + }) + + //测试脚本格式 + t.Run("GetCmdHooks PreHookScript Fail", func(t *testing.T) { + hookconfig.PreHookScript = "./" + err := GetCmdHooks(hookconfig) + if err == nil { + t.Error("Expected error, got nil") + } + }) + + //测试yaml格式 + t.Run("GetCmdHooks PostHookYaml Fail", func(t *testing.T) { + hookconfig.PostHookYaml = "test.sh" + err := GetCmdHooks(hookconfig) + if err == nil { + t.Error("Expected error, got nil") + } + }) +} -- Gitee