Skip to content

Commit

Permalink
Used random values for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbristowe committed Nov 5, 2020
1 parent ed0d641 commit 6efbc7e
Showing 1 changed file with 30 additions and 66 deletions.
96 changes: 30 additions & 66 deletions octopusdeploy/resource_azure_service_principal_test.go
Expand Up @@ -12,91 +12,55 @@ import (
)

func TestAccOctopusDeployAzureServicePrincipalBasic(t *testing.T) {
name := acctest.RandString(10)
clientID := uuid.New().String()
key := acctest.RandString(10)
subscriptionID := uuid.New().String()
tagName := acctest.RandString(10)
tagSetName := acctest.RandString(10)
tenantID := uuid.New().String()
localName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
prefix := constOctopusDeployAzureServicePrincipal + "." + localName

const accountPrefix = constOctopusDeployAzureServicePrincipal + ".foo"
var tenantTags = fmt.Sprintf("%s/%s", tagSetName, tagName)
const tenantedDeploymentParticipation = octopusdeploy.TenantedDeploymentModeTenantedOrUntenanted
name := acctest.RandString(10)
clientID := uuid.New()
clientSecret := acctest.RandString(10)
subscriptionID := uuid.New()
tenantedDeploymentMode := octopusdeploy.TenantedDeploymentModeTenantedOrUntenanted
tenantID := uuid.New()

resource.Test(t, resource.TestCase{
CheckDestroy: testAccountDestroy,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testOctopusDeployAzureServicePrincipalDestroy,
Steps: []resource.TestStep{
{
Config: testAzureServicePrincipalBasic(tagSetName, tagName, name, clientID, tenantID, subscriptionID, key, tenantedDeploymentParticipation),
Check: resource.ComposeTestCheckFunc(
testOctopusDeployAzureServicePrincipalExists(accountPrefix),
resource.TestCheckResourceAttr(accountPrefix, constName, name),
resource.TestCheckResourceAttr(accountPrefix, constClientID, clientID),
resource.TestCheckResourceAttr(accountPrefix, constTenantID, tenantID),
resource.TestCheckResourceAttr(accountPrefix, constSubscriptionNumber, subscriptionID),
resource.TestCheckResourceAttr(accountPrefix, "tenant_tags.0", tenantTags),
resource.TestCheckResourceAttr(accountPrefix, constTenantedDeploymentParticipation, string(tenantedDeploymentParticipation)),
testAzureServicePrincipalExists(prefix),
resource.TestCheckResourceAttr(prefix, constClientID, clientID.String()),
resource.TestCheckResourceAttr(prefix, constName, name),
resource.TestCheckResourceAttr(prefix, constSubscriptionNumber, subscriptionID.String()),
resource.TestCheckResourceAttr(prefix, constTenantID, tenantID.String()),
resource.TestCheckResourceAttr(prefix, constTenantedDeploymentParticipation, string(tenantedDeploymentMode)),
),
Config: testAzureServicePrincipalBasic(localName, name, clientID, tenantID, subscriptionID, clientSecret, tenantedDeploymentMode),
},
},
})
}

func testAzureServicePrincipalBasic(tagSetName string, tagName string, name string, clientID string, tenantID string, subscriptionID string, clientSecret string, tenantedDeploymentParticipation octopusdeploy.TenantedDeploymentMode) string {
return fmt.Sprintf(`
resource "%s" "testtagset" {
name = "%s"
tag {
name = "%s"
color = "#6e6e6f"
}
}
resource "%s" "foo" {
name = "%s"
client_id = "%s"
tenant_id = "%s"
subscription_number = "%s"
key = "%s"
tenant_tags = ["${octopusdeploy_tag_set.testtagset.name}/%s"]
tenanted_deployment_participation = "%s"
}
`,
constOctopusDeployTagSet, tagSetName, tagName, constOctopusDeployAzureServicePrincipal, name, clientID, tenantID, subscriptionID, clientSecret, tagSetName, tenantedDeploymentParticipation,
)
func testAzureServicePrincipalBasic(localName string, name string, clientID uuid.UUID, tenantID uuid.UUID, subscriptionID uuid.UUID, clientSecret string, tenantedDeploymentParticipation octopusdeploy.TenantedDeploymentMode) string {
return fmt.Sprintf(`resource "%s" "%s" {
client_id = "%s"
name = "%s"
subscription_number = "%s"
tenant_id = "%s"
key = "%s"
tenanted_deployment_participation = "%s"
}`, constOctopusDeployAzureServicePrincipal, localName, clientID, name, subscriptionID, tenantID, clientSecret, tenantedDeploymentParticipation)
}

func testOctopusDeployAzureServicePrincipalExists(n string) resource.TestCheckFunc {
func testAzureServicePrincipalExists(prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*octopusdeploy.Client)
return existsAzureServicePrincipalHelper(s, client)
}
}

func existsAzureServicePrincipalHelper(s *terraform.State, client *octopusdeploy.Client) error {
accountID := s.RootModule().Resources[constOctopusDeployAzureServicePrincipal+".foo"].Primary.ID
if _, err := client.Accounts.GetByID(accountID); err != nil {
return err
}

return nil
}

func testOctopusDeployAzureServicePrincipalDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*octopusdeploy.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != constOctopusDeployAzureServicePrincipal {
continue
}

accountID := rs.Primary.ID
accountID := s.RootModule().Resources[prefix].Primary.ID
if _, err := client.Accounts.GetByID(accountID); err != nil {
return fmt.Errorf("account (%s) still exists", rs.Primary.ID)
return err
}
}

return nil
return nil
}
}

0 comments on commit 6efbc7e

Please sign in to comment.