Skip to content

Commit

Permalink
Merge pull request #415 from hashicorp/sebasslash/upgrade-org-feature…
Browse files Browse the repository at this point in the history
…s-test

Upgrade org feature set [test helper]
  • Loading branch information
sebasslash committed Jun 1, 2022
2 parents 80a18ff + c20e716 commit 3d3537f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/TESTS.md
Expand Up @@ -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.
69 changes: 69 additions & 0 deletions helper_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"sync"
"testing"
Expand All @@ -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
Expand Down Expand Up @@ -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{}
Expand Down

0 comments on commit 3d3537f

Please sign in to comment.