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 e0d266d commit 9948db2
Showing 1 changed file with 21 additions and 59 deletions.
80 changes: 21 additions & 59 deletions octopusdeploy/resource_sshkey_account_test.go
Expand Up @@ -5,81 +5,43 @@ import (
"testing"

"github.com/OctopusDeploy/go-octopusdeploy/octopusdeploy"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestSSHKeyBasic(t *testing.T) {
const accountPrefix = constOctopusDeploySSHKeyAccount + ".foo"
const username = "foo"
const passphrase = "H3lloWorld"
localName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
prefix := constOctopusDeploySSHKeyAccount + "." + localName

const tagSetName = "TagSet"
const tagName = "Tag"
var tenantTags = fmt.Sprintf("%s/%s", tagSetName, tagName)
const tenantedDeploymentParticipation = octopusdeploy.TenantedDeploymentModeTenantedOrUntenanted
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
passphrase := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
tenantedDeploymentParticipation := octopusdeploy.TenantedDeploymentModeTenantedOrUntenanted
username := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)

resource.Test(t, resource.TestCase{
CheckDestroy: testAccountDestroy,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testOctopusDeploySSHKeyDestroy,
Steps: []resource.TestStep{
{
Config: testSSHKeyBasic(tagSetName, tagName, username, passphrase, tenantedDeploymentParticipation),
Config: testSSHKeyBasic(localName, name, username, passphrase, tenantedDeploymentParticipation),
Check: resource.ComposeTestCheckFunc(
testSSHKeyExists(accountPrefix),
resource.TestCheckResourceAttr(accountPrefix, constUsername, username),
resource.TestCheckResourceAttr(accountPrefix, constPassphrase, passphrase),
resource.TestCheckResourceAttr(accountPrefix, "tenant_tags.0", tenantTags),
resource.TestCheckResourceAttr(accountPrefix, constTenantedDeploymentParticipation, string(tenantedDeploymentParticipation)),
testAccountExists(prefix),
resource.TestCheckResourceAttr(prefix, constName, name),
resource.TestCheckResourceAttr(prefix, constPassphrase, passphrase),
resource.TestCheckResourceAttr(prefix, constTenantedDeploymentParticipation, string(tenantedDeploymentParticipation)),
resource.TestCheckResourceAttr(prefix, constUsername, username),
),
},
},
})
}

func testSSHKeyBasic(tagSetName string, tagName string, username string, passphrase string, tenantedDeploymentParticipation octopusdeploy.TenantedDeploymentMode) string {
return fmt.Sprintf(`
resource "%s" "foo" {
username = "%s"
passphrase = "%s"
tagSetName = "%s"
tenant_tags = ["${octopusdeploy_tag_set.testtagset.name}/%s"]
tenanted_deployment_participation = "%s"
}
`,
constOctopusDeploySSHKeyAccount, tagSetName, tagName, username, passphrase, tenantedDeploymentParticipation,
)
}

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

func existsSSHKeyHelper(s *terraform.State, client *octopusdeploy.Client) error {
accountID := s.RootModule().Resources[constOctopusDeploySSHKeyAccount+".foo"].Primary.ID
if _, err := client.Accounts.GetByID(accountID); err != nil {
return fmt.Errorf("Received an error retrieving SSH key account %s", err)
}

return nil
}

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

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

return nil
func testSSHKeyBasic(localName string, name string, username string, passphrase string, tenantedDeploymentParticipation octopusdeploy.TenantedDeploymentMode) string {
return fmt.Sprintf(`resource "%s" "%s" {
name = "%s"
passphrase = "%s"
tenanted_deployment_participation = "%s"
username = "%s"
}`, constOctopusDeploySSHKeyAccount, localName, name, passphrase, tenantedDeploymentParticipation, username)
}

0 comments on commit 9948db2

Please sign in to comment.