github.com/GoogleContainerTools/kaniko@v1.23.0/pkg/buildcontext/git_test.go (about) 1 /* 2 Copyright 2020 Google LLC 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package buildcontext 18 19 import ( 20 "os" 21 "testing" 22 23 "github.com/GoogleContainerTools/kaniko/testutil" 24 "github.com/go-git/go-git/v5/plumbing/transport" 25 "github.com/go-git/go-git/v5/plumbing/transport/http" 26 ) 27 28 func TestGetGitPullMethod(t *testing.T) { 29 tests := []struct { 30 testName string 31 setEnv func() (expectedValue string) 32 }{ 33 { 34 testName: "noEnv", 35 setEnv: func() (expectedValue string) { 36 expectedValue = gitPullMethodHTTPS 37 return 38 }, 39 }, 40 { 41 testName: "emptyEnv", 42 setEnv: func() (expectedValue string) { 43 _ = os.Setenv(gitPullMethodEnvKey, "") 44 expectedValue = gitPullMethodHTTPS 45 return 46 }, 47 }, 48 { 49 testName: "httpEnv", 50 setEnv: func() (expectedValue string) { 51 err := os.Setenv(gitPullMethodEnvKey, gitPullMethodHTTP) 52 if nil != err { 53 expectedValue = gitPullMethodHTTPS 54 } else { 55 expectedValue = gitPullMethodHTTP 56 } 57 return 58 }, 59 }, 60 { 61 testName: "httpsEnv", 62 setEnv: func() (expectedValue string) { 63 _ = os.Setenv(gitPullMethodEnvKey, gitPullMethodHTTPS) 64 expectedValue = gitPullMethodHTTPS 65 return 66 }, 67 }, 68 { 69 testName: "unknownEnv", 70 setEnv: func() (expectedValue string) { 71 _ = os.Setenv(gitPullMethodEnvKey, "unknown") 72 expectedValue = gitPullMethodHTTPS 73 return 74 }, 75 }, 76 } 77 78 for _, tt := range tests { 79 t.Run(tt.testName, func(t *testing.T) { 80 expectedValue := tt.setEnv() 81 testutil.CheckDeepEqual(t, expectedValue, getGitPullMethod()) 82 }) 83 } 84 } 85 86 func TestGetGitAuth(t *testing.T) { 87 tests := []struct { 88 testName string 89 setEnv func() (expectedValue transport.AuthMethod) 90 }{ 91 { 92 testName: "noEnv", 93 setEnv: func() (expectedValue transport.AuthMethod) { 94 expectedValue = nil 95 return 96 }, 97 }, 98 { 99 testName: "emptyUsernameEnv", 100 setEnv: func() (expectedValue transport.AuthMethod) { 101 _ = os.Setenv(gitAuthUsernameEnvKey, "") 102 expectedValue = nil 103 return 104 }, 105 }, 106 { 107 testName: "emptyPasswordEnv", 108 setEnv: func() (expectedValue transport.AuthMethod) { 109 _ = os.Setenv(gitAuthPasswordEnvKey, "") 110 expectedValue = nil 111 return 112 }, 113 }, 114 { 115 testName: "emptyEnv", 116 setEnv: func() (expectedValue transport.AuthMethod) { 117 _ = os.Setenv(gitAuthUsernameEnvKey, "") 118 _ = os.Setenv(gitAuthPasswordEnvKey, "") 119 expectedValue = nil 120 return 121 }, 122 }, 123 { 124 testName: "withUsername", 125 setEnv: func() (expectedValue transport.AuthMethod) { 126 username := "foo" 127 _ = os.Setenv(gitAuthUsernameEnvKey, username) 128 expectedValue = &http.BasicAuth{Username: username} 129 return 130 }, 131 }, 132 { 133 testName: "withPassword", 134 setEnv: func() (expectedValue transport.AuthMethod) { 135 pass := "super-secret-password-1234" 136 _ = os.Setenv(gitAuthPasswordEnvKey, pass) 137 expectedValue = &http.BasicAuth{Password: pass} 138 return 139 }, 140 }, 141 { 142 testName: "withUsernamePassword", 143 setEnv: func() (expectedValue transport.AuthMethod) { 144 username := "foo" 145 pass := "super-secret-password-1234" 146 _ = os.Setenv(gitAuthUsernameEnvKey, username) 147 _ = os.Setenv(gitAuthPasswordEnvKey, pass) 148 expectedValue = &http.BasicAuth{Username: username, Password: pass} 149 return 150 }, 151 }, 152 { 153 testName: "withToken", 154 setEnv: func() (expectedValue transport.AuthMethod) { 155 token := "some-other-token" 156 _ = os.Setenv(gitAuthTokenEnvKey, token) 157 expectedValue = &http.BasicAuth{Username: token} 158 return 159 }, 160 }, 161 { 162 testName: "withTokenUsernamePassword", 163 setEnv: func() (expectedValue transport.AuthMethod) { 164 username := "foo-user" 165 token := "some-token-45678" 166 pass := "some-password-12345" 167 _ = os.Setenv(gitAuthUsernameEnvKey, username) 168 _ = os.Setenv(gitAuthPasswordEnvKey, pass) 169 _ = os.Setenv(gitAuthTokenEnvKey, token) 170 expectedValue = &http.BasicAuth{Username: token} 171 return 172 }, 173 }, 174 } 175 176 for _, tt := range tests { 177 t.Run(tt.testName, func(t *testing.T) { 178 // Make sure to unset environment vars to get a clean test each time 179 defer clearTestAuthEnv() 180 181 expectedValue := tt.setEnv() 182 testutil.CheckDeepEqual(t, expectedValue, getGitAuth()) 183 }) 184 } 185 186 } 187 188 func clearTestAuthEnv() { 189 _ = os.Unsetenv(gitAuthUsernameEnvKey) 190 _ = os.Unsetenv(gitAuthPasswordEnvKey) 191 }