| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package archiver | 5 package archiver |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | |
| 9 "fmt" | 8 "fmt" |
| 10 "io/ioutil" | 9 "io/ioutil" |
| 11 "log" | 10 "log" |
| 12 "net/http/httptest" | 11 "net/http/httptest" |
| 13 "os" | 12 "os" |
| 14 "path/filepath" | 13 "path/filepath" |
| 15 "testing" | 14 "testing" |
| 16 | 15 |
| 17 "github.com/luci/luci-go/client/internal/common" | 16 "github.com/luci/luci-go/client/internal/common" |
| 18 "github.com/luci/luci-go/client/isolatedclient" | 17 "github.com/luci/luci-go/client/isolatedclient" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 ut.AssertEqual(t, nil, server.Error()) | 78 ut.AssertEqual(t, nil, server.Error()) |
| 80 } | 79 } |
| 81 | 80 |
| 82 func TestArchiverFileHit(t *testing.T) { | 81 func TestArchiverFileHit(t *testing.T) { |
| 83 t.Parallel() | 82 t.Parallel() |
| 84 server := isolatedfake.New() | 83 server := isolatedfake.New() |
| 85 ts := httptest.NewServer(server) | 84 ts := httptest.NewServer(server) |
| 86 defer ts.Close() | 85 defer ts.Close() |
| 87 a := New(isolatedclient.New(nil, ts.URL, "default-gzip"), nil) | 86 a := New(isolatedclient.New(nil, ts.URL, "default-gzip"), nil) |
| 88 server.Inject([]byte("foo")) | 87 server.Inject([]byte("foo")) |
| 89 » future := a.Push("foo", bytes.NewReader([]byte("foo")), 0) | 88 » future := a.Push("foo", isolatedclient.NewBytesSource([]byte("foo")), 0) |
| 90 future.WaitForHashed() | 89 future.WaitForHashed() |
| 91 ut.AssertEqual(t, isolated.HexDigest("0beec7b5ea3f0fdbc95d0dd47f3c5bc275
da8a33"), future.Digest()) | 90 ut.AssertEqual(t, isolated.HexDigest("0beec7b5ea3f0fdbc95d0dd47f3c5bc275
da8a33"), future.Digest()) |
| 92 ut.AssertEqual(t, nil, a.Close()) | 91 ut.AssertEqual(t, nil, a.Close()) |
| 93 | 92 |
| 94 stats := a.Stats() | 93 stats := a.Stats() |
| 95 ut.AssertEqual(t, 1, stats.TotalHits()) | 94 ut.AssertEqual(t, 1, stats.TotalHits()) |
| 96 ut.AssertEqual(t, 0, stats.TotalMisses()) | 95 ut.AssertEqual(t, 0, stats.TotalMisses()) |
| 97 ut.AssertEqual(t, units.Size(3), stats.TotalBytesHits()) | 96 ut.AssertEqual(t, units.Size(3), stats.TotalBytesHits()) |
| 98 ut.AssertEqual(t, units.Size(0), stats.TotalBytesPushed()) | 97 ut.AssertEqual(t, units.Size(0), stats.TotalBytesPushed()) |
| 99 } | 98 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 120 | 119 |
| 121 fileName := filepath.Join(tmpDir, "existent") | 120 fileName := filepath.Join(tmpDir, "existent") |
| 122 ut.AssertEqual(t, nil, ioutil.WriteFile(fileName, []byte("foo"), 0600)) | 121 ut.AssertEqual(t, nil, ioutil.WriteFile(fileName, []byte("foo"), 0600)) |
| 123 future2 := a.PushFile("existent", fileName, 0) | 122 future2 := a.PushFile("existent", fileName, 0) |
| 124 future1.WaitForHashed() | 123 future1.WaitForHashed() |
| 125 future2.WaitForHashed() | 124 future2.WaitForHashed() |
| 126 osErr := "no such file or directory" | 125 osErr := "no such file or directory" |
| 127 if common.IsWindows() { | 126 if common.IsWindows() { |
| 128 osErr = "The system cannot find the file specified." | 127 osErr = "The system cannot find the file specified." |
| 129 } | 128 } |
| 130 » expected := fmt.Errorf("hash(foo) failed: open %s: %s\n", nonexistent, o
sErr) | 129 » expected := fmt.Errorf("source(foo) failed: open %s: %s\n", nonexistent,
osErr) |
| 131 ut.AssertEqual(t, expected, <-a.Channel()) | 130 ut.AssertEqual(t, expected, <-a.Channel()) |
| 132 ut.AssertEqual(t, expected, a.Close()) | 131 ut.AssertEqual(t, expected, a.Close()) |
| 133 ut.AssertEqual(t, nil, server.Error()) | 132 ut.AssertEqual(t, nil, server.Error()) |
| 134 } | 133 } |
| 135 | 134 |
| 136 func TestArchiverPushClosed(t *testing.T) { | 135 func TestArchiverPushClosed(t *testing.T) { |
| 137 t.Parallel() | 136 t.Parallel() |
| 138 a := New(nil, nil) | 137 a := New(nil, nil) |
| 139 ut.AssertEqual(t, nil, a.Close()) | 138 ut.AssertEqual(t, nil, a.Close()) |
| 140 ut.AssertEqual(t, nil, a.PushFile("ignored", "ignored", 0)) | 139 ut.AssertEqual(t, nil, a.PushFile("ignored", "ignored", 0)) |
| 141 } | 140 } |
| 142 | |
| 143 func TestArchiverPushSeeked(t *testing.T) { | |
| 144 t.Parallel() | |
| 145 server := isolatedfake.New() | |
| 146 ts := httptest.NewServer(server) | |
| 147 defer ts.Close() | |
| 148 a := New(isolatedclient.New(nil, ts.URL, "default-gzip"), nil) | |
| 149 misplaced := bytes.NewReader([]byte("foo")) | |
| 150 _, _ = misplaced.Seek(1, os.SEEK_SET) | |
| 151 future := a.Push("works", misplaced, 0) | |
| 152 future.WaitForHashed() | |
| 153 ut.AssertEqual(t, isolated.HexDigest("0beec7b5ea3f0fdbc95d0dd47f3c5bc275
da8a33"), future.Digest()) | |
| 154 ut.AssertEqual(t, nil, a.Close()) | |
| 155 } | |
| OLD | NEW |