github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/step/step_replicate_test.go (about) 1 // +build unit 2 3 package step 4 5 import ( 6 cmd_mocks "github.com/jenkins-x/jx/v2/pkg/cmd/clients/mocks" 7 "github.com/jenkins-x/jx/v2/pkg/cmd/opts" 8 "github.com/jenkins-x/jx/v2/pkg/cmd/opts/step" 9 "github.com/jenkins-x/jx/v2/pkg/tekton/tekton_helpers_test" 10 . "github.com/petergtz/pegomock" 11 "github.com/stretchr/testify/assert" 12 v1 "k8s.io/api/core/v1" 13 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 14 kube_mocks "k8s.io/client-go/kubernetes/fake" 15 16 "path" 17 "testing" 18 ) 19 20 func TestSetReplicatorAnnotationsNoExistingAnnotations(t *testing.T) { 21 22 existingAnnotations := make(map[string]string) 23 namespacesToReplicate := []string{"staging", "production"} 24 updatedAnnotations := setReplicatorAnnotations(existingAnnotations, namespacesToReplicate) 25 26 assert.Equal(t, updatedAnnotations[annotationReplicationAllowed], "true") 27 assert.Equal(t, updatedAnnotations[annotationEeplicationAllowedNamespaces], "staging,production") 28 } 29 30 func TestSetReplicatorAnnotationsWithExistingAnnotations(t *testing.T) { 31 32 existingAnnotations := make(map[string]string) 33 existingAnnotations[annotationEeplicationAllowedNamespaces] = "foo" 34 35 namespacesToReplicate := []string{"staging", "production"} 36 updatedAnnotations := setReplicatorAnnotations(existingAnnotations, namespacesToReplicate) 37 38 assert.Equal(t, updatedAnnotations[annotationReplicationAllowed], "true") 39 assert.Equal(t, updatedAnnotations[annotationEeplicationAllowedNamespaces], "foo,staging,production") 40 } 41 42 func TestSetReplicatorAnnotationsWithWildcard(t *testing.T) { 43 44 existingAnnotations := make(map[string]string) 45 46 namespacesToReplicate := []string{"staging", "production", "preview*"} 47 updatedAnnotations := setReplicatorAnnotations(existingAnnotations, namespacesToReplicate) 48 49 assert.Equal(t, updatedAnnotations[annotationReplicationAllowed], "true") 50 assert.Equal(t, updatedAnnotations[annotationEeplicationAllowedNamespaces], "staging,production,preview*") 51 } 52 53 func TestSetReplicatorAnnotationsNoDuplicates(t *testing.T) { 54 55 existingAnnotations := make(map[string]string) 56 existingAnnotations[annotationReplicationAllowed] = "true" 57 existingAnnotations[annotationEeplicationAllowedNamespaces] = "staging" 58 59 namespacesToReplicate := []string{"staging", "production", "preview*"} 60 updatedAnnotations := setReplicatorAnnotations(existingAnnotations, namespacesToReplicate) 61 62 assert.Equal(t, updatedAnnotations[annotationReplicationAllowed], "true") 63 assert.Equal(t, updatedAnnotations[annotationEeplicationAllowedNamespaces], "staging,production,preview*") 64 } 65 66 func TestRun(t *testing.T) { 67 o := ReplicateOptions{ 68 ReplicateToNamepace: []string{"bar"}, 69 StepOptions: step.StepOptions{ 70 CommonOptions: &opts.CommonOptions{}, 71 }, 72 } 73 74 currentNamespace := &v1.Namespace{ 75 ObjectMeta: metav1.ObjectMeta{ 76 Name: "foo", 77 Namespace: "foo", 78 }, 79 } 80 81 // the namespace we want to replicate secrets into, we have validation to make sure a namespace exists 82 stagingNamespace := &v1.Namespace{ 83 ObjectMeta: metav1.ObjectMeta{ 84 Name: "bar", 85 }, 86 } 87 88 // load a secret that we will annotate 89 testCaseDir := path.Join("test_data", "step_replicate") 90 secret := tekton_helpers_test.AssertLoadSecret(t, testCaseDir) 91 92 // setup mocks 93 factory := cmd_mocks.NewMockFactory() 94 kubernetesInterface := kube_mocks.NewSimpleClientset(currentNamespace) 95 96 // create resources used in the test 97 _, err := kubernetesInterface.CoreV1().Secrets("foo").Create(secret) 98 assert.NoError(t, err) 99 _, err = kubernetesInterface.CoreV1().Namespaces().Create(stagingNamespace) 100 assert.NoError(t, err) 101 102 // return our fake kubernetes client in the test 103 When(factory.CreateKubeClient()).ThenReturn(kubernetesInterface, "foo", nil) 104 105 // represents the command `jx step replicate secret tls-foo` 106 o.Args = []string{"secret", "tls-foo"} 107 o.SetFactory(factory) 108 109 // run the command 110 err = o.Run() 111 assert.NoError(t, err) 112 113 secret, err = kubernetesInterface.CoreV1().Secrets("foo").Get("tls-foo", metav1.GetOptions{}) 114 assert.NoError(t, err) 115 116 assert.Equal(t, secret.Annotations[annotationReplicationAllowed], "true") 117 assert.Equal(t, secret.Annotations[annotationEeplicationAllowedNamespaces], "bar") 118 } 119 120 func TestRunWithWildcard(t *testing.T) { 121 o := ReplicateOptions{ 122 ReplicateToNamepace: []string{"wine*"}, 123 StepOptions: step.StepOptions{ 124 CommonOptions: &opts.CommonOptions{}, 125 }, 126 } 127 128 currentNamespace := &v1.Namespace{ 129 ObjectMeta: metav1.ObjectMeta{ 130 Name: "foo", 131 Namespace: "foo", 132 }, 133 } 134 135 // load a secret that we will annotate 136 testCaseDir := path.Join("test_data", "step_replicate") 137 secret := tekton_helpers_test.AssertLoadSecret(t, testCaseDir) 138 139 // setup mocks 140 factory := cmd_mocks.NewMockFactory() 141 kubernetesInterface := kube_mocks.NewSimpleClientset(currentNamespace) 142 143 // create resources used in the test 144 _, err := kubernetesInterface.CoreV1().Secrets("foo").Create(secret) 145 assert.NoError(t, err) 146 147 // return our fake kubernetes client in the test 148 When(factory.CreateKubeClient()).ThenReturn(kubernetesInterface, "foo", nil) 149 150 // represents the command `jx step replicate secret tls-foo` 151 o.Args = []string{"secret", "tls-foo"} 152 o.SetFactory(factory) 153 154 // run the command 155 err = o.Run() 156 assert.NoError(t, err) 157 158 secret, err = kubernetesInterface.CoreV1().Secrets("foo").Get("tls-foo", metav1.GetOptions{}) 159 assert.NoError(t, err) 160 161 assert.Equal(t, secret.Annotations[annotationReplicationAllowed], "true") 162 assert.Equal(t, secret.Annotations[annotationEeplicationAllowedNamespaces], "wine*") 163 }