Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: ct/go/util/gs_test.go

Issue 779633003: CT Google Storage utils to download/upload artifacts for workers (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: goimports Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ct/go/util/gs.go ('k') | ct/go/util/testdata/testupload/10ktest/TIMESTAMP » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Will need a local valid google_storage_token.data file to run the below test.
jcgregorio 2014/12/03 19:52:06 Can we protect these tests with -test.short so not
rmistry 2014/12/04 12:17:02 Cool, I did not know about this. Done.
16 func Auth_TestDownloadWorkerArtifacts(t *testing.T) {
17 testPagesetsDirName := filepath.Join("unit-tests", "util", "page_sets")
18 client, _ := GetOAuthClient()
19 gs, err := NewGsUtil(client)
20 if err != nil {
21 t.Errorf("Unexpected error: %s", err)
22 }
23
24 tmpDir := filepath.Join(os.TempDir(), "util_test")
25 StorageDir = tmpDir
26 defer os.RemoveAll(tmpDir)
27 if err := gs.DownloadWorkerArtifacts(testPagesetsDirName, "10k", 1); err != nil {
28 t.Errorf("Unexpected error: %s", err)
29 }
30
31 // Examine contents of the local directory.
32 localDir := filepath.Join(tmpDir, testPagesetsDirName, "10k")
33 files, err := ioutil.ReadDir(localDir)
34 if err != nil {
35 t.Errorf("Unexpected error: %s", err)
36 }
37 assert.Equal(t, 3, len(files))
38 assert.Equal(t, "TIMESTAMP", files[0].Name())
39 assert.Equal(t, "alexa1-1.py", files[1].Name())
40 assert.Equal(t, "alexa2-2.py", files[2].Name())
41 }
42
43 // Will need a local valid google_storage_token.data file to run the below test.
44 func Auth_TestUploadWorkerArtifacts(t *testing.T) {
45 // testPagesetsDirName := filepath.Join("unit-tests", "util", "page_sets ")
46 client, _ := GetOAuthClient()
47 gs, err := NewGsUtil(client)
48 if err != nil {
49 t.Errorf("Unexpected error: %s", err)
50 }
51 testDir := "testupload"
52 testPagesetType := "10ktest"
53 StorageDir = "testdata"
54 // defer deleting this remove dir!!
55 if err := gs.UploadWorkerArtifacts(testDir, testPagesetType, 1); err != nil {
56 t.Errorf("Unexpected error: %s", err)
57 }
58
59 // Examine contents of the remote directory and then clean it up.
60 service, err := storage.New(gs.client)
61 if err != nil {
62 t.Errorf("Unexpected error: %s", err)
63 }
64 gsDir := filepath.Join(testDir, testPagesetType, "slave1")
65 resp, err := service.Objects.List(GS_BUCKET_NAME).Prefix(gsDir + "/").Do ()
66 if err != nil {
67 t.Errorf("Unexpected error: %s", err)
68 }
69 assert.Equal(t, 3, len(resp.Items))
70 for index, fileName := range []string{"TIMESTAMP", "alexa1-1.py", "alexa 2-2.py"} {
71 filePath := fmt.Sprintf("%s/%s", gsDir, fileName)
72 defer service.Objects.Delete(GS_BUCKET_NAME, filePath).Do()
73 assert.Equal(t, filePath, resp.Items[index].Name)
74 }
75 }
76
77 func TestAreTimestampsEqual(t *testing.T) {
78 gs, err := NewGsUtil(util.NewTimeoutClient())
79 if err != nil {
80 t.Errorf("Unexpected error: %s", err)
81 }
82
83 tmpDir := filepath.Join(os.TempDir(), "util_test")
84 os.Mkdir(tmpDir, 0777)
85 defer os.RemoveAll(tmpDir)
86
87 f, err := os.Create(filepath.Join(tmpDir, TIMESTAMP_FILE_NAME))
88 if err != nil {
89 t.Errorf("Unexpected error: %s", err)
90 }
91 defer f.Close()
92
93 // Test with matching timestamps.
94 f.WriteString(GS_TEST_TIMESTAMP_VALUE)
95 result1, err := gs.AreTimeStampsEqual(tmpDir, "unit-tests/util/")
96 if err != nil {
97 t.Errorf("Unexpected error: %s", err)
98 }
99 assert.True(t, result1)
100
101 // Test with differing timestamps.
102 f.WriteString(GS_TEST_TIMESTAMP_VALUE)
103 result2, err := gs.AreTimeStampsEqual(tmpDir, "unit-tests/util/")
104 if err != nil {
105 t.Errorf("Unexpected error: %s", err)
106 }
107 assert.False(t, result2)
108
109 // Test with Google Storage timestamp missing.
110 result3, err := gs.AreTimeStampsEqual(tmpDir, "unit-tests/util/dummy_nam e/")
111 if err == nil {
112 t.Error("Expected an error")
113 }
114 assert.False(t, result3)
115
116 // Test with local timestamp missing.
117 result4, err := gs.AreTimeStampsEqual(tmpDir+"dummy_name", "unit-tests/u til/")
118 if err == nil {
119 t.Error("Expected an error")
120 }
121 assert.False(t, result4)
122 }
OLDNEW
« no previous file with comments | « ct/go/util/gs.go ('k') | ct/go/util/testdata/testupload/10ktest/TIMESTAMP » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698