diff --git a/lib/awsorgs/organization.go b/lib/awsorgs/organization.go index ae75fc9..90c5b4c 100644 --- a/lib/awsorgs/organization.go +++ b/lib/awsorgs/organization.go @@ -188,7 +188,6 @@ func (c Client) CreateOrganizationUnit( mgmtAcct resource.Account, ouName, newParentId string, ) (*organizations.OrganizationalUnit, error) { - consoleUI.Print(fmt.Sprintf("Creating OU: Name=%s\n", ouName), mgmtAcct) out, err := c.organizationClient.CreateOrganizationalUnitWithContext(ctx, &organizations.CreateOrganizationalUnitInput{ Name: &ouName, @@ -253,7 +252,6 @@ func (c Client) CreateAccount( mgmtAcct resource.Account, acct *organizations.Account, ) (string, error) { - consoleUI.Print(fmt.Sprintf("Creating Account: Name=%s Email=%s\n", *acct.Name, *acct.Email), mgmtAcct) out, err := c.organizationClient.CreateAccount(&organizations.CreateAccountInput{ AccountName: acct.Name, @@ -298,7 +296,7 @@ func (c Client) CreateAccount( return "", fmt.Errorf("unexpected state: %s", state) } - time.Sleep(15 * time.Second) + time.Sleep(5 * time.Second) } } diff --git a/tests/end2end_test.go b/tests/end2end_test.go index b66a24a..0df6604 100644 --- a/tests/end2end_test.go +++ b/tests/end2end_test.go @@ -926,12 +926,12 @@ func TestEndToEnd(t *testing.T) { } }() + orgClient := awsorgs.New() for _, test := range tests { fmt.Printf("Running test: %s\n", test.Name) setupTest() ctx := context.Background() - orgClient := awsorgs.New() rootId, err := orgClient.GetRootId() assert.NoError(t, err, "Failed to fetch rootId") @@ -980,5 +980,6 @@ func TestEndToEnd(t *testing.T) { compareOrganizationUnits(t, test.FetchExpected, &fetchedOrg, true) test.ExpectedResources(t) + } } diff --git a/tests/main_test.go b/tests/main_test.go index f06a22b..d83e21e 100644 --- a/tests/main_test.go +++ b/tests/main_test.go @@ -1,11 +1,18 @@ package tests import ( + "archive/zip" "bytes" + "encoding/json" "fmt" + "io" + "net/http" "os" "os/exec" + "path/filepath" + "runtime" "sort" + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -23,6 +30,8 @@ func setup() error { if _, stderr, err := runCmd(cmd); err != nil { return fmt.Errorf("Failed to run setup: %v\n %s \n", err, stderr) } + + cacheProvider() return nil } @@ -40,6 +49,112 @@ func setupTest() { } } +func cacheProvider() { + version, err := getLatestProviderVersion("https://api.github.com/repos/hashicorp/terraform-provider-aws/releases/latest") + if err != nil { + fmt.Printf("Error fetching version: %v\n", err) + return + } + + if string(version[0]) == "v" { + version = version[1:] + } + + url := fmt.Sprintf("https://releases.hashicorp.com/terraform-provider-aws/%s/terraform-provider-aws_%s_%s_%s.zip", version, version, runtime.GOOS, runtime.GOARCH) + + destinationDir := filepath.Join(os.Getenv("HOME"), ".terraform.d", "plugins", "registry.terraform.io", "hashicorp", "aws", version, fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH)) + err = downloadAndUnzip(url, destinationDir) + if err != nil { + fmt.Printf("Error downloading and unzipping file: %v\n", err) + return + } + + fmt.Println("Provider installed successfully.") +} + +func getLatestProviderVersion(url string) (string, error) { + resp, err := http.Get(url) + if err != nil { + return "", err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("failed to fetch data: %s", resp.Status) + } + + var result struct { + TagName string `json:"tag_name"` + } + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { + return "", err + } + + return result.TagName, nil +} + +func downloadAndUnzip(url, destinationDir string) error { + tmpFile, err := os.CreateTemp("", "provider-*.zip") + if err != nil { + return err + } + defer os.Remove(tmpFile.Name()) + + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + if _, err := io.Copy(tmpFile, resp.Body); err != nil { + return err + } + + if err := tmpFile.Close(); err != nil { + return err + } + + r, err := zip.OpenReader(tmpFile.Name()) + if err != nil { + return err + } + defer r.Close() + + for _, f := range r.File { + rc, err := f.Open() + if err != nil { + return err + } + + path := filepath.Join(destinationDir, f.Name) + if f.FileInfo().IsDir() { + os.MkdirAll(path, f.Mode()) + } else { + var fdir string + if lastIndex := strings.LastIndex(path, string(os.PathSeparator)); lastIndex > -1 { + fdir = path[:lastIndex] + } + err = os.MkdirAll(fdir, os.ModePerm) + if err != nil { + return err + } + f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) + if err != nil { + return err + } + defer f.Close() + + _, err = io.Copy(f, rc) + if err != nil { + return err + } + } + rc.Close() + } + + return nil +} + func teardown() error { fmt.Println("Running teardown") cmd := exec.Command("bash", "teardown.sh") diff --git a/tests/teardown.sh b/tests/teardown.sh index eecb717..d0b41d2 100644 --- a/tests/teardown.sh +++ b/tests/teardown.sh @@ -3,4 +3,5 @@ set -eux localstack stop -rm organization.yml \ No newline at end of file +rm organization.yml +rm -rf telophasedirs/ \ No newline at end of file