From 377553ee0ffb1ff2ffd6d1d8fc0589a75ed5b5cc Mon Sep 17 00:00:00 2001 From: lauk001 Date: Tue, 9 Jul 2024 10:12:18 +0800 Subject: [PATCH] Delete the temporary file generated by the test case --- cmd/deploy_test.go | 22 ++++++------ cmd/destroy_test.go | 4 +++ cmd/extend_test.go | 13 +++++++ cmd/template_test.go | 29 ++++++++++----- cmd/upgrade_test.go | 35 ------------------- .../bootconfig/cloudinit/cloudinit_test.go | 2 +- pkg/osmanager/osmanager_test.go | 8 +++++ pkg/terraform/deploy_test.go | 10 ++++++ 8 files changed, 69 insertions(+), 54 deletions(-) delete mode 100644 cmd/upgrade_test.go diff --git a/cmd/deploy_test.go b/cmd/deploy_test.go index 2cf67b0..1f2840b 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 69c64f1..7bef417 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 f09ca79..3e020e2 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 0eeb01d..fe4d4f7 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 cb27748..0000000 --- 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 af5db8e..ea0fe9f 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 5c33534..f06477a 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 4fdab5d..4bea9c6 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) { -- Gitee