github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/containerinfra/v1/clusters/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/vnpaycloud-console/gophercloud/v2" 8 "github.com/vnpaycloud-console/gophercloud/v2/openstack/containerinfra/v1/clusters" 9 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 10 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 11 fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 12 ) 13 14 func TestCreateCluster(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 HandleCreateClusterSuccessfully(t) 19 20 masterCount := 1 21 nodeCount := 1 22 createTimeout := 30 23 masterLBEnabled := true 24 opts := clusters.CreateOpts{ 25 ClusterTemplateID: "0562d357-8641-4759-8fed-8173f02c9633", 26 CreateTimeout: &createTimeout, 27 DiscoveryURL: "", 28 FlavorID: "m1.small", 29 Keypair: "my_keypair", 30 Labels: map[string]string{}, 31 MasterCount: &masterCount, 32 MasterFlavorID: "m1.small", 33 MasterLBEnabled: &masterLBEnabled, 34 Name: "k8s", 35 NodeCount: &nodeCount, 36 FloatingIPEnabled: gophercloud.Enabled, 37 FixedNetwork: "private_network", 38 FixedSubnet: "private_subnet", 39 MergeLabels: gophercloud.Enabled, 40 } 41 42 sc := fake.ServiceClient() 43 sc.Endpoint = sc.Endpoint + "v1/" 44 res := clusters.Create(context.TODO(), sc, opts) 45 th.AssertNoErr(t, res.Err) 46 47 requestID := res.Header.Get("X-OpenStack-Request-Id") 48 th.AssertEquals(t, requestUUID, requestID) 49 50 actual, err := res.Extract() 51 th.AssertNoErr(t, err) 52 53 th.AssertDeepEquals(t, clusterUUID, actual) 54 } 55 56 func TestGetCluster(t *testing.T) { 57 th.SetupHTTP() 58 defer th.TeardownHTTP() 59 60 HandleGetClusterSuccessfully(t) 61 62 sc := fake.ServiceClient() 63 sc.Endpoint = sc.Endpoint + "v1/" 64 actual, err := clusters.Get(context.TODO(), sc, "746e779a-751a-456b-a3e9-c883d734946f").Extract() 65 th.AssertNoErr(t, err) 66 actual.CreatedAt = actual.CreatedAt.UTC() 67 actual.UpdatedAt = actual.UpdatedAt.UTC() 68 th.AssertDeepEquals(t, ExpectedCluster, *actual) 69 } 70 71 func TestListClusters(t *testing.T) { 72 th.SetupHTTP() 73 defer th.TeardownHTTP() 74 75 HandleListClusterSuccessfully(t) 76 77 count := 0 78 sc := fake.ServiceClient() 79 sc.Endpoint = sc.Endpoint + "v1/" 80 err := clusters.List(sc, clusters.ListOpts{Limit: 2}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 81 count++ 82 actual, err := clusters.ExtractClusters(page) 83 th.AssertNoErr(t, err) 84 for idx := range actual { 85 actual[idx].CreatedAt = actual[idx].CreatedAt.UTC() 86 actual[idx].UpdatedAt = actual[idx].UpdatedAt.UTC() 87 } 88 th.AssertDeepEquals(t, ExpectedClusters, actual) 89 90 return true, nil 91 }) 92 th.AssertNoErr(t, err) 93 94 if count != 1 { 95 t.Errorf("Expected 1 page, got %d", count) 96 } 97 } 98 99 func TestListDetailClusters(t *testing.T) { 100 th.SetupHTTP() 101 defer th.TeardownHTTP() 102 103 HandleListDetailClusterSuccessfully(t) 104 105 count := 0 106 sc := fake.ServiceClient() 107 sc.Endpoint = sc.Endpoint + "v1/" 108 err := clusters.ListDetail(sc, clusters.ListOpts{Limit: 2}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 109 count++ 110 actual, err := clusters.ExtractClusters(page) 111 th.AssertNoErr(t, err) 112 for idx := range actual { 113 actual[idx].CreatedAt = actual[idx].CreatedAt.UTC() 114 actual[idx].UpdatedAt = actual[idx].UpdatedAt.UTC() 115 } 116 th.AssertDeepEquals(t, ExpectedClusters, actual) 117 118 return true, nil 119 }) 120 th.AssertNoErr(t, err) 121 122 if count != 1 { 123 t.Errorf("Expected 1 page, got %d", count) 124 } 125 } 126 127 func TestUpdateCluster(t *testing.T) { 128 th.SetupHTTP() 129 defer th.TeardownHTTP() 130 131 HandleUpdateClusterSuccessfully(t) 132 133 updateOpts := []clusters.UpdateOptsBuilder{ 134 clusters.UpdateOpts{ 135 Op: clusters.ReplaceOp, 136 Path: "/master_lb_enabled", 137 Value: "True", 138 }, 139 clusters.UpdateOpts{ 140 Op: clusters.ReplaceOp, 141 Path: "/registry_enabled", 142 Value: "True", 143 }, 144 } 145 146 sc := fake.ServiceClient() 147 sc.Endpoint = sc.Endpoint + "v1/" 148 res := clusters.Update(context.TODO(), sc, clusterUUID, updateOpts) 149 th.AssertNoErr(t, res.Err) 150 151 requestID := res.Header.Get("X-OpenStack-Request-Id") 152 th.AssertEquals(t, requestUUID, requestID) 153 154 actual, err := res.Extract() 155 th.AssertNoErr(t, err) 156 157 th.AssertDeepEquals(t, clusterUUID, actual) 158 } 159 160 func TestUpgradeCluster(t *testing.T) { 161 th.SetupHTTP() 162 defer th.TeardownHTTP() 163 164 HandleUpgradeClusterSuccessfully(t) 165 166 opts := clusters.UpgradeOpts{ 167 ClusterTemplate: "0562d357-8641-4759-8fed-8173f02c9633", 168 } 169 170 sc := fake.ServiceClient() 171 sc.Endpoint = sc.Endpoint + "v1/" 172 res := clusters.Upgrade(context.TODO(), sc, clusterUUID, opts) 173 th.AssertNoErr(t, res.Err) 174 175 requestID := res.Header.Get("X-OpenStack-Request-Id") 176 th.AssertEquals(t, requestUUID, requestID) 177 178 actual, err := res.Extract() 179 th.AssertNoErr(t, err) 180 181 th.AssertDeepEquals(t, clusterUUID, actual) 182 } 183 184 func TestDeleteCluster(t *testing.T) { 185 th.SetupHTTP() 186 defer th.TeardownHTTP() 187 188 HandleDeleteClusterSuccessfully(t) 189 190 sc := fake.ServiceClient() 191 sc.Endpoint = sc.Endpoint + "v1/" 192 r := clusters.Delete(context.TODO(), sc, clusterUUID) 193 err := r.ExtractErr() 194 th.AssertNoErr(t, err) 195 196 uuid := "" 197 idKey := "X-Openstack-Request-Id" 198 if len(r.Header[idKey]) > 0 { 199 uuid = r.Header[idKey][0] 200 if uuid == "" { 201 t.Errorf("No value for header [%s]", idKey) 202 } 203 } else { 204 t.Errorf("Missing header [%s]", idKey) 205 } 206 207 th.AssertEquals(t, requestUUID, uuid) 208 } 209 210 func TestResizeCluster(t *testing.T) { 211 th.SetupHTTP() 212 defer th.TeardownHTTP() 213 214 HandleResizeClusterSuccessfully(t) 215 216 nodeCount := 2 217 218 opts := clusters.ResizeOpts{ 219 NodeCount: &nodeCount, 220 } 221 222 sc := fake.ServiceClient() 223 sc.Endpoint = sc.Endpoint + "v1/" 224 res := clusters.Resize(context.TODO(), sc, clusterUUID, opts) 225 th.AssertNoErr(t, res.Err) 226 227 requestID := res.Header.Get("X-OpenStack-Request-Id") 228 th.AssertEquals(t, requestUUID, requestID) 229 230 actual, err := res.Extract() 231 th.AssertNoErr(t, err) 232 233 th.AssertDeepEquals(t, clusterUUID, actual) 234 }