Skip to content

Commit

Permalink
Use real module config with variable options in test
Browse files Browse the repository at this point in the history
  • Loading branch information
dsa0x committed Oct 22, 2024
1 parent a43eff0 commit 1b6bced
Showing 1 changed file with 82 additions and 14 deletions.
96 changes: 82 additions & 14 deletions internal/provider/resource_tfe_no_code_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,57 @@ func TestAccTFENoCodeModule_basic(t *testing.T) {
}

func TestAccTFENoCodeModule_with_variable_options(t *testing.T) {
skipUnlessBeta(t)
nocodeModule := &tfe.RegistryNoCodeModule{}
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
regionOptions := `"us-east-1", "us-west-1", "eu-west-2"`
tfeClient, err := getClientUsingEnv()
if err != nil {
t.Fatalf("error getting client %v", err)
}
org, cleanup := createBusinessOrganization(t, tfeClient)
defer cleanup()
providers := providerWithDefaultOrganization(org.Name)
cfg := testAccTFENoCodeModule_with_variable_options(org.Name)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFENoCodeModuleDestroy,
Providers: providers,
CheckDestroy: func(s *terraform.State) error { return nil },
Steps: []resource.TestStep{
{
Config: testAccTFENoCodeModule_with_options(rInt, regionOptions),
Config: cfg,
Check: resource.ComposeTestCheckFunc(
testAccCheckTFENoCodeModuleExists(
"tfe_no_code_module.foobar", nocodeModule),
resource.TestCheckResourceAttr(
"tfe_no_code_module.foobar", "enabled", "true"),
resource.TestCheckResourceAttr(
"tfe_no_code_module.foobar", "organization", fmt.Sprintf("tst-terraform-%d", rInt)),
testAccCheckTFENoCodeModuleVariableOptions(nocodeModule),
func(s *terraform.State) error {
n := "tfe_no_code_module.sensitive"
rs, ok := s.RootModule().Resources[n]
if !ok || rs.Primary.ID == "" {
return fmt.Errorf("Not found: %s", n)
}

config := providers["tfe"].Meta().(ConfiguredClient)
opts := &tfe.RegistryNoCodeModuleReadOptions{
Include: []tfe.RegistryNoCodeModuleIncludeOpt{tfe.RegistryNoCodeIncludeVariableOptions},
}
nocodeModule, err := config.Client.RegistryNoCodeModules.Read(ctx, rs.Primary.ID, opts)
if err != nil {
return fmt.Errorf("unable to read nocodeModule with ID %s", rs.Primary.ID)
}

if !nocodeModule.Enabled {
return fmt.Errorf("Bad 'enabled' attribute: %t", nocodeModule.Enabled)
}

if len(nocodeModule.VariableOptions) == 0 {
return fmt.Errorf("Bad 'variable_options' attribute: %v", nocodeModule.VariableOptions)
}

for _, vo := range nocodeModule.VariableOptions {
if vo.VariableName == "min_lower" {
if len(vo.Options) != 5 {
return fmt.Errorf("Bad 'min_lower' attribute options: %v", nocodeModule.VariableOptions)
}
}
}

return nil
},
),
},
},
Expand Down Expand Up @@ -361,3 +392,40 @@ func testAccCheckTFENoCodeModuleVariableOptions(
return nil
}
}

func testAccTFENoCodeModule_with_variable_options(org string) string {
return fmt.Sprintf(`
locals {
organization_name = "%s"
identifier = "%s"
}
resource "tfe_oauth_client" "github" {
organization = local.organization_name
api_url = "https://api.github.com"
http_url = "https://github.com"
oauth_token = "%s"
service_provider = "github"
}
resource "tfe_registry_module" "sensitive" {
vcs_repo {
display_identifier = local.identifier
identifier = local.identifier
oauth_token_id = tfe_oauth_client.github.oauth_token_id
}
}
resource "tfe_no_code_module" "sensitive" {
organization = local.organization_name
registry_module = tfe_registry_module.sensitive.id
version_pin = "1.1.0"
variable_options {
name = "min_lower"
type = "number"
options = [ "1", "2", "3", "4", "5" ]
}
}
`, org, envGithubRegistryModuleIdentifer, envGithubToken)
}

0 comments on commit 1b6bced

Please sign in to comment.