2021-09-22 23:53:33 +02:00
|
|
|
//go:build e2e
|
|
|
|
// +build e2e
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-10-12 00:14:44 +02:00
|
|
|
"os"
|
2021-09-22 23:53:33 +02:00
|
|
|
"testing"
|
2021-09-22 23:53:33 +02:00
|
|
|
"time"
|
2021-09-22 23:53:33 +02:00
|
|
|
|
|
|
|
tfe "github.com/hashicorp/go-tfe"
|
|
|
|
"github.com/hashicorp/go-uuid"
|
2021-10-28 01:07:12 +02:00
|
|
|
tfversion "github.com/hashicorp/terraform/version"
|
2021-09-22 23:53:33 +02:00
|
|
|
)
|
|
|
|
|
2021-09-22 23:53:33 +02:00
|
|
|
const (
|
|
|
|
expectConsoleTimeout = 15 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
type tfCommand struct {
|
2021-10-09 14:47:12 +02:00
|
|
|
command []string
|
|
|
|
expectedCmdOutput string
|
|
|
|
expectedErr string
|
|
|
|
expectError bool
|
|
|
|
userInput []string
|
|
|
|
postInputOutput []string
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type operationSets struct {
|
|
|
|
commands []tfCommand
|
|
|
|
prep func(t *testing.T, orgName, dir string)
|
|
|
|
}
|
|
|
|
|
2021-10-12 00:14:44 +02:00
|
|
|
type testCases map[string]struct {
|
|
|
|
operations []operationSets
|
|
|
|
validations func(t *testing.T, orgName string)
|
|
|
|
}
|
|
|
|
|
2021-09-22 23:53:33 +02:00
|
|
|
func createOrganization(t *testing.T) (*tfe.Organization, func()) {
|
|
|
|
ctx := context.Background()
|
|
|
|
org, err := tfeClient.Organizations.Create(ctx, tfe.OrganizationCreateOptions{
|
2021-10-28 07:35:58 +02:00
|
|
|
Name: tfe.String("tst-" + randomString(t)),
|
|
|
|
Email: tfe.String(fmt.Sprintf("%s@tfe.local", randomString(t))),
|
|
|
|
CostEstimationEnabled: tfe.Bool(false),
|
2021-09-22 23:53:33 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-27 17:14:54 +02:00
|
|
|
_, err = tfeClient.Admin.Organizations.Update(ctx, org.Name, tfe.AdminOrganizationUpdateOptions{
|
|
|
|
AccessBetaTools: tfe.Bool(true),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-22 23:53:33 +02:00
|
|
|
return org, func() {
|
|
|
|
if err := tfeClient.Organizations.Delete(ctx, org.Name); err != nil {
|
|
|
|
t.Errorf("Error destroying organization! WARNING: Dangling resources\n"+
|
|
|
|
"may exist! The full error is shown below.\n\n"+
|
|
|
|
"Organization: %s\nError: %s", org.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 14:47:12 +02:00
|
|
|
func createWorkspace(t *testing.T, orgName string, wOpts tfe.WorkspaceCreateOptions) *tfe.Workspace {
|
2021-09-22 23:53:33 +02:00
|
|
|
ctx := context.Background()
|
2021-10-09 14:47:12 +02:00
|
|
|
w, err := tfeClient.Workspaces.Create(ctx, orgName, wOpts)
|
2021-09-22 23:53:33 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2021-10-09 14:47:12 +02:00
|
|
|
func getWorkspace(workspaces []*tfe.Workspace, workspace string) (*tfe.Workspace, bool) {
|
|
|
|
for _, ws := range workspaces {
|
|
|
|
if ws.Name == workspace {
|
|
|
|
return ws, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, true
|
|
|
|
}
|
|
|
|
|
2021-09-22 23:53:33 +02:00
|
|
|
func randomString(t *testing.T) string {
|
|
|
|
v, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
2021-09-22 23:53:33 +02:00
|
|
|
|
|
|
|
func terraformConfigLocalBackend() string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
terraform {
|
|
|
|
backend "local" {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output "val" {
|
|
|
|
value = "${terraform.workspace}"
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2021-10-09 14:47:12 +02:00
|
|
|
func terraformConfigRemoteBackendName(org, name string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
terraform {
|
|
|
|
backend "remote" {
|
|
|
|
hostname = "%s"
|
|
|
|
organization = "%s"
|
|
|
|
|
|
|
|
workspaces {
|
|
|
|
name = "%s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output "val" {
|
|
|
|
value = "${terraform.workspace}"
|
|
|
|
}
|
|
|
|
`, tfeHostname, org, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func terraformConfigRemoteBackendPrefix(org, prefix string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
terraform {
|
|
|
|
backend "remote" {
|
|
|
|
hostname = "%s"
|
|
|
|
organization = "%s"
|
|
|
|
|
|
|
|
workspaces {
|
|
|
|
prefix = "%s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output "val" {
|
|
|
|
value = "${terraform.workspace}"
|
|
|
|
}
|
|
|
|
`, tfeHostname, org, prefix)
|
|
|
|
}
|
|
|
|
|
2021-09-22 23:53:33 +02:00
|
|
|
func terraformConfigCloudBackendTags(org, tag string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
terraform {
|
|
|
|
cloud {
|
|
|
|
hostname = "%s"
|
|
|
|
organization = "%s"
|
|
|
|
|
|
|
|
workspaces {
|
|
|
|
tags = ["%s"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 23:44:44 +02:00
|
|
|
output "tag_val" {
|
|
|
|
value = "%s"
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
2021-10-11 23:44:44 +02:00
|
|
|
`, tfeHostname, org, tag, tag)
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func terraformConfigCloudBackendName(org, name string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
terraform {
|
|
|
|
cloud {
|
|
|
|
hostname = "%s"
|
|
|
|
organization = "%s"
|
|
|
|
|
|
|
|
workspaces {
|
|
|
|
name = "%s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 23:44:44 +02:00
|
|
|
output "val" {
|
|
|
|
value = "${terraform.workspace}"
|
2021-09-22 23:53:33 +02:00
|
|
|
}
|
|
|
|
`, tfeHostname, org, name)
|
|
|
|
}
|
2021-10-12 00:14:44 +02:00
|
|
|
|
|
|
|
func writeMainTF(t *testing.T, block string, dir string) {
|
|
|
|
f, err := os.Create(fmt.Sprintf("%s/main.tf", dir))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = f.WriteString(block)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
2021-10-11 23:44:44 +02:00
|
|
|
|
|
|
|
// Ensure that TFC/E has a particular terraform version.
|
2021-10-28 01:07:12 +02:00
|
|
|
func skipWithoutRemoteTerraformVersion(t *testing.T) {
|
|
|
|
version := tfversion.String()
|
2021-10-11 23:44:44 +02:00
|
|
|
opts := tfe.AdminTerraformVersionsListOptions{
|
|
|
|
ListOptions: tfe.ListOptions{
|
|
|
|
PageNumber: 1,
|
|
|
|
PageSize: 100,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
hasVersion := false
|
|
|
|
|
|
|
|
findTfVersion:
|
|
|
|
for {
|
|
|
|
// TODO: update go-tfe Read() to retrieve a terraform version by name.
|
|
|
|
// Currently you can only retrieve by ID.
|
|
|
|
tfVersionList, err := tfeClient.Admin.TerraformVersions.List(context.Background(), opts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not retrieve list of terraform versions: %v", err)
|
|
|
|
}
|
|
|
|
for _, item := range tfVersionList.Items {
|
|
|
|
if item.Version == version {
|
|
|
|
hasVersion = true
|
|
|
|
break findTfVersion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit the loop when we've seen all pages.
|
|
|
|
if tfVersionList.CurrentPage >= tfVersionList.TotalPages {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the page number to get the next page.
|
|
|
|
opts.PageNumber = tfVersionList.NextPage
|
|
|
|
}
|
|
|
|
|
2021-10-28 01:07:12 +02:00
|
|
|
if !hasVersion {
|
|
|
|
t.Skip(fmt.Sprintf("Skipping test because TFC/E does not have current Terraform version to test with (%s)", version))
|
|
|
|
}
|
2021-10-11 23:44:44 +02:00
|
|
|
}
|