From ae4197f328a47141e24de2f7bd49fd04666a2a13 Mon Sep 17 00:00:00 2001 From: weihuanhuan Date: Fri, 5 Jul 2024 10:39:11 +0800 Subject: [PATCH] add terraform test file --- pkg/terraform/deploy_test.go | 65 +++++++++++++++++++++++++ pkg/terraform/generate_test.go | 89 ++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 pkg/terraform/deploy_test.go create mode 100644 pkg/terraform/generate_test.go diff --git a/pkg/terraform/deploy_test.go b/pkg/terraform/deploy_test.go new file mode 100644 index 0000000..4fdab5d --- /dev/null +++ b/pkg/terraform/deploy_test.go @@ -0,0 +1,65 @@ +/* +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 terraform + +import ( + "os" + "testing" +) + +func TestExecuteApplyTerraform(t *testing.T) { + err := os.Chdir("../../data") + if err != nil { + t.Fatal(err) + } + tfFileDir := "" + persistDir := "" + t.Run("ExecuteApplyTerraform_empty_dir", func(t *testing.T) { + terraform, err := ExecuteApplyTerraform(tfFileDir, persistDir) + if err != nil { + t.Error(err) + } + t.Log(string(terraform)) + }) + ///media/weihuanhuan/本地磁盘/weihuanhuan/goWorkspace/src/nestos-kubernetes-deployer/data/data/terraform/x86_64/nestos/libvirt/master.tf.template + tfFileDir = "data/terraform/x86_64/nestos/libvirt" + persistDir = "pkg/terraform" + t.Run("ExecuteApplyTerraform", func(t *testing.T) { + terraform, err := ExecuteApplyTerraform(tfFileDir, persistDir) + if err != nil { + t.Error(err) + } + t.Log(string(terraform)) + }) + + t.Run("ExecuteDestroyTerraform", func(t *testing.T) { + err := ExecuteDestroyTerraform(tfFileDir, persistDir) + if err != nil { + t.Error(err) + return + } + t.Log("ExecuteDestroyTerraform,success") + }) + + t.Run("Outputs", func(t *testing.T) { + by, err := Outputs(tfFileDir) + if err != nil { + t.Error(err) + return + } + t.Log("Outputs,success", string(by)) + }) +} diff --git a/pkg/terraform/generate_test.go b/pkg/terraform/generate_test.go new file mode 100644 index 0000000..a76a070 --- /dev/null +++ b/pkg/terraform/generate_test.go @@ -0,0 +1,89 @@ +/* +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 terraform + +import ( + "fmt" + "nestos-kubernetes-deployer/pkg/configmanager" + "nestos-kubernetes-deployer/pkg/configmanager/asset" + "nestos-kubernetes-deployer/pkg/configmanager/asset/infraasset" + "nestos-kubernetes-deployer/pkg/configmanager/globalconfig" + "os" + "testing" +) + +func TestGenerate(t *testing.T) { + //err := os.Chdir("../../data") + //if err != nil { + // t.Fatal(err) + //} + + conf := &asset.ClusterAsset{ + Platform: "libvirt", + Master: []asset.NodeAsset{ + {Hostname: "master"}, + {Hostname: "master02"}, + }, + Worker: []asset.NodeAsset{ + {Hostname: "worker"}, + }, + ClusterID: "k8s-001", + } + configmanager.GlobalConfig = &globalconfig.GlobalConfig{ + PersistDir: "./", + } + + inf := &Infra{} + + node := "master" + + t.Run("Generate_libvirt", func(t *testing.T) { + + libvirt := &infraasset.LibvirtAsset{ + URI: "sss", + } + conf.InfraPlatform = libvirt + err := inf.Generate(conf, node) + if err != nil { + t.Error(err) + return + } + t.Log("success") + }) + node = "worker" + conf.Architecture = "x86_64" + t.Run("Generate_openstack", func(t *testing.T) { + openstack := &infraasset.OpenStackAsset{ + UserName: "ssss", + } + conf.InfraPlatform = openstack + + conf.Platform = "openstack" + err := inf.Generate(conf, node) + if err != nil { + t.Error(err) + return + } + t.Log("success") + }) + rmDir := configmanager.GlobalConfig.PersistDir + conf.ClusterID + err := os.RemoveAll(rmDir) + if err != nil { + fmt.Printf("Error removing directory: %s\n", err) + } else { + fmt.Println("Directory removed successfully") + } +} -- Gitee