From e37aa349ccf44a7491ed92e1345aba89abc5018a Mon Sep 17 00:00:00 2001 From: lauk001 Date: Fri, 5 Jul 2024 16:45:41 +0800 Subject: [PATCH] add cmd test files --- cmd/deploy_test.go | 126 +++++++++++++++++++++++++++++++++++++++++++ cmd/destroy_test.go | 38 +++++++++++++ cmd/extend_test.go | 37 +++++++++++++ cmd/template_test.go | 51 ++++++++++++++++++ cmd/upgrade_test.go | 35 ++++++++++++ 5 files changed, 287 insertions(+) create mode 100644 cmd/deploy_test.go create mode 100644 cmd/destroy_test.go create mode 100644 cmd/extend_test.go create mode 100644 cmd/template_test.go create mode 100644 cmd/upgrade_test.go diff --git a/cmd/deploy_test.go b/cmd/deploy_test.go new file mode 100644 index 0000000..2cf67b0 --- /dev/null +++ b/cmd/deploy_test.go @@ -0,0 +1,126 @@ +/* +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 cmd + +import ( + "nestos-kubernetes-deployer/cmd/command/opts" + "nestos-kubernetes-deployer/pkg/configmanager/asset" + "nestos-kubernetes-deployer/pkg/configmanager/asset/infraasset" + "os" + "testing" + + "gopkg.in/yaml.v2" +) + +func TestDeploy(t *testing.T) { + err := os.Chdir("../data") + if err != nil { + t.Fatal(err) + } + opts.Opts.RootOptDir = "./" + + cc := &asset.ClusterAsset{ + ClusterID: "cluster", + Architecture: "amd64", + Platform: "pxe", + InfraPlatform: infraasset.PXEAsset{ + IP: "", + HTTPServerPort: "10", + HTTPRootDir: "./", + TFTPServerPort: "20", + TFTPRootDir: "./", + }, + OSImage: asset.OSImage{Type: "nestos"}, + UserName: "root", + SSHKey: "./test.yaml", + Password: "123", + Master: []asset.NodeAsset{ + { + Hostname: "k8s-master01", + IP: "192.168.132.11", + HardwareInfo: asset.HardwareInfo{ + CPU: 2, + RAM: 2048, + Disk: 30, + }, + }, + }, + Worker: []asset.NodeAsset{ + { + Hostname: "k8s-worker01", + IP: "192.168.132.12", + HardwareInfo: asset.HardwareInfo{ + CPU: 2, + RAM: 2048, + Disk: 30, + }, + }, + }, + Runtime: "crio", + Kubernetes: asset.Kubernetes{ + KubernetesVersion: "v1.29.1", + KubernetesAPIVersion: "v1beta3", + ApiServerEndpoint: "192.168.132.11:6443", + ImageRegistry: "registry.k8s.io", + PauseImage: "pause:3.9", + Network: asset.Network{ + ServiceSubnet: "10.96.0.0/16", + PodSubnet: "10.244.0.0/16", + }, + }, + } + + clusterData, err := yaml.Marshal(cc) + if err != nil { + return + } + if err := os.WriteFile("test.yaml", clusterData, 0644); err != nil { + return + } + + cmd := NewDeployCommand() + args := []string{"--file", "test.yaml"} + cmd.SetArgs(args) + if err := cmd.Execute(); err != nil { + t.Errorf("Failed to execute command: %v", err) + } + + t.Run("DeployCmd Fail", func(t *testing.T) { + if err := runDeployCmd(cmd, args); err == nil { + t.Error("Expected error, got nil") + } + }) + + t.Run("clusterCreatePost Fail", func(t *testing.T) { + if err := clusterCreatePost(cc); err == nil { + t.Error("Expected error, got nil") + } + }) + + t.Run("deployHousekeeper Fail", func(t *testing.T) { + err := deployHousekeeper(nil, "./test.yaml") + if err == nil { + t.Error("Expected error, got nil") + } + }) + + t.Run("applyNetworkPlugin Fail", func(t *testing.T) { + err := applyNetworkPlugin("./", true) + if err == nil { + t.Error("Expected error, got nil") + } + }) +} diff --git a/cmd/destroy_test.go b/cmd/destroy_test.go new file mode 100644 index 0000000..69c64f1 --- /dev/null +++ b/cmd/destroy_test.go @@ -0,0 +1,38 @@ +/* +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 cmd + +import ( + "nestos-kubernetes-deployer/cmd/command/opts" + "testing" +) + +func TestDestroy(t *testing.T) { + opts.Opts.RootOptDir = "../data" + cmd := NewDestroyCommand() + + args := []string{"--cluster-id", "cluster"} + cmd.SetArgs(args) + if err := cmd.Execute(); err != nil { + t.Errorf("Failed to execute command: %v", err) + } + + t.Run("DestroyCmd Fail", func(t *testing.T) { + if err := runDestroyCmd(cmd, args); err == nil { + t.Error("Expected error, got nil") + } + }) +} diff --git a/cmd/extend_test.go b/cmd/extend_test.go new file mode 100644 index 0000000..f09ca79 --- /dev/null +++ b/cmd/extend_test.go @@ -0,0 +1,37 @@ +/* +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 cmd + +import ( + "nestos-kubernetes-deployer/cmd/command/opts" + "testing" +) + +func TestExtend(t *testing.T) { + opts.Opts.RootOptDir = "../data" + cmd := NewExtendCommand() + args := []string{"--num", "0"} + cmd.SetArgs(args) + if err := cmd.Execute(); err != nil { + t.Errorf("Failed to execute command: %v", err) + } + + t.Run("ExtendCmd Fail", func(t *testing.T) { + if err := runExtendCmd(cmd, args); err == nil { + t.Error("Expected error, got nil") + } + }) +} diff --git a/cmd/template_test.go b/cmd/template_test.go new file mode 100644 index 0000000..0eeb01d --- /dev/null +++ b/cmd/template_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 cmd + +import "testing" + +func TestTemplate(t *testing.T) { + platformTests := []struct { + platform string + }{ + {"openstack"}, + {"pxe"}, + {"ipxe"}, + {"libvirt"}, + } + + cmd := NewTemplateCommand() + t.Run("Template Fail", func(t *testing.T) { + err := createTemplate(cmd, nil) + if err == nil { + t.Error("Expected error, got nil") + } + }) + + for _, pt := range platformTests { + t.Run("Template "+pt.platform+" Success", func(t *testing.T) { + args := []string{"--platform", pt.platform} + cmd.SetArgs(args) + if err := cmd.Execute(); err != nil { + t.Errorf("Failed to execute command: %v", err) + } + + if err := createTemplate(cmd, args); err != nil { + t.Errorf("createTemplate failed: %v", err) + } + }) + } +} diff --git a/cmd/upgrade_test.go b/cmd/upgrade_test.go new file mode 100644 index 0000000..cb27748 --- /dev/null +++ b/cmd/upgrade_test.go @@ -0,0 +1,35 @@ +/* +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 cmd + +import ( + "testing" +) + +func TestUpgrade(t *testing.T) { + cmd := NewUpgradeCommand() + args := []string{"--cluster-id", "cluster", "--imageurl", "http://example.com/image"} + cmd.SetArgs(args) + if err := cmd.Execute(); err != nil { + t.Errorf("Failed to execute command: %v", err) + } + + t.Run("UpgradeCmd Fail", func(t *testing.T) { + if err := runUpgradeCmd(cmd, args); err == nil { + t.Error("Expected error, got nil") + } + }) +} -- Gitee