diff --git a/cmd/deploy_test.go b/cmd/deploy_test.go index 2cf67b05da14c93208ecdb8914b0203a69af46fa..1f2840b102a595e659570a9d93a21bacb7db7893 100644 --- a/cmd/deploy_test.go +++ b/cmd/deploy_test.go @@ -21,8 +21,6 @@ import ( "nestos-kubernetes-deployer/pkg/configmanager/asset/infraasset" "os" "testing" - - "gopkg.in/yaml.v2" ) func TestDeploy(t *testing.T) { @@ -83,14 +81,6 @@ func TestDeploy(t *testing.T) { }, } - 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) @@ -102,6 +92,18 @@ func TestDeploy(t *testing.T) { if err := runDeployCmd(cmd, args); err == nil { t.Error("Expected error, got nil") } + // Clean up + if err := os.RemoveAll("logs"); err != nil { + t.Errorf("Failed to remove logs folder: %v", err) + } + + if _, err := os.Stat("global_config.yaml"); os.IsNotExist(err) { + t.Errorf("Expected global_config.yaml to be created, but it does not exist") + } + + if err := os.Remove("global_config.yaml"); err != nil { + t.Errorf("Failed to remove global_config.yaml: %v", err) + } }) t.Run("clusterCreatePost Fail", func(t *testing.T) { diff --git a/cmd/destroy_test.go b/cmd/destroy_test.go index 69c64f1afc1e2862ef7f7dd514d1d0467103805d..7bef4174bc8a1d6cfbb4426409c96c26c3126161 100644 --- a/cmd/destroy_test.go +++ b/cmd/destroy_test.go @@ -17,6 +17,7 @@ package cmd import ( "nestos-kubernetes-deployer/cmd/command/opts" + "os" "testing" ) @@ -34,5 +35,8 @@ func TestDestroy(t *testing.T) { if err := runDestroyCmd(cmd, args); err == nil { t.Error("Expected error, got nil") } + if err := os.RemoveAll("logs"); err != nil { + t.Errorf("Failed to remove logs folder: %v", err) + } }) } diff --git a/cmd/extend_test.go b/cmd/extend_test.go index f09ca790891e3c6dab22b8af118724baf7e26a72..3e020e22abae275c4223e887cc0694d34e033517 100644 --- a/cmd/extend_test.go +++ b/cmd/extend_test.go @@ -17,6 +17,7 @@ package cmd import ( "nestos-kubernetes-deployer/cmd/command/opts" + "os" "testing" ) @@ -33,5 +34,17 @@ func TestExtend(t *testing.T) { if err := runExtendCmd(cmd, args); err == nil { t.Error("Expected error, got nil") } + // Clean up + if err := os.RemoveAll("logs"); err != nil { + t.Errorf("Failed to remove logs folder: %v", err) + } + + if _, err := os.Stat("global_config.yaml"); os.IsNotExist(err) { + t.Errorf("Expected global_config.yaml to be created, but it does not exist") + } + + if err := os.Remove("global_config.yaml"); err != nil { + t.Errorf("Failed to remove global_config.yaml: %v", err) + } }) } diff --git a/cmd/template_test.go b/cmd/template_test.go index 0eeb01de9827b51a479ca1252b3a28762ad17766..fe4d4f78e4bc3c0c59041218ad78d3020e8495b8 100644 --- a/cmd/template_test.go +++ b/cmd/template_test.go @@ -15,19 +15,24 @@ limitations under the License. */ package cmd -import "testing" +import ( + "os" + "testing" +) func TestTemplate(t *testing.T) { platformTests := []struct { platform string + args []string }{ - {"openstack"}, - {"pxe"}, - {"ipxe"}, - {"libvirt"}, + {"openstack", []string{"--platform", "openstack"}}, + {"pxe", []string{"--platform", "pxe"}}, + {"ipxe", []string{"--platform", "ipxe"}}, + {"libvirt", []string{"--platform", "libvirt"}}, } cmd := NewTemplateCommand() + t.Run("Template Fail", func(t *testing.T) { err := createTemplate(cmd, nil) if err == nil { @@ -37,15 +42,23 @@ func TestTemplate(t *testing.T) { for _, pt := range platformTests { t.Run("Template "+pt.platform+" Success", func(t *testing.T) { - args := []string{"--platform", pt.platform} - cmd.SetArgs(args) + cmd.SetArgs(pt.args) if err := cmd.Execute(); err != nil { t.Errorf("Failed to execute command: %v", err) } - if err := createTemplate(cmd, args); err != nil { + if err := createTemplate(cmd, pt.args); err != nil { t.Errorf("createTemplate failed: %v", err) } + + // Clean up + if _, err := os.Stat("template.yaml"); os.IsNotExist(err) { + t.Errorf("Expected template.yaml to be created, but it does not exist") + } + + if err := os.Remove("template.yaml"); err != nil { + t.Errorf("Failed to remove template.yaml: %v", err) + } }) } } diff --git a/cmd/upgrade_test.go b/cmd/upgrade_test.go deleted file mode 100644 index cb27748a56676a27acb2099adf0034f5bb92d9c5..0000000000000000000000000000000000000000 --- a/cmd/upgrade_test.go +++ /dev/null @@ -1,35 +0,0 @@ -/* -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") - } - }) -} diff --git a/pkg/osmanager/bootconfig/cloudinit/cloudinit_test.go b/pkg/osmanager/bootconfig/cloudinit/cloudinit_test.go index af5db8ec03919baec47cb1de52923d02f4baeedd..ea0fe9f4dc528fd6869bbfdae8289f9dd70fe38c 100644 --- a/pkg/osmanager/bootconfig/cloudinit/cloudinit_test.go +++ b/pkg/osmanager/bootconfig/cloudinit/cloudinit_test.go @@ -30,7 +30,7 @@ func TestCloudinit(t *testing.T) { t.Fatal(err) } clusterAsset := &asset.ClusterAsset{ - ClusterID: "k8s-master", + ClusterID: "cluster", Master: []asset.NodeAsset{ {Hostname: "master"}, {Hostname: "master02"}, diff --git a/pkg/osmanager/osmanager_test.go b/pkg/osmanager/osmanager_test.go index 5c3353406142818be130e9fcefd7d2139b4b01b5..f06477a39d14265d8ca6c1aaebc5f8592294973a 100644 --- a/pkg/osmanager/osmanager_test.go +++ b/pkg/osmanager/osmanager_test.go @@ -115,6 +115,10 @@ func TestOsmanager(t *testing.T) { if err != nil { t.Fatalf("Failed to create NewOSManager instance: %v", err) } + + if err := os.RemoveAll(clusterconfig.ClusterID); err != nil { + t.Errorf("Failed to remove cluster folder: %v", err) + } }) t.Run("GenerateOSConfig_Fail", func(t *testing.T) { @@ -127,5 +131,9 @@ func TestOsmanager(t *testing.T) { if err == nil { t.Error("Expected error, got nil") } + + if err := os.RemoveAll(clusterconfig.ClusterID); err != nil { + t.Errorf("Failed to remove cluster folder: %v", err) + } }) } diff --git a/pkg/terraform/deploy_test.go b/pkg/terraform/deploy_test.go index 4fdab5d7500fb68d770d451be3b06b6778e3eacb..4bea9c67c07fafe82aabb035478e5659490afb50 100644 --- a/pkg/terraform/deploy_test.go +++ b/pkg/terraform/deploy_test.go @@ -52,6 +52,16 @@ func TestExecuteApplyTerraform(t *testing.T) { return } t.Log("ExecuteDestroyTerraform,success") + + tmpFile := tfFileDir + "/terraform.tfstate" + if err := os.Remove(tmpFile); err != nil { + t.Errorf("Failed to remove terraform.tfstate: %v", err) + } + + tmpDir := tfFileDir + "/.terraform" + if err := os.RemoveAll(tmpDir); err != nil { + t.Errorf("Failed to remove %s: %v", tmpDir, err) + } }) t.Run("Outputs", func(t *testing.T) {