github.com/kubeshop/testkube@v1.17.23/test/e2e/e2e_test.go (about)

     1  //go:build e2e
     2  
     3  package main
     4  
     5  import (
     6  	"flag"
     7  	"fmt"
     8  	"os"
     9  	"regexp"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/kubeshop/testkube/pkg/rand"
    16  	"github.com/kubeshop/testkube/test/e2e/testkube"
    17  )
    18  
    19  var namespace = "testkube"
    20  
    21  func init() {
    22  	if ns, ok := os.LookupEnv("NAMESPACE"); ok {
    23  		namespace = ns
    24  	}
    25  
    26  }
    27  
    28  var install = flag.Bool("install", false, "test")
    29  
    30  func TestMain(m *testing.M) {
    31  	flag.Parse()
    32  	os.Exit(m.Run())
    33  }
    34  func TestE2E(t *testing.T) {
    35  	a := require.New(t)
    36  	test := testkube.NewTestkube(namespace)
    37  	testName := fmt.Sprintf("test-%s", rand.Name())
    38  	collectionFile := "test.postman_collection.json"
    39  
    40  	t.Logf("Sctipt name: %s", testName)
    41  	t.Logf("Collection file name: %s", collectionFile)
    42  	t.Logf("Kubernetes namespace: %s", namespace)
    43  
    44  	t.Run("install test", func(t *testing.T) {
    45  		if !*install {
    46  			t.Skip("install flag not passed ignoring install test")
    47  		}
    48  		// given
    49  		test.Output = "json"
    50  
    51  		// uninstall first before installing
    52  		test.Uninstall()
    53  
    54  		// TODO change to watch
    55  		sleep(t, 10*time.Second)
    56  
    57  		// when
    58  		out, err := test.Install()
    59  
    60  		// then
    61  		a.NoError(err)
    62  		a.Contains(string(out), "STATUS: deployed")
    63  		a.Contains(string(out), "Visit http://127.0.0.1:8088 to use your application")
    64  
    65  		// TODO change to watch for changes
    66  		sleep(t, time.Minute)
    67  	})
    68  
    69  	t.Run("tests management", func(t *testing.T) {
    70  		// given
    71  		out, err := test.CreateTest(testName, collectionFile)
    72  		a.NoError(err)
    73  		a.Contains(string(out), "Test created")
    74  
    75  		// when
    76  		out, err = test.List()
    77  		a.NoError(err)
    78  
    79  		// then
    80  		a.Contains(string(out), testName)
    81  
    82  		sleep(t, 5*time.Second)
    83  	})
    84  
    85  	t.Run("tests run", func(t *testing.T) {
    86  		// given
    87  		executionName := rand.Name()
    88  
    89  		// when
    90  		out, err := test.StartTest(testName, executionName)
    91  		a.NoError(err)
    92  
    93  		// then check if info about collection steps exists somewhere in output
    94  		a.Contains(string(out), "Kasia.in Homepage")
    95  		a.Contains(string(out), "Google")
    96  
    97  		// then check if tests completed with success
    98  		a.Contains(string(out), "Test execution completed with success")
    99  
   100  		executionID := GetExecutionID(out)
   101  		t.Logf("Execution completed ID: %s", executionID)
   102  		a.NotEmpty(executionID)
   103  
   104  		out, err = test.Execution(testName, executionID)
   105  		// check tests results for postman collection
   106  		a.Contains(string(out), "Google")
   107  		a.Contains(string(out), "Successful GET request")
   108  		// check tests results for postman collection
   109  		a.Contains(string(out), "Kasia.in Homepage")
   110  		a.Contains(string(out), "Body matches string")
   111  	})
   112  
   113  	t.Run("delete test", func(t *testing.T) {
   114  		// given
   115  		out, err := test.DeleteTest(testName)
   116  		a.NoError(err)
   117  		a.Contains(string(out), "Succesfully deleted")
   118  
   119  		// when
   120  		out, err = test.List()
   121  		a.NoError(err)
   122  
   123  		// then
   124  		a.NotContains(string(out), testName)
   125  	})
   126  
   127  	sleep(t, time.Second)
   128  
   129  	// t.Run("cleaning helm release", func(t *testing.T) {
   130  	// 	out, err := test.Uninstall()
   131  	// 	a.NoError(err)
   132  	// 	a.Contains(string(out), "uninstalled")
   133  	// })
   134  
   135  }
   136  
   137  func sleep(t *testing.T, d time.Duration) {
   138  	t.Logf("Waiting for changes for %s (because I can't watch yet :P)", d)
   139  	time.Sleep(d)
   140  }
   141  
   142  func GetExecutionID(out []byte) string {
   143  	r := regexp.MustCompile("kubectl testkube get execution test ([0-9a-zA-Z]+)")
   144  	matches := r.FindStringSubmatch(string(out))
   145  	if len(matches) == 2 {
   146  		return matches[1]
   147  	}
   148  	return ""
   149  }