github.com/mponton/terratest@v0.44.0/modules/k8s/secret_test.go (about) 1 //go:build kubeall || kubernetes 2 // +build kubeall kubernetes 3 4 // NOTE: we have build tags to differentiate kubernetes tests from non-kubernetes tests. This is done because minikube 5 // is heavy and can interfere with docker related tests in terratest. Specifically, many of the tests start to fail with 6 // `connection refused` errors from `minikube`. To avoid overloading the system, we run the kubernetes tests and helm 7 // tests separately from the others. This may not be necessary if you have a sufficiently powerful machine. We 8 // recommend at least 4 cores and 16GB of RAM if you want to run all the tests together. 9 10 package k8s 11 12 import ( 13 "fmt" 14 "strings" 15 "testing" 16 "time" 17 18 "github.com/stretchr/testify/require" 19 20 "github.com/mponton/terratest/modules/random" 21 ) 22 23 func TestGetSecretEReturnsErrorForNonExistantSecret(t *testing.T) { 24 t.Parallel() 25 26 options := NewKubectlOptions("", "", "default") 27 _, err := GetSecretE(t, options, "master-password") 28 require.Error(t, err) 29 } 30 31 func TestGetSecretEReturnsCorrectSecretInCorrectNamespace(t *testing.T) { 32 t.Parallel() 33 34 uniqueID := strings.ToLower(random.UniqueId()) 35 options := NewKubectlOptions("", "", uniqueID) 36 configData := fmt.Sprintf(EXAMPLE_SECRET_YAML_TEMPLATE, uniqueID, uniqueID) 37 defer KubectlDeleteFromString(t, options, configData) 38 KubectlApplyFromString(t, options, configData) 39 40 secret := GetSecret(t, options, "master-password") 41 require.Equal(t, secret.Name, "master-password") 42 require.Equal(t, secret.Namespace, uniqueID) 43 } 44 45 func TestWaitUntilSecretAvailableReturnsSuccessfully(t *testing.T) { 46 t.Parallel() 47 48 uniqueID := strings.ToLower(random.UniqueId()) 49 options := NewKubectlOptions("", "", uniqueID) 50 configData := fmt.Sprintf(EXAMPLE_SECRET_YAML_TEMPLATE, uniqueID, uniqueID) 51 defer KubectlDeleteFromString(t, options, configData) 52 53 KubectlApplyFromString(t, options, configData) 54 WaitUntilSecretAvailable(t, options, "master-password", 10, 1*time.Second) 55 } 56 57 const EXAMPLE_SECRET_YAML_TEMPLATE = `--- 58 apiVersion: v1 59 kind: Namespace 60 metadata: 61 name: %s 62 --- 63 apiVersion: v1 64 kind: Secret 65 metadata: 66 name: master-password 67 namespace: %s 68 `