Files
terraform-provider-threshold/internal/provider/resource_host_set_plugin_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

286 lines
11 KiB
Go

// Copyright IBM Corp. 2020, 2026
// SPDX-License-Identifier: MPL-2.0
package provider
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"github.com/hashicorp/boundary/api/hostsets"
"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/assert"
"github.com/stretchr/testify/require"
)
var currentPluginHostSetAttributesValue string
func TestAccHostSetPlugin(t *testing.T) {
t.Parallel()
initialPreferredEndpoints := []string{"cidr:1.2.3.4/32", "dns:bar.foo.com"}
initialPreferredEndpointsRaw, err := json.Marshal(initialPreferredEndpoints)
require.NoError(t, err)
initialPreferredEndpointsStr := string(initialPreferredEndpointsRaw)
updatePreferredEndpoints := []string{"dns:bar.foo.com", "cidr:4.3.2.0/24"}
updatePreferredEndpointsRaw, err := json.Marshal(updatePreferredEndpoints)
require.NoError(t, err)
updatePreferredEndpointsStr := string(updatePreferredEndpointsRaw)
initialSyncIntervalSeconds := -1
updateSyncIntervalSeconds := 60
catalogBlock := `
resource "boundary_host_catalog_plugin" "foo" {
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
plugin_name = "loopback"
}`
hostSetBlock := `
resource "boundary_host_set_plugin" "foo" {
host_catalog_id = boundary_host_catalog_plugin.foo.id
name = "test"
description = "test hostset"
preferred_endpoints = %s
sync_interval_seconds = %d
%s
}`
cfg, err := loadTestConfig()
require.NoError(t, err)
suffix := id.UniqueId()
url := cfg.BoundaryAddr
fooSetName := "boundary_host_set_plugin.foo"
attrBlock1 := `attributes_json = jsonencode({
foo = "bar"
zip = "zap"
})`
attrBlock2 := `attributes_json = jsonencode({
foo = "bar"
zip = "zoop"
})`
attrBlock3 := `attributes_json = jsonencode({
foo = "bar"
zip = "zoop"
})`
attrBlock4 := ``
attrBlock5 := `attributes_json = jsonencode({
foo = "bar"
zip = "zoop"
})`
attrBlock6 := `attributes_json = "null"`
hsBlock := fmt.Sprintf(hostSetBlock, initialPreferredEndpointsStr, initialSyncIntervalSeconds, attrBlock1)
hsUpdate1Block := fmt.Sprintf(hostSetBlock, updatePreferredEndpointsStr, updateSyncIntervalSeconds, attrBlock2)
hsUpdate2Block := fmt.Sprintf(hostSetBlock, updatePreferredEndpointsStr, updateSyncIntervalSeconds, attrBlock3)
hsUpdate3Block := fmt.Sprintf(hostSetBlock, updatePreferredEndpointsStr, updateSyncIntervalSeconds, attrBlock4)
hsUpdate4Block := fmt.Sprintf(hostSetBlock, updatePreferredEndpointsStr, updateSyncIntervalSeconds, attrBlock5)
hsUpdate5Block := fmt.Sprintf(hostSetBlock, updatePreferredEndpointsStr, updateSyncIntervalSeconds, attrBlock6)
var provider *schema.Provider
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories(&provider),
CheckDestroy: testAccCheckHostSetResourceDestroy(t, provider, baseHostSetType),
Steps: []resource.TestStep{
{
// test project hostset create
Config: testConfig(url, fooOrg(suffix), firstProjectFoo, catalogBlock, hsBlock),
Check: resource.ComposeTestCheckFunc(
testAccCheckHostSetPluginResourceExists(provider, fooSetName, expectedAttributesStatePreviouslyEmptyNowSet),
resource.TestCheckResourceAttr(fooSetName, NameKey, "test"),
resource.TestCheckResourceAttr(fooSetName, DescriptionKey, "test hostset"),
resource.TestCheckResourceAttr(fooSetName, SyncIntervalSecondsKey, fmt.Sprintf("%d", initialSyncIntervalSeconds)),
testAccCheckHostSetPluginPreferredEndpoints(t, provider, fooSetName, initialPreferredEndpoints),
),
ExpectNonEmptyPlan: true,
},
importStep(fooSetName),
{
// test project hostset update
Config: testConfig(url, fooOrg(suffix), firstProjectFoo, catalogBlock, hsUpdate1Block),
Check: resource.ComposeTestCheckFunc(
testAccCheckHostSetPluginResourceExists(provider, fooSetName, expectedAttributesStatePreviouslySetButChanged),
resource.TestCheckResourceAttr(fooSetName, NameKey, "test"),
resource.TestCheckResourceAttr(fooSetName, DescriptionKey, "test hostset"),
resource.TestCheckResourceAttr(fooSetName, SyncIntervalSecondsKey, fmt.Sprintf("%d", updateSyncIntervalSeconds)),
testAccCheckHostSetPluginPreferredEndpoints(t, provider, fooSetName, updatePreferredEndpoints),
),
ExpectNonEmptyPlan: true,
},
importStep(fooSetName),
{
// test project hostset update
Config: testConfig(url, fooOrg(suffix), firstProjectFoo, catalogBlock, hsUpdate2Block),
Check: resource.ComposeTestCheckFunc(
testAccCheckHostSetPluginResourceExists(provider, fooSetName, expectedAttributesStatePreviouslySetNoChange),
resource.TestCheckResourceAttr(fooSetName, NameKey, "test"),
resource.TestCheckResourceAttr(fooSetName, DescriptionKey, "test hostset"),
resource.TestCheckResourceAttr(fooSetName, SyncIntervalSecondsKey, fmt.Sprintf("%d", updateSyncIntervalSeconds)),
testAccCheckHostSetPluginPreferredEndpoints(t, provider, fooSetName, updatePreferredEndpoints),
),
ExpectNonEmptyPlan: true,
},
importStep(fooSetName),
{
// test project hostset update
Config: testConfig(url, fooOrg(suffix), firstProjectFoo, catalogBlock, hsUpdate3Block),
Check: resource.ComposeTestCheckFunc(
testAccCheckHostSetPluginResourceExists(provider, fooSetName, expectedAttributesStatePreviouslySetNowEmpty),
resource.TestCheckResourceAttr(fooSetName, NameKey, "test"),
resource.TestCheckResourceAttr(fooSetName, DescriptionKey, "test hostset"),
resource.TestCheckResourceAttr(fooSetName, SyncIntervalSecondsKey, fmt.Sprintf("%d", updateSyncIntervalSeconds)),
testAccCheckHostSetPluginPreferredEndpoints(t, provider, fooSetName, updatePreferredEndpoints),
),
ExpectNonEmptyPlan: true,
},
importStep(fooSetName),
{
// test project hostset update
Config: testConfig(url, fooOrg(suffix), firstProjectFoo, catalogBlock, hsUpdate4Block),
Check: resource.ComposeTestCheckFunc(
testAccCheckHostSetPluginResourceExists(provider, fooSetName, expectedAttributesStatePreviouslyEmptyNowSet),
resource.TestCheckResourceAttr(fooSetName, NameKey, "test"),
resource.TestCheckResourceAttr(fooSetName, DescriptionKey, "test hostset"),
resource.TestCheckResourceAttr(fooSetName, SyncIntervalSecondsKey, fmt.Sprintf("%d", updateSyncIntervalSeconds)),
testAccCheckHostSetPluginPreferredEndpoints(t, provider, fooSetName, updatePreferredEndpoints),
),
ExpectNonEmptyPlan: true,
},
importStep(fooSetName),
{
// test project hostset update
Config: testConfig(url, fooOrg(suffix), firstProjectFoo, catalogBlock, hsUpdate5Block),
Check: resource.ComposeTestCheckFunc(
testAccCheckHostSetPluginResourceExists(provider, fooSetName, expectedAttributesStatePreviouslySetNowEmpty),
resource.TestCheckResourceAttr(fooSetName, NameKey, "test"),
resource.TestCheckResourceAttr(fooSetName, DescriptionKey, "test hostset"),
resource.TestCheckResourceAttr(fooSetName, SyncIntervalSecondsKey, fmt.Sprintf("%d", updateSyncIntervalSeconds)),
testAccCheckHostSetPluginPreferredEndpoints(t, provider, fooSetName, updatePreferredEndpoints),
),
ExpectNonEmptyPlan: true,
},
importStep(fooSetName),
},
})
}
func testAccCheckHostSetPluginResourceExists(testProvider *schema.Provider, name string, expAttrs expectedAttributesState) 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)
hostsetsClient := hostsets.NewClient(md.client)
hsrr, err := hostsetsClient.Read(context.Background(), id)
if err != nil {
return fmt.Errorf("Got an error when reading hostset %q: %v", id, err)
}
attrs := hsrr.GetResponse().Map["attributes"]
switch expAttrs {
case expectedAttributesStatePreviouslyEmptyNowSet:
if currentPluginHostSetAttributesValue != "" {
return fmt.Errorf("expected no previous attributes value, got %s", currentPluginHostSetAttributesValue)
}
if attrs == nil {
return errors.New("expected non-empty new attributes value")
}
attrsVal, err := json.Marshal(attrs)
if err != nil {
return fmt.Errorf("error marshaling attrs: %w", err)
}
currentPluginHostSetAttributesValue = string(attrsVal)
case expectedAttributesStatePreviouslySetButChanged:
if currentPluginHostSetAttributesValue == "" {
return errors.New("expected previous attributes value")
}
if attrs == nil {
return errors.New("expected non-empty new attributes value")
}
attrsVal, err := json.Marshal(attrs)
if err != nil {
return fmt.Errorf("error marshaling attrs: %w", err)
}
if string(attrsVal) == currentPluginHostSetAttributesValue {
return errors.New("expected changed attrs value")
}
currentPluginHostSetAttributesValue = string(attrsVal)
case expectedAttributesStatePreviouslySetNoChange:
if currentPluginHostSetAttributesValue == "" {
return errors.New("expected previous attributes value")
}
if attrs == nil {
return errors.New("expected non-empty new attributes value")
}
attrsVal, err := json.Marshal(attrs)
if err != nil {
return fmt.Errorf("error marshaling attrs: %w", err)
}
if string(attrsVal) == "" {
return errors.New("expected non-empty new attributes value")
}
if string(attrsVal) != currentPluginHostSetAttributesValue {
return errors.New("expected same attributes value")
}
case expectedAttributesStatePreviouslySetNowEmpty:
if currentPluginHostSetAttributesValue == "" {
return errors.New("expected previous attributes value")
}
if attrs != nil {
return fmt.Errorf("expected empty new attributes value, got %s", attrs)
}
currentPluginHostSetAttributesValue = ""
}
return nil
}
}
func testAccCheckHostSetPluginPreferredEndpoints(t *testing.T, testProvider *schema.Provider, name string, wantPreferredEndpoints []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("host set resource not found: %s", name)
}
id := rs.Primary.ID
if id == "" {
return fmt.Errorf("host set resource ID is not set")
}
// check boundary to ensure it matches
md := testProvider.Meta().(*metaData)
client := md.client.Clone()
hstClient := hostsets.NewClient(client)
hs, err := hstClient.Read(context.Background(), id)
if err != nil {
return fmt.Errorf("Got an error when reading hostset %q: %v", id, err)
}
assert.Equal(t, wantPreferredEndpoints, hs.Item.PreferredEndpoints)
return nil
}
}