github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/cce/v3/nodes_test.go (about)

     1  package v3
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/opentelekomcloud/gophertelekomcloud"
    11  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
    12  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack/cce"
    13  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/nodes"
    14  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    15  )
    16  
    17  type testNodes struct {
    18  	suite.Suite
    19  
    20  	vpcID     string
    21  	subnetID  string
    22  	clusterID string
    23  	kmsID     string
    24  }
    25  
    26  func TestNodes(t *testing.T) {
    27  	suite.Run(t, new(testNodes))
    28  }
    29  
    30  func (s *testNodes) SetupSuite() {
    31  	t := s.T()
    32  	s.vpcID = clients.EnvOS.GetEnv("VPC_ID")
    33  	s.subnetID = clients.EnvOS.GetEnv("NETWORK_ID")
    34  	s.kmsID = clients.EnvOS.GetEnv("KMS_ID")
    35  	if s.vpcID == "" || s.subnetID == "" {
    36  		t.Skip("OS_VPC_ID and OS_NETWORK_ID are required for this test")
    37  	}
    38  	s.clusterID = cce.CreateCluster(t, s.vpcID, s.subnetID)
    39  }
    40  
    41  func (s *testNodes) TearDownSuite() {
    42  	t := s.T()
    43  	if s.clusterID != "" {
    44  		cce.DeleteCluster(t, s.clusterID)
    45  		s.clusterID = ""
    46  	}
    47  }
    48  
    49  func (s *testNodes) TestNodeLifecycle() {
    50  	t := s.T()
    51  	client, err := clients.NewCceV3Client()
    52  	th.AssertNoErr(t, err)
    53  
    54  	privateIP := "192.168.0.12" // suppose used subnet is 192.168.0.0/16
    55  
    56  	kp := cce.CreateKeypair(t)
    57  	defer cce.DeleteKeypair(t, kp)
    58  
    59  	var encryption string
    60  	if s.kmsID != "" {
    61  		encryption = "1"
    62  	} else {
    63  		encryption = "0"
    64  	}
    65  
    66  	opts := nodes.CreateOpts{
    67  		Kind:       "Node",
    68  		ApiVersion: "v3",
    69  		Metadata: nodes.CreateMetaData{
    70  			Name: "nodes-test",
    71  		},
    72  		Spec: nodes.Spec{
    73  			Flavor: "s2.xlarge.2",
    74  			Az:     "eu-de-01",
    75  			Os:     "EulerOS 2.9",
    76  			Login: nodes.LoginSpec{
    77  				SshKey: kp,
    78  			},
    79  			RootVolume: nodes.VolumeSpec{
    80  				Size:       40,
    81  				VolumeType: "SSD",
    82  			},
    83  			DataVolumes: []nodes.VolumeSpec{
    84  				{
    85  					Size:       100,
    86  					VolumeType: "SSD",
    87  					Metadata: map[string]interface{}{
    88  						"__system__encrypted": encryption,
    89  						"__system__cmkid":     s.kmsID,
    90  					},
    91  				},
    92  			},
    93  			Count: 1,
    94  			NodeNicSpec: nodes.NodeNicSpec{
    95  				PrimaryNic: nodes.PrimaryNic{
    96  					SubnetId: s.subnetID,
    97  					FixedIPs: []string{privateIP},
    98  				},
    99  			},
   100  			Runtime: nodes.RuntimeSpec{
   101  				Name: "containerd",
   102  			},
   103  			ExtendParam: nodes.ExtendParam{
   104  				MaxPods:        16,
   105  				DockerBaseSize: 20,
   106  			},
   107  		},
   108  	}
   109  
   110  	if v := os.Getenv("OS_AGENCY_NAME"); v != "" {
   111  		opts.Spec.ExtendParam.AgencyName = v
   112  	}
   113  
   114  	node, err := nodes.Create(client, s.clusterID, opts).Extract()
   115  	th.AssertNoErr(t, err)
   116  
   117  	nodeID := node.Metadata.Id
   118  
   119  	th.AssertNoErr(t, golangsdk.WaitFor(1800, func() (bool, error) {
   120  		n, err := nodes.Get(client, s.clusterID, nodeID).Extract()
   121  		if err != nil {
   122  			return false, err
   123  		}
   124  		if n.Status.Phase == "Active" {
   125  			return true, nil
   126  		}
   127  		time.Sleep(10 * time.Second)
   128  		return false, nil
   129  	}))
   130  
   131  	state, err := nodes.Get(client, s.clusterID, nodeID).Extract()
   132  	th.AssertNoErr(t, err)
   133  	th.AssertEquals(t, privateIP, state.Status.PrivateIP)
   134  
   135  	th.AssertNoErr(t, nodes.Delete(client, s.clusterID, nodeID).ExtractErr())
   136  
   137  	err = golangsdk.WaitFor(1800, func() (bool, error) {
   138  		_, err := nodes.Get(client, s.clusterID, nodeID).Extract()
   139  		if err != nil {
   140  			if _, ok := err.(golangsdk.ErrDefault404); ok {
   141  				return true, nil
   142  			}
   143  			return false, err
   144  		}
   145  		return false, nil
   146  	})
   147  	th.AssertNoErr(t, err)
   148  }