Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismarget-j committed Apr 13, 2024
1 parent e5d5e30 commit ef30d6d
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions apstra/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var (
ResourceDatacenterGenericSystem = resourceDatacenterGenericSystem{}
ResourceDatacenterRoutingZone = resourceDatacenterRoutingZone{}
ResourceTemplatePodBased = resourceTemplatePodBased{}
ResourceTemplateCollapsed = resourceTemplateCollapsed{}
)

func ResourceName(ctx context.Context, r resource.Resource) string {
Expand Down
128 changes: 128 additions & 0 deletions apstra/resource_template_collapsed_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//go:build integration

package tfapstra_test

import (
"context"
"fmt"
tfapstra "github.com/Juniper/terraform-provider-apstra/apstra"
testutils "github.com/Juniper/terraform-provider-apstra/apstra/test_utils"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"strconv"
"testing"
)

const (
resourceTemplateCollapsedHCL = `
resource %q %q {
name = %q // mandatory field
rack_type_id = %q // mandatory field
mesh_link_speed = %q // mandatory field
mesh_link_count = %d // mandatory field
}
`
)

type collapsedTemplateConfig struct {
name string
rackTypeId string
meshLinkSpeed string
meshLinkCount int
}

func (o collapsedTemplateConfig) render(rType, rName string) string {
return fmt.Sprintf(resourceTemplateCollapsedHCL,
rType, rName,
o.name,
o.rackTypeId,
o.meshLinkSpeed,
o.meshLinkCount,
)
}

func (o collapsedTemplateConfig) testChecks(t testing.TB, rType, rName string) testChecks {
result := newTestChecks(rType + "." + rName)

// required and computed attributes can always be checked
result.append(t, "TestCheckResourceAttrSet", "id")
result.append(t, "TestCheckResourceAttr", "name", o.name)
result.append(t, "TestCheckResourceAttr", "rack_type_id", o.rackTypeId)
result.append(t, "TestCheckResourceAttr", "mesh_link_speed", o.meshLinkSpeed)
result.append(t, "TestCheckResourceAttr", "mesh_link_count", strconv.Itoa(o.meshLinkCount))

return result
}

func TestResourceTemplateCollapsed(t *testing.T) {
ctx := context.Background()
client := testutils.GetTestClient(t, ctx)

type testCase struct {
apiVersionConstraints version.Constraints
stepConfigs []collapsedTemplateConfig
}

testCases := map[string]testCase{
"a": {
//apiVersionConstraints: version.MustConstraints(version.NewConstraint(">=" + apiversions.Apstra411)),
stepConfigs: []collapsedTemplateConfig{
{
name: acctest.RandString(6),
rackTypeId: "L3_collapsed_acs",
meshLinkSpeed: "10G",
meshLinkCount: 1,
},
{
name: acctest.RandString(6),
rackTypeId: "L3_collapsed_acs",
meshLinkSpeed: "10G",
meshLinkCount: 2,
},
{
name: acctest.RandString(6),
rackTypeId: "L3_collapsed_ESI",
meshLinkSpeed: "10G",
meshLinkCount: 1,
},
},
},
}

apiVersion := version.Must(version.NewVersion(client.ApiVersion()))
resourceType := tfapstra.ResourceName(ctx, &tfapstra.ResourceTemplateCollapsed)

for tName, tCase := range testCases {
tName, tCase := tName, tCase
t.Run(tName, func(t *testing.T) {
t.Parallel()
if !tCase.apiVersionConstraints.Check(apiVersion) {
t.Skipf("API version %s does not satisfy version constraints(%s) of test %q",
apiVersion, tCase.apiVersionConstraints, tName)
}

steps := make([]resource.TestStep, len(tCase.stepConfigs))
for i, stepConfig := range tCase.stepConfigs {
config := stepConfig.render(resourceType, tName)
checks := stepConfig.testChecks(t, resourceType, tName)

chkLog := checks.string()
stepName := fmt.Sprintf("test case %q step %d", tName, i+1)

t.Logf("\n// ------ begin config for %s ------\n%s// -------- end config for %s ------\n\n", stepName, config, stepName)
t.Logf("\n// ------ begin checks for %s ------\n%s// -------- end checks for %s ------\n\n", stepName, chkLog, stepName)

steps[i] = resource.TestStep{
Config: insecureProviderConfigHCL + config,
Check: resource.ComposeAggregateTestCheckFunc(checks.checks...),
}
}

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: steps,
})
})
}
}

0 comments on commit ef30d6d

Please sign in to comment.