github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/config/config_test.go (about) 1 package config 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "reflect" 8 "strings" 9 "testing" 10 11 "github.com/henvic/wedeploycli/defaults" 12 "github.com/henvic/wedeploycli/remotes" 13 "github.com/henvic/wedeploycli/tdata" 14 ) 15 16 func TestSetupNonExistingConfigFileAndTeardown(t *testing.T) { 17 wectx, err := Setup("./mocks/invalid/.lcp") 18 19 if err != nil { 20 panic(err) 21 } 22 23 conf := wectx.Config() 24 25 if err := wectx.SetEndpoint(defaults.CloudRemote); err != nil { 26 panic(err) 27 } 28 29 if conf == nil { 30 t.Error("Expected config to be mocked") 31 } 32 33 var ( 34 wantUsername = "" 35 wantToken = "" 36 wantRemote = "lcp" 37 wantInfrastructureDomain = "liferay.cloud" 38 ) 39 40 var params = conf.GetParams() 41 42 if len(params.Remotes.Keys()) != 1 { 43 t.Errorf("Expected to have one remote, got %v", params.Remotes) 44 } 45 46 if wectx.Username() != wantUsername { 47 t.Errorf("Wanted username to be %v, got %v instead", wantUsername, wectx.Username()) 48 } 49 50 if wectx.Token() != wantToken { 51 t.Errorf("Wanted token to be %v, got %v instead", wantToken, wectx.Token()) 52 } 53 54 if wectx.Remote() != wantRemote { 55 t.Errorf("Wanted remote to be %v, got %v instead", wantRemote, wectx.Remote()) 56 } 57 58 if wectx.InfrastructureDomain() != wantInfrastructureDomain { 59 t.Errorf("Wanted InfrastructureDomain to be %v, got %v instead", wantInfrastructureDomain, wectx.InfrastructureDomain()) 60 } 61 } 62 63 func TestSetupDefaultAndTeardown(t *testing.T) { 64 wectx, err := Setup("./mocks/home/.lcp") 65 66 if err != nil { 67 panic(err) 68 } 69 70 conf := wectx.Config() 71 params := conf.GetParams() 72 73 if err := wectx.SetEndpoint(defaults.CloudRemote); err != nil { 74 panic(err) 75 } 76 77 if conf == nil { 78 t.Error("Expected config to be mocked") 79 } 80 81 var ( 82 wantUsername = "foo@example.com" 83 wantToken = "mock_token" 84 wantRemote = "lcp" 85 wantInfrastructure = "liferay.cloud" 86 ) 87 88 if len(params.Remotes.Keys()) != 2 { 89 t.Errorf("Expected to have 2 remotes, got %v", params.Remotes) 90 } 91 92 if wectx.Username() != wantUsername { 93 t.Errorf("Wanted username to be %v, got %v instead", wantUsername, wectx.Username()) 94 } 95 96 if wectx.Token() != wantToken { 97 t.Errorf("Wanted token to be %v, got %v instead", wantToken, wectx.Token()) 98 } 99 100 if wectx.Remote() != wantRemote { 101 t.Errorf("Wanted remote to be %v, got %v instead", wantRemote, wectx.Remote()) 102 } 103 104 if wectx.InfrastructureDomain() != wantInfrastructure { 105 t.Errorf("Wanted remoteAddress to be %v, got %v instead", wantInfrastructure, wectx.InfrastructureDomain()) 106 } 107 } 108 109 func TestSetupRemoteAndTeardown(t *testing.T) { 110 wectx, err := Setup("./mocks/home/.lcp") 111 112 if err != nil { 113 panic(err) 114 } 115 116 conf := wectx.Config() 117 params := conf.GetParams() 118 119 if err := wectx.SetEndpoint(defaults.CloudRemote); err != nil { 120 panic(err) 121 } 122 123 if conf == nil { 124 t.Error("Expected config to be mocked") 125 } 126 127 var ( 128 wantUsername = "foo@example.com" 129 wantToken = "mock_token" 130 wantRemote = "lcp" 131 wantInfrastructure = "liferay.cloud" 132 ) 133 134 if len(params.Remotes.Keys()) != 2 { 135 t.Errorf("Expected to have 2 remotes, got %v", params.Remotes) 136 } 137 138 if wectx.Username() != wantUsername { 139 t.Errorf("Wanted username to be %v, got %v instead", wantUsername, wectx.Username()) 140 } 141 142 if wectx.Token() != wantToken { 143 t.Errorf("Wanted token to be %v, got %v instead", wantToken, wectx.Token()) 144 } 145 146 if wectx.Remote() != wantRemote { 147 t.Errorf("Wanted remote to be %v, got %v instead", wantRemote, wectx.Remote()) 148 } 149 150 if wectx.InfrastructureDomain() != wantInfrastructure { 151 t.Errorf("Wanted remoteAddress to be %v, got %v instead", wantInfrastructure, wectx.InfrastructureDomain()) 152 } 153 154 if params.NotifyUpdates { 155 t.Errorf("Wrong NotifyUpdate value") 156 } 157 158 if params.ReleaseChannel != "stable" { 159 t.Errorf("Wrong ReleaseChannel value") 160 } 161 } 162 163 func TestSetupAndTeardownProject(t *testing.T) { 164 var workingDir, _ = os.Getwd() 165 166 if err := os.Chdir(filepath.Join(workingDir, "mocks/project/non-service")); err != nil { 167 t.Error(err) 168 } 169 170 if _, err := Setup("../../home/.lcp"); err != nil { 171 panic(err) 172 } 173 174 if err := os.Chdir(workingDir); err != nil { 175 panic(err) 176 } 177 } 178 179 func TestSetupAndTeardownProjectAndService(t *testing.T) { 180 var workingDir, _ = os.Getwd() 181 182 if err := os.Chdir(filepath.Join(workingDir, "mocks/project/service/inside")); err != nil { 183 t.Error(err) 184 } 185 186 _, err := Setup("../../../home/.lcp") 187 188 if err != nil { 189 panic(err) 190 } 191 192 if err := os.Chdir(workingDir); err != nil { 193 panic(err) 194 } 195 } 196 197 func TestSave(t *testing.T) { 198 wectx, err := Setup("mocks/home/.lcp") 199 200 if err != nil { 201 panic(err) 202 } 203 204 conf := wectx.Config() 205 206 tmp, err := ioutil.TempFile(os.TempDir(), "lcp") 207 208 if err != nil { 209 panic(err) 210 } 211 212 // save in a different location 213 conf.Path = tmp.Name() 214 215 if err = conf.Save(); err != nil { 216 panic(err) 217 } 218 219 var got = tdata.FromFile(conf.Path) 220 var want = []string{ 221 `; Configuration file for Liferay Cloud 222 ; https://www.liferay.com/products/dxp-cloud`, 223 `default_remote = lcp`, 224 `local_http_port = 80`, 225 `local_https_port = 443`, 226 `disable_autocomplete_autoinstall = true`, 227 `disable_colors = false`, 228 `notify_updates = false`, 229 `release_channel = stable`, 230 `enable_analytics = false`, 231 `[remote "lcp"] 232 ; Default cloud remote 233 infrastructure = liferay.cloud 234 username = foo@example.com 235 token = mock_token 236 `, 237 `[remote "xyz"] 238 infrastructure = wedeploy.xyz 239 username = foobar@example.net 240 token = 123`, 241 } 242 243 for _, w := range want { 244 if !strings.Contains(got, w) { 245 t.Errorf("Expected string does not exists in generated configuration file: %v", w) 246 } 247 } 248 249 if err = tmp.Close(); err != nil { 250 panic(err) 251 } 252 253 if err = os.Remove(tmp.Name()); err != nil { 254 panic(err) 255 } 256 } 257 258 func TestRemotes(t *testing.T) { 259 wectx, err := Setup("./mocks/remotes/.lcp") 260 261 if err != nil { 262 panic(err) 263 } 264 265 conf := wectx.Config() 266 params := conf.GetParams() 267 268 tmp, err := ioutil.TempFile(os.TempDir(), "lcp") 269 270 if err != nil { 271 panic(err) 272 } 273 274 params.Remotes.Set("staging", remotes.Entry{ 275 Infrastructure: "https://staging.example.net/", 276 }) 277 278 params.Remotes.Set("beta", remotes.Entry{ 279 Infrastructure: "https://beta.example.com/", 280 Comment: "remote for beta testing", 281 }) 282 283 params.Remotes.Set("new", remotes.Entry{ 284 Infrastructure: "http://foo/", 285 }) 286 287 params.Remotes.Del("temporary") 288 289 params.Remotes.Set("remain", remotes.Entry{ 290 Comment: "commented vars remains even when empty", 291 }) 292 293 params.Remotes.Set("dontremain", remotes.Entry{}) 294 295 params.Remotes.Del("dontremain2") 296 297 // save in a different location 298 conf.Path = tmp.Name() 299 300 if err = conf.Save(); err != nil { 301 panic(err) 302 } 303 304 var got = tdata.FromFile(conf.Path) 305 var want = []string{` 306 [remote "alternative"] 307 infrastructure = http://example.net/ 308 `, 309 ` 310 [remote "staging"] 311 infrastructure = https://staging.example.net/ 312 `, 313 ` 314 ; remote for beta testing 315 [remote "beta"] 316 infrastructure = https://beta.example.com/ 317 `, 318 ` 319 ; commented vars remains even when empty 320 [remote "remain"] 321 `, 322 ` 323 [remote "lcp"] 324 ; Default cloud remote 325 infrastructure = liferay.cloud 326 `, 327 ` 328 [remote "new"] 329 infrastructure = http://foo/ 330 `} 331 332 for _, w := range want { 333 if !strings.Contains(got, w) { 334 t.Errorf("Expected string does not exists in generated configuration file: %v", w) 335 } 336 } 337 338 if err = tmp.Close(); err != nil { 339 panic(err) 340 } 341 342 if err = os.Remove(tmp.Name()); err != nil { 343 panic(err) 344 } 345 } 346 347 func TestRemotesListAndGet(t *testing.T) { 348 wectx, err := Setup("./mocks/remotes/.lcp") 349 350 if err != nil { 351 panic(err) 352 } 353 354 conf := wectx.Config() 355 params := conf.GetParams() 356 357 var wantOriginalRemotes = remotes.List{} 358 359 wantOriginalRemotes.Set("lcp", remotes.Entry{ 360 Infrastructure: "liferay.cloud", 361 InfrastructureComment: "Default cloud remote", 362 }) 363 364 wantOriginalRemotes.Set("alternative", remotes.Entry{ 365 Infrastructure: "http://example.net/", 366 }) 367 368 wantOriginalRemotes.Set("staging", remotes.Entry{ 369 Infrastructure: "http://staging.example.net/", 370 }) 371 372 wantOriginalRemotes.Set("beta", remotes.Entry{ 373 Infrastructure: "http://beta.example.com/", 374 InfrastructureComment: "; my beta comment", 375 }) 376 377 wantOriginalRemotes.Set("remain", remotes.Entry{ 378 Infrastructure: "http://localhost/", 379 Comment: "; commented vars remains even when empty", 380 }) 381 382 wantOriginalRemotes.Set("dontremain", remotes.Entry{ 383 Infrastructure: "http://localhost/", 384 Comment: "; commented vars remains even when empty", 385 }) 386 387 wantOriginalRemotes.Set("dontremain2", remotes.Entry{ 388 Infrastructure: "http://localhost/", 389 }) 390 391 worKeys := wantOriginalRemotes.Keys() 392 prKeys := params.Remotes.Keys() 393 394 if len(worKeys) != len(prKeys) { 395 t.Errorf("Number of remotes doesn't match: wanted %v, got %v instead", len(worKeys), len(prKeys)) 396 } 397 398 for _, k := range wantOriginalRemotes.Keys() { 399 want := wantOriginalRemotes.Get(k) 400 got := params.Remotes.Get(k) 401 402 if want != got { 403 t.Errorf("Expected remote doesn't match for %v: %+v instead of %+v", k, got, want) 404 } 405 } 406 407 var wantList = []string{ 408 "alternative", 409 "beta", 410 "dontremain", 411 "dontremain2", 412 "lcp", 413 "remain", 414 "staging", 415 } 416 417 var names = params.Remotes.Keys() 418 419 if !reflect.DeepEqual(names, wantList) { 420 t.Errorf("Wanted %v, got %v instead", wantList, names) 421 } 422 423 var wantRemain = remotes.Entry{ 424 Infrastructure: "http://localhost/", 425 Comment: "; commented vars remains even when empty", 426 } 427 428 gotRemain := params.Remotes.Get("remain") 429 430 if gotRemain != wantRemain { 431 t.Errorf("Wanted %v, got %v instead", wantRemain, gotRemain) 432 } 433 }