Skip to content

Commit

Permalink
increase test coverage for few resources beyond 85
Browse files Browse the repository at this point in the history
  • Loading branch information
shrsr committed Jul 20, 2021
1 parent c5e665b commit 61ae9e3
Show file tree
Hide file tree
Showing 16 changed files with 480 additions and 416 deletions.
173 changes: 156 additions & 17 deletions mso/resource_mso_schema_site_anp_epg_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

// func TestAccMSOSchemaSiteAnpEpgSelector_Initial(t *testing.T) {
// resource.Test(t, resource.TestCase{
// Providers: testAccProviders,
// Steps: []resource.TestStep{
// {
// Config: testAccCheckMSOSiteAnpEpgSelectorConfig_initial("ANP"),
// },
// },
// })
// }
func TestAccMSOSchemaSiteAnpEpgSelector_Basic(t *testing.T) {
var ss SiteAnpEpgSelectorTest
resource.Test(t, resource.TestCase{
Expand All @@ -21,10 +31,16 @@ func TestAccMSOSchemaSiteAnpEpgSelector_Basic(t *testing.T) {
{
Config: testAccCheckMSOSiteAnpEpgSelectorConfig_basic("one"),
Check: resource.ComposeTestCheckFunc(
testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema_site_anp_epg_selector.selector1", &ss),
testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema.schema1", "mso_schema_site_anp_epg_selector.selector1", &ss),
testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("one", &ss),
),
},

{
ResourceName: "mso_schema_site_anp_epg_selector.selector1",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -40,14 +56,14 @@ func TestAccMSOSchemaSiteAnpEpgSelector_Update(t *testing.T) {
{
Config: testAccCheckMSOSiteAnpEpgSelectorConfig_basic("one"),
Check: resource.ComposeTestCheckFunc(
testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema_site_anp_epg_selector.selector1", &ss),
testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema.schema1", "mso_schema_site_anp_epg_selector.selector1", &ss),
testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("one", &ss),
),
},
{
Config: testAccCheckMSOSiteAnpEpgSelectorConfig_basic("two"),
Check: resource.ComposeTestCheckFunc(
testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema_site_anp_epg_selector.selector1", &ss),
testAccCheckMSOSchemaSiteAnpEpgSelectorExists("mso_schema.schema1", "mso_schema_site_anp_epg_selector.selector1", &ss),
testAccCheckMSOSchemaSiteAnpEpgSelectorAttributes("two", &ss),
),
},
Expand All @@ -57,35 +73,150 @@ func TestAccMSOSchemaSiteAnpEpgSelector_Update(t *testing.T) {

func testAccCheckMSOSiteAnpEpgSelectorConfig_basic(key string) string {
return fmt.Sprintf(`
resource "mso_schema" "schema1" {
name = "Schema2"
template_name = "Template1"
tenant_id = "5fb5fed8520000452a9e8911"
}
resource "mso_schema_template_vrf" "vrf1" {
schema_id = mso_schema.schema1.id
template = mso_schema.schema1.template_name
name = "VRF"
display_name = "vz1"
layer3_multicast= false
vzany = false
}
resource "mso_rest" "azure_site" {
path = "api/v1/schemas/${mso_schema.schema1.id}"
method = "PATCH"
payload = <<EOF
[
{
"op": "add",
"path": "/sites/-",
"value": {
"siteId": "5fb680aa4e0000d0513660c2",
"templateName": "${mso_schema_template_vrf.vrf1.template}",
"vrfs": [{
"vrfRef": {
"schemaId": "${mso_schema.schema1.id}",
"templateName": "Template1",
"vrfName": "${mso_schema_template_vrf.vrf1.name}"
},
"regions": [{
"name": "westus",
"cidrs": [{
"ip": "10.101.210.0/24",
"primary": true,
"subnets": [{
"ip": "10.101.210.0/25",
"zone": "",
"name": ""
}, {
"ip": "10.101.210.128/25",
"zone": "",
"name": ""
}],
"associatedRegion": "westus"
}],
"isVpnGatewayRouter": false,
"isTGWAttachment": true,
"cloudRsCtxProfileToGatewayRouterP": {
"name": "default",
"tenantName": "infra"
},
"hubnetworkPeering": false
}]
}],
"intersiteL3outs": null
}
}
]
EOF
}
resource "mso_schema_template_anp" "anp1" {
schema_id=mso_schema.schema1.id
template= mso_schema.schema1.template_name
name = "ANP"
display_name="ANP"
}
resource "mso_schema_template_bd" "bd" {
schema_id = mso_schema.schema1.id
template_name = mso_schema_template_anp.anp1.template
name = "BD"
display_name = "test"
vrf_name = "VRF"
}
resource "mso_schema_template_anp_epg" "anp_epg" {
schema_id = mso_schema.schema1.id
template_name = mso_schema_template_anp.anp1.template
anp_name = "ANP"
name = "EPG"
bd_name = mso_schema_template_bd.bd.name
vrf_name = "VRF"
display_name = "mso_epg1"
}
resource "mso_schema_site_anp" "anp1" {
schema_id = mso_schema.schema1.id
anp_name = "ANP"
template_name = mso_schema_template_anp_epg.anp_epg.template_name
site_id = "5fb680aa4e0000d0513660c2"
}
resource "mso_schema_site_anp_epg" "site_anp_epg" {
schema_id = mso_schema.schema1.id
template_name = mso_schema_site_anp.anp1.template_name
site_id = "5fb680aa4e0000d0513660c2"
anp_name = "ANP"
epg_name = "EPG"
}
resource "mso_schema_site_anp_epg_selector" "selector1" {
schema_id = "5c4d5bb72700000401f80948"
site_id = "5c7c95b25100008f01c1ee3c"
template_name = "Template1"
schema_id = mso_schema.schema1.id
site_id = "5fb680aa4e0000d0513660c2"
template_name = mso_schema_site_anp_epg.site_anp_epg.template_name
anp_name = "ANP"
epg_name = "DB"
epg_name = "EPG"
name = "test_check"
expressions {
key = "%s"
operator = "keyExist"
value = "32"
key = "%s"
operator = "equals"
value = "1"
}
}
}
`, key)
}

func testAccCheckMSOSchemaSiteAnpEpgSelectorExists(selectorName string, ss *SiteAnpEpgSelectorTest) resource.TestCheckFunc {
func testAccCheckMSOSchemaSiteAnpEpgSelectorExists(schemaName, selectorName string, ss *SiteAnpEpgSelectorTest) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*client.Client)
rs1, err1 := s.RootModule().Resources[selectorName]
rs1, err1 := s.RootModule().Resources[schemaName]
rs2, err1 := s.RootModule().Resources[selectorName]

if !err1 {
return fmt.Errorf("Selector %s not found", selectorName)
}
if rs1.Primary.ID == "" {
return fmt.Errorf("No Schema id was set")
}
if rs2.Primary.ID == "" {
return fmt.Errorf("No Selector id was set")
}

cont, err := client.GetViaURL(fmt.Sprintf("api/v1/schemas/5c4d5bb72700000401f80948"))
cont, err := client.GetViaURL(fmt.Sprintf("api/v1/schemas/" + rs1.Primary.ID))
if err != nil {
return err
}
Expand All @@ -106,7 +237,7 @@ func testAccCheckMSOSchemaSiteAnpEpgSelectorExists(selectorName string, ss *Site
currentSite := models.StripQuotes(siteCont.S("siteId").String())
currentTemp := models.StripQuotes(siteCont.S("templateName").String())

if currentTemp == "Template1" && currentSite == "5c7c95b25100008f01c1ee3c" {
if currentTemp == "Template1" && currentSite == "5fb680aa4e0000d0513660c2" {
anpCount, err := siteCont.ArrayCount("anps")
if err != nil {
return fmt.Errorf("No Anp found")
Expand Down Expand Up @@ -200,8 +331,16 @@ func testAccCheckMSOSchemaSiteAnpEpgSelectorExists(selectorName string, ss *Site
func testAccCheckMSOSchemaSiteAnpEpgSelectorDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*client.Client)

rs1, err1 := s.RootModule().Resources["mso_schema.schema1"]

if !err1 {
return fmt.Errorf("Schema %s not found", "mso_schema.schema1")
}

schemaid := rs1.Primary.ID

for _, rs := range s.RootModule().Resources {
cont, err := client.GetViaURL(fmt.Sprintf("api/v1/schemas/5c4d5bb72700000401f80948"))
cont, err := client.GetViaURL(fmt.Sprintf("api/v1/schemas/%s", schemaid))
if rs.Type == "mso_schema_site_anp_epg_selector" {
if err != nil {
return err
Expand All @@ -221,7 +360,7 @@ func testAccCheckMSOSchemaSiteAnpEpgSelectorDestroy(s *terraform.State) error {
currentSite := models.StripQuotes(siteCont.S("siteId").String())
currentTemp := models.StripQuotes(siteCont.S("templateName").String())

if currentTemp == "Template1" && currentSite == "5c7c95b25100008f01c1ee3c" {
if currentTemp == "Template1" && currentSite == "5fb680aa4e0000d0513660c2" {
anpCount, err := siteCont.ArrayCount("anps")
if err != nil {
return fmt.Errorf("No Anp found")
Expand Down
6 changes: 3 additions & 3 deletions mso/resource_mso_schema_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func resourceMSOSchemaTemplateImport(d *schema.ResourceData, m interface{}) ([]*
log.Printf("[DEBUG] %s: Beginning Import", d.Id())
get_attribute := strings.Split(d.Id(), "/")
msoClient := m.(*client.Client)
name := get_attribute[2]
schemaId := get_attribute[0]
cont, err := msoClient.GetViaURL(fmt.Sprintf("api/v1/schemas/%s", schemaId))
if err != nil {
Expand All @@ -69,6 +68,7 @@ func resourceMSOSchemaTemplateImport(d *schema.ResourceData, m interface{}) ([]*
data := cont.S("templates").Data().([]interface{})
var flag bool
var count int
name := get_attribute[2]
for _, info := range data {
val := info.(map[string]interface{})
if val["name"].(string) == name {
Expand All @@ -83,7 +83,7 @@ func resourceMSOSchemaTemplateImport(d *schema.ResourceData, m interface{}) ([]*
}

dataCon := cont.S("templates").Index(count)
d.SetId(models.StripQuotes(dataCon.S("name").String()))
d.SetId(name)
d.Set("name", models.StripQuotes(dataCon.S("name").String()))
d.Set("display_name", models.StripQuotes(dataCon.S("displayName").String()))
d.Set("tenant_id", models.StripQuotes(dataCon.S("tenantId").String()))
Expand Down Expand Up @@ -145,7 +145,7 @@ func resourceMSOSchemaTemplateRead(d *schema.ResourceData, m interface{}) error
apiTemplateDisplayName := models.StripQuotes(tempCont.S("displayName").String())

if apiTenantId == stateTenantId && apiTemplateName == stateTemplateName && apiTemplateDisplayName == stateTemplateDisplayName {
d.SetId(apiTemplateName)
d.SetId(schemaId + "/templates/" + apiTemplateName)
d.Set("tenant_id", apiTenantId)
d.Set("name", apiTemplateName)
d.Set("display_name", apiTemplateDisplayName)
Expand Down
Loading

0 comments on commit 61ae9e3

Please sign in to comment.