github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/infra/test/container_test.go (about) 1 // Copyright © 2021 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 infra 16 17 import ( 18 "fmt" 19 "strconv" 20 "testing" 21 "time" 22 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 25 "github.com/sealerio/sealer/pkg/infra/container" 26 dc "github.com/sealerio/sealer/pkg/infra/container/client" 27 v1 "github.com/sealerio/sealer/types/api/v1" 28 ) 29 30 func SetUpClient() (*container.ApplyProvider, error) { 31 cluster := &v1.Cluster{ 32 TypeMeta: metav1.TypeMeta{ 33 Kind: "Cluster", 34 APIVersion: "zlink.aliyun.com/v1alpha1", 35 }, 36 ObjectMeta: metav1.ObjectMeta{ 37 Name: "my-cluster", 38 }, 39 Spec: v1.ClusterSpec{ 40 Masters: v1.Hosts{ 41 Count: "1", 42 CPU: "2", 43 Memory: "4", 44 SystemDisk: "100", 45 DataDisks: []string{"100"}, 46 }, 47 Nodes: v1.Hosts{ 48 Count: "1", 49 CPU: "2", 50 Memory: "4", 51 SystemDisk: "100", 52 DataDisks: []string{"100"}, 53 }, 54 Provider: container.CONTAINER, 55 SSH: v1.SSH{ 56 Passwd: "zhy76", 57 }, 58 }, 59 } 60 61 client, err := container.NewClientWithCluster(cluster) 62 63 if err != nil { 64 fmt.Printf("new docker client failed,%v", err) 65 } 66 return client, nil 67 } 68 69 func TestContainerResource(t *testing.T) { 70 //setup Container client 71 client, _ := SetUpClient() 72 t.Run("apply docker container", func(t *testing.T) { 73 id, err := client.Provider.RunContainer(&dc.CreateOptsForContainer{ 74 ContainerName: "test-container", 75 ContainerHostName: "test-container-host-name", 76 ImageName: container.ImageName, 77 NetworkName: container.NetworkName, 78 }) 79 if err != nil { 80 t.Logf("failed to run container %v", err) 81 return 82 } 83 84 info, err := client.Provider.GetContainerInfo(id, container.NetworkName) 85 if err != nil { 86 t.Logf("failed to get container info of %s ,error is %v", id, err) 87 return 88 } 89 if info.Status != "running" { 90 t.Logf("failed to get container info %s,container is %v", id, info.Status) 91 return 92 } 93 err = client.Provider.RmContainer(id) 94 if err != nil { 95 t.Logf("failed to delete container:%s", id) 96 return 97 } 98 99 t.Logf("succuss to apply docker container") 100 }) 101 } 102 103 func TestContainerApply(t *testing.T) { 104 cluster := &v1.Cluster{ 105 TypeMeta: metav1.TypeMeta{ 106 Kind: "Cluster", 107 APIVersion: "zlink.aliyun.com/v1alpha1", 108 }, 109 ObjectMeta: metav1.ObjectMeta{ 110 Name: "my-cluster", 111 }, 112 Spec: v1.ClusterSpec{ 113 Masters: v1.Hosts{ 114 Count: "1", 115 CPU: "2", 116 Memory: "2", 117 SystemDisk: "100", 118 DataDisks: []string{"100"}, 119 }, 120 Nodes: v1.Hosts{ 121 Count: "1", 122 CPU: "2", 123 Memory: "2", 124 SystemDisk: "100", 125 DataDisks: []string{"100"}, 126 }, 127 Provider: container.CONTAINER, 128 SSH: v1.SSH{ 129 Passwd: "zhy76", 130 }, 131 }, 132 } 133 134 client, err := container.NewClientWithCluster(cluster) 135 if err != nil { 136 t.Logf("failed to new container client") 137 return 138 } 139 // new apply: 1 master + 1 node 140 err = client.Apply() 141 if err != nil { 142 t.Logf("failed to apply container infra %v", err) 143 return 144 } 145 if CheckContainerApplyResult(cluster) { 146 t.Logf("container infra does not meet expectation %+v", cluster) 147 return 148 } 149 t.Logf("succuss to apply container infra") 150 // change apply :scale up ,3 master + 3 node 151 cluster.Spec.Masters.Count = "3" 152 cluster.Spec.Nodes.Count = "3" 153 err = client.Apply() 154 if err != nil { 155 t.Logf("failed to scale up container infra %v", err) 156 return 157 } 158 if CheckContainerApplyResult(cluster) { 159 t.Logf("container infra does not meet expectation %+v", cluster) 160 return 161 } 162 t.Logf("succuss to scale up container infra") 163 // change apply:scale down, 3 master + 1 node 164 cluster.Spec.Masters.Count = "3" 165 cluster.Spec.Nodes.Count = "1" 166 err = client.Apply() 167 if err != nil { 168 t.Logf("failed to scale down container infra %v", err) 169 return 170 } 171 if CheckContainerApplyResult(cluster) { 172 t.Logf("container infra does not meet expectation %+v", cluster) 173 return 174 } 175 t.Logf("succuss to scale down container infra") 176 // delete apply 177 time.Sleep(60 * time.Second) 178 now := metav1.Now() 179 cluster.ObjectMeta.DeletionTimestamp = &now 180 fmt.Printf("%v", client.Apply()) 181 t.Logf("succuss to clean up container infra") 182 } 183 184 func CheckContainerApplyResult(cluster *v1.Cluster) bool { 185 // return false if result do not meet expectation 186 // len(iplist) must equal count 187 masterCount, err := strconv.Atoi(cluster.Spec.Masters.Count) 188 if err != nil { 189 return true 190 } 191 nodeCount, err := strconv.Atoi(cluster.Spec.Nodes.Count) 192 if err != nil { 193 return true 194 } 195 196 if len(cluster.Spec.Masters.IPList) != masterCount || 197 len(cluster.Spec.Nodes.IPList) != nodeCount { 198 return true 199 } 200 201 return false 202 }