-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1291 from hashicorp/gs/fix-testing
Fix acceptance test of Run Tasks
- Loading branch information
Showing
12 changed files
with
228 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package provider | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
tfe "github.com/hashicorp/go-tfe" | ||
) | ||
|
||
type adminRoleType string | ||
|
||
const ( | ||
siteAdmin adminRoleType = "site-admin" | ||
configurationAdmin adminRoleType = "configuration" | ||
provisionLicensesAdmin adminRoleType = "provision-licenses" | ||
subscriptionAdmin adminRoleType = "subscription" | ||
supportAdmin adminRoleType = "support" | ||
securityMaintenanceAdmin adminRoleType = "security-maintenance" | ||
versionMaintenanceAdmin adminRoleType = "version-maintenance" | ||
) | ||
|
||
func getTokenForAdminRole(adminRole adminRoleType) string { | ||
token := "" | ||
|
||
switch adminRole { | ||
case siteAdmin: | ||
token = os.Getenv("TFE_ADMIN_SITE_ADMIN_TOKEN") | ||
case configurationAdmin: | ||
token = os.Getenv("TFE_ADMIN_CONFIGURATION_TOKEN") | ||
case provisionLicensesAdmin: | ||
token = os.Getenv("TFE_ADMIN_PROVISION_LICENSES_TOKEN") | ||
case subscriptionAdmin: | ||
token = os.Getenv("TFE_ADMIN_SUBSCRIPTION_TOKEN") | ||
case supportAdmin: | ||
token = os.Getenv("TFE_ADMIN_SUPPORT_TOKEN") | ||
case securityMaintenanceAdmin: | ||
token = os.Getenv("TFE_ADMIN_SECURITY_MAINTENANCE_TOKEN") | ||
case versionMaintenanceAdmin: | ||
token = os.Getenv("TFE_ADMIN_VERSION_MAINTENANCE_TOKEN") | ||
} | ||
|
||
return token | ||
} | ||
|
||
func testAdminClient(t *testing.T, adminRole adminRoleType) *tfe.Client { | ||
token := getTokenForAdminRole(adminRole) | ||
if token == "" { | ||
t.Fatal("missing API token for admin role " + adminRole) | ||
} | ||
|
||
client, err := tfe.NewClient(&tfe.Config{ | ||
Token: token, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return client | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ package provider | |
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"testing" | ||
"time" | ||
|
@@ -18,36 +17,14 @@ import ( | |
) | ||
|
||
const RunTasksURLEnvName = "RUN_TASKS_URL" | ||
const RunTasksHMACKeyEnvName = "RUN_TASKS_HMAC" | ||
|
||
type testClientOptions struct { | ||
defaultOrganization string | ||
defaultWorkspaceID string | ||
remoteStateConsumersResponse string | ||
} | ||
|
||
type featureSet struct { | ||
ID string `jsonapi:"primary,feature-sets"` | ||
} | ||
|
||
type featureSetList struct { | ||
Items []*featureSet | ||
*tfe.Pagination | ||
} | ||
|
||
type featureSetListOptions struct { | ||
Q string `url:"q,omitempty"` | ||
} | ||
|
||
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"` | ||
} | ||
|
||
// testTfeClient creates a mock client that creates workspaces with their ID | ||
// set to workspaceID. | ||
func testTfeClient(t *testing.T, options testClientOptions) *tfe.Client { | ||
|
@@ -72,50 +49,10 @@ func testTfeClient(t *testing.T, options testClientOptions) *tfe.Client { | |
return client | ||
} | ||
|
||
func upgradeOrganizationSubscription(t *testing.T, client *tfe.Client, org *tfe.Organization) { | ||
if enterpriseEnabled() { | ||
t.Skip("Cannot 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 = req.Do(context.Background(), fsl) | ||
if err != nil { | ||
t.Fatalf("failed to enumerate feature sets: %v", err) | ||
return | ||
} else if len(fsl.Items) == 0 { | ||
// this will serve as our catch all if enterprise is not enabled | ||
// but the instance itself is enterprise | ||
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(org.Name)) | ||
req, err = client.NewRequest("POST", u, &opts) | ||
if err != nil { | ||
t.Fatalf("Failed to create request: %v", err) | ||
return | ||
} | ||
|
||
err = req.Do(context.Background(), nil) | ||
if err != nil { | ||
t.Fatalf("Failed to upgrade subscription: %v", err) | ||
} | ||
// Attempts to upgrade an organization to the business plan. Requires a user token with admin access. | ||
// DEPRECATED : Please use the newSubscriptionUpdater instead. | ||
func upgradeOrganizationSubscription(t *testing.T, _ *tfe.Client, organization *tfe.Organization) { | ||
newSubscriptionUpdater(organization).WithBusinessPlan().Update(t) | ||
} | ||
|
||
func createBusinessOrganization(t *testing.T, client *tfe.Client) (*tfe.Organization, func()) { | ||
|
@@ -124,7 +61,7 @@ func createBusinessOrganization(t *testing.T, client *tfe.Client) (*tfe.Organiza | |
Email: tfe.String(fmt.Sprintf("%[email protected]", randomString(t))), | ||
}) | ||
|
||
upgradeOrganizationSubscription(t, client, org) | ||
newSubscriptionUpdater(org).WithBusinessPlan().Update(t) | ||
|
||
return org, orgCleanup | ||
} | ||
|
@@ -275,6 +212,10 @@ func runTasksURL() string { | |
return os.Getenv(RunTasksURLEnvName) | ||
} | ||
|
||
func runTasksHMACKey() string { | ||
return os.Getenv(RunTasksHMACKeyEnvName) | ||
} | ||
|
||
// Checks to see if ENABLE_BETA is set to 1, thereby enabling tests for beta features. | ||
func betaFeaturesEnabled() bool { | ||
return os.Getenv("ENABLE_BETA") == "1" | ||
|
Oops, something went wrong.