From d62b84791082c848fd85ed0391c1e981e13dd84e Mon Sep 17 00:00:00 2001 From: akinross Date: Mon, 4 Nov 2024 15:42:51 +0100 Subject: [PATCH] [ignore] Add acceptance tests for tenant type templates --- mso/resource_mso_template_test.go | 282 ++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 mso/resource_mso_template_test.go diff --git a/mso/resource_mso_template_test.go b/mso/resource_mso_template_test.go new file mode 100644 index 00000000..0e0d6b74 --- /dev/null +++ b/mso/resource_mso_template_test.go @@ -0,0 +1,282 @@ +package mso + +import ( + "fmt" + "strconv" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccMsoTemplateTenant(t *testing.T) { + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckMsoTemplateTenantConfig(), + Check: resource.ComposeTestCheckFunc( + testAccCheckMSOTemplateState( + "mso_template.template_tenant", + &TemplateTest{ + TemplateName: "test_template_tenant", + TemplateType: "tenant", + Tenant: "ansible_test", + Sites: []string{}, + }, + ), + ), + }, + { + Config: testAccCheckMsoTemplateTenanSiteAnsibleTestConfig(), + Check: resource.ComposeTestCheckFunc( + testAccCheckMSOTemplateState( + "mso_template.template_tenant", + &TemplateTest{ + TemplateName: "test_template_tenant", + TemplateType: "tenant", + Tenant: "ansible_test", + Sites: []string{"ansible_test"}, + }, + ), + ), + }, + { + Config: testAccCheckMsoTemplateTenanTwoSitesConfig(), + Check: resource.ComposeTestCheckFunc( + testAccCheckMSOTemplateState( + "mso_template.template_tenant", + &TemplateTest{ + TemplateName: "test_template_tenant", + TemplateType: "tenant", + Tenant: "ansible_test", + Sites: []string{"ansible_test", "ansible_test_2"}, + }, + ), + ), + }, + { + Config: testAccCheckMsoTemplateTenanTwoSitesReversedConfig(), + Check: resource.ComposeTestCheckFunc( + testAccCheckMSOTemplateState( + "mso_template.template_tenant", + &TemplateTest{ + TemplateName: "test_template_tenant", + TemplateType: "tenant", + Tenant: "ansible_test", + Sites: []string{"ansible_test", "ansible_test_2"}, + }, + ), + ), + }, + { + Config: testAccCheckMsoTemplateTenanSiteAnsibleTest2Config(), + Check: resource.ComposeTestCheckFunc( + testAccCheckMSOTemplateState( + "mso_template.template_tenant", + &TemplateTest{ + TemplateName: "test_template_tenant", + TemplateType: "tenant", + Tenant: "ansible_test", + Sites: []string{"ansible_test_2"}, + }, + ), + ), + }, + { + Config: testAccCheckMsoTemplateTenantNoSitesConfig(), + Check: resource.ComposeTestCheckFunc( + testAccCheckMSOTemplateState( + "mso_template.template_tenant", + &TemplateTest{ + TemplateName: "test_template_tenant", + TemplateType: "tenant", + Tenant: "ansible_test", + Sites: []string{}, + }, + ), + ), + }, + { + Config: testAccCheckMsoTemplateTenantNameChangeConfig(), + Check: resource.ComposeTestCheckFunc( + testAccCheckMSOTemplateState( + "mso_template.template_tenant", + &TemplateTest{ + TemplateName: "test_template_tenant_changed", + TemplateType: "tenant", + Tenant: "ansible_test", + Sites: []string{}, + }, + ), + ), + }, + }, + }) +} + +type TemplateTest struct { + TemplateName string `json:",omitempty"` + TemplateType string `json:",omitempty"` + Tenant string `json:",omitempty"` + Sites []string `json:",omitempty"` +} + +func testAccCheckMSOTemplateState(resourceName string, stateTemplate *TemplateTest) resource.TestCheckFunc { + return func(s *terraform.State) error { + + rootModule, err := s.RootModule().Resources[resourceName] + if !err { + return fmt.Errorf("%v", err) + } + + if rootModule.Primary.ID == "" { + return fmt.Errorf("No ID is set for the template") + } + + if rootModule.Primary.Attributes["tenant_id"] == "" && stateTemplate.Tenant != "" { + return fmt.Errorf("No tenant ID is set for the template") + } else if stateTemplate.Tenant != "" { + tenantState, err := s.RootModule().Resources[fmt.Sprintf("data.mso_tenant.%s", stateTemplate.Tenant)] + if !err { + return fmt.Errorf("Tenant %s not found in state", stateTemplate.Tenant) + } + if tenantState.Primary.Attributes["display_name"] != stateTemplate.Tenant { + return fmt.Errorf("Tenant display name does not match, expected: %s, got: %s", stateTemplate.Tenant, tenantState.Primary.Attributes["display_name"]) + } + } + + if rootModule.Primary.Attributes["template_name"] != stateTemplate.TemplateName { + return fmt.Errorf("Template name does not match, expected: %s, got: %s", stateTemplate.TemplateName, rootModule.Primary.Attributes["template_name"]) + } + + if rootModule.Primary.Attributes["template_type"] != stateTemplate.TemplateType { + return fmt.Errorf("Template type does not match, expected: %s, got: %s", stateTemplate.TemplateType, rootModule.Primary.Attributes["template_type"]) + } + + if sites, ok := rootModule.Primary.Attributes["sites.#"]; ok { + if siteAmount, e := strconv.Atoi(sites); e != nil { + return fmt.Errorf("Could not convert sites amount to integer") + } else if siteAmount != len(stateTemplate.Sites) { + return fmt.Errorf("Amount of sites do not match, expected: %d, got: %d", len(stateTemplate.Sites), len(rootModule.Primary.Attributes["sites.#"])) + } + + for _, site := range stateTemplate.Sites { + siteState, err := s.RootModule().Resources[fmt.Sprintf("data.mso_site.%s", site)] + if !err { + return fmt.Errorf("Site %s not found in state", site) + } + if siteState.Primary.Attributes["name"] != site { + return fmt.Errorf("Site display name does not match, expected: %s, got: %s", site, siteState.Primary.Attributes["display_name"]) + } + } + } else { + if len(stateTemplate.Sites) != 0 { + return fmt.Errorf("Amount of sites do not match, expected: %d, got: 0", len(stateTemplate.Sites)) + } + } + + return nil + } +} + +func testSiteConfigAnsibleTest() string { + return fmt.Sprintf(` + data "mso_site" "ansible_test" { + name = "ansible_test" + } + `) +} + +func testSiteConfigAnsibleTest2() string { + return fmt.Sprintf(` + data "mso_site" "ansible_test_2" { + name = "ansible_test_2" + } + `) +} + +func testTenantConfigAnsibleTest() string { + return fmt.Sprintf(` + data "mso_tenant" "ansible_test" { + name = "ansible_test" + } + `) +} + +func testAccCheckMsoTemplateTenantConfig() string { + return fmt.Sprintf(`%s + resource "mso_template" "template_tenant" { + template_name = "test_template_tenant" + template_type = "tenant" + tenant_id = data.mso_tenant.ansible_test.id + } + `, testTenantConfigAnsibleTest()) +} + +func testAccCheckMsoTemplateTenantNameChangeConfig() string { + return fmt.Sprintf(`%s + resource "mso_template" "template_tenant" { + template_name = "test_template_tenant_changed" + template_type = "tenant" + tenant_id = data.mso_tenant.ansible_test.id + } + `, testTenantConfigAnsibleTest()) +} + +func testAccCheckMsoTemplateTenantNoSitesConfig() string { + return fmt.Sprintf(`%s + resource "mso_template" "template_tenant" { + template_name = "test_template_tenant" + template_type = "tenant" + tenant_id = data.mso_tenant.ansible_test.id + sites = [] + } + `, testTenantConfigAnsibleTest()) +} + +func testAccCheckMsoTemplateTenanSiteAnsibleTestConfig() string { + return fmt.Sprintf(`%s%s + resource "mso_template" "template_tenant" { + template_name = "test_template_tenant" + template_type = "tenant" + tenant_id = data.mso_tenant.ansible_test.id + sites = [data.mso_site.ansible_test.id] + } + `, testSiteConfigAnsibleTest(), testTenantConfigAnsibleTest()) +} + +func testAccCheckMsoTemplateTenanSiteAnsibleTest2Config() string { + return fmt.Sprintf(`%s%s + resource "mso_template" "template_tenant" { + template_name = "test_template_tenant" + template_type = "tenant" + tenant_id = data.mso_tenant.ansible_test.id + sites = [data.mso_site.ansible_test_2.id] + } + `, testSiteConfigAnsibleTest2(), testTenantConfigAnsibleTest()) +} + +func testAccCheckMsoTemplateTenanTwoSitesConfig() string { + return fmt.Sprintf(`%s%s%s + resource "mso_template" "template_tenant" { + template_name = "test_template_tenant" + template_type = "tenant" + tenant_id = data.mso_tenant.ansible_test.id + sites = [data.mso_site.ansible_test.id, data.mso_site.ansible_test_2.id] + } + `, testSiteConfigAnsibleTest(), testSiteConfigAnsibleTest2(), testTenantConfigAnsibleTest()) +} + +func testAccCheckMsoTemplateTenanTwoSitesReversedConfig() string { + return fmt.Sprintf(`%s%s%s + resource "mso_template" "template_tenant" { + template_name = "test_template_tenant" + template_type = "tenant" + tenant_id = data.mso_tenant.ansible_test.id + sites = [data.mso_site.ansible_test_2.id, data.mso_site.ansible_test.id] + } + `, testSiteConfigAnsibleTest(), testSiteConfigAnsibleTest2(), testTenantConfigAnsibleTest()) +}