| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package isolate | 5 package isolate |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "errors" | 9 "errors" |
| 10 "fmt" | 10 "fmt" |
| 11 "io/ioutil" | 11 "io/ioutil" |
| 12 "os" | 12 "os" |
| 13 "path/filepath" | 13 "path/filepath" |
| 14 "sort" | 14 "sort" |
| 15 "strings" | 15 "strings" |
| 16 "testing" | 16 "testing" |
| 17 | 17 |
| 18 "github.com/luci/luci-go/client/internal/common" | |
| 19 "github.com/luci/luci-go/common/isolated" | 18 "github.com/luci/luci-go/common/isolated" |
| 20 | 19 |
| 21 . "github.com/smartystreets/goconvey/convey" | 20 . "github.com/smartystreets/goconvey/convey" |
| 22 ) | 21 ) |
| 23 | 22 |
| 24 func TestReadOnlyValue(t *testing.T) { | 23 func TestReadOnlyValue(t *testing.T) { |
| 25 t.Parallel() | 24 t.Parallel() |
| 26 Convey(`Isolate should properly support read-only values.`, t, func() { | 25 Convey(`Isolate should properly support read-only values.`, t, func() { |
| 27 So(NotSet.ToIsolated(), ShouldBeNil) | 26 So(NotSet.ToIsolated(), ShouldBeNil) |
| 28 So(ReadOnlyValue(100).ToIsolated(), ShouldBeNil) | 27 So(ReadOnlyValue(100).ToIsolated(), ShouldBeNil) |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 vars, ok = getSortedVarValues(p.varsValsSet, "bit") | 379 vars, ok = getSortedVarValues(p.varsValsSet, "bit") |
| 381 So(ok, ShouldBeTrue) | 380 So(ok, ShouldBeTrue) |
| 382 So(vvToStr(vars), ShouldResemble, vvToStr(makeVVs("32", "64"))) | 381 So(vvToStr(vars), ShouldResemble, vvToStr(makeVVs("32", "64"))) |
| 383 }) | 382 }) |
| 384 } | 383 } |
| 385 | 384 |
| 386 func TestLoadIsolateAsConfig(t *testing.T) { | 385 func TestLoadIsolateAsConfig(t *testing.T) { |
| 387 t.Parallel() | 386 t.Parallel() |
| 388 Convey(`Isolate should properly load a config from a isolate file.`, t,
func() { | 387 Convey(`Isolate should properly load a config from a isolate file.`, t,
func() { |
| 389 root := "/dir" | 388 root := "/dir" |
| 390 » » if common.IsWindows() { | 389 » » if IsWindows() { |
| 391 root = "x:\\dir" | 390 root = "x:\\dir" |
| 392 } | 391 } |
| 393 isolate, err := LoadIsolateAsConfig(root, []byte(sampleIsolateDa
ta)) | 392 isolate, err := LoadIsolateAsConfig(root, []byte(sampleIsolateDa
ta)) |
| 394 So(err, ShouldBeNil) | 393 So(err, ShouldBeNil) |
| 395 So(isolate.ConfigVariables, ShouldResemble, []string{"OS", "bit"
}) | 394 So(isolate.ConfigVariables, ShouldResemble, []string{"OS", "bit"
}) |
| 396 }) | 395 }) |
| 397 } | 396 } |
| 398 | 397 |
| 399 func TestLoadIsolateForConfigMissingVars(t *testing.T) { | 398 func TestLoadIsolateForConfigMissingVars(t *testing.T) { |
| 400 t.Parallel() | 399 t.Parallel() |
| 401 Convey(`Isolate should properly handle missing variables when loading a
config.`, t, func() { | 400 Convey(`Isolate should properly handle missing variables when loading a
config.`, t, func() { |
| 402 isoData := []byte(sampleIsolateData) | 401 isoData := []byte(sampleIsolateData) |
| 403 root := "/dir" | 402 root := "/dir" |
| 404 » » if common.IsWindows() { | 403 » » if IsWindows() { |
| 405 root = "x:\\dir" | 404 root = "x:\\dir" |
| 406 } | 405 } |
| 407 _, _, _, _, err := LoadIsolateForConfig(root, isoData, nil) | 406 _, _, _, _, err := LoadIsolateForConfig(root, isoData, nil) |
| 408 So(err, ShouldNotBeNil) | 407 So(err, ShouldNotBeNil) |
| 409 Convey(fmt.Sprintf("Verify error message: %s", err), func() { | 408 Convey(fmt.Sprintf("Verify error message: %s", err), func() { |
| 410 So(err.Error(), ShouldContainSubstring, "variables were
missing") | 409 So(err.Error(), ShouldContainSubstring, "variables were
missing") |
| 411 So(err.Error(), ShouldContainSubstring, "bit") | 410 So(err.Error(), ShouldContainSubstring, "bit") |
| 412 So(err.Error(), ShouldContainSubstring, "OS") | 411 So(err.Error(), ShouldContainSubstring, "OS") |
| 413 _, _, _, _, err = LoadIsolateForConfig(root, isoData, ma
p[string]string{"bit": "32"}) | 412 _, _, _, _, err = LoadIsolateForConfig(root, isoData, ma
p[string]string{"bit": "32"}) |
| 414 So(err, ShouldNotBeNil) | 413 So(err, ShouldNotBeNil) |
| 415 So(err.Error(), ShouldContainSubstring, "variables were
missing") | 414 So(err.Error(), ShouldContainSubstring, "variables were
missing") |
| 416 So(err.Error(), ShouldContainSubstring, "OS") | 415 So(err.Error(), ShouldContainSubstring, "OS") |
| 417 }) | 416 }) |
| 418 }) | 417 }) |
| 419 } | 418 } |
| 420 | 419 |
| 421 func TestLoadIsolateForConfig(t *testing.T) { | 420 func TestLoadIsolateForConfig(t *testing.T) { |
| 422 t.Parallel() | 421 t.Parallel() |
| 423 Convey(`Isolate should properly load and return config data from a isola
te file.`, t, func() { | 422 Convey(`Isolate should properly load and return config data from a isola
te file.`, t, func() { |
| 424 // Case linux64, matches first condition. | 423 // Case linux64, matches first condition. |
| 425 root := "/dir" | 424 root := "/dir" |
| 426 » » if common.IsWindows() { | 425 » » if IsWindows() { |
| 427 root = "x:\\dir" | 426 root = "x:\\dir" |
| 428 } | 427 } |
| 429 vars := map[string]string{"bit": "64", "OS": "linux"} | 428 vars := map[string]string{"bit": "64", "OS": "linux"} |
| 430 cmd, deps, ro, dir, err := LoadIsolateForConfig(root, []byte(sam
pleIsolateData), vars) | 429 cmd, deps, ro, dir, err := LoadIsolateForConfig(root, []byte(sam
pleIsolateData), vars) |
| 431 So(err, ShouldBeNil) | 430 So(err, ShouldBeNil) |
| 432 So(dir, ShouldResemble, root) | 431 So(dir, ShouldResemble, root) |
| 433 So(ro, ShouldResemble, NotSet) // first condition has no read_on
ly specified. | 432 So(ro, ShouldResemble, NotSet) // first condition has no read_on
ly specified. |
| 434 So(cmd, ShouldResemble, []string{"python", "64linuxOrWin"}) | 433 So(cmd, ShouldResemble, []string{"python", "64linuxOrWin"}) |
| 435 So(deps, ShouldResemble, []string{"64linuxOrWin", filepath.Join(
"<(PRODUCT_DIR)", "unittest<(EXECUTABLE_SUFFIX)")}) | 434 So(deps, ShouldResemble, []string{"64linuxOrWin", filepath.Join(
"<(PRODUCT_DIR)", "unittest<(EXECUTABLE_SUFFIX)")}) |
| 436 | 435 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 So(out.IsolateDir, ShouldResemble, right.IsolateDir) | 550 So(out.IsolateDir, ShouldResemble, right.IsolateDir) |
| 552 So(right.IsolateDir, ShouldResemble, absToOS("/var/lib")) | 551 So(right.IsolateDir, ShouldResemble, absToOS("/var/lib")) |
| 553 So(out.Files, ShouldResemble, []string{"../../le/f/t", "../../tm
p/bar/foo/", "../ri/g/ht", "bar/"}) | 552 So(out.Files, ShouldResemble, []string{"../../le/f/t", "../../tm
p/bar/foo/", "../ri/g/ht", "bar/"}) |
| 554 }) | 553 }) |
| 555 } | 554 } |
| 556 | 555 |
| 557 // Helper functions. | 556 // Helper functions. |
| 558 | 557 |
| 559 // absToOS converts a POSIX path to OS specific format. | 558 // absToOS converts a POSIX path to OS specific format. |
| 560 func absToOS(p string) string { | 559 func absToOS(p string) string { |
| 561 » if common.IsWindows() { | 560 » if IsWindows() { |
| 562 return "e:" + strings.Replace(p, "/", "\\", -1) | 561 return "e:" + strings.Replace(p, "/", "\\", -1) |
| 563 } | 562 } |
| 564 return p | 563 return p |
| 565 } | 564 } |
| 566 | 565 |
| 567 // makeVVs simplifies creating variableValue: | 566 // makeVVs simplifies creating variableValue: |
| 568 // "unbound" => unbound | 567 // "unbound" => unbound |
| 569 // "123" => int(123) | 568 // "123" => int(123) |
| 570 // "s123" => string("123") | 569 // "s123" => string("123") |
| 571 func makeVVs(ss ...string) []variableValue { | 570 func makeVVs(ss ...string) []variableValue { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 630 for key := range valueSet { | 629 for key := range valueSet { |
| 631 keys = append(keys, string(key)) | 630 keys = append(keys, string(key)) |
| 632 } | 631 } |
| 633 sort.Strings(keys) | 632 sort.Strings(keys) |
| 634 values := make([]variableValue, len(valueSet)) | 633 values := make([]variableValue, len(valueSet)) |
| 635 for i, key := range keys { | 634 for i, key := range keys { |
| 636 values[i] = valueSet[variableValueKey(key)] | 635 values[i] = valueSet[variableValueKey(key)] |
| 637 } | 636 } |
| 638 return values, true | 637 return values, true |
| 639 } | 638 } |
| OLD | NEW |