Skip to content

Commit

Permalink
Fix tests failing on CI (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrolla authored Oct 17, 2019
1 parent e79e049 commit 282e597
Show file tree
Hide file tree
Showing 39 changed files with 4,065 additions and 6,636 deletions.
25 changes: 12 additions & 13 deletions circleci/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,31 @@ import (
"github.com/hashicorp/terraform/terraform"
)

var providerOrgTestProvider *schema.Provider
var providerOrgTestProviders map[string]terraform.ResourceProvider
var testAccNoOrgProvider *schema.Provider
var testAccNoOrgProviders map[string]terraform.ResourceProvider

var resourceOrgTestProvider *schema.Provider
var resourceOrgTestProviders map[string]terraform.ResourceProvider
var testAccOrgProvider *schema.Provider
var testAccOrgProviders map[string]terraform.ResourceProvider

func init() {
resourceOrgTestProvider = Provider().(*schema.Provider)
resourceOrgTestProviders = map[string]terraform.ResourceProvider{
"circleci": resourceOrgTestProvider,
testAccNoOrgProvider = Provider().(*schema.Provider)
testAccNoOrgProviders = map[string]terraform.ResourceProvider{
"circleci": testAccNoOrgProvider,
}

providerOrgTestProvider = Provider().(*schema.Provider)
providerOrgTestProvider.Schema["organization"] = &schema.Schema{
testAccOrgProvider = Provider().(*schema.Provider)
testAccOrgProvider.Schema["organization"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("TEST_CIRCLECI_ORGANIZATION", nil),
Description: "The CircleCI organization.",
}
providerOrgTestProviders = map[string]terraform.ResourceProvider{
"circleci": providerOrgTestProvider,
testAccOrgProviders = map[string]terraform.ResourceProvider{
"circleci": testAccOrgProvider,
}
}

func testPreCheck(t *testing.T) {

func testAccPreCheck(t *testing.T) {
if v := os.Getenv("CIRCLECI_TOKEN"); v == "" {
t.Fatal("CIRCLECI_TOKEN must be set for acceptance tests")
}
Expand Down
91 changes: 45 additions & 46 deletions circleci/resource_circleci_environment_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,39 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestCircleCIEnvironmentVariableOrganizationNotSet(t *testing.T) {
func TestAccCircleCIEnvironmentVariableOrganizationNotSet(t *testing.T) {
project := "TEST_" + acctest.RandString(8)
envName := "TEST_" + acctest.RandString(8)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testPreCheck(t)
testAccPreCheck(t)
},
Providers: resourceOrgTestProviders,
IsUnitTest: true,
Providers: testAccNoOrgProviders,
Steps: []resource.TestStep{
{
Config: testCircleCIEnvironmentVariableConfigProviderOrg(project, envName, "value-for-the-test"),
Config: testAccCircleCIEnvironmentVariableConfigProviderOrg(project, envName, "value-for-the-test"),
ExpectError: regexp.MustCompile("organization has not been set for environment variable .*"),
},
},
})
}

func TestCircleCIEnvironmentVariableCreateThenUpdateProviderOrg(t *testing.T) {
func TestAccCircleCIEnvironmentVariableCreateThenUpdateProviderOrg(t *testing.T) {
project := os.Getenv("CIRCLECI_PROJECT")
envName := "TEST_" + acctest.RandString(8)
resourceName := "circleci_environment_variable." + envName
organization := os.Getenv("TEST_CIRCLECI_ORGANIZATION")

resource.Test(t, resource.TestCase{
PreCheck: func() {
testPreCheck(t)
testAccPreCheck(t)
},
Providers: providerOrgTestProviders,
CheckDestroy: testCircleCIEnvironmentVariableProviderOrgCheckDestroy,
Providers: testAccOrgProviders,
CheckDestroy: testAccCircleCIEnvironmentVariableProviderOrgCheckDestroy,
Steps: []resource.TestStep{
{
Config: testCircleCIEnvironmentVariableConfigProviderOrg(project, envName, "value-for-the-test"),
Config: testAccCircleCIEnvironmentVariableConfigProviderOrg(project, envName, "value-for-the-test"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s.%s.%s", organization, project, envName)),
resource.TestCheckResourceAttr(resourceName, "project", project),
Expand All @@ -55,7 +54,7 @@ func TestCircleCIEnvironmentVariableCreateThenUpdateProviderOrg(t *testing.T) {
),
},
{
Config: testCircleCIEnvironmentVariableConfigProviderOrg(project, envName, "value-for-the-test-again"),
Config: testAccCircleCIEnvironmentVariableConfigProviderOrg(project, envName, "value-for-the-test-again"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s.%s.%s", organization, project, envName)),
resource.TestCheckResourceAttr(resourceName, "project", project),
Expand All @@ -67,7 +66,7 @@ func TestCircleCIEnvironmentVariableCreateThenUpdateProviderOrg(t *testing.T) {
})
}

func TestCircleCIEnvironmentVariableCreateThenUpdateResourceOrg(t *testing.T) {
func TestAccCircleCIEnvironmentVariableCreateThenUpdateResourceOrg(t *testing.T) {
project := os.Getenv("CIRCLECI_PROJECT")
organization := os.Getenv("TEST_CIRCLECI_ORGANIZATION")
envName := "TEST_" + acctest.RandString(8)
Expand All @@ -76,13 +75,13 @@ func TestCircleCIEnvironmentVariableCreateThenUpdateResourceOrg(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() {
testPreCheck(t)
testAccPreCheck(t)
},
Providers: resourceOrgTestProviders,
CheckDestroy: testCircleCIEnvironmentVariableResourceOrgCheckDestroy,
Providers: testAccNoOrgProviders,
CheckDestroy: testAccCircleCIEnvironmentVariableResourceOrgCheckDestroy,
Steps: []resource.TestStep{
{
Config: testCircleCIEnvironmentVariableConfigResourceOrg(organization, project, envName, "value-for-the-test"),
Config: testAccCircleCIEnvironmentVariableConfigResourceOrg(organization, project, envName, "value-for-the-test"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s.%s.%s", organization, project, envName)),
resource.TestCheckResourceAttr(resourceName, "project", project),
Expand All @@ -91,7 +90,7 @@ func TestCircleCIEnvironmentVariableCreateThenUpdateResourceOrg(t *testing.T) {
),
},
{
Config: testCircleCIEnvironmentVariableConfigResourceOrg(organization, project, envName, "value-for-the-test-again"),
Config: testAccCircleCIEnvironmentVariableConfigResourceOrg(organization, project, envName, "value-for-the-test-again"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s.%s.%s", organization, project, envName)),
resource.TestCheckResourceAttr(resourceName, "project", project),
Expand All @@ -103,7 +102,7 @@ func TestCircleCIEnvironmentVariableCreateThenUpdateResourceOrg(t *testing.T) {
})
}

func TestCircleCIEnvironmentVariableCreateAlreadyExists(t *testing.T) {
func TestAccCircleCIEnvironmentVariableCreateAlreadyExists(t *testing.T) {
project := os.Getenv("CIRCLECI_PROJECT")
envName := "TEST_" + acctest.RandString(8)
envValue := acctest.RandString(8)
Expand All @@ -112,14 +111,14 @@ func TestCircleCIEnvironmentVariableCreateAlreadyExists(t *testing.T) {
resourceName := "circleci_environment_variable." + envName

resource.Test(t, resource.TestCase{
Providers: providerOrgTestProviders,
Providers: testAccOrgProviders,
PreCheck: func() {
testPreCheck(t)
testAccPreCheck(t)
},
CheckDestroy: testCircleCIEnvironmentVariableProviderOrgCheckDestroy,
CheckDestroy: testAccCircleCIEnvironmentVariableProviderOrgCheckDestroy,
Steps: []resource.TestStep{
{
Config: testCircleCIEnvironmentVariableConfigProviderOrg(project, envName, envValue),
Config: testAccCircleCIEnvironmentVariableConfigProviderOrg(project, envName, envValue),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s.%s.%s", organization, project, envName)),
resource.TestCheckResourceAttr(resourceName, "project", project),
Expand All @@ -128,28 +127,28 @@ func TestCircleCIEnvironmentVariableCreateAlreadyExists(t *testing.T) {
),
},
{
Config: testCircleCIEnvironmentVariableConfigIdentical(project, envName, envValue),
Config: testAccCircleCIEnvironmentVariableConfigIdentical(project, envName, envValue),
ExpectError: regexp.MustCompile("already exists"),
},
},
})
}

func TestCircleCIEnvironmentVariableImportProviderOrg(t *testing.T) {
func TestAccCircleCIEnvironmentVariableImportProviderOrg(t *testing.T) {
project := os.Getenv("CIRCLECI_PROJECT")
envName := "TEST_" + acctest.RandString(8)
resourceName := "circleci_environment_variable." + envName
organization := os.Getenv("TEST_CIRCLECI_ORGANIZATION")

resource.Test(t, resource.TestCase{
PreCheck: func() {
testPreCheck(t)
testAccPreCheck(t)
},
Providers: providerOrgTestProviders,
CheckDestroy: testCircleCIEnvironmentVariableProviderOrgCheckDestroy,
Providers: testAccOrgProviders,
CheckDestroy: testAccCircleCIEnvironmentVariableProviderOrgCheckDestroy,
Steps: []resource.TestStep{
{
Config: testCircleCIEnvironmentVariableConfigResourceOrg(organization, project, envName, "value-for-the-test"),
Config: testAccCircleCIEnvironmentVariableConfigResourceOrg(organization, project, envName, "value-for-the-test"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s.%s.%s", organization, project, envName)),
resource.TestCheckResourceAttr(resourceName, "project", project),
Expand All @@ -169,7 +168,7 @@ func TestCircleCIEnvironmentVariableImportProviderOrg(t *testing.T) {
})
}

func TestCircleCIEnvironmentVariableImportResourceOrg(t *testing.T) {
func TestAccCircleCIEnvironmentVariableImportResourceOrg(t *testing.T) {
project := os.Getenv("CIRCLECI_PROJECT")
organization := os.Getenv("TEST_CIRCLECI_ORGANIZATION")
envName := "TEST_" + acctest.RandString(8)
Expand All @@ -178,13 +177,13 @@ func TestCircleCIEnvironmentVariableImportResourceOrg(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() {
testPreCheck(t)
testAccPreCheck(t)
},
Providers: resourceOrgTestProviders,
CheckDestroy: testCircleCIEnvironmentVariableResourceOrgCheckDestroy,
Providers: testAccNoOrgProviders,
CheckDestroy: testAccCircleCIEnvironmentVariableResourceOrgCheckDestroy,
Steps: []resource.TestStep{
{
Config: testCircleCIEnvironmentVariableConfigResourceOrg(organization, project, envName, "value-for-the-test"),
Config: testAccCircleCIEnvironmentVariableConfigResourceOrg(organization, project, envName, "value-for-the-test"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s.%s.%s", organization, project, envName)),
resource.TestCheckResourceAttr(resourceName, "project", project),
Expand Down Expand Up @@ -290,17 +289,17 @@ func TestCircleCIEnvironmentVariableProviderOrgStateUpgradeV0(t *testing.T) {
assert.Equal(t, expected, actual)
}

func testCircleCIEnvironmentVariableResourceOrgCheckDestroy(s *terraform.State) error {
providerClient := resourceOrgTestProvider.Meta().(*ProviderClient)
return testCircleCIEnvironmentVariableCheckDestroy(providerClient, s)
func testAccCircleCIEnvironmentVariableResourceOrgCheckDestroy(s *terraform.State) error {
providerClient := testAccNoOrgProvider.Meta().(*ProviderClient)
return testAccCircleCIEnvironmentVariableCheckDestroy(providerClient, s)
}

func testCircleCIEnvironmentVariableProviderOrgCheckDestroy(s *terraform.State) error {
providerClient := providerOrgTestProvider.Meta().(*ProviderClient)
return testCircleCIEnvironmentVariableCheckDestroy(providerClient, s)
func testAccCircleCIEnvironmentVariableProviderOrgCheckDestroy(s *terraform.State) error {
providerClient := testAccOrgProvider.Meta().(*ProviderClient)
return testAccCircleCIEnvironmentVariableCheckDestroy(providerClient, s)
}

func testCircleCIEnvironmentVariableCheckDestroy(providerClient *ProviderClient, s *terraform.State) error {
func testAccCircleCIEnvironmentVariableCheckDestroy(providerClient *ProviderClient, s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "circleci_environment_variable" {
continue
Expand All @@ -324,7 +323,7 @@ func testCircleCIEnvironmentVariableCheckDestroy(providerClient *ProviderClient,
return nil
}

func testCircleCIEnvironmentVariableConfigProviderOrg(project, name, value string) string {
func testAccCircleCIEnvironmentVariableConfigProviderOrg(project, name, value string) string {
return fmt.Sprintf(`
resource "circleci_environment_variable" "%[2]s" {
project = "%[1]s"
Expand All @@ -333,17 +332,17 @@ resource "circleci_environment_variable" "%[2]s" {
}`, project, name, value)
}

func testCircleCIEnvironmentVariableConfigResourceOrg(organization, project, name, value string) string {
func testAccCircleCIEnvironmentVariableConfigResourceOrg(organization, project, name, value string) string {
return fmt.Sprintf(`
resource "circleci_environment_variable" "%[2]s" {
organization = "%[4]s"
project = "%[1]s"
name = "%[2]s"
value = "%[3]s"
project = "%[1]s"
name = "%[2]s"
value = "%[3]s"
}`, project, name, value, organization)
}

func testCircleCIEnvironmentVariableConfigIdentical(project, name, value string) string {
func testAccCircleCIEnvironmentVariableConfigIdentical(project, name, value string) string {
return fmt.Sprintf(`
resource "circleci_environment_variable" "%[2]s" {
project = "%[1]s"
Expand Down
Loading

0 comments on commit 282e597

Please sign in to comment.