/acceptance/openstack/containerinfra/v1/nodegroups_test.go

https://github.com/gophercloud/gophercloud · Go · 172 lines · 130 code · 32 blank · 10 comment · 8 complexity · 1617558a300da3f5f15ee26831235d97 MD5 · raw file

  1. // +build acceptance containerinfra
  2. package v1
  3. import (
  4. "fmt"
  5. "testing"
  6. "time"
  7. "github.com/gophercloud/gophercloud"
  8. "github.com/gophercloud/gophercloud/acceptance/clients"
  9. "github.com/gophercloud/gophercloud/acceptance/tools"
  10. "github.com/gophercloud/gophercloud/openstack/containerinfra/v1/nodegroups"
  11. th "github.com/gophercloud/gophercloud/testhelper"
  12. )
  13. func TestNodeGroupsCRUD(t *testing.T) {
  14. // API not available until Magnum train
  15. clients.SkipRelease(t, "stable/mitaka")
  16. clients.SkipRelease(t, "stable/newton")
  17. clients.SkipRelease(t, "stable/ocata")
  18. clients.SkipRelease(t, "stable/pike")
  19. clients.SkipRelease(t, "stable/queens")
  20. clients.SkipRelease(t, "stable/rocky")
  21. clients.SkipRelease(t, "stable/stein")
  22. client, err := clients.NewContainerInfraV1Client()
  23. th.AssertNoErr(t, err)
  24. client.Microversion = "1.9"
  25. clusterTemplate, err := CreateKubernetesClusterTemplate(t, client)
  26. th.AssertNoErr(t, err)
  27. defer DeleteClusterTemplate(t, client, clusterTemplate.UUID)
  28. clusterID, err := CreateKubernetesCluster(t, client, clusterTemplate.UUID)
  29. th.AssertNoErr(t, err)
  30. defer DeleteCluster(t, client, clusterID)
  31. var nodeGroupID string
  32. t.Run("list", func(t *testing.T) { testNodeGroupsList(t, client, clusterID) })
  33. t.Run("listone-get", func(t *testing.T) { testNodeGroupGet(t, client, clusterID) })
  34. t.Run("create", func(t *testing.T) { nodeGroupID = testNodeGroupCreate(t, client, clusterID) })
  35. t.Logf("Created nodegroup: %s", nodeGroupID)
  36. // Wait for the node group to finish creating
  37. err = tools.WaitForTimeout(func() (bool, error) {
  38. ng, err := nodegroups.Get(client, clusterID, nodeGroupID).Extract()
  39. if err != nil {
  40. return false, fmt.Errorf("error waiting for node group to create: %v", err)
  41. }
  42. return (ng.Status == "CREATE_COMPLETE"), nil
  43. }, 900*time.Second)
  44. th.AssertNoErr(t, err)
  45. t.Run("update", func(t *testing.T) { testNodeGroupUpdate(t, client, clusterID, nodeGroupID) })
  46. t.Run("delete", func(t *testing.T) { testNodeGroupDelete(t, client, clusterID, nodeGroupID) })
  47. }
  48. func testNodeGroupsList(t *testing.T, client *gophercloud.ServiceClient, clusterID string) {
  49. allPages, err := nodegroups.List(client, clusterID, nil).AllPages()
  50. th.AssertNoErr(t, err)
  51. allNodeGroups, err := nodegroups.ExtractNodeGroups(allPages)
  52. th.AssertNoErr(t, err)
  53. // By default two node groups should be created
  54. th.AssertEquals(t, 2, len(allNodeGroups))
  55. }
  56. func testNodeGroupGet(t *testing.T, client *gophercloud.ServiceClient, clusterID string) {
  57. listOpts := nodegroups.ListOpts{
  58. Role: "worker",
  59. }
  60. allPages, err := nodegroups.List(client, clusterID, listOpts).AllPages()
  61. th.AssertNoErr(t, err)
  62. allNodeGroups, err := nodegroups.ExtractNodeGroups(allPages)
  63. th.AssertNoErr(t, err)
  64. // Should be one worker node group
  65. th.AssertEquals(t, 1, len(allNodeGroups))
  66. ngID := allNodeGroups[0].UUID
  67. ng, err := nodegroups.Get(client, clusterID, ngID).Extract()
  68. th.AssertNoErr(t, err)
  69. // Should have got the same node group as from the list
  70. th.AssertEquals(t, ngID, ng.UUID)
  71. th.AssertEquals(t, "worker", ng.Role)
  72. }
  73. func testNodeGroupCreate(t *testing.T, client *gophercloud.ServiceClient, clusterID string) string {
  74. name := tools.RandomString("test-ng-", 8)
  75. // have to create two nodes for the Update test (can't set minimum above actual node count)
  76. two := 2
  77. createOpts := nodegroups.CreateOpts{
  78. Name: name,
  79. NodeCount: &two,
  80. }
  81. ng, err := nodegroups.Create(client, clusterID, createOpts).Extract()
  82. th.AssertNoErr(t, err)
  83. th.AssertEquals(t, name, ng.Name)
  84. return ng.UUID
  85. }
  86. func testNodeGroupUpdate(t *testing.T, client *gophercloud.ServiceClient, clusterID, nodeGroupID string) {
  87. // Node group starts with min=1, max=unset
  88. // Set min, then set max, then set both
  89. updateOpts := []nodegroups.UpdateOptsBuilder{
  90. nodegroups.UpdateOpts{
  91. Op: nodegroups.ReplaceOp,
  92. Path: "/min_node_count",
  93. Value: 2,
  94. },
  95. }
  96. ng, err := nodegroups.Update(client, clusterID, nodeGroupID, updateOpts).Extract()
  97. th.AssertNoErr(t, err)
  98. th.AssertEquals(t, 2, ng.MinNodeCount)
  99. updateOpts = []nodegroups.UpdateOptsBuilder{
  100. nodegroups.UpdateOpts{
  101. Op: nodegroups.ReplaceOp,
  102. Path: "/max_node_count",
  103. Value: 5,
  104. },
  105. }
  106. ng, err = nodegroups.Update(client, clusterID, nodeGroupID, updateOpts).Extract()
  107. th.AssertNoErr(t, err)
  108. th.AssertEquals(t, false, ng.MaxNodeCount == nil)
  109. th.AssertEquals(t, 5, *ng.MaxNodeCount)
  110. updateOpts = []nodegroups.UpdateOptsBuilder{
  111. nodegroups.UpdateOpts{
  112. Op: nodegroups.ReplaceOp,
  113. Path: "/min_node_count",
  114. Value: 1,
  115. },
  116. nodegroups.UpdateOpts{
  117. Op: nodegroups.ReplaceOp,
  118. Path: "/max_node_count",
  119. Value: 3,
  120. },
  121. }
  122. ng, err = nodegroups.Update(client, clusterID, nodeGroupID, updateOpts).Extract()
  123. th.AssertNoErr(t, err)
  124. th.AssertEquals(t, false, ng.MaxNodeCount == nil)
  125. th.AssertEquals(t, 1, ng.MinNodeCount)
  126. th.AssertEquals(t, 3, *ng.MaxNodeCount)
  127. }
  128. func testNodeGroupDelete(t *testing.T, client *gophercloud.ServiceClient, clusterID, nodeGroupID string) {
  129. err := nodegroups.Delete(client, clusterID, nodeGroupID).ExtractErr()
  130. th.AssertNoErr(t, err)
  131. // Wait for the node group to be deleted
  132. err = tools.WaitFor(func() (bool, error) {
  133. _, err := nodegroups.Get(client, clusterID, nodeGroupID).Extract()
  134. if _, ok := err.(gophercloud.ErrDefault404); ok {
  135. return true, nil
  136. }
  137. return false, nil
  138. })
  139. th.AssertNoErr(t, err)
  140. }