// archiveData archives a tree fictious data. // Returns (tree of sha1s, name of the node, sha1 of the node entry). // Accept the paths as posix. func archiveData(t testing.TB, cas CasTable, nodes NodesTable, tree map[string]string) (map[string]string, string, string) { sha1tree, entries := marshalData(t, tree) for k, v := range tree { err := cas.AddEntry(bytes.NewBuffer([]byte(v)), sha1tree[k]) ut.AssertEqualf(t, true, err == nil || err == os.ErrExist, "Unexpected error: %s", err) } entrySha1, err := AddBytes(cas, entries) ut.AssertEqual(t, nil, err) // And finally add the node. now := time.Now().UTC() nodeName, err := nodes.AddEntry(&Node{entrySha1, "useful comment"}, "fictious") ut.AssertEqual(t, nil, err) ut.AssertEqualf(t, true, strings.HasPrefix(nodeName, now.Format("2006-01")+string(filepath.Separator)), "Invalid node name %s", nodeName) return sha1tree, nodeName, entrySha1 }
func run(t *testing.T, tmpDir string, env []string, args ...string) string { internal := &git{root: tmpDir} out, code, err := internal.capture(env, args...) ut.AssertEqualf(t, 0, code, "%s", out) ut.AssertEqual(t, nil, err) return out }
// CheckBuffer asserts the content of os.Stdout and os.Stderr mocks. func (t *TB) CheckBuffer(out, err bool) { if out { // Print Stderr to see what happened. ut.AssertEqualf(t, true, t.bufOut.Len() != 0, "Expected stdout") } else { ut.AssertEqualf(t, t.bufOut.Len(), 0, "Unexpected stdout") } if err { ut.AssertEqualf(t, true, t.bufErr.Len() != 0, "Expected stderr") } else { ut.AssertEqualf(t, t.bufErr.Len(), 0, "Unexpected stderr") } t.bufOut.Reset() t.bufErr.Reset() }
func TestLoadIsolateForConfigMissingVars(t *testing.T) { t.Parallel() isoData := []byte(sampleIsolateData) root := "/dir" if common.IsWindows() { root = "x:\\dir" } _, _, _, _, err := LoadIsolateForConfig(root, isoData, nil) ut.AssertEqual(t, true, err != nil) ut.AssertEqualf(t, true, strings.Contains(err.Error(), "variables were missing"), "%s", err) ut.AssertEqualf(t, true, strings.Contains(err.Error(), "bit"), "%s", err) ut.AssertEqualf(t, true, strings.Contains(err.Error(), "OS"), "%s", err) _, _, _, _, err = LoadIsolateForConfig(root, isoData, map[string]string{"bit": "32"}) ut.AssertEqual(t, true, err != nil) ut.AssertEqualf(t, true, strings.Contains(err.Error(), "variables were missing"), "%s", err) ut.AssertEqualf(t, true, strings.Contains(err.Error(), "OS"), "%s", err) }
func request(t testing.TB, nodes NodesTable, path string, expectedCode int, expectedBody string) string { req, err := http.ReadRequest(bufio.NewReader(bytes.NewBufferString("GET " + path + " HTTP/1.1\r\nHost: test\r\n\r\n"))) ut.AssertEqual(t, nil, err) resp := httptest.NewRecorder() nodes.ServeHTTP(resp, req) bytes, err := ioutil.ReadAll(resp.Body) ut.AssertEqual(t, nil, err) body := string(bytes) ut.AssertEqual(t, expectedCode, resp.Code) ut.AssertEqualf(t, true, expectedBody == "" || body == expectedBody, "%s: %#s != %#s", path, expectedBody, body) return body }
func TestVariableValueOrder(t *testing.T) { t.Parallel() I := func(i int) variableValue { return variableValue{nil, &i} } S := func(s string) variableValue { return variableValue{&s, nil} } unbound := variableValue{} expectations := []struct { res int l variableValue r variableValue }{ {0, unbound, unbound}, {1, unbound, I(1)}, {1, unbound, S("s")}, {0, I(1), I(1)}, {1, I(1), I(2)}, {1, I(1), S("1")}, {0, S("s"), S("s")}, {1, S("a"), S("b")}, } for _, e := range expectations { ut.AssertEqualf(t, e.res, e.l.compare(e.r), "%d != (%v < %v)", e.res, e.l.String(), e.r.String()) ut.AssertEqualf(t, -e.res, e.r.compare(e.l), "%d != (%v > %v)", -e.res, e.l.String(), e.r.String()) } }
func testCasTableImpl(t testing.TB, cas CasTable) { items, err := EnumerateCasAsList(cas) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, []string{}, items) file1, err := AddBytes(cas, []byte("content1")) ut.AssertEqual(t, nil, err) items, err = EnumerateCasAsList(cas) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, []string{file1}, items) // Add the same content. file2, err := AddBytes(cas, []byte("content1")) ut.AssertEqualf(t, true, os.IsExist(err), "Unexpected error: %s", err) ut.AssertEqual(t, file1, file2) items, err = EnumerateCasAsList(cas) ut.AssertEqual(t, nil, err) ut.AssertEqual(t, []string{file1}, items) f, err := cas.Open(file1) ut.AssertEqual(t, nil, err) data, err := ioutil.ReadAll(f) f.Close() ut.AssertEqual(t, nil, err) ut.AssertEqual(t, "content1", string(data)) _, err = cas.Open("0") ut.AssertEqual(t, false, err == nil) err = cas.Remove(file1) ut.AssertEqual(t, nil, err) err = cas.Remove(file1) ut.AssertEqual(t, false, err == nil) // Test fsck bit. ut.AssertEqual(t, false, cas.GetFsckBit()) cas.SetFsckBit() ut.AssertEqual(t, true, cas.GetFsckBit()) cas.ClearFsckBit() ut.AssertEqual(t, false, cas.GetFsckBit()) }
func testGoroutinePoolCancel(t *testing.T, newPool newPoolFunc) { t.Parallel() cancelError := errors.New("cancelError") const MAX = 10 const J = 11 * MAX pool := newPool(MAX) logs := make(chan int, 2*J) // Avoid job blocking when writing to log. for i := 1; i <= J; i++ { i := i pool.Schedule(func() { if i == 1 { pool.Cancel(cancelError) } logs <- i }, func() { // Push negative on canceled tasks. logs <- -i }) } var wait1 error var wait2 error go func() { defer close(logs) wait1 = pool.Wait() // Schedule more jobs, all of which must be cancelled. for i := J + 1; i <= 2*J; i++ { i := i pool.Schedule(func() { logs <- i }, func() { logs <- -i }) } wait2 = pool.Wait() }() // At most MAX-1 could have been executing concurrently with job[i=1] // after it had cancelled the pool and before it wrote to log. // No new job should have started after that. So, log must have at most MAX-1 // jobs after 1. finishedPre := 0 finishedAfter := 0 canceled := 0 for i := range logs { if i == 1 { finishedAfter++ break } finishedPre++ } for i := range logs { ut.AssertEqual(t, true, i <= J) if i > 0 { finishedAfter++ } else { canceled++ } } t.Logf("JOBS: Total %d Pre %d After %d Cancelled %d", 2*J, finishedPre, finishedAfter, canceled) ut.AssertEqual(t, 2*J, finishedPre+finishedAfter+canceled) ut.AssertEqualf(t, true, finishedAfter < MAX, "%d (>=%d MAX) jobs started after cancellation.", finishedAfter, MAX) ut.AssertEqualf(t, true, canceled >= J, "%d > %d", canceled, J) ut.AssertEqual(t, cancelError, wait1) ut.AssertEqual(t, cancelError, wait2) }
func (f *WebDumbcasAppMock) get(url string, expectedURL string) *http.Response { r, err := http.Get(f.baseURL + url) ut.AssertEqual(f, nil, err) ut.AssertEqualf(f, true, expectedURL == "" || r.Request.URL.Path == expectedURL, "%s != %s", expectedURL, r.Request.URL.Path) return r }
func TestArchive(t *testing.T) { // Create a .isolate file and archive it. t.Parallel() server := isolatedfake.New() ts := httptest.NewServer(server) defer ts.Close() a := archiver.New(isolatedclient.New(ts.URL, "default-gzip"), nil) // Setup temporary directory. // /base/bar // /base/ignored // /foo/baz.isolate // /link -> /base/bar // Result: // /baz.isolated tmpDir, err := ioutil.TempDir("", "isolate") ut.AssertEqual(t, nil, err) defer func() { if err := os.RemoveAll(tmpDir); err != nil { t.Fail() } }() baseDir := filepath.Join(tmpDir, "base") fooDir := filepath.Join(tmpDir, "foo") ut.AssertEqual(t, nil, os.Mkdir(baseDir, 0700)) ut.AssertEqual(t, nil, os.Mkdir(fooDir, 0700)) ut.AssertEqual(t, nil, ioutil.WriteFile(filepath.Join(baseDir, "bar"), []byte("foo"), 0600)) ut.AssertEqual(t, nil, ioutil.WriteFile(filepath.Join(baseDir, "ignored"), []byte("ignored"), 0600)) isolate := `{ 'variables': { 'files': [ '../base/', '../link', ], }, 'conditions': [ ['OS=="amiga"', { 'variables': { 'command': ['amiga', '<(EXTRA)'], }, }], ['OS=="win"', { 'variables': { 'command': ['win'], }, }], ], }` isolatePath := filepath.Join(fooDir, "baz.isolate") ut.AssertEqual(t, nil, ioutil.WriteFile(isolatePath, []byte(isolate), 0600)) if !common.IsWindows() { ut.AssertEqual(t, nil, os.Symlink(filepath.Join("base", "bar"), filepath.Join(tmpDir, "link"))) } else { ut.AssertEqual(t, nil, ioutil.WriteFile(filepath.Join(tmpDir, "link"), []byte("no link on Windows"), 0600)) } opts := &ArchiveOptions{ Isolate: isolatePath, Isolated: filepath.Join(tmpDir, "baz.isolated"), Blacklist: common.Strings{"ignored", "*.isolate"}, PathVariables: map[string]string{"VAR": "wonderful"}, ExtraVariables: map[string]string{"EXTRA": "really"}, ConfigVariables: map[string]string{"OS": "amiga"}, } future := Archive(a, opts) ut.AssertEqual(t, "baz.isolated", future.DisplayName()) future.WaitForHashed() ut.AssertEqual(t, nil, future.Error()) ut.AssertEqual(t, nil, a.Close()) mode := 0600 if common.IsWindows() { mode = 0666 } // /base/ isolatedDirData := isolated.Isolated{ Algo: "sha-1", Files: map[string]isolated.File{ filepath.Join("base", "bar"): {Digest: "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33", Mode: newInt(mode), Size: newInt64(3)}, }, Version: isolated.IsolatedFormatVersion, } encoded, err := json.Marshal(isolatedDirData) ut.AssertEqual(t, nil, err) isolatedDirEncoded := string(encoded) + "\n" isolatedDirHash := isolated.HashBytes([]byte(isolatedDirEncoded)) isolatedData := isolated.Isolated{ Algo: "sha-1", Command: []string{"amiga", "really"}, Files: map[string]isolated.File{}, Includes: []isolated.HexDigest{isolatedDirHash}, RelativeCwd: "foo", Version: isolated.IsolatedFormatVersion, } if !common.IsWindows() { isolatedData.Files["link"] = isolated.File{Link: newString(filepath.Join("base", "bar"))} } else { isolatedData.Files["link"] = isolated.File{Digest: "12339b9756c2994f85c310d560bc8c142a6b79a1", Mode: newInt(0666), Size: newInt64(18)} } encoded, err = json.Marshal(isolatedData) ut.AssertEqual(t, nil, err) isolatedEncoded := string(encoded) + "\n" isolatedHash := isolated.HashBytes([]byte(isolatedEncoded)) expected := map[string]string{ "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33": "foo", string(isolatedDirHash): isolatedDirEncoded, string(isolatedHash): isolatedEncoded, } if common.IsWindows() { expected["12339b9756c2994f85c310d560bc8c142a6b79a1"] = "no link on Windows" } actual := map[string]string{} for k, v := range server.Contents() { actual[string(k)] = string(v) ut.AssertEqualf(t, expected[string(k)], actual[string(k)], "%s: %#v", k, actual[string(k)]) } ut.AssertEqual(t, expected, actual) ut.AssertEqual(t, isolatedHash, future.Digest()) stats := a.Stats() ut.AssertEqual(t, 0, stats.TotalHits()) ut.AssertEqual(t, common.Size(0), stats.TotalBytesHits()) if !common.IsWindows() { ut.AssertEqual(t, 3, stats.TotalMisses()) ut.AssertEqual(t, common.Size(3+len(isolatedDirEncoded)+len(isolatedEncoded)), stats.TotalBytesPushed()) } else { ut.AssertEqual(t, 4, stats.TotalMisses()) ut.AssertEqual(t, common.Size(3+18+len(isolatedDirEncoded)+len(isolatedEncoded)), stats.TotalBytesPushed()) } ut.AssertEqual(t, nil, server.Error()) digest, err := isolated.HashFile(filepath.Join(tmpDir, "baz.isolated")) ut.AssertEqual(t, isolated.DigestItem{isolatedHash, false, int64(len(isolatedEncoded))}, digest) ut.AssertEqual(t, nil, err) }