func check(t *testing.T, b *bytes.Buffer, expected []event) { actual := &traceFile{} ut.AssertEqual(t, nil, json.Unmarshal(b.Bytes(), actual)) // Zap out .Timestamp since it is not deterministic. Convert Duration to // binary value, either 0 or 1 since it's value is either set or not set. for i := range actual.Events { ut.AssertEqual(t, true, actual.Events[i].Timestamp >= 0) actual.Events[i].Timestamp = 0 if actual.Events[i].Duration != 0 { actual.Events[i].Duration = 1 } } for i := range expected { if expected[i].Pid == 0 { expected[i].Pid = 1 } if expected[i].Tid == 0 { expected[i].Tid = 1 } } wd, _ := os.Getwd() e := &traceFile{traceContext{os.Args, wd}, expected} ut.AssertEqual(t, e.Context, actual.Context) ut.AssertEqual(t, e.Events, actual.Events) ut.AssertEqual(t, e, actual) }
func TestParseDumpAsm(t *testing.T) { data := []string{ "panic: reflect.Set: value of type", "", "goroutine 16 [garbage collection]:", "runtime.switchtoM()", "\t" + goroot + "/src/runtime/asm_amd64.s:198 fp=0xc20cfb80d8 sp=0xc20cfb80d0", "", } extra := &bytes.Buffer{} goroutines, err := ParseDump(bytes.NewBufferString(strings.Join(data, "\n")), extra) ut.AssertEqual(t, nil, err) expected := []Goroutine{ { Signature: Signature{ State: "garbage collection", Stack: []Call{ { SourcePath: goroot + "/src/runtime/asm_amd64.s", Line: 198, Func: Function{Raw: "runtime.switchtoM"}, }, }, }, ID: 16, First: true, }, } ut.AssertEqual(t, expected, goroutines) ut.AssertEqual(t, "panic: reflect.Set: value of type\n\n", extra.String()) }
func testNodesTableImpl(t testing.TB, cas CasTable, nodes NodesTable) { items, err := EnumerateNodesAsList(nodes) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, []string{}, items) tree1 := map[string]string{ "file1": "content1", "dir1/dir2/file2": "content2", } archiveData(t, cas, nodes, tree1) items, err = EnumerateNodesAsList(nodes) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, 2, len(items)) name := strings.Replace(items[0], string(filepath.Separator), "/", -1) body := request(t, nodes, "/", 200, "") ut.AssertEqual(t, 2, strings.Count(body, "<a ")) request(t, nodes, "/foo", 404, "") request(t, nodes, "/foo/", 404, "") request(t, nodes, "/"+name, 301, "") request(t, nodes, "/"+name+"/", 200, "") request(t, nodes, "/"+name+"/file1", 200, "content1") request(t, nodes, "/"+name+"/dir1/dir2/file2", 200, "content2") request(t, nodes, "/"+name+"/dir1/dir2/file3", 404, "") request(t, nodes, "/"+name+"/dir1/dir2", 301, "") }
func TestDoRetryExceeded(t *testing.T) { c := &Config{1, 0, 0, 0} r := &retriable{errs: []error{errRetry}} ut.AssertEqual(t, errRetry, c.Do(r)) ut.AssertEqual(t, 1, r.closed) ut.AssertEqual(t, 1, r.tries) }
func TestDoRetry(t *testing.T) { c := &Config{2, 0, time.Millisecond, 0} r := &retriable{errs: []error{errRetry}} ut.AssertEqual(t, nil, c.Do(r)) ut.AssertEqual(t, 1, r.closed) ut.AssertEqual(t, 2, r.tries) }
func TestDoOnce(t *testing.T) { c := &Config{1, 0, 0, 0} r := &retriable{} ut.AssertEqual(t, nil, c.Do(r)) ut.AssertEqual(t, 1, r.closed) ut.AssertEqual(t, 1, r.tries) }
func TestParseDumpUnavail(t *testing.T) { data := []string{ "panic: reflect.Set: value of type", "", "goroutine 24 [running]:", "\tgoroutine running on other thread; stack unavailable", "created by github.com/foo.New", "\t/gopath/src/github.com/foo/bar.go:131 +0x381", "", } extra := &bytes.Buffer{} goroutines, err := ParseDump(bytes.NewBufferString(strings.Join(data, "\n")), extra) ut.AssertEqual(t, nil, err) expected := []Goroutine{ { Signature: Signature{ State: "running", Stack: []Call{ { SourcePath: "<unavailable>", }, }, CreatedBy: Call{ SourcePath: "/gopath/src/github.com/foo/bar.go", Line: 131, Func: Function{Raw: "github.com/foo.New"}, }, }, ID: 24, First: true, }, } ut.AssertEqual(t, expected, goroutines) ut.AssertEqual(t, "panic: reflect.Set: value of type\n\n", extra.String()) }
func TestChangIgnore(t *testing.T) { t.Parallel() c := newChange(&dummyRepo{t, "<root>"}, nil, nil, IgnorePatterns{"*.pb.go"}) ut.AssertEqual(t, false, c.IsIgnored("foo.go")) ut.AssertEqual(t, true, c.IsIgnored("foo.pb.go")) ut.AssertEqual(t, true, c.IsIgnored("bar/foo.pb.go")) }
func testCacheImpl(t testing.TB, load func() (Cache, error)) { now := time.Now().UTC().Unix() { c, err := load() ut.AssertEqual(t, nil, err) if c.Root().CountMembers() != 1 { c.Root().Print(os.Stderr, "") t.Fatalf("Oops: %d", c.Root().CountMembers()) } if c.Root().Files != nil { c.Root().Print(os.Stderr, "") t.Fatalf("Oops: %d", c.Root().CountMembers()) } i := FindInCache(c, filepath.Join("foo", "bar")) i.Sha1 = "x" i.Size = 1 i.Timestamp = 2 i.LastTested = now c.Close() } { c, err := load() ut.AssertEqual(t, nil, err) b := &bytes.Buffer{} c.Root().Print(b, "") ut.AssertEqual(t, "- 'foo'\n - 'bar'\n Sha1: x\n Size: 1\n", b.String()) foo := c.Root().Files["foo"] bar := foo.Files["bar"] if bar.Sha1 != "x" || bar.Size != 1 || bar.Timestamp != 2 || bar.LastTested != now { t.Fatalf("Oops: %d", c.Root().CountMembers()) } c.Close() } }
func TestParseDumpNoOffset(t *testing.T) { data := []string{ "panic: runtime error: index out of range", "", "goroutine 37 [runnable]:", "github.com/foo.func·002()", " /gopath/src/github.com/foo/bar.go:110", "created by github.com/foo.New", " /gopath/src/github.com/foo/bar.go:113 +0x43b", } goroutines, err := ParseDump(bytes.NewBufferString(strings.Join(data, "\n")), &bytes.Buffer{}) ut.AssertEqual(t, nil, err) expectedGR := []Goroutine{ { Signature: Signature{ State: "runnable", Stack: []Call{ { SourcePath: "/gopath/src/github.com/foo/bar.go", Line: 110, Func: Function{"github.com/foo.func·002"}, }, }, CreatedBy: Call{ SourcePath: "/gopath/src/github.com/foo/bar.go", Line: 113, Func: Function{"github.com/foo.New"}, }, }, ID: 37, First: true, }, } ut.AssertEqual(t, expectedGR, goroutines) }
func TestInstalledApp(t *testing.T) { i := &InstalledApp{ ClientID: "C", ClientSecret: "S", AuthURL: "http://localhost/auth", TokenURL: "http://localhost/token", ScopedTokenCache: make(map[string]*TokenCache), } tokReply := `{"access_token":"a", "refresh_token": "r", "id_token": "i"}` resp := []*http.Response{ {StatusCode: 200, Body: asReader(tokReply)}, } r := &roundTripperStub{[]*http.Request{}, resp} prompt := func(string) string { return "auth" } _, err := i.GetClientPrompt("scope", r, prompt) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, 1, len(r.requests)) i.Lock() defer i.Unlock() ut.AssertEqual(t, true, i.ShouldSave()) i.ClearDirtyBit() ut.AssertEqual(t, false, i.ShouldSave()) }
func TestRound(t *testing.T) { t.Parallel() ut.AssertEqual(t, 1500*time.Millisecond, round(1549*time.Millisecond, 100*time.Millisecond)) ut.AssertEqual(t, 1600*time.Millisecond, round(1550*time.Millisecond, 100*time.Millisecond)) ut.AssertEqual(t, -1500*time.Millisecond, round(-1549*time.Millisecond, 100*time.Millisecond)) ut.AssertEqual(t, -1600*time.Millisecond, round(-1550*time.Millisecond, 100*time.Millisecond)) }
func TestCartesianProductOfValues(t *testing.T) { t.Parallel() set := func(vs ...string) map[variableValueKey]variableValue { out := map[variableValueKey]variableValue{} for _, v := range makeVVs(vs...) { out[v.key()] = v } return out } test := func(vvs variablesValuesSet, keys []string, expected ...[]variableValue) { res, err := vvs.cartesianProductOfValues(keys) ut.AssertEqual(t, nil, err) vvSort(expected) vvSort(res) ut.AssertEqual(t, vvToStr2D(expected), vvToStr2D(res)) } keys := func(vs ...string) []string { return vs } vvs := variablesValuesSet{} test(vvs, keys()) vvs["OS"] = set("win", "unbound") test(vvs, keys("OS"), makeVVs("win"), makeVVs("unbound")) vvs["bit"] = set("32") test(vvs, keys("OS"), makeVVs("win"), makeVVs("unbound")) // bit var name must be ignored. test(vvs, keys("bit", "OS"), makeVVs("32", "win"), makeVVs("32", "unbound")) }
func TestRestore(t *testing.T) { t.Parallel() f := makeDumbcasAppMock(t) // Force the creation of CAS and NodesTable so content can be archived in // memory before running the command. _, _ = f.MakeCasTable("") _, _ = f.LoadNodesTable("", f.cas) // Create an archive. tree := map[string]string{ "dir1/bar": "bar\n", "dir1/dir2/dir3/foo": "foo\n", "dir1/dir2/file2": "content2", "file1": "content1", "x": "x\n", } _, nodeName, _ := archiveData(f.TB, f.cas, f.nodes, tree) tempData := makeTempDir(t, "restore") defer removeDir(t, tempData) args := []string{"restore", "-root=\\test_archive", "-out=" + tempData, nodeName} f.Run(args, 0) f.CheckBuffer(true, false) actualTree, err := readTree(tempData) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, tree, actualTree) }
func TestConfigYAMLBadMode(t *testing.T) { data, err := yaml.Marshal("foo") ut.AssertEqual(t, nil, err) v := PreCommit ut.AssertEqual(t, errors.New("invalid mode \"foo\""), yaml.Unmarshal(data, &v)) ut.AssertEqual(t, PreCommit, v) }
func TestCancelable(t *testing.T) { t.Parallel() c := newCanceler() ut.AssertEqual(t, nil, c.CancelationReason()) select { case <-c.Channel(): t.FailNow() default: } wg := sync.WaitGroup{} wg.Add(1) go func() { defer wg.Done() select { case err, isCanceled := <-c.Channel(): ut.ExpectEqualf(t, true, isCanceled, "Closed, but shouldn't be.") ut.ExpectEqual(t, ErrCanceled, err) } }() c.Cancel(nil) ut.AssertEqual(t, ErrCanceled, c.CancelationReason()) t.Log("waiting for goroutine above to end.") wg.Wait() ut.AssertEqual(t, nil, c.Close()) assertClosed(t, c) }
func setup(t *testing.T, tmpDir string) { _, code, err := internal.Capture(tmpDir, nil, "git", "init") ut.AssertEqual(t, 0, code) ut.AssertEqual(t, nil, err) run(t, tmpDir, nil, "config", "user.email", "nobody@localhost") run(t, tmpDir, nil, "config", "user.name", "nobody") }
func TestFunctionAnonymous(t *testing.T) { f := Function{"main.func·001"} ut.AssertEqual(t, "main.func·001", f.String()) ut.AssertEqual(t, "main.func·001", f.PkgDotName()) ut.AssertEqual(t, "func·001", f.Name()) ut.AssertEqual(t, "main", f.PkgName()) ut.AssertEqual(t, false, f.IsExported()) }
func TestCaptureOne(t *testing.T) { t.Parallel() wd, err := os.Getwd() ut.AssertEqual(t, nil, err) _, code, err := Capture(wd, nil, "go") ut.AssertEqual(t, 2, code) ut.AssertEqual(t, nil, err) }
func TestFunctionGC(t *testing.T) { f := Function{"gc"} ut.AssertEqual(t, "gc", f.String()) ut.AssertEqual(t, "gc", f.PkgDotName()) ut.AssertEqual(t, "gc", f.Name()) ut.AssertEqual(t, "", f.PkgName()) ut.AssertEqual(t, false, f.IsExported()) }
func TestCacheNormal(t *testing.T) { // Just makes sure loading the real cache doesn't crash. t.Parallel() cache, err := LoadCache() ut.AssertEqual(t, nil, err) defer cache.Close() ut.AssertEqual(t, false, nil == cache.Root()) }
func TestConfigYAML(t *testing.T) { config := New("0.1") data, err := yaml.Marshal(config) ut.AssertEqual(t, nil, err) actual := &Config{} ut.AssertEqual(t, nil, yaml.Unmarshal(data, actual)) ut.AssertEqual(t, config, actual) }
func TestGcEmpty(t *testing.T) { t.Parallel() f := makeDumbcasAppMock(t) args := []string{"gc", "-root=\\test_gc_empty"} f.Run(args, 0) i, err := dumbcaslib.EnumerateCasAsList(f.cas) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, []string{}, i) }
func TestCancelableDoubleCloseAndCancel(t *testing.T) { t.Parallel() errReason := errors.New("reason") c := newCanceler() ut.AssertEqual(t, nil, c.Close()) ut.AssertEqual(t, nil, c.Close()) c.Cancel(errReason) ut.AssertEqual(t, errReason, c.CancelationReason()) }
func TestCaptureNormal(t *testing.T) { t.Parallel() wd, err := os.Getwd() ut.AssertEqual(t, nil, err) out, code, err := Capture(wd, []string{"FOO=BAR"}, "go", "version") ut.AssertEqual(t, true, strings.Contains(out, runtime.Version())) ut.AssertEqual(t, 0, code) ut.AssertEqual(t, nil, err) }
func TestCaptureEmpty(t *testing.T) { t.Parallel() wd, err := os.Getwd() ut.AssertEqual(t, nil, err) out, code, err := Capture(wd, nil) ut.AssertEqual(t, "", out) ut.AssertEqual(t, -1, code) ut.AssertEqual(t, errors.New("no command specified"), err) }
func TestCaptureMissing(t *testing.T) { t.Parallel() wd, err := os.Getwd() ut.AssertEqual(t, nil, err) out, code, err := Capture(wd, nil, "program_is_non_existent") ut.AssertEqual(t, "", out) ut.AssertEqual(t, -1, code) ut.AssertEqual(t, true, err != nil) }
func TestLoadIsolateAsConfig(t *testing.T) { t.Parallel() root := "/dir" if common.IsWindows() { root = "x:\\dir" } isolate, err := LoadIsolateAsConfig(root, []byte(sampleIsolateData)) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, []string{"OS", "bit"}, isolate.ConfigVariables) }
func TestIsolateServerBadURL(t *testing.T) { t.Parallel() if testing.Short() { t.SkipNow() } client := newIsolateServer("http://asdfad.nonexistent", "default-gzip", fastRetry) caps, err := client.ServerCapabilities() ut.AssertEqual(t, (*isolated.ServerCapabilities)(nil), caps) ut.AssertEqual(t, true, err != nil) }
func TestArchiverEmpty(t *testing.T) { t.Parallel() a := New(isolatedclient.New("https://localhost:1", "default-gzip"), nil) stats := a.Stats() ut.AssertEqual(t, 0, stats.TotalHits()) ut.AssertEqual(t, 0, stats.TotalMisses()) ut.AssertEqual(t, common.Size(0), stats.TotalBytesHits()) ut.AssertEqual(t, common.Size(0), stats.TotalBytesPushed()) ut.AssertEqual(t, nil, a.Close()) }