github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/env_test.go (about) 1 // +build unit 2 3 package kube_test 4 5 import ( 6 "testing" 7 8 expect "github.com/Netflix/go-expect" 9 jenkinsio_v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1" 10 versiond_mocks "github.com/jenkins-x/jx-api/pkg/client/clientset/versioned/fake" 11 cmd_mocks "github.com/jenkins-x/jx/v2/pkg/cmd/clients/mocks" 12 "github.com/jenkins-x/jx/v2/pkg/config" 13 "github.com/jenkins-x/jx/v2/pkg/gits" 14 "github.com/jenkins-x/jx/v2/pkg/kube" 15 "github.com/jenkins-x/jx/v2/pkg/tests" 16 "github.com/jenkins-x/jx/v2/pkg/util" 17 v1 "k8s.io/api/core/v1" 18 19 git_mocks "github.com/jenkins-x/jx/v2/pkg/gits/mocks" 20 . "github.com/petergtz/pegomock" 21 "github.com/stretchr/testify/assert" 22 "gopkg.in/AlecAivazis/survey.v1/core" 23 k8sv1 "k8s.io/api/core/v1" 24 apiextentions_mocks "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 kube_mocks "k8s.io/client-go/kubernetes/fake" 27 ) 28 29 func init() { 30 // disable color output for all prompts to simplify testing 31 core.DisableColor = true 32 } 33 34 func TestSortEnvironments(t *testing.T) { 35 t.Parallel() 36 environments := []jenkinsio_v1.Environment{ 37 { 38 ObjectMeta: metav1.ObjectMeta{ 39 Name: "c", 40 }, 41 Spec: jenkinsio_v1.EnvironmentSpec{ 42 Order: 100, 43 }, 44 }, 45 { 46 ObjectMeta: metav1.ObjectMeta{ 47 Name: "z", 48 }, 49 Spec: jenkinsio_v1.EnvironmentSpec{ 50 Order: 5, 51 }, 52 }, 53 { 54 ObjectMeta: metav1.ObjectMeta{ 55 Name: "d", 56 }, 57 Spec: jenkinsio_v1.EnvironmentSpec{ 58 Order: 100, 59 }, 60 }, 61 { 62 ObjectMeta: metav1.ObjectMeta{ 63 Name: "a", 64 }, 65 Spec: jenkinsio_v1.EnvironmentSpec{ 66 Order: 150, 67 }, 68 }, 69 } 70 71 kube.SortEnvironments(environments) 72 73 assert.Equal(t, "z", environments[0].Name, "Environment 0") 74 assert.Equal(t, "c", environments[1].Name, "Environment 1") 75 assert.Equal(t, "d", environments[2].Name, "Environment 2") 76 assert.Equal(t, "a", environments[3].Name, "Environment 3") 77 } 78 79 func TestSortEnvironments2(t *testing.T) { 80 t.Parallel() 81 environments := []jenkinsio_v1.Environment{ 82 { 83 ObjectMeta: metav1.ObjectMeta{ 84 Name: "dev", 85 }, 86 Spec: jenkinsio_v1.EnvironmentSpec{ 87 Order: 0, 88 }, 89 }, 90 { 91 ObjectMeta: metav1.ObjectMeta{ 92 Name: "prod", 93 }, 94 Spec: jenkinsio_v1.EnvironmentSpec{ 95 Order: 200, 96 }, 97 }, 98 { 99 ObjectMeta: metav1.ObjectMeta{ 100 Name: "staging", 101 }, 102 Spec: jenkinsio_v1.EnvironmentSpec{ 103 Order: 100, 104 }, 105 }, 106 } 107 108 kube.SortEnvironments(environments) 109 110 assert.Equal(t, "dev", environments[0].Name, "Environment 0") 111 assert.Equal(t, "staging", environments[1].Name, "Environment 1") 112 assert.Equal(t, "prod", environments[2].Name, "Environment 2") 113 } 114 115 func TestReplaceMakeVariable(t *testing.T) { 116 t.Parallel() 117 lines := []string{"FOO", "NAMESPACE:=\"abc\"", "BAR", "NAMESPACE := \"abc\""} 118 119 actual := append([]string{}, lines...) 120 expectedValue := "\"changed\"" 121 kube.ReplaceMakeVariable(actual, "NAMESPACE", expectedValue) 122 123 assert.Equal(t, "FOO", actual[0], "line 0") 124 assert.Equal(t, "NAMESPACE := "+expectedValue, actual[1], "line 1") 125 assert.Equal(t, "BAR", actual[2], "line 2") 126 assert.Equal(t, "NAMESPACE := "+expectedValue, actual[3], "line 3") 127 } 128 129 func TestGetDevNamespace(t *testing.T) { 130 namespace := &k8sv1.Namespace{ 131 ObjectMeta: metav1.ObjectMeta{ 132 Name: "jx-testing", 133 Namespace: "jx-testing", 134 }, 135 } 136 kubernetesInterface := kube_mocks.NewSimpleClientset(namespace) 137 testNS := "jx-testing" 138 testEnv := "" 139 140 ns, env, err := kube.GetDevNamespace(kubernetesInterface, testNS) 141 142 assert.NoError(t, err, "Should not error") 143 assert.Equal(t, testNS, ns) 144 assert.Equal(t, testEnv, env) 145 } 146 147 func TestCreateEnvironmentSurvey(t *testing.T) { 148 tests.SkipForWindows(t, "go-expect does not work on Windows. ") 149 // namespace fixture 150 namespace := &v1.Namespace{ 151 ObjectMeta: metav1.ObjectMeta{ 152 Name: "jx-testing", 153 Namespace: "jx-testing", 154 }, 155 } 156 // mock factory 157 factory := cmd_mocks.NewMockFactory() 158 159 // mock Kubernetes interface 160 kubernetesInterface := kube_mocks.NewSimpleClientset(namespace) 161 // Override CreateKubeClient to return mock Kubernetes interface 162 When(factory.CreateKubeClient()).ThenReturn(kubernetesInterface, "jx-testing", nil) 163 164 // mock versiond interface 165 versiondInterface := versiond_mocks.NewSimpleClientset() 166 // Override CreateJXClient to return mock versiond interface 167 When(factory.CreateJXClient()).ThenReturn(versiondInterface, "jx-testing", nil) 168 169 // mock apiExtensions interface 170 apiextensionsInterface := apiextentions_mocks.NewSimpleClientset() 171 // Override CreateApiExtensionsClient to return mock apiextensions interface 172 When(factory.CreateApiExtensionsClient()).ThenReturn(apiextensionsInterface, nil) 173 174 console := tests.NewTerminal(t, nil) 175 defer console.Cleanup() 176 177 donec := make(chan struct{}) 178 go func() { 179 defer close(donec) 180 console.ExpectString("Name:") 181 console.SendLine("staging") 182 console.ExpectString("Label:") 183 console.SendLine("Staging") 184 console.ExpectString("Namespace:") 185 console.SendLine("jx-testing") 186 console.ExpectString("Environment in separate cluster to Dev Environment:") 187 console.SendLine("n") 188 console.ExpectString("Promotion Strategy:") 189 console.SendLine("A") 190 console.ExpectString("Order:") 191 console.SendLine("1") 192 console.ExpectString("We will now create a Git repository to store your staging environment, ok? :") 193 console.SendLine("N") 194 console.ExpectString("Git URL for the Environment source code:") 195 console.SendLine("https://github.com/derekzoolanderreallyreallygoodlooking/staging-env") 196 console.ExpectString("Git branch for the Environment source code:") 197 console.SendLine("master") 198 console.ExpectEOF() 199 }() 200 201 batchMode := false 202 authConfigSvc := tests.CreateAuthConfigService() 203 devEnv := jenkinsio_v1.Environment{} 204 data := jenkinsio_v1.Environment{} 205 conf := jenkinsio_v1.Environment{} 206 forkEnvGitURL := "" 207 ns := "jx-testing" 208 envDir := "" 209 gitRepoOptions := gits.GitRepositoryOptions{} 210 helmValues := config.HelmValuesConfig{ 211 ExposeController: &config.ExposeController{ 212 Config: config.ExposeControllerConfig{ 213 Domain: "good.looking.zoolander.com", 214 }, 215 }, 216 } 217 prefix := "" 218 gitter := git_mocks.NewMockGitter() 219 handles := util.IOFileHandles{ 220 Err: console.Err, 221 In: console.In, 222 Out: console.Out, 223 } 224 225 _, err := kube.CreateEnvironmentSurvey( 226 batchMode, 227 authConfigSvc, 228 &devEnv, 229 &data, 230 &conf, 231 true, 232 forkEnvGitURL, 233 ns, 234 versiondInterface, 235 kubernetesInterface, 236 envDir, 237 &gitRepoOptions, 238 helmValues, 239 prefix, 240 gitter, 241 nil, 242 handles, 243 ) 244 assert.NoError(t, err, "Should not error") 245 246 // Close the worker end of the pty, and read the remaining bytes from the master end. 247 // Dump the terminal's screen. 248 t.Log(expect.StripTrailingEmptyLines(console.CurrentState())) 249 console.Close() 250 <-donec 251 } 252 253 func TestGetPreviewEnvironmentReleaseName(t *testing.T) { 254 t.Parallel() 255 256 tests := []struct { 257 env *jenkinsio_v1.Environment 258 expectedReleaseName string 259 }{ 260 { 261 env: nil, 262 expectedReleaseName: "", 263 }, 264 { 265 env: &jenkinsio_v1.Environment{}, 266 expectedReleaseName: "", 267 }, 268 { 269 env: kube.NewPreviewEnvironment("test"), 270 expectedReleaseName: "", 271 }, 272 { 273 env: func() *jenkinsio_v1.Environment { 274 env := kube.NewPreviewEnvironment("test") 275 if env.Annotations == nil { 276 env.Annotations = map[string]string{} 277 } 278 env.Annotations[kube.AnnotationReleaseName] = "release-name" 279 return env 280 }(), 281 expectedReleaseName: "release-name", 282 }, 283 } 284 285 for i, test := range tests { 286 releaseName := kube.GetPreviewEnvironmentReleaseName(test.env) 287 if releaseName != test.expectedReleaseName { 288 t.Errorf("[%d] Expected release name %s but got %s", i, test.expectedReleaseName, releaseName) 289 } 290 } 291 }