Skip to content

Commit

Permalink
[ignore] Add acceptance tests for mso_template resource and datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
akinross committed Nov 5, 2024
1 parent 3359905 commit 5ea9e9a
Show file tree
Hide file tree
Showing 7 changed files with 1,000 additions and 112 deletions.
8 changes: 4 additions & 4 deletions examples/template/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ data "mso_tenant" "example_tenant" {
resource "mso_template" "tenant_template" {
template_name = "tenant_template"
template_type = "tenant"
tenant_name = data.mso_tenant.example_tenant.display_name
tenant_id = data.mso_tenant.example_tenant.id
sites = [data.mso_site.site_1.id, data.mso_site.site_2.id]
}

Expand All @@ -39,7 +39,7 @@ resource "mso_template" "tenant_template" {
resource "mso_template" "l3out_template" {
template_name = "l3out_template"
template_type = "l3out"
tenant_name = data.mso_tenant.example_tenant.display_name
tenant_id = data.mso_tenant.example_tenant.id
sites = [data.mso_site.site_1.id]
}

Expand All @@ -64,7 +64,7 @@ resource "mso_template" "fabric_resource_template" {
resource "mso_template" "monitoring_tenant_template" {
template_name = "monitoring_tenant_template"
template_type = "monitoring_tenant"
tenant_name = data.mso_tenant.example_tenant.display_name
tenant_id = data.mso_tenant.example_tenant.id
sites = [data.mso_site.site_1.id]
}

Expand All @@ -81,6 +81,6 @@ resource "mso_template" "monitoring_access_template" {
resource "mso_template" "service_device_template" {
template_name = "service_device_template"
template_type = "service_device"
tenant_name = data.mso_tenant.example_tenant.display_name
tenant_id = data.mso_tenant.example_tenant.id
sites = [data.mso_site.site_1.id, data.mso_site.site_2.id]
}
5 changes: 1 addition & 4 deletions mso/datasource_mso_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@ func datasourceMSOTemplate() *schema.Resource {
"template_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"template_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"template_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"tenant",
"l3out",
Expand All @@ -43,7 +40,7 @@ func datasourceMSOTemplate() *schema.Resource {
"service_device",
}, false),
},
"tenant_name": &schema.Schema{
"tenant_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
Expand Down
143 changes: 143 additions & 0 deletions mso/datasource_mso_template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package mso

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccMSOTemplateDatasourceTenantErrors(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
PreConfig: func() { fmt.Println("Test: No template_id or template_name provided in Template configuration") },
Config: testAccMSOTemplateDatasourceErrorNoIdOrNameConfig(),
ExpectError: regexp.MustCompile("either `template_id` or `template_name` must be provided"),
},
{
PreConfig: func() { fmt.Println("Test: No template_type with name provided in Template configuration") },
Config: testAccMSOTemplateDatasourceErrorNoTypeConfig(),
ExpectError: regexp.MustCompile("`template_type` must be provided when `template_name` is provided"),
},
{
PreConfig: func() { fmt.Println("Test: Both template_id and template_name provided in Template configuration") },
Config: testAccMSOTemplateDatasourceErrorIdAndNameConfig(),
ExpectError: regexp.MustCompile("only one of `template_id` or `template_name` must be provided"),
},
{
PreConfig: func() { fmt.Println("Test: Non existing template name provided in Template configuration") },
Config: testAccMSOTemplateDatasourceErrorNonExistingConfig(),
ExpectError: regexp.MustCompile("Template with name 'non_existing_template_name' not found."),
},
},
})
}

func TestAccMSOTemplateDatasourceTenantName(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
PreConfig: func() { fmt.Println("Test: Tenant template with name and type provided in Template configuration") },
Config: testAccMSOTemplateDatasourceNameAndTypeConfig(),
Check: resource.ComposeTestCheckFunc(
testAccMSOTemplateState(
"data.mso_template.template_tenant",
&TemplateTest{
TemplateName: "test_template_tenant",
TemplateType: "tenant",
Tenant: msoTemplateTenantName,
Sites: []string{msoTemplateSiteName1, msoTemplateSiteName2},
},
false,
),
),
},
},
})
}

func TestAccMSOTemplateDatasourceTenantId(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
PreConfig: func() { fmt.Println("Test: Tenant template with id provided in Template configuration") },
Config: testAccMSOTemplateDatasourceIdConfig(),
Check: resource.ComposeTestCheckFunc(
testAccMSOTemplateState(
"data.mso_template.template_tenant",
&TemplateTest{
TemplateName: "test_template_tenant",
TemplateType: "tenant",
Tenant: msoTemplateTenantName,
Sites: []string{msoTemplateSiteName1, msoTemplateSiteName2},
},
false,
),
),
},
},
})
}

func testAccMSOTemplateDatasourceNameAndTypeConfig() string {
return fmt.Sprintf(`%s
data "mso_template" "template_tenant" {
template_name = mso_template.template_tenant.template_name
template_type = "tenant"
}
`, testAccMSOTemplateResourceTenanTwoSitesConfig())
}

func testAccMSOTemplateDatasourceIdConfig() string {
return fmt.Sprintf(`%s
data "mso_template" "template_tenant" {
template_id = mso_template.template_tenant.id
}
`, testAccMSOTemplateResourceTenanTwoSitesConfig())
}

func testAccMSOTemplateDatasourceErrorNoIdOrNameConfig() string {
return fmt.Sprintf(`
data "mso_template" "template_tenant" {
template_type = "tenant"
}
`)
}

func testAccMSOTemplateDatasourceErrorNoTypeConfig() string {
return fmt.Sprintf(`
data "mso_template" "template_tenant" {
template_name = "non_existing_template_name"
}
`)
}

func testAccMSOTemplateDatasourceErrorIdAndNameConfig() string {
return fmt.Sprintf(`
data "mso_template" "template_tenant" {
template_id = "non_existing_template_id"
template_name = "non_existing_template_name"
template_type = "tenant"
}
`)
}

func testAccMSOTemplateDatasourceErrorNonExistingConfig() string {
return fmt.Sprintf(`
data "mso_template" "template_tenant" {
template_name = "non_existing_template_name"
template_type = "tenant"
}
`)
}
Loading

0 comments on commit 5ea9e9a

Please sign in to comment.