github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/internal/test/e2e/e2e_helper.go (about)

     1  package e2e
     2  
     3  import (
     4  	"math/rand"
     5  	"os"
     6  	"os/exec"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  const charset = "abcdefghijklmnopqrstuvwxyz"
    12  
    13  var seededRand *rand.Rand = rand.New(
    14  	rand.NewSource(time.Now().UnixNano()))
    15  
    16  func randomString(length int) string {
    17  	b := make([]byte, length)
    18  	for i := range b {
    19  		b[i] = charset[seededRand.Intn(len(charset))]
    20  	}
    21  	return string(b)
    22  }
    23  
    24  func setup(t *testing.T) string {
    25  	t.Log("SETUP: Checking for local cluster ...")
    26  	cmd := exec.Command("oc", []string{"whoami"}...)
    27  	_, err := cmd.CombinedOutput()
    28  	if err == nil {
    29  		t.Log("SETUP: Local cluster running ...")
    30  	} else if os.Getenv("LAUNCH_LOCAL_CLUSTER") == "yes" {
    31  		launchLocalCluster(t)
    32  	}
    33  	return makeTestProject(t)
    34  }
    35  
    36  func teardown(t *testing.T, project string) {
    37  	deleteTestProject(t, project)
    38  }
    39  
    40  func getTailorBinary() string {
    41  	dir, _ := os.Getwd()
    42  	return dir + "/../tailor-test"
    43  }
    44  
    45  func launchLocalCluster(t *testing.T) {
    46  	t.Log("SETUP: Launching local cluster ...")
    47  	cmd := exec.Command("oc", []string{"cluster", "up"}...)
    48  	out, err := cmd.CombinedOutput()
    49  	if err != nil {
    50  		t.Fatalf("SETUP: Could not launch local cluster: %s", out)
    51  	}
    52  	t.Log("SETUP: Local cluster launched")
    53  }
    54  
    55  func deleteTestProject(t *testing.T, project string) {
    56  	cmd := exec.Command("oc", []string{"delete", "project", project}...)
    57  	out, err := cmd.CombinedOutput()
    58  	if err != nil {
    59  		t.Fatalf("TEARDOWN: Could not delete project %s: %s", project, out)
    60  	}
    61  	t.Log("TEARDOWN:", project, "project deleted")
    62  }
    63  
    64  func makeTestProject(t *testing.T) string {
    65  	project := "tailor-e2e-test-" + randomString(6)
    66  	cmd := exec.Command("oc", []string{"new-project", project}...)
    67  	out, err := cmd.CombinedOutput()
    68  	if err != nil {
    69  		t.Fatalf("SETUP: Could not create project %s: %s", project, out)
    70  	}
    71  	t.Log("SETUP: Project", project, "created")
    72  	return project
    73  }