github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/infra/infra_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 "testing" 20 21 "github.com/sealerio/sealer/utils/net" 22 ) 23 24 /* 25 func TestApply(t *testing.T) { 26 cluster := v1.Cluster{} 27 yamlFile, err := ioutil.ReadFile("./Clusterfile") 28 if err != nil { 29 t.Errorf("read yaml file get an err #%v", err) 30 } 31 err = yaml.Unmarshal(yamlFile, &cluster) 32 if err != nil { 33 t.Errorf("read yaml file get an err #%v", err) 34 } 35 36 aliProvider, err := NewDefaultProvider(&cluster) 37 if err != nil { 38 fmt.Printf("%v", err) 39 } else { 40 err = aliProvider.Apply() 41 if err != nil { 42 fmt.Printf("%v", err) 43 } 44 } 45 data, err := yaml.Marshal(&cluster) 46 if err != nil { 47 fmt.Printf("%v \n", err) 48 } 49 err = os.NewCommonWriter("./Clusterfile").WriteFile(data) 50 if err != nil { 51 fmt.Printf("%v \n", err) 52 } 53 } 54 55 func TestGetAKSKFromEnv(t *testing.T) { 56 config := aliyun.Config{} 57 err := aliyun.LoadConfig(&config) 58 if err != nil { 59 fmt.Printf("%v \n", err) 60 } 61 fmt.Printf("%v", config) 62 } 63 64 func TestDeleteInstances(t *testing.T) { 65 config := aliyun.Config{} 66 err := aliyun.LoadConfig(&config) 67 if err != nil { 68 fmt.Printf("%v \n", err) 69 } 70 client, err := ecs.NewClientWithAccessKey(config.RegionID, config.AccessKey, config.AccessSecret) 71 if client == nil { 72 fmt.Printf("%v \n", err) 73 } 74 if err != nil { 75 fmt.Printf("%v \n", err) 76 } 77 request := ecs.CreateDeleteInstancesRequest() 78 request.Scheme = aliyun.Scheme 79 request.Force = requests.NewBoolean(true) 80 request.InstanceId = &[]string{} 81 response, err := client.DeleteInstances(request) 82 if err != nil { 83 fmt.Print(err.Error()) 84 } 85 fmt.Printf("response is %#v\n", response) 86 } 87 func TestDeleteSecurityGroup(t *testing.T) { 88 config := aliyun.Config{} 89 err := aliyun.LoadConfig(&config) 90 if err != nil { 91 fmt.Printf("%v \n", err) 92 } 93 securityGroupIds := []string{ 94 "sg-hp38q702bczjtnb5qxdh", 95 "sg-hp33dkye42vdg38i49mg", 96 "sg-hp36xu038m1cqwcmltc7", 97 "sg-hp38q702bczjt9hxyjjk", 98 "sg-hp36xu038m1cqsekdpdy", 99 "sg-hp3250tdy1vv64i866dv", 100 "sg-hp36utl2950o9m7b0eg9", 101 "sg-hp34sj0h93usb66rs5zq", 102 } 103 for _, id := range securityGroupIds { 104 client, err := ecs.NewClientWithAccessKey(config.RegionID, config.AccessKey, config.AccessSecret) 105 if client == nil { 106 fmt.Printf("%v \n", err) 107 } 108 if err != nil { 109 fmt.Printf("%v \n", err) 110 } 111 request := ecs.CreateDeleteSecurityGroupRequest() 112 request.Scheme = aliyun.Scheme 113 request.SecurityGroupId = id 114 response, err := client.DeleteSecurityGroup(request) 115 if err != nil { 116 fmt.Print(err.Error()) 117 } 118 fmt.Printf("response is %#v\n", response) 119 } 120 } 121 122 func TestDeleteVswitch(t *testing.T) { 123 config := aliyun.Config{} 124 err := aliyun.LoadConfig(&config) 125 if err != nil { 126 fmt.Printf("%v \n", err) 127 } 128 vSwitchIDs := []string{ 129 "vsw-hp3kk6dkf7msos0cdej2f", 130 "vsw-hp31isullq0d3n32bd8bu", 131 "vsw-hp3y1hzn0dxpiagf9pwc5", 132 "vsw-hp37frkvd9hck3pyir2go", 133 "vsw-hp33g2g8nhh9d72mx4w6o", 134 "vsw-hp3mywuhbc77fpxagcft6", 135 "vsw-hp3xfh2gv576nx26t59kn", 136 "vsw-hp33c9qnqd73vehangsok", 137 "vsw-hp38rwznx0y14xi48nu7y", 138 } 139 for _, vSwitchID := range vSwitchIDs { 140 client, err := ecs.NewClientWithAccessKey(config.RegionID, config.AccessKey, config.AccessSecret) 141 if err != nil { 142 fmt.Printf("%v \n", err) 143 } 144 if client == nil { 145 fmt.Printf("%v \n", err) 146 } 147 request := ecs.CreateDeleteVSwitchRequest() 148 request.Scheme = aliyun.Scheme 149 request.VSwitchId = vSwitchID 150 response, err := client.DeleteVSwitch(request) 151 if err != nil { 152 fmt.Print(err.Error()) 153 } 154 fmt.Printf("response is %#v\n", response) 155 } 156 } 157 158 func TestDeleteVpc(t *testing.T) { 159 config := aliyun.Config{} 160 err := aliyun.LoadConfig(&config) 161 if err != nil { 162 fmt.Printf("%v \n", err) 163 } 164 vpcids := []string{ 165 "vpc-hp3e6ckmb1ngp1hkob9cb", 166 "vpc-hp3bw0rlgmuhdq68b1t6m", 167 "vpc-hp3qh53u6w15psvh856xz", 168 "vpc-hp3lii8nsnosi0bwt460o", 169 "vpc-hp3rn2b8i05l4pt7ksmed", 170 "vpc-hp35s2er96rn7lqiw9sgx", 171 "vpc-hp3djex4ingvqv8um5jtv", 172 "vpc-hp3byr74ugj7zg4r547ap", 173 "vpc-hp33jer8cd3epf8mu5m0k", 174 "vpc-hp35gdcac444eyrwbmv6z", 175 } 176 for _, vpcid := range vpcids { 177 client, err := ecs.NewClientWithAccessKey(config.RegionID, config.AccessKey, config.AccessSecret) 178 if client == nil { 179 fmt.Printf("%v \n", err) 180 } 181 if err != nil { 182 fmt.Printf("%v \n", err) 183 } 184 request := ecs.CreateDeleteVpcRequest() 185 request.Scheme = aliyun.Scheme 186 request.VpcId = vpcid 187 response, err := client.DeleteVpc(request) 188 if err != nil { 189 fmt.Print(err.Error()) 190 } 191 fmt.Printf("response is %#v\n", response) 192 } 193 } 194 195 func TestGetEIP(t *testing.T) { 196 config := aliyun.Config{} 197 err := aliyun.LoadConfig(&config) 198 if err != nil { 199 fmt.Printf("%v \n", err) 200 } 201 client, err := ecs.NewClientWithAccessKey(config.RegionID, config.AccessKey, config.AccessSecret) 202 if client == nil { 203 fmt.Printf("%v \n", err) 204 } 205 if err != nil { 206 fmt.Printf("%v \n", err) 207 } 208 request := ecs.CreateAllocateEipAddressRequest() 209 request.Scheme = aliyun.Scheme 210 response, err := client.AllocateEipAddress(request) 211 if err != nil { 212 fmt.Print(err.Error()) 213 } 214 fmt.Printf("response is %#v\n", response) 215 } 216 217 func TestReleaseEIP(t *testing.T) { 218 config := aliyun.Config{} 219 err := aliyun.LoadConfig(&config) 220 if err != nil { 221 fmt.Printf("%v \n", err) 222 } 223 client, _ := ecs.NewClientWithAccessKey(config.RegionID, config.AccessKey, config.AccessSecret) 224 eipid := []string{ 225 "eip-uf66uj4susq4lfmcyym8n", 226 "eip-uf6nvcr1zomdj805digd3", 227 "eip-uf6gpfd9h0bji4oj6qfqi", 228 "eip-uf6fh5mtal0mzai3higj3", 229 } 230 for _, s := range eipid { 231 request := ecs.CreateReleaseEipAddressRequest() 232 request.Scheme = aliyun.Scheme 233 request.AllocationId = s 234 response, err := client.ReleaseEipAddress(request) 235 if err != nil { 236 fmt.Print(err.Error()) 237 } 238 fmt.Printf("response is %#v\n", response) 239 } 240 } 241 */ 242 243 func TestSort(t *testing.T) { 244 iplist := []string{"192.168.0.3", "192.168.0.16", "192.168.0.4", "192.168.0.1"} 245 net.SortIPList(iplist) 246 fmt.Printf("%v", iplist) 247 }