diff --git a/docs/TESTS.md b/docs/TESTS.md index faa5a99ea..b1205cc87 100644 --- a/docs/TESTS.md +++ b/docs/TESTS.md @@ -84,3 +84,7 @@ $ envchain YOUR_NAMESPACE_HERE go test ./... -timeout=30m -tags=integration ```sh $ TFE_TOKEN=xyz TFE_ADDRESS=xyz ENABLE_TFE=1 go test ./... -timeout=30m -tags=integration ``` + +### Running tests for TFC features that require paid plans (HashiCorp Employees) + +You can use the test helper `upgradeOrganizationSubscription()` to upgrade your test organization to a Business Plan, giving the organization access to all features in Terraform Cloud. This method requires `TFE_TOKEN` to be a user token with administrator access in the target test environment. Furthermore, you **can not** have enterprise features enabled (`ENABLE_TFE=1`) in order to use this method since the API call fails against TFE test environments. diff --git a/helper_test.go b/helper_test.go index 4aef68cf1..3d04c0b9f 100644 --- a/helper_test.go +++ b/helper_test.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "io/ioutil" + "net/url" "os" "sync" "testing" @@ -24,8 +25,31 @@ const tickDuration = 2 // Memoize test account details var _testAccountDetails *TestAccountDetails +type featureSet struct { + ID string `jsonapi:"primary,feature-sets"` +} + +type featureSetList struct { + Items []*featureSet + *Pagination +} + +type featureSetListOptions struct { + Q string `url:"q,omitempty"` +} + type retryableFn func() (interface{}, error) +type updateFeatureSetOptions struct { + Type string `jsonapi:"primary,subscription"` + RunsCeiling int `jsonapi:"attr,runs-ceiling"` + ContractStartAt time.Time `jsonapi:"attr,contract-start-at,iso8601"` + ContractUserLimit int `jsonapi:"attr,contract-user-limit"` + ContractApplyLimit int `jsonapi:"attr,contract-apply-limit"` + + FeatureSet *featureSet `jsonapi:"relation,feature-set"` +} + func testClient(t *testing.T) *Client { client, err := NewClient(nil) client.RetryServerErrors(true) // because occasionally we get a 500 internal when deleting an organization's workspace @@ -1177,6 +1201,51 @@ func createVariableSetVariable(t *testing.T, client *Client, vs *VariableSet, op } } +// Attempts to upgrade an organization to the business plan. Requires a user token with admin access. +func upgradeOrganizationSubscription(t *testing.T, client *Client, organization *Organization) { + if enterpriseEnabled() { + t.Skip("Can not upgrade an organization's subscription when enterprise is enabled. Set ENABLE_TFE=0 to run.") + } + + req, err := client.newRequest("GET", "admin/feature-sets", featureSetListOptions{ + Q: "Business", + }) + if err != nil { + t.Fatal(err) + return + } + + fsl := &featureSetList{} + err = client.do(context.Background(), req, fsl) + if err != nil { + t.Fatalf("failed to enumerate feature sets: %v", err) + return + } else if len(fsl.Items) == 0 { + t.Fatalf("feature set response was empty") + return + } + + opts := updateFeatureSetOptions{ + RunsCeiling: 10, + ContractStartAt: time.Now(), + ContractUserLimit: 1000, + ContractApplyLimit: 5000, + FeatureSet: fsl.Items[0], + } + + u := fmt.Sprintf("admin/organizations/%s/subscription", url.QueryEscape(organization.Name)) + req, err = client.newRequest("POST", u, &opts) + if err != nil { + t.Fatalf("Failed to create request: %v", err) + return + } + + err = client.do(context.Background(), req, nil) + if err != nil { + t.Fatalf("Failed to upgrade subscription: %v", err) + } +} + func waitForSVOutputs(t *testing.T, client *Client, svID string) { t.Helper() wg := &sync.WaitGroup{}