github.com/hazelops/ize@v1.1.12-0.20230915191306-97d7c0e48f11/tests/e2e/ecs_apps_test.go (about)

     1  //go:build e2e && ecs_apps
     2  // +build e2e,ecs_apps
     3  
     4  package test
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"io/fs"
    10  	"io/ioutil"
    11  	"math/rand"
    12  	"net/http"
    13  	"os"
    14  	"path/filepath"
    15  	"strings"
    16  	"testing"
    17  	"time"
    18  )
    19  
    20  var (
    21  	exampleGoblinSecret = ""
    22  	exampleGoblinApiKey = ""
    23  
    24  	exampleSquibbySecret = ""
    25  	exampleSquibbyApiKey = ""
    26  )
    27  
    28  func TestIzeSecretsPushGoblin(t *testing.T) {
    29  	if examplesRootDir == "" {
    30  		t.Fatalf("Missing required environment variable IZE_EXAMPLES_PATH")
    31  	}
    32  
    33  	rand.Seed(time.Now().UTC().UnixNano())
    34  
    35  	b := make([]byte, 12)
    36  
    37  	for i := 0; i < 12; i++ {
    38  		b[i] = byte(randInt(48, 127))
    39  	}
    40  	exampleGoblinSecret = string(b)
    41  
    42  	for i := 0; i < 12; i++ {
    43  		b[i] = byte(randInt(48, 127))
    44  	}
    45  	exampleGoblinApiKey = string(b)
    46  
    47  	data := map[string]interface{}{
    48  		"EXAMPLE_SECRET":  exampleGoblinSecret,
    49  		"EXAMPLE_API_KEY": exampleGoblinApiKey,
    50  	}
    51  
    52  	jsonString, _ := json.Marshal(data)
    53  
    54  	secretPath := filepath.Join(examplesRootDir, ".ize/env", os.Getenv("ENV"), "secrets/goblin.json")
    55  
    56  	err := ioutil.WriteFile(secretPath, jsonString, os.ModePerm)
    57  	if err != nil {
    58  		fmt.Println(err)
    59  	}
    60  
    61  	ize := NewBinary(t, izeBinary, examplesRootDir)
    62  
    63  	stdout, stderr, err := ize.RunRaw("secrets", "push", "--force", "goblin")
    64  
    65  	if err != nil {
    66  		t.Errorf("error: %s", err)
    67  	}
    68  
    69  	if stderr != "" {
    70  		t.Errorf("unexpected stderr output ize secret push: %s", err)
    71  	}
    72  
    73  	if !strings.Contains(stdout, "Pushing secrets complete!") {
    74  		t.Errorf("No success message detected after ize secret push:\n%s", stdout)
    75  	}
    76  
    77  	if os.Getenv("RUNNER_DEBUG") == "1" {
    78  		t.Log(stdout)
    79  	}
    80  }
    81  
    82  func TestIzeSecretsPushSquibby(t *testing.T) {
    83  	if examplesRootDir == "" {
    84  		t.Fatalf("Missing required environment variable IZE_EXAMPLES_PATH")
    85  	}
    86  
    87  	rand.Seed(time.Now().UTC().UnixNano())
    88  
    89  	b := make([]byte, 12)
    90  
    91  	for i := 0; i < 12; i++ {
    92  		b[i] = byte(randInt(48, 127))
    93  	}
    94  	exampleSquibbySecret = string(b)
    95  
    96  	for i := 0; i < 12; i++ {
    97  		b[i] = byte(randInt(48, 127))
    98  	}
    99  	exampleSquibbyApiKey = string(b)
   100  
   101  	data := map[string]interface{}{
   102  		"EXAMPLE_SECRET":  exampleSquibbySecret,
   103  		"EXAMPLE_API_KEY": exampleSquibbyApiKey,
   104  	}
   105  
   106  	secretPath := filepath.Join(examplesRootDir, ".ize/env", os.Getenv("ENV"), "secrets/squibby.json")
   107  
   108  	jsonString, _ := json.Marshal(data)
   109  	err := ioutil.WriteFile(secretPath, jsonString, os.ModePerm)
   110  	if err != nil {
   111  		fmt.Println(err)
   112  	}
   113  
   114  	ize := NewBinary(t, izeBinary, examplesRootDir)
   115  
   116  	stdout, stderr, err := ize.RunRaw("secrets", "push", "--force", "squibby")
   117  
   118  	if err != nil {
   119  		t.Errorf("error: %s", err)
   120  	}
   121  
   122  	if stderr != "" {
   123  		t.Errorf("unexpected stderr output ize secret push: %s", err)
   124  	}
   125  
   126  	if !strings.Contains(stdout, "Pushing secrets complete!") {
   127  		t.Errorf("No success message detected after ize secret push:\n%s", stdout)
   128  	}
   129  
   130  	if os.Getenv("RUNNER_DEBUG") == "1" {
   131  		t.Log(stdout)
   132  	}
   133  }
   134  
   135  func TestIzeUpInfra(t *testing.T) {
   136  	if examplesRootDir == "" {
   137  		t.Fatalf("Missing required environment variable IZE_EXAMPLES_PATH")
   138  	}
   139  
   140  	defer recovery(t)
   141  
   142  	foundIZEConfig := false
   143  	err := filepath.Walk(examplesRootDir, func(path string, info fs.FileInfo, err error) error {
   144  		if err != nil {
   145  			return err
   146  		}
   147  
   148  		if info.Name() == "ize.toml" {
   149  			foundIZEConfig = true
   150  		}
   151  
   152  		return nil
   153  	})
   154  	if err != nil {
   155  		t.Fatalf("Failed listing files in project template path %s: %s", examplesRootDir, err)
   156  	}
   157  
   158  	if !foundIZEConfig {
   159  		t.Fatalf("No ize.toml file in project template path %s", examplesRootDir)
   160  	}
   161  
   162  	ize := NewBinary(t, izeBinary, examplesRootDir)
   163  
   164  	stdout, stderr, err := ize.RunRaw("up", "infra")
   165  
   166  	if err != nil {
   167  		t.Errorf("error: %s", err)
   168  	}
   169  
   170  	if stderr != "" {
   171  		t.Errorf("unexpected stderr output ize up all: %s", err)
   172  	}
   173  
   174  	if !strings.Contains(stdout, "Deploy infra completed!") {
   175  		t.Errorf("No success message detected after ize up infra:\n%s", stdout)
   176  	}
   177  
   178  	if os.Getenv("RUNNER_DEBUG") == "1" {
   179  		t.Log(stdout)
   180  	}
   181  
   182  	time.Sleep(time.Minute)
   183  }
   184  
   185  func TestIzeUpGoblin(t *testing.T) {
   186  	if examplesRootDir == "" {
   187  		t.Fatalf("Missing required environment variable IZE_EXAMPLES_PATH")
   188  	}
   189  
   190  	defer recovery(t)
   191  
   192  	foundIZEConfig := false
   193  	err := filepath.Walk(examplesRootDir, func(path string, info fs.FileInfo, err error) error {
   194  		if err != nil {
   195  			return err
   196  		}
   197  
   198  		if info.Name() == "ize.toml" {
   199  			foundIZEConfig = true
   200  		}
   201  
   202  		return nil
   203  	})
   204  	if err != nil {
   205  		t.Fatalf("Failed listing files in project template path %s: %s", examplesRootDir, err)
   206  	}
   207  
   208  	if !foundIZEConfig {
   209  		t.Fatalf("No ize.toml file in project template path %s", examplesRootDir)
   210  	}
   211  
   212  	ize := NewBinary(t, izeBinary, examplesRootDir)
   213  
   214  	stdout, stderr, err := ize.RunRaw("up", "goblin")
   215  
   216  	if err != nil {
   217  		t.Errorf("error: %s", err)
   218  	}
   219  
   220  	if stderr != "" {
   221  		t.Errorf("unexpected stderr output ize up all: %s", err)
   222  	}
   223  
   224  	if !strings.Contains(stdout, "Deploy app goblin completed") {
   225  		t.Errorf("No success message detected after ize up squibby:\n%s", stdout)
   226  	}
   227  
   228  	if os.Getenv("RUNNER_DEBUG") == "1" {
   229  		t.Log(stdout)
   230  	}
   231  }
   232  
   233  func TestIzeUpSquibby(t *testing.T) {
   234  	if examplesRootDir == "" {
   235  		t.Fatalf("Missing required environment variable IZE_EXAMPLES_PATH")
   236  	}
   237  
   238  	defer recovery(t)
   239  
   240  	foundIZEConfig := false
   241  	err := filepath.Walk(examplesRootDir, func(path string, info fs.FileInfo, err error) error {
   242  		if err != nil {
   243  			return err
   244  		}
   245  
   246  		if info.Name() == "ize.toml" {
   247  			foundIZEConfig = true
   248  		}
   249  
   250  		return nil
   251  	})
   252  	if err != nil {
   253  		t.Fatalf("Failed listing files in project template path %s: %s", examplesRootDir, err)
   254  	}
   255  
   256  	if !foundIZEConfig {
   257  		t.Fatalf("No ize.toml file in project template path %s", examplesRootDir)
   258  	}
   259  
   260  	ize := NewBinary(t, izeBinary, examplesRootDir)
   261  
   262  	stdout, stderr, err := ize.RunRaw("up", "squibby")
   263  
   264  	if err != nil {
   265  		t.Errorf("error: %s", err)
   266  	}
   267  
   268  	if stderr != "" {
   269  		t.Errorf("unexpected stderr output ize up all: %s", err)
   270  	}
   271  
   272  	if !strings.Contains(stdout, "squibby") {
   273  		t.Errorf("No success message detected after ize up squibby:\n%s", stdout)
   274  	}
   275  
   276  	if os.Getenv("RUNNER_DEBUG") == "1" {
   277  		t.Log(stdout)
   278  	}
   279  }
   280  
   281  func TestCheckSecretsSquibby(t *testing.T) {
   282  	defer recovery(t)
   283  
   284  	url := fmt.Sprintf("http://squibby.%s.examples.ize.sh/", os.Getenv("ENV"))
   285  
   286  	for i := 0; i < 10; i++ {
   287  		resp, err := http.Get(url)
   288  		if err != nil {
   289  			t.Error(err)
   290  		}
   291  
   292  		body, err := ioutil.ReadAll(resp.Body)
   293  		if err != nil {
   294  			t.Error(err)
   295  			t.Log(string(body))
   296  
   297  		}
   298  
   299  		if os.Getenv("RUNNER_DEBUG") == "1" {
   300  			t.Logf("body: \n%s", string(body))
   301  			t.Logf("status: \n%s", string(resp.Status))
   302  		}
   303  
   304  		if strings.Contains(string(body), exampleSquibbySecret) {
   305  			return
   306  		}
   307  
   308  		time.Sleep(time.Second * 5)
   309  	}
   310  
   311  	t.Errorf("The expected string was not found in the response: %s", url)
   312  }
   313  
   314  func TestCheckSecretsGoblin(t *testing.T) {
   315  	defer recovery(t)
   316  
   317  	url := fmt.Sprintf("http://goblin.%s.examples.ize.sh/", os.Getenv("ENV"))
   318  
   319  	for i := 0; i < 10; i++ {
   320  		resp, err := http.Get(url)
   321  		if err != nil {
   322  			t.Error(err)
   323  		}
   324  
   325  		body, err := ioutil.ReadAll(resp.Body)
   326  		if err != nil {
   327  			t.Error(err)
   328  		}
   329  
   330  		if os.Getenv("RUNNER_DEBUG") == "1" {
   331  			t.Logf("body: \n%s", string(body))
   332  			t.Logf("status: \n%s", string(resp.Status))
   333  		}
   334  
   335  		if strings.Contains(string(body), exampleGoblinSecret) {
   336  			return
   337  		}
   338  
   339  		time.Sleep(time.Second * 5)
   340  	}
   341  
   342  	t.Errorf("The expected string was not found in the response: %s", url)
   343  }
   344  
   345  func TestIzeExecGoblin(t *testing.T) {
   346  	if examplesRootDir == "" {
   347  		t.Fatalf("Missing required environment variable IZE_EXAMPLES_PATH")
   348  	}
   349  
   350  	defer recovery(t)
   351  
   352  	ize := NewBinary(t, izeBinary, examplesRootDir)
   353  
   354  	stdout, stderr, err := ize.RunRaw("--plain-text-output", "exec", "goblin", "--", "sh -c \"echo $APP_NAME\"")
   355  
   356  	if err != nil {
   357  		t.Errorf("error: %s", err)
   358  	}
   359  
   360  	if stderr != "" {
   361  		t.Errorf("unexpected stderr output ize exec goblin: %s", err)
   362  	}
   363  
   364  	if !strings.Contains(stdout, "goblin") || strings.Contains(stdout, "EOF") {
   365  		t.Errorf("No success message detected after exec goblin:\n%s", stdout)
   366  	}
   367  
   368  	if os.Getenv("RUNNER_DEBUG") == "1" {
   369  		t.Log(stdout)
   370  	}
   371  }
   372  
   373  func TestIzeExecSquibby(t *testing.T) {
   374  	if examplesRootDir == "" {
   375  		t.Fatalf("Missing required environment variable IZE_EXAMPLES_PATH")
   376  	}
   377  
   378  	defer recovery(t)
   379  
   380  	ize := NewBinary(t, izeBinary, examplesRootDir)
   381  
   382  	stdout, stderr, err := ize.RunRaw("--plain-text-output", "exec", "squibby", "--", "sh -c \"echo $APP_NAME\"")
   383  
   384  	if err != nil {
   385  		t.Errorf("error: %s", err)
   386  	}
   387  
   388  	if stderr != "" {
   389  		t.Errorf("unexpected stderr output ize exec squibby: %s", err)
   390  	}
   391  
   392  	if !strings.Contains(stdout, "squibby") || strings.Contains(stdout, "EOF") {
   393  		t.Errorf("No success message detected after exec squibby:\n%s", stdout)
   394  	}
   395  
   396  	if os.Getenv("RUNNER_DEBUG") == "1" {
   397  		t.Log(stdout)
   398  	}
   399  }
   400  
   401  func TestIzeSecretsRmGoblin(t *testing.T) {
   402  	if examplesRootDir == "" {
   403  		t.Fatalf("Missing required environment variable IZE_EXAMPLES_PATH")
   404  	}
   405  
   406  	defer recovery(t)
   407  
   408  	rand.Seed(time.Now().UTC().UnixNano())
   409  
   410  	b := make([]byte, 12)
   411  
   412  	for i := 0; i < 12; i++ {
   413  		b[i] = byte(randInt(48, 127))
   414  	}
   415  	exampleGoblinSecret = string(b)
   416  
   417  	for i := 0; i < 12; i++ {
   418  		b[i] = byte(randInt(48, 127))
   419  	}
   420  	exampleGoblinApiKey = string(b)
   421  
   422  	data := map[string]interface{}{
   423  		"EXAMPLE_SECRET":  exampleGoblinSecret,
   424  		"EXAMPLE_API_KEY": exampleGoblinApiKey,
   425  	}
   426  
   427  	jsonString, _ := json.Marshal(data)
   428  
   429  	secretPath := filepath.Join(examplesRootDir, ".ize/env", os.Getenv("ENV"), "secrets/goblin.json")
   430  
   431  	err := ioutil.WriteFile(secretPath, jsonString, os.ModePerm)
   432  	if err != nil {
   433  		fmt.Println(err)
   434  	}
   435  
   436  	ize := NewBinary(t, izeBinary, examplesRootDir)
   437  
   438  	stdout, stderr, err := ize.RunRaw("secrets", "rm", "goblin")
   439  
   440  	if err != nil {
   441  		t.Errorf("error: %s", err)
   442  	}
   443  
   444  	if stderr != "" {
   445  		t.Errorf("unexpected stderr output ize secret push: %s", err)
   446  	}
   447  
   448  	if !strings.Contains(stdout, "Removing secrets complete!") {
   449  		t.Errorf("No success message detected after ize secret push:\n%s", stdout)
   450  	}
   451  
   452  	if os.Getenv("RUNNER_DEBUG") == "1" {
   453  		t.Log(stdout)
   454  	}
   455  }
   456  
   457  func TestIzeSecretsRmSquibby(t *testing.T) {
   458  	if examplesRootDir == "" {
   459  		t.Fatalf("Missing required environment variable IZE_EXAMPLES_PATH")
   460  	}
   461  
   462  	defer recovery(t)
   463  
   464  	rand.Seed(time.Now().UTC().UnixNano())
   465  
   466  	b := make([]byte, 12)
   467  
   468  	for i := 0; i < 12; i++ {
   469  		b[i] = byte(randInt(48, 127))
   470  	}
   471  	exampleSquibbySecret = string(b)
   472  
   473  	for i := 0; i < 12; i++ {
   474  		b[i] = byte(randInt(48, 127))
   475  	}
   476  	exampleSquibbyApiKey = string(b)
   477  
   478  	data := map[string]interface{}{
   479  		"EXAMPLE_SECRET":  exampleSquibbySecret,
   480  		"EXAMPLE_API_KEY": exampleSquibbyApiKey,
   481  	}
   482  
   483  	secretPath := filepath.Join(examplesRootDir, ".ize/env", os.Getenv("ENV"), "secrets/squibby.json")
   484  
   485  	jsonString, _ := json.Marshal(data)
   486  	err := ioutil.WriteFile(secretPath, jsonString, os.ModePerm)
   487  	if err != nil {
   488  		fmt.Println(err)
   489  	}
   490  
   491  	ize := NewBinary(t, izeBinary, examplesRootDir)
   492  
   493  	stdout, stderr, err := ize.RunRaw("secrets", "rm", "squibby")
   494  
   495  	if err != nil {
   496  		t.Errorf("error: %s", err)
   497  	}
   498  
   499  	if stderr != "" {
   500  		t.Errorf("unexpected stderr output ize secret push: %s", err)
   501  	}
   502  
   503  	if !strings.Contains(stdout, "Removing secrets complete!") {
   504  		t.Errorf("No success message detected after ize secret push:\n%s", stdout)
   505  	}
   506  
   507  	if os.Getenv("RUNNER_DEBUG") == "1" {
   508  		t.Log(stdout)
   509  	}
   510  }
   511  
   512  func randInt(min int, max int) int {
   513  	return min + rand.Intn(max-min)
   514  }
   515  
   516  func TestIzeDown(t *testing.T) {
   517  	if examplesRootDir == "" {
   518  		t.Fatalf("Missing required environment variable IZE_EXAMPLES_PATH")
   519  	}
   520  
   521  	defer recovery(t)
   522  
   523  	ize := NewBinary(t, izeBinary, examplesRootDir)
   524  
   525  	stdout, stderr, err := ize.RunRaw("down", "--auto-approve")
   526  
   527  	if err != nil {
   528  		t.Errorf("error: %s", err)
   529  	}
   530  
   531  	if stderr != "" {
   532  		t.Errorf("unexpected stderr output ize dowb all: %s", err)
   533  	}
   534  
   535  	if !strings.Contains(stdout, "Destroy all completed!") {
   536  		t.Errorf("No success message detected after down:\n%s", stdout)
   537  	}
   538  
   539  	if os.Getenv("RUNNER_DEBUG") == "1" {
   540  		t.Log(stdout)
   541  	}
   542  }