github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/containerinfra/v1/nodegroups/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/vnpaycloud-console/gophercloud/v2"
     9  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/containerinfra/v1/nodegroups"
    10  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    11  	fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    12  )
    13  
    14  // TestGetNodeGroupSuccess gets a node group successfully.
    15  func TestGetNodeGroupSuccess(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	handleGetNodeGroupSuccess(t)
    20  
    21  	sc := fake.ServiceClient()
    22  	sc.Endpoint = sc.Endpoint + "v1/"
    23  
    24  	ng, err := nodegroups.Get(context.TODO(), sc, clusterUUID, nodeGroup1UUID).Extract()
    25  	th.AssertNoErr(t, err)
    26  
    27  	th.AssertDeepEquals(t, expectedNodeGroup1, *ng)
    28  }
    29  
    30  // TestGetNodeGroupNotFound tries to get a node group which does not exist.
    31  func TestGetNodeGroupNotFound(t *testing.T) {
    32  	th.SetupHTTP()
    33  	defer th.TeardownHTTP()
    34  
    35  	handleGetNodeGroupNotFound(t)
    36  
    37  	sc := fake.ServiceClient()
    38  	sc.Endpoint = sc.Endpoint + "v1/"
    39  
    40  	_, err := nodegroups.Get(context.TODO(), sc, clusterUUID, badNodeGroupUUID).Extract()
    41  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusNotFound))
    42  }
    43  
    44  // TestGetNodeGroupClusterNotFound tries to get a node group in
    45  // a cluster which does not exist.
    46  func TestGetNodeGroupClusterNotFound(t *testing.T) {
    47  	th.SetupHTTP()
    48  	defer th.TeardownHTTP()
    49  
    50  	handleGetNodeGroupClusterNotFound(t)
    51  
    52  	sc := fake.ServiceClient()
    53  	sc.Endpoint = sc.Endpoint + "v1/"
    54  
    55  	_, err := nodegroups.Get(context.TODO(), sc, badClusterUUID, badNodeGroupUUID).Extract()
    56  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusNotFound))
    57  }
    58  
    59  // TestListNodeGroupsSuccess lists the node groups of a cluster successfully.
    60  func TestListNodeGroupsSuccess(t *testing.T) {
    61  	th.SetupHTTP()
    62  	defer th.TeardownHTTP()
    63  
    64  	handleListNodeGroupsSuccess(t)
    65  
    66  	sc := fake.ServiceClient()
    67  	sc.Endpoint = sc.Endpoint + "v1/"
    68  
    69  	ngPages, err := nodegroups.List(sc, clusterUUID, nodegroups.ListOpts{}).AllPages(context.TODO())
    70  	th.AssertNoErr(t, err)
    71  
    72  	ngs, err := nodegroups.ExtractNodeGroups(ngPages)
    73  	th.AssertNoErr(t, err)
    74  	th.AssertEquals(t, 2, len(ngs))
    75  	th.AssertEquals(t, nodeGroup1UUID, ngs[0].UUID)
    76  	th.AssertEquals(t, nodeGroup2UUID, ngs[1].UUID)
    77  }
    78  
    79  // TestListNodeGroupsLimitSuccess tests listing node groups
    80  // with each returned page limited to one node group and
    81  // also giving a URL to get the next page.
    82  func TestListNodeGroupsLimitSuccess(t *testing.T) {
    83  	th.SetupHTTP()
    84  	defer th.TeardownHTTP()
    85  
    86  	handleListNodeGroupsLimitSuccess(t)
    87  
    88  	sc := fake.ServiceClient()
    89  	sc.Endpoint = sc.Endpoint + "v1/"
    90  
    91  	listOpts := nodegroups.ListOpts{Limit: 1}
    92  	ngPages, err := nodegroups.List(sc, clusterUUID, listOpts).AllPages(context.TODO())
    93  	th.AssertNoErr(t, err)
    94  
    95  	ngs, err := nodegroups.ExtractNodeGroups(ngPages)
    96  	th.AssertNoErr(t, err)
    97  	th.AssertEquals(t, 2, len(ngs))
    98  	th.AssertEquals(t, nodeGroup1UUID, ngs[0].UUID)
    99  	th.AssertEquals(t, nodeGroup2UUID, ngs[1].UUID)
   100  }
   101  
   102  // TestListNodeGroupsClusterNotFound tries to list node groups
   103  // of a cluster which does not exist.
   104  func TestListNodeGroupsClusterNotFound(t *testing.T) {
   105  	th.SetupHTTP()
   106  	defer th.TeardownHTTP()
   107  
   108  	handleListNodeGroupsClusterNotFound(t)
   109  
   110  	sc := fake.ServiceClient()
   111  	sc.Endpoint = sc.Endpoint + "v1/"
   112  
   113  	_, err := nodegroups.List(sc, clusterUUID, nodegroups.ListOpts{}).AllPages(context.TODO())
   114  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusNotFound))
   115  }
   116  
   117  // TestCreateNodeGroupSuccess creates a node group successfully.
   118  func TestCreateNodeGroupSuccess(t *testing.T) {
   119  	th.SetupHTTP()
   120  	defer th.TeardownHTTP()
   121  
   122  	handleCreateNodeGroupSuccess(t)
   123  
   124  	sc := fake.ServiceClient()
   125  	sc.Endpoint = sc.Endpoint + "v1/"
   126  
   127  	createOpts := nodegroups.CreateOpts{
   128  		Name:        "test-ng",
   129  		MergeLabels: gophercloud.Enabled,
   130  	}
   131  
   132  	ng, err := nodegroups.Create(context.TODO(), sc, clusterUUID, createOpts).Extract()
   133  	th.AssertNoErr(t, err)
   134  	th.AssertDeepEquals(t, expectedCreatedNodeGroup, *ng)
   135  }
   136  
   137  // TestCreateNodeGroupDuplicate creates a node group with
   138  // the same name as an existing one.
   139  func TestCreateNodeGroupDuplicate(t *testing.T) {
   140  	th.SetupHTTP()
   141  	defer th.TeardownHTTP()
   142  
   143  	handleCreateNodeGroupDuplicate(t)
   144  
   145  	sc := fake.ServiceClient()
   146  	sc.Endpoint = sc.Endpoint + "v1/"
   147  
   148  	createOpts := nodegroups.CreateOpts{
   149  		Name: "default-worker",
   150  	}
   151  
   152  	_, err := nodegroups.Create(context.TODO(), sc, clusterUUID, createOpts).Extract()
   153  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusConflict))
   154  }
   155  
   156  // TestCreateNodeGroupMaster creates a node group with
   157  // role=master which is not allowed.
   158  func TestCreateNodeGroupMaster(t *testing.T) {
   159  	th.SetupHTTP()
   160  	defer th.TeardownHTTP()
   161  
   162  	handleCreateNodeGroupMaster(t)
   163  
   164  	sc := fake.ServiceClient()
   165  	sc.Endpoint = sc.Endpoint + "v1/"
   166  
   167  	createOpts := nodegroups.CreateOpts{
   168  		Name: "new-ng",
   169  		Role: "master",
   170  	}
   171  
   172  	_, err := nodegroups.Create(context.TODO(), sc, clusterUUID, createOpts).Extract()
   173  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusBadRequest))
   174  }
   175  
   176  // TestCreateNodeGroupBadSizes creates a node group with
   177  // min_nodes greater than max_nodes.
   178  func TestCreateNodeGroupBadSizes(t *testing.T) {
   179  	th.SetupHTTP()
   180  	defer th.TeardownHTTP()
   181  
   182  	handleCreateNodeGroupBadSizes(t)
   183  
   184  	sc := fake.ServiceClient()
   185  	sc.Endpoint = sc.Endpoint + "v1/"
   186  
   187  	maxNodes := 3
   188  	createOpts := nodegroups.CreateOpts{
   189  		Name:         "default-worker",
   190  		MinNodeCount: 5,
   191  		MaxNodeCount: &maxNodes,
   192  	}
   193  
   194  	_, err := nodegroups.Create(context.TODO(), sc, clusterUUID, createOpts).Extract()
   195  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusConflict))
   196  }
   197  
   198  // TestUpdateNodeGroupSuccess updates a node group successfully.
   199  func TestUpdateNodeGroupSuccess(t *testing.T) {
   200  	th.SetupHTTP()
   201  	defer th.TeardownHTTP()
   202  
   203  	handleUpdateNodeGroupSuccess(t)
   204  
   205  	sc := fake.ServiceClient()
   206  	sc.Endpoint = sc.Endpoint + "v1/"
   207  
   208  	updateOpts := []nodegroups.UpdateOptsBuilder{
   209  		nodegroups.UpdateOpts{
   210  			Op:    nodegroups.ReplaceOp,
   211  			Path:  "/max_node_count",
   212  			Value: 3,
   213  		},
   214  	}
   215  
   216  	ng, err := nodegroups.Update(context.TODO(), sc, clusterUUID, nodeGroup2UUID, updateOpts).Extract()
   217  	th.AssertNoErr(t, err)
   218  	th.AssertDeepEquals(t, expectedUpdatedNodeGroup, *ng)
   219  }
   220  
   221  // TestUpdateNodeGroupInternal tries to update an internal
   222  // property of the node group.
   223  func TestUpdateNodeGroupInternal(t *testing.T) {
   224  	th.SetupHTTP()
   225  	defer th.TeardownHTTP()
   226  
   227  	handleUpdateNodeGroupInternal(t)
   228  
   229  	sc := fake.ServiceClient()
   230  	sc.Endpoint = sc.Endpoint + "v1/"
   231  
   232  	updateOpts := []nodegroups.UpdateOptsBuilder{
   233  		nodegroups.UpdateOpts{
   234  			Op:    nodegroups.ReplaceOp,
   235  			Path:  "/name",
   236  			Value: "newname",
   237  		},
   238  	}
   239  
   240  	_, err := nodegroups.Update(context.TODO(), sc, clusterUUID, nodeGroup2UUID, updateOpts).Extract()
   241  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusBadRequest))
   242  }
   243  
   244  // TestUpdateNodeGroupBadField tries to update a
   245  // field of the node group that does not exist.
   246  func TestUpdateNodeGroupBadField(t *testing.T) {
   247  	th.SetupHTTP()
   248  	defer th.TeardownHTTP()
   249  
   250  	handleUpdateNodeGroupBadField(t)
   251  
   252  	sc := fake.ServiceClient()
   253  	sc.Endpoint = sc.Endpoint + "v1/"
   254  
   255  	updateOpts := []nodegroups.UpdateOptsBuilder{
   256  		nodegroups.UpdateOpts{
   257  			Op:    nodegroups.ReplaceOp,
   258  			Path:  "/bad_field",
   259  			Value: "abc123",
   260  		},
   261  	}
   262  
   263  	_, err := nodegroups.Update(context.TODO(), sc, clusterUUID, nodeGroup2UUID, updateOpts).Extract()
   264  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusBadRequest))
   265  }
   266  
   267  // TestUpdateNodeGroupBadMin tries to set a minimum node count
   268  // greater than the current node count
   269  func TestUpdateNodeGroupBadMin(t *testing.T) {
   270  	th.SetupHTTP()
   271  	defer th.TeardownHTTP()
   272  
   273  	handleUpdateNodeGroupBadMin(t)
   274  
   275  	sc := fake.ServiceClient()
   276  	sc.Endpoint = sc.Endpoint + "v1/"
   277  
   278  	updateOpts := []nodegroups.UpdateOptsBuilder{
   279  		nodegroups.UpdateOpts{
   280  			Op:    nodegroups.ReplaceOp,
   281  			Path:  "/min_node_count",
   282  			Value: 5,
   283  		},
   284  	}
   285  
   286  	_, err := nodegroups.Update(context.TODO(), sc, clusterUUID, nodeGroup2UUID, updateOpts).Extract()
   287  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusConflict))
   288  }
   289  
   290  // TestDeleteNodeGroupSuccess deletes a node group successfully.
   291  func TestDeleteNodeGroupSuccess(t *testing.T) {
   292  	th.SetupHTTP()
   293  	defer th.TeardownHTTP()
   294  
   295  	handleDeleteNodeGroupSuccess(t)
   296  
   297  	sc := fake.ServiceClient()
   298  	sc.Endpoint = sc.Endpoint + "v1/"
   299  
   300  	err := nodegroups.Delete(context.TODO(), sc, clusterUUID, nodeGroup2UUID).ExtractErr()
   301  	th.AssertNoErr(t, err)
   302  }
   303  
   304  // TestDeleteNodeGroupNotFound tries to delete a node group that does not exist.
   305  func TestDeleteNodeGroupNotFound(t *testing.T) {
   306  	th.SetupHTTP()
   307  	defer th.TeardownHTTP()
   308  
   309  	handleDeleteNodeGroupNotFound(t)
   310  
   311  	sc := fake.ServiceClient()
   312  	sc.Endpoint = sc.Endpoint + "v1/"
   313  
   314  	err := nodegroups.Delete(context.TODO(), sc, clusterUUID, badNodeGroupUUID).ExtractErr()
   315  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusNotFound))
   316  }
   317  
   318  // TestDeleteNodeGroupClusterNotFound tries to delete a node group in a cluster that does not exist.
   319  func TestDeleteNodeGroupClusterNotFound(t *testing.T) {
   320  	th.SetupHTTP()
   321  	defer th.TeardownHTTP()
   322  
   323  	handleDeleteNodeGroupClusterNotFound(t)
   324  
   325  	sc := fake.ServiceClient()
   326  	sc.Endpoint = sc.Endpoint + "v1/"
   327  
   328  	err := nodegroups.Delete(context.TODO(), sc, badClusterUUID, badNodeGroupUUID).ExtractErr()
   329  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusNotFound))
   330  }
   331  
   332  // TestDeleteNodeGroupDefault tries to delete a protected default node group.
   333  func TestDeleteNodeGroupDefault(t *testing.T) {
   334  	th.SetupHTTP()
   335  	defer th.TeardownHTTP()
   336  
   337  	handleDeleteNodeGroupDefault(t)
   338  
   339  	sc := fake.ServiceClient()
   340  	sc.Endpoint = sc.Endpoint + "v1/"
   341  
   342  	err := nodegroups.Delete(context.TODO(), sc, clusterUUID, nodeGroup2UUID).ExtractErr()
   343  	th.AssertEquals(t, true, gophercloud.ResponseCodeIs(err, http.StatusBadRequest))
   344  }