github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/clusterfile/clusterfile_test.go (about) 1 // Copyright © 2022 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package clusterfile 16 17 import ( 18 "net" 19 "os" 20 "path/filepath" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 25 v1 "github.com/sealerio/sealer/types/api/v1" 26 v2 "github.com/sealerio/sealer/types/api/v2" 27 28 "github.com/sealerio/sealer/common" 29 ) 30 31 var defaultHA = true 32 var defaultInsecure = false 33 34 func TestSaveAll(t *testing.T) { 35 cluster := v2.Cluster{ 36 Spec: v2.ClusterSpec{ 37 Image: "kubernetes:v1.19.8", 38 DataRoot: "/var/lib/sealer/data", 39 Env: []string{"key1=value1", "key2=value2;value3", "key=value", 40 "LocalRegistryDomain=sea.hub", "LocalRegistryPort=5000", "LocalRegistryURL=sea.hub:5000", 41 "RegistryDomain=sea.hub", "RegistryPort=5000", "RegistryURL=sea.hub:5000"}, 42 Registry: v2.Registry{ 43 LocalRegistry: &v2.LocalRegistry{ 44 RegistryConfig: v2.RegistryConfig{ 45 Domain: "sea.hub", 46 Port: 5000, 47 }, 48 HA: &defaultHA, 49 Insecure: &defaultInsecure, 50 }, 51 }, 52 SSH: v1.SSH{ 53 User: "root", 54 Passwd: "test123", 55 Port: "22", 56 Pk: "xxx", 57 PkPasswd: "xxx", 58 }, 59 Hosts: []v2.Host{ 60 { 61 IPS: []net.IP{net.IPv4(192, 168, 0, 2)}, 62 Roles: []string{"master"}, 63 Env: []string{"etcd-dir=/data/etcd"}, 64 SSH: v1.SSH{ 65 User: "root", 66 Passwd: "test456", 67 Port: "22", 68 }, 69 }, 70 { 71 IPS: []net.IP{net.IPv4(192, 168, 0, 3)}, 72 Roles: []string{"node", "db"}, 73 }, 74 }, 75 }, 76 } 77 cluster.APIVersion = "sealer.io/v2" 78 cluster.Kind = "Cluster" 79 cluster.Name = "my-cluster" 80 81 plugin2 := v1.Plugin{ 82 Spec: v1.PluginSpec{ 83 Type: "SHELL", 84 Data: "kubectl get nodes\n", 85 Scope: "master", 86 Action: "PostInstall", 87 }, 88 } 89 plugin2.Name = "MyShell" 90 plugin2.Kind = "Plugin" 91 plugin2.APIVersion = "sealer.io/v1" 92 93 config := v1.Config{ 94 Spec: v1.ConfigSpec{ 95 Path: "etc/mysql.yaml", 96 Data: "mysql-user: root\nmysql-passwd: xxx\n", 97 }, 98 } 99 config.Name = "mysql-config" 100 config.Kind = "Config" 101 config.APIVersion = "sealer.com/v1alpha1" 102 103 type wanted struct { 104 cluster v2.Cluster 105 config []v1.Config 106 plugins []v1.Plugin 107 } 108 109 type args struct { 110 wanted wanted 111 } 112 113 var tests = []struct { 114 name string 115 args args 116 }{ 117 { 118 name: "test decode cluster file", 119 args: args{ 120 wanted: wanted{ 121 cluster: cluster, 122 config: []v1.Config{config}, 123 plugins: []v1.Plugin{plugin2}, 124 }, 125 }, 126 }, 127 } 128 129 for _, tt := range tests { 130 t.Run(tt.name, func(t *testing.T) { 131 clusterFile := &ClusterFile{cluster: &cluster, configs: []v1.Config{config}, plugins: []v1.Plugin{plugin2}} 132 clusterFilePath := common.GetDefaultClusterfile() 133 if err := os.MkdirAll(filepath.Dir(clusterFilePath), common.FileMode0755); err != nil { 134 t.Errorf("failed to create directory, error is:(%v)", err) 135 } 136 if err := clusterFile.SaveAll(SaveOptions{}); err != nil { 137 t.Errorf("failed to save all file, error is:(%v)", err) 138 } 139 clusterFileData, err := os.ReadFile(filepath.Clean(clusterFilePath)) 140 if err != nil { 141 t.Errorf("failed to read cluster file, error is:(%v)", err) 142 } 143 144 cf, err := NewClusterFile(clusterFileData) 145 if err != nil { 146 t.Errorf("failed to get clusterfile interface, error is:(%v)", err) 147 } 148 149 assert.Equal(t, tt.args.wanted.config, cf.GetConfigs()) 150 assert.Equal(t, tt.args.wanted.plugins, cf.GetPlugins()) 151 assert.Equal(t, tt.args.wanted.cluster, cf.GetCluster()) 152 153 if err := os.Remove(clusterFilePath); err != nil { 154 t.Errorf("failed to remove clusterfile, error is:(%v)", err) 155 } 156 }) 157 } 158 }