OLD | NEW |
(Empty) | |
| 1 package util |
| 2 |
| 3 import ( |
| 4 "fmt" |
| 5 "io/ioutil" |
| 6 "os" |
| 7 "path/filepath" |
| 8 "testing" |
| 9 |
| 10 "code.google.com/p/google-api-go-client/storage/v1" |
| 11 "github.com/stretchr/testify/assert" |
| 12 "skia.googlesource.com/buildbot.git/go/util" |
| 13 ) |
| 14 |
| 15 func TestDownloadWorkerArtifacts(t *testing.T) { |
| 16 // Skip if we are running short tests because this test needs a valid |
| 17 // google_storage_token.data file with read write access. |
| 18 if testing.Short() { |
| 19 t.Skip("Skipping test because we are running in short mode.") |
| 20 } |
| 21 |
| 22 testPagesetsDirName := filepath.Join("unit-tests", "util", "page_sets") |
| 23 client, _ := GetOAuthClient() |
| 24 gs, err := NewGsUtil(client) |
| 25 if err != nil { |
| 26 t.Errorf("Unexpected error: %s", err) |
| 27 } |
| 28 |
| 29 tmpDir := filepath.Join(os.TempDir(), "util_test") |
| 30 StorageDir = tmpDir |
| 31 defer os.RemoveAll(tmpDir) |
| 32 if err := gs.DownloadWorkerArtifacts(testPagesetsDirName, "10k", 1); err
!= nil { |
| 33 t.Errorf("Unexpected error: %s", err) |
| 34 } |
| 35 |
| 36 // Examine contents of the local directory. |
| 37 localDir := filepath.Join(tmpDir, testPagesetsDirName, "10k") |
| 38 files, err := ioutil.ReadDir(localDir) |
| 39 if err != nil { |
| 40 t.Errorf("Unexpected error: %s", err) |
| 41 } |
| 42 assert.Equal(t, 3, len(files)) |
| 43 assert.Equal(t, "TIMESTAMP", files[0].Name()) |
| 44 assert.Equal(t, "alexa1-1.py", files[1].Name()) |
| 45 assert.Equal(t, "alexa2-2.py", files[2].Name()) |
| 46 } |
| 47 |
| 48 func TestUploadWorkerArtifacts(t *testing.T) { |
| 49 // Skip if we are running short tests because this test needs a valid |
| 50 // google_storage_token.data file with read write access. |
| 51 if testing.Short() { |
| 52 t.Skip("Skipping test because we are running in short mode.") |
| 53 } |
| 54 |
| 55 client, _ := GetOAuthClient() |
| 56 gs, err := NewGsUtil(client) |
| 57 if err != nil { |
| 58 t.Errorf("Unexpected error: %s", err) |
| 59 } |
| 60 testDir := "testupload" |
| 61 testPagesetType := "10ktest" |
| 62 StorageDir = "testdata" |
| 63 if err := gs.UploadWorkerArtifacts(testDir, testPagesetType, 1); err !=
nil { |
| 64 t.Errorf("Unexpected error: %s", err) |
| 65 } |
| 66 |
| 67 // Examine contents of the remote directory and then clean it up. |
| 68 service, err := storage.New(gs.client) |
| 69 if err != nil { |
| 70 t.Errorf("Unexpected error: %s", err) |
| 71 } |
| 72 gsDir := filepath.Join(testDir, testPagesetType, "slave1") |
| 73 resp, err := service.Objects.List(GS_BUCKET_NAME).Prefix(gsDir + "/").Do
() |
| 74 if err != nil { |
| 75 t.Errorf("Unexpected error: %s", err) |
| 76 } |
| 77 assert.Equal(t, 3, len(resp.Items)) |
| 78 for index, fileName := range []string{"TIMESTAMP", "alexa1-1.py", "alexa
2-2.py"} { |
| 79 filePath := fmt.Sprintf("%s/%s", gsDir, fileName) |
| 80 defer service.Objects.Delete(GS_BUCKET_NAME, filePath).Do() |
| 81 assert.Equal(t, filePath, resp.Items[index].Name) |
| 82 } |
| 83 } |
| 84 |
| 85 func TestAreTimestampsEqual(t *testing.T) { |
| 86 gs, err := NewGsUtil(util.NewTimeoutClient()) |
| 87 if err != nil { |
| 88 t.Errorf("Unexpected error: %s", err) |
| 89 } |
| 90 |
| 91 tmpDir := filepath.Join(os.TempDir(), "util_test") |
| 92 os.Mkdir(tmpDir, 0777) |
| 93 defer os.RemoveAll(tmpDir) |
| 94 |
| 95 f, err := os.Create(filepath.Join(tmpDir, TIMESTAMP_FILE_NAME)) |
| 96 if err != nil { |
| 97 t.Errorf("Unexpected error: %s", err) |
| 98 } |
| 99 defer f.Close() |
| 100 |
| 101 // Test with matching timestamps. |
| 102 f.WriteString(GS_TEST_TIMESTAMP_VALUE) |
| 103 result1, err := gs.AreTimeStampsEqual(tmpDir, "unit-tests/util/") |
| 104 if err != nil { |
| 105 t.Errorf("Unexpected error: %s", err) |
| 106 } |
| 107 assert.True(t, result1) |
| 108 |
| 109 // Test with differing timestamps. |
| 110 f.WriteString(GS_TEST_TIMESTAMP_VALUE) |
| 111 result2, err := gs.AreTimeStampsEqual(tmpDir, "unit-tests/util/") |
| 112 if err != nil { |
| 113 t.Errorf("Unexpected error: %s", err) |
| 114 } |
| 115 assert.False(t, result2) |
| 116 |
| 117 // Test with Google Storage timestamp missing. |
| 118 result3, err := gs.AreTimeStampsEqual(tmpDir, "unit-tests/util/dummy_nam
e/") |
| 119 if err == nil { |
| 120 t.Error("Expected an error") |
| 121 } |
| 122 assert.False(t, result3) |
| 123 |
| 124 // Test with local timestamp missing. |
| 125 result4, err := gs.AreTimeStampsEqual(tmpDir+"dummy_name", "unit-tests/u
til/") |
| 126 if err == nil { |
| 127 t.Error("Expected an error") |
| 128 } |
| 129 assert.False(t, result4) |
| 130 } |
OLD | NEW |