Skip to content

Commit

Permalink
Test Speedups: cache tf provider (#47)
Browse files Browse the repository at this point in the history
* save

* remove metrics and add speedups
  • Loading branch information
EthanBlackburn authored Apr 19, 2024
1 parent 8e50550 commit 83b7c0b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 deletions.
4 changes: 1 addition & 3 deletions lib/awsorgs/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -980,5 +980,6 @@ func TestEndToEnd(t *testing.T) {
compareOrganizationUnits(t, test.FetchExpected, &fetchedOrg, true)

test.ExpectedResources(t)

}
}
115 changes: 115 additions & 0 deletions tests/main_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}

Expand All @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion tests/teardown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
set -eux

localstack stop
rm organization.yml
rm organization.yml
rm -rf telophasedirs/

0 comments on commit 83b7c0b

Please sign in to comment.