-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ignore] Add acceptance tests for tenant type templates
- Loading branch information
Showing
1 changed file
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |