Files
terraform-provider-threshold/internal/provider/resource_auth_method_test.go
T
Michael Li 2d920805d4 test: Update tests to run against boundary-enterprise (#834)
* chore: Update make testacc to use boundary dev

Use boundary dev instead of test controller in order to get an enterprise cluster

* test: Update tests to use boundary dev cluster

- Set up environment variables via loadTestConfig
- Update testConfigWithRecovery to use recovery key from config
- Update testCOnfigWithoutAMPWCredentials to use boundary dev's auth method

* test: Update tests to use their own orgs

This is to address some test failures due to conflicts when creating resources within the same org.

* test: Enable tests that require boundary-enterprise

* chore: Update workflow to use boundary enterprise
2026-09-10 15:00:05 -04:00

193 lines
6.2 KiB
Go

// Copyright IBM Corp. 2020, 2026
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/authmethods"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/require"
)
const (
fooBaseAuthMethodDesc = "test auth method"
fooBaseAuthMethodDescUpdate = "test auth method update"
)
// TODO(malnick) - remove these tests after deprecating use of password params in
// favor of attributes map
var (
fooBaseAuthMethod = fmt.Sprintf(`
resource "boundary_auth_method" "foo" {
name = "test"
description = "%s"
type = "password"
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
}`, fooBaseAuthMethodDesc)
fooBaseAuthMethodUpdate = fmt.Sprintf(`
resource "boundary_auth_method" "foo" {
name = "test"
description = "%s"
type = "password"
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
}`, fooBaseAuthMethodDescUpdate)
fooBaseAuthMethodIsPrimaryUpdate = fmt.Sprintf(`
resource "boundary_auth_method" "foo" {
name = "test"
description = "%s"
type = "password"
scope_id = boundary_scope.org1.id
is_primary_for_scope = true
depends_on = [boundary_role.org1_admin]
}`, fooAuthMethodDesc)
)
func TestAccBaseAuthMethodPassword(t *testing.T) {
cfg, err := loadTestConfig()
require.NoError(t, err)
suffix := id.UniqueId()
url := cfg.BoundaryAddr
var provider *schema.Provider
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories(&provider),
CheckDestroy: testAccCheckAuthMethodResourceDestroy(t, provider, baseAuthMethodType),
Steps: []resource.TestStep{
{
// create
Config: testConfig(url, fooOrg(suffix), fooBaseAuthMethod),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("boundary_auth_method.foo", "description", fooBaseAuthMethodDesc),
resource.TestCheckResourceAttr("boundary_auth_method.foo", "name", "test"),
resource.TestCheckResourceAttr("boundary_auth_method.foo", "type", "password"),
testAccCheckAuthMethodResourceExists(provider, "boundary_auth_method.foo"),
),
},
importStep("boundary_auth_method.foo"),
{
// update
Config: testConfig(url, fooOrg(suffix), fooBaseAuthMethodUpdate),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("boundary_auth_method.foo", "description", fooBaseAuthMethodDescUpdate),
resource.TestCheckResourceAttr("boundary_auth_method.foo", "name", "test"),
resource.TestCheckResourceAttr("boundary_auth_method.foo", "type", "password"),
testAccCheckAuthMethodResourceExists(provider, "boundary_auth_method.foo"),
),
},
importStep("boundary_auth_method.foo"),
},
})
}
func TestAccBaseAuthMethodPasswordIsPrimary(t *testing.T) {
cfg, err := loadTestConfig()
require.NoError(t, err)
suffix := id.UniqueId()
url := cfg.BoundaryAddr
var provider *schema.Provider
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories(&provider),
CheckDestroy: testAccCheckAuthMethodResourceDestroy(t, provider, passwordAuthMethodType),
Steps: []resource.TestStep{
{
// create
Config: testConfig(url, fooOrg(suffix), fooBaseAuthMethod),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("boundary_auth_method.foo", "description", fooBaseAuthMethodDesc),
resource.TestCheckResourceAttr("boundary_auth_method.foo", "name", "test"),
resource.TestCheckResourceAttr("boundary_auth_method.foo", "type", "password"),
testAccCheckAuthMethodResourceExists(provider, "boundary_auth_method.foo"),
testAccIsPrimaryForScope(provider, "boundary_auth_method.foo", false),
),
},
importStep("boundary_auth_method.foo"),
{
// update
Config: testConfig(url, fooOrg(suffix), fooBaseAuthMethodIsPrimaryUpdate),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("boundary_auth_method.foo", "description", fooBaseAuthMethodDesc),
resource.TestCheckResourceAttr("boundary_auth_method.foo", "name", "test"),
resource.TestCheckResourceAttr("boundary_auth_method.foo", "type", "password"),
testAccCheckAuthMethodResourceExists(provider, "boundary_auth_method.foo"),
testAccIsPrimaryForScope(provider, "boundary_auth_method.foo", true),
),
},
},
})
}
func testAccCheckAuthMethodResourceExists(testProvider *schema.Provider, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
id := rs.Primary.ID
if id == "" {
return fmt.Errorf("No ID is set")
}
md := testProvider.Meta().(*metaData)
amClient := authmethods.NewClient(md.client)
if _, err := amClient.Read(context.Background(), id); err != nil {
return fmt.Errorf("Got an error when reading auth method %q: %w", id, err)
}
return nil
}
}
type authMethodType string
const (
baseAuthMethodType authMethodType = "boundary_auth_method"
passwordAuthMethodType authMethodType = "boundary_auth_method_password"
ldapAuthMethodType authMethodType = "boundary_auth_method_ldap"
oidcAuthMethodType authMethodType = "boundary_auth_method_oidc"
)
func testAccCheckAuthMethodResourceDestroy(t *testing.T, testProvider *schema.Provider, typ authMethodType) resource.TestCheckFunc {
return func(s *terraform.State) error {
if testProvider.Meta() == nil {
t.Fatal("got nil provider metadata")
}
md := testProvider.Meta().(*metaData)
for _, rs := range s.RootModule().Resources {
switch rs.Type {
case string(typ):
id := rs.Primary.ID
amClient := authmethods.NewClient(md.client)
_, err := amClient.Read(context.Background(), id)
if apiErr := api.AsServerError(err); apiErr == nil || apiErr.Response().StatusCode() != http.StatusNotFound {
return fmt.Errorf("didn't get a 404 when reading destroyed auth method %q: %v", id, err)
}
default:
continue
}
}
return nil
}
}