From 3f54931d07fb2886ae6c330d5cdd0e3031c6dd63 Mon Sep 17 00:00:00 2001 From: "Muneeb H. Jan" Date: Wed, 18 Dec 2024 14:31:10 +0100 Subject: [PATCH] [Test] Update and refactor CCE addons test --- ...urce_opentelekomcloud_cce_addon_v3_test.go | 273 +++++++----------- 1 file changed, 101 insertions(+), 172 deletions(-) diff --git a/opentelekomcloud/acceptance/cce/resource_opentelekomcloud_cce_addon_v3_test.go b/opentelekomcloud/acceptance/cce/resource_opentelekomcloud_cce_addon_v3_test.go index dd70f31d0..cf6b919f3 100644 --- a/opentelekomcloud/acceptance/cce/resource_opentelekomcloud_cce_addon_v3_test.go +++ b/opentelekomcloud/acceptance/cce/resource_opentelekomcloud_cce_addon_v3_test.go @@ -7,8 +7,8 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/addons" + "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/cce/shared" "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common" - "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common/quotas" "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/env" "github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg" ) @@ -16,47 +16,44 @@ import ( const resourceAddonName = "opentelekomcloud_cce_addon_v3.autoscaler" const resourceAddonNameDns = "opentelekomcloud_cce_addon_v3.coredns" +func getCceAddonResourceFunc(cfg *cfg.Config, state *terraform.ResourceState) (interface{}, error) { + client, err := cfg.CceV3AddonClient(env.OS_REGION_NAME) + if err != nil { + return nil, fmt.Errorf("error creating CCE v3 Addon Client: %s", err) + } + return addons.Get(client, state.Primary.ID, state.Primary.Attributes["cluster_id"]) +} + func TestAccCCEAddonV3Basic(t *testing.T) { - clusterName := randClusterName() + var addon addons.Addon + rc := common.InitResourceCheck( + resourceAddonName, + &addon, + getCceAddonResourceFunc, + ) + + shared.BookCluster(t) t.Parallel() - quotas.BookOne(t, quotas.CCEClusterQuota) resource.Test(t, resource.TestCase{ PreCheck: func() { common.TestAccPreCheck(t) }, ProviderFactories: common.TestAccProviderFactories, - CheckDestroy: testAccCheckCCEAddonV3Destroy, + CheckDestroy: rc.CheckResourceDestroy(), Steps: []resource.TestStep{ { - Config: testAccCCEAddonV3Basic(clusterName), + Config: testAccCCEAddonV3Basic(), Check: resource.ComposeTestCheckFunc( checkScaleDownForAutoscaler(resourceAddonName, true), resource.TestCheckResourceAttr(resourceAddonName, "values.0.custom.scaleDownDelayAfterDelete", "11"), ), }, { - Config: testAccCCEAddonV3Updated(clusterName), + Config: testAccCCEAddonV3Updated(), Check: resource.ComposeTestCheckFunc( checkScaleDownForAutoscaler(resourceAddonName, false), resource.TestCheckResourceAttr(resourceAddonName, "values.0.custom.scaleDownDelayAfterDelete", "8"), ), }, - }, - }) -} - -func TestAccCCEAddonV3ImportBasic(t *testing.T) { - t.Parallel() - clusterName := randClusterName() - quotas.BookOne(t, quotas.CCEClusterQuota) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { common.TestAccPreCheck(t) }, - ProviderFactories: common.TestAccProviderFactories, - CheckDestroy: testAccCheckCCEAddonV3Destroy, - Steps: []resource.TestStep{ - { - Config: testAccCCEAddonV3Basic(clusterName), - }, { ResourceName: resourceAddonName, ImportState: true, @@ -70,43 +67,30 @@ func TestAccCCEAddonV3ImportBasic(t *testing.T) { }) } -func testAccCCEAddonV3ImportStateIdFunc() resource.ImportStateIdFunc { - return func(s *terraform.State) (string, error) { - var clusterID string - var addonID string - for _, rs := range s.RootModule().Resources { - if rs.Type == "opentelekomcloud_cce_cluster_v3" { - clusterID = rs.Primary.ID - } else if rs.Type == "opentelekomcloud_cce_addon_v3" { - addonID = rs.Primary.ID - } - } - if clusterID == "" || addonID == "" { - return "", fmt.Errorf("resource not found: %s/%s", clusterID, addonID) - } - return fmt.Sprintf("%s/%s", clusterID, addonID), nil - } -} - func TestAccCCEAddonV3ForceNewCCE(t *testing.T) { - clusterName := randClusterName() + var addon addons.Addon + rc := common.InitResourceCheck( + resourceAddonName, + &addon, + getCceAddonResourceFunc, + ) + shared.BookCluster(t) t.Parallel() - quotas.BookOne(t, quotas.CCEClusterQuota) resource.Test(t, resource.TestCase{ PreCheck: func() { common.TestAccPreCheck(t) }, ProviderFactories: common.TestAccProviderFactories, - CheckDestroy: testAccCheckCCEAddonV3Destroy, + CheckDestroy: rc.CheckResourceDestroy(), Steps: []resource.TestStep{ { - Config: testAccCCEAddonV3Basic(clusterName), + Config: testAccCCEAddonV3Basic(), Check: resource.ComposeTestCheckFunc( checkScaleDownForAutoscaler(resourceAddonName, true), resource.TestCheckResourceAttr(resourceAddonName, "values.0.custom.scaleDownDelayAfterDelete", "11"), ), }, { - Config: testAccCCEAddonV3ForceNew(clusterName), + Config: testAccCCEAddonV3ForceNew(), Check: resource.ComposeTestCheckFunc( checkScaleDownForAutoscaler(resourceAddonName, true), resource.TestCheckResourceAttr(resourceAddonName, "values.0.custom.scaleDownDelayAfterDelete", "11"), @@ -117,17 +101,23 @@ func TestAccCCEAddonV3ForceNewCCE(t *testing.T) { } func TestAccCCEAddonV3CoreDNS(t *testing.T) { - clusterName := randClusterName() + var addon addons.Addon + rc := common.InitResourceCheck( + resourceAddonNameDns, + &addon, + getCceAddonResourceFunc, + ) + + shared.BookCluster(t) t.Parallel() - quotas.BookOne(t, quotas.CCEClusterQuota) resource.Test(t, resource.TestCase{ PreCheck: func() { common.TestAccPreCheck(t) }, ProviderFactories: common.TestAccProviderFactories, - CheckDestroy: testAccCheckCCEAddonV3Destroy, + CheckDestroy: rc.CheckResourceDestroy(), Steps: []resource.TestStep{ { - Config: testAccCCEAddonV3StubDomains(clusterName), + Config: testAccCCEAddonV3StubDomains(), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceAddonNameDns, "template_name", "coredns"), ), @@ -140,24 +130,30 @@ const flavorRef = " {\n \"description\": \"Has only one instance\",\ const flavorRefUpdate = " {\n \"description\": \"Has only one instance\",\n \"name\": \"Single\",\n \"replicas\": 1,\n \"resources\": [\n {\n \"limitsCpu\": \"8000m\",\n \"limitsMem\": \"4Gi\",\n \"name\": \"autoscaler\",\n \"requestsCpu\": \"4000m\",\n \"requestsMem\": \"2Gi\"\n }\n ]\n }\n" func TestAccCCEAddonV3Flavor(t *testing.T) { - clusterName := randClusterName() + var addon addons.Addon + rc := common.InitResourceCheck( + resourceAddonName, + &addon, + getCceAddonResourceFunc, + ) + + shared.BookCluster(t) t.Parallel() - quotas.BookOne(t, quotas.CCEClusterQuota) resource.Test(t, resource.TestCase{ PreCheck: func() { common.TestAccPreCheck(t) }, ProviderFactories: common.TestAccProviderFactories, - CheckDestroy: testAccCheckCCEAddonV3Destroy, + CheckDestroy: rc.CheckResourceDestroy(), Steps: []resource.TestStep{ { - Config: testAccCCEAddonV3Flavor(clusterName), + Config: testAccCCEAddonV3Flavor(), Check: resource.ComposeTestCheckFunc( checkScaleDownForAutoscaler(resourceAddonName, true), resource.TestCheckResourceAttr(resourceAddonName, "values.0.flavor", flavorRef), ), }, { - Config: testAccCCEAddonV3FlavorUpdate(clusterName), + Config: testAccCCEAddonV3FlavorUpdate(), Check: resource.ComposeTestCheckFunc( checkScaleDownForAutoscaler(resourceAddonName, true), resource.TestCheckResourceAttr(resourceAddonName, "values.0.flavor", flavorRefUpdate), @@ -167,25 +163,22 @@ func TestAccCCEAddonV3Flavor(t *testing.T) { }) } -func testAccCheckCCEAddonV3Destroy(s *terraform.State) error { - config := common.TestAccProvider.Meta().(*cfg.Config) - client, err := config.CceV3Client(env.OS_REGION_NAME) - if err != nil { - return fmt.Errorf("error creating OpenTelekomCloud CCEv3 client: %w", err) - } - - for _, rs := range s.RootModule().Resources { - if rs.Type != "opentelekomcloud_cce_addon_v3" { - continue +func testAccCCEAddonV3ImportStateIdFunc() resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + var clusterID string + var addonID string + for _, rs := range s.RootModule().Resources { + if rs.Type == "opentelekomcloud_cce_cluster_v3" { + clusterID = rs.Primary.ID + } else if rs.Type == "opentelekomcloud_cce_addon_v3" { + addonID = rs.Primary.ID + } } - - _, err := addons.Get(client, rs.Primary.ID, rs.Primary.Attributes["cluster_id"]).Extract() - if err == nil { - return fmt.Errorf("addon still exists") + if clusterID == "" || addonID == "" { + return "", fmt.Errorf("resource not found: %s/%s", clusterID, addonID) } + return fmt.Sprintf("%s/%s", clusterID, addonID), nil } - - return nil } func checkScaleDownForAutoscaler(name string, enabled bool) resource.TestCheckFunc { @@ -205,7 +198,7 @@ func checkScaleDownForAutoscaler(name string, enabled bool) resource.TestCheckFu return fmt.Errorf("error creating opentelekomcloud CCE client: %w", err) } - found, err := addons.Get(client, rs.Primary.ID, rs.Primary.Attributes["cluster_id"]).Extract() + found, err := addons.Get(client, rs.Primary.ID, rs.Primary.Attributes["cluster_id"]) if err != nil { return err } @@ -222,38 +215,27 @@ func checkScaleDownForAutoscaler(name string, enabled bool) resource.TestCheckFu } } -func testAccCCEAddonV3Basic(cName string) string { +func testAccCCEAddonV3Basic() string { return fmt.Sprintf(` %s %s -resource opentelekomcloud_cce_cluster_v3 cluster_1 { - name = "%s" - cluster_type = "VirtualMachine" - flavor_id = "cce.s1.small" - vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id - subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id - cluster_version = "v1.27" - container_network_type = "overlay_l2" - kubernetes_svc_ip_range = "10.247.0.0/16" -} - resource "opentelekomcloud_cce_addon_v3" "autoscaler" { template_name = "autoscaler" - template_version = "1.27.53" - cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id + template_version = "1.29.17" + cluster_id = data.opentelekomcloud_cce_cluster_v3.cluster.id values { basic = { "cceEndpoint" : "https://cce.eu-de.otc.t-systems.com", "ecsEndpoint" : "https://ecs.eu-de.otc.t-systems.com", - "image_version" : "1.27.53", + "image_version" : "1.29.17", "region" : "eu-de", "swr_addr" : "100.125.7.25:20202", "swr_user" : "cce-addons" } custom = { - "cluster_id" : opentelekomcloud_cce_cluster_v3.cluster_1.id, + "cluster_id" : data.opentelekomcloud_cce_cluster_v3.cluster.id, "coresTotal" : 32000, "expander" : "priority", "logLevel" : 4, @@ -292,41 +274,31 @@ resource "opentelekomcloud_cce_addon_v3" "autoscaler" { EOF } } -`, common.DataSourceSubnet, common.DataSourceProject, cName) +`, common.DataSourceProject, shared.DataSourceCluster) } -func testAccCCEAddonV3Updated(cName string) string { +func testAccCCEAddonV3Updated() string { return fmt.Sprintf(` %s -%s -resource opentelekomcloud_cce_cluster_v3 cluster_1 { - name = "%s" - cluster_type = "VirtualMachine" - flavor_id = "cce.s1.small" - vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id - subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id - cluster_version = "v1.27" - container_network_type = "overlay_l2" - kubernetes_svc_ip_range = "10.247.0.0/16" -} +%s resource "opentelekomcloud_cce_addon_v3" "autoscaler" { template_name = "autoscaler" - template_version = "1.27.53" - cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id + template_version = "1.29.17" + cluster_id = data.opentelekomcloud_cce_cluster_v3.cluster.id values { basic = { "cceEndpoint" : "https://cce.eu-de.otc.t-systems.com", "ecsEndpoint" : "https://ecs.eu-de.otc.t-systems.com", - "image_version" : "1.27.53", + "image_version" : "1.29.17", "region" : "eu-de", "swr_addr" : "100.125.7.25:20202", "swr_user" : "cce-addons" } custom = { - "cluster_id" : opentelekomcloud_cce_cluster_v3.cluster_1.id, + "cluster_id" : data.opentelekomcloud_cce_cluster_v3.cluster.id, "coresTotal" : 32000, "expander" : "priority", "logLevel" : 4, @@ -365,29 +337,19 @@ resource "opentelekomcloud_cce_addon_v3" "autoscaler" { EOF } } -`, common.DataSourceSubnet, common.DataSourceProject, cName) +`, common.DataSourceProject, shared.DataSourceCluster) } -func testAccCCEAddonV3ForceNew(cName string) string { +func testAccCCEAddonV3ForceNew() string { return fmt.Sprintf(` %s -%s -resource opentelekomcloud_cce_cluster_v3 cluster_1 { - name = "%s" - cluster_type = "VirtualMachine" - flavor_id = "cce.s1.medium" - vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id - subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id - cluster_version = "v1.19" - container_network_type = "overlay_l2" - kubernetes_svc_ip_range = "10.247.0.0/16" -} +%s resource "opentelekomcloud_cce_addon_v3" "autoscaler" { template_name = "autoscaler" template_version = "1.19.1" - cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id + cluster_id = data.opentelekomcloud_cce_cluster_v3.cluster.id values { basic = { @@ -434,35 +396,24 @@ resource "opentelekomcloud_cce_addon_v3" "autoscaler" { EOF } } -`, common.DataSourceSubnet, common.DataSourceProject, cName) +`, common.DataSourceProject, shared.DataSourceCluster) } -func testAccCCEAddonV3StubDomains(name string) string { +func testAccCCEAddonV3StubDomains() string { return fmt.Sprintf(` %s -%s - -resource opentelekomcloud_cce_cluster_v3 cluster_1 { - name = "%s" - cluster_type = "VirtualMachine" - flavor_id = "cce.s1.medium" - vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id - subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id - cluster_version = "v1.23" - container_network_type = "overlay_l2" - kubernetes_svc_ip_range = "10.247.0.0/16" - no_addons = true -} resource "opentelekomcloud_cce_addon_v3" "coredns" { template_name = "coredns" - template_version = "1.28.4" - cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id + template_version = "1.29.4" + cluster_id = data.opentelekomcloud_cce_cluster_v3.cluster.id values { basic = { + "cluster_ip": "10.247.3.10", + "image_version": "1.29.4", "swr_addr" : "100.125.7.25:20202", - "swr_user" : "hwofficial" + "swr_user" : "cce-addons" } custom = { "stub_domains" : "{\"test\":[\"10.10.40.10\"], \"test2\":[\"10.10.40.20\"]}" @@ -470,41 +421,30 @@ resource "opentelekomcloud_cce_addon_v3" "coredns" { } } } -`, common.DataSourceSubnet, common.DataSourceProject, name) +`, shared.DataSourceCluster) } -func testAccCCEAddonV3Flavor(cName string) string { +func testAccCCEAddonV3Flavor() string { return fmt.Sprintf(` %s %s -resource opentelekomcloud_cce_cluster_v3 cluster_1 { - name = "%s" - cluster_type = "VirtualMachine" - flavor_id = "cce.s1.small" - vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id - subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id - cluster_version = "v1.27" - container_network_type = "overlay_l2" - kubernetes_svc_ip_range = "10.247.0.0/16" -} - resource "opentelekomcloud_cce_addon_v3" "autoscaler" { template_name = "autoscaler" - template_version = "1.27.53" - cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id + template_version = "1.29.17" + cluster_id = data.opentelekomcloud_cce_cluster_v3.cluster.id values { basic = { "cceEndpoint" : "https://cce.eu-de.otc.t-systems.com", "ecsEndpoint" : "https://ecs.eu-de.otc.t-systems.com", - "image_version" : "1.27.53", + "image_version" : "1.29.17", "region" : "eu-de", "swr_addr" : "100.125.7.25:20202", "swr_user" : "cce-addons" } custom = { - "cluster_id" : opentelekomcloud_cce_cluster_v3.cluster_1.id, + "cluster_id" : data.opentelekomcloud_cce_cluster_v3.cluster.id, "coresTotal" : 32000, "expander" : "priority", "logLevel" : 4, @@ -543,41 +483,30 @@ resource "opentelekomcloud_cce_addon_v3" "autoscaler" { EOF } } -`, common.DataSourceSubnet, common.DataSourceProject, cName) +`, common.DataSourceProject, shared.DataSourceCluster) } -func testAccCCEAddonV3FlavorUpdate(cName string) string { +func testAccCCEAddonV3FlavorUpdate() string { return fmt.Sprintf(` %s %s -resource opentelekomcloud_cce_cluster_v3 cluster_1 { - name = "%s" - cluster_type = "VirtualMachine" - flavor_id = "cce.s1.small" - vpc_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.vpc_id - subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id - cluster_version = "v1.27" - container_network_type = "overlay_l2" - kubernetes_svc_ip_range = "10.247.0.0/16" -} - resource "opentelekomcloud_cce_addon_v3" "autoscaler" { template_name = "autoscaler" - template_version = "1.27.53" - cluster_id = opentelekomcloud_cce_cluster_v3.cluster_1.id + template_version = "1.29.17" + cluster_id = data.opentelekomcloud_cce_cluster_v3.cluster.id values { basic = { "cceEndpoint" : "https://cce.eu-de.otc.t-systems.com", "ecsEndpoint" : "https://ecs.eu-de.otc.t-systems.com", - "image_version" : "1.27.53", + "image_version" : "1.29.17", "region" : "eu-de", "swr_addr" : "100.125.7.25:20202", "swr_user" : "cce-addons" } custom = { - "cluster_id" : opentelekomcloud_cce_cluster_v3.cluster_1.id, + "cluster_id" : data.opentelekomcloud_cce_cluster_v3.cluster.id, "coresTotal" : 32000, "expander" : "priority", "logLevel" : 4, @@ -616,5 +545,5 @@ resource "opentelekomcloud_cce_addon_v3" "autoscaler" { EOF } } -`, common.DataSourceSubnet, common.DataSourceProject, cName) +`, common.DataSourceProject, shared.DataSourceCluster) }