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