OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package cipd | |
6 | |
7 import ( | |
8 "bytes" | |
9 "io/ioutil" | |
10 "net/http" | |
11 "os" | |
12 "path/filepath" | |
13 "testing" | |
14 "time" | |
15 | |
16 . "github.com/smartystreets/goconvey/convey" | |
17 ) | |
18 | |
19 func TestParseDesiredState(t *testing.T) { | |
20 call := func(data string) ([]PackageState, error) { | |
21 return ParseDesiredState(bytes.NewBufferString(data)) | |
22 } | |
23 | |
24 Convey("ParseDesiredState works", t, func() { | |
25 out, err := call(` | |
26 # Comment | |
27 | |
28 pkg/a 0000000000000000000000000000000000000000 | |
29 pkg/b 1000000000000000000000000000000000000000 | |
30 `) | |
31 So(err, ShouldBeNil) | |
32 So(out, ShouldResemble, []PackageState{ | |
33 PackageState{ | |
34 PackageName: "pkg/a", | |
35 InstanceID: "0000000000000000000000000000000000
000000", | |
36 }, | |
37 PackageState{ | |
38 PackageName: "pkg/b", | |
39 InstanceID: "1000000000000000000000000000000000
000000", | |
40 }, | |
41 }) | |
42 }) | |
43 | |
44 Convey("ParseDesiredState empty", t, func() { | |
45 out, err := call("") | |
46 So(err, ShouldBeNil) | |
47 So(out, ShouldResemble, []PackageState{}) | |
48 }) | |
49 | |
50 Convey("ParseDesiredState bad package name", t, func() { | |
51 _, err := call("bad.package.name/a 0000000000000000000000000000
000000000000") | |
52 So(err, ShouldNotBeNil) | |
53 }) | |
54 | |
55 Convey("ParseDesiredState bad instance ID", t, func() { | |
56 _, err := call("pkg/a 0000") | |
57 So(err, ShouldNotBeNil) | |
58 }) | |
59 | |
60 Convey("ParseDesiredState bad line", t, func() { | |
61 _, err := call("pkg/a)") | |
62 So(err, ShouldNotBeNil) | |
63 }) | |
64 } | |
65 | |
66 func TestEnsurePackages(t *testing.T) { | |
67 Convey("Mocking temp dir", t, func() { | |
68 tempDir, err := ioutil.TempDir("", "cipd_test") | |
69 So(err, ShouldBeNil) | |
70 Reset(func() { os.RemoveAll(tempDir) }) | |
71 | |
72 assertFile := func(relPath, data string) { | |
73 body, err := ioutil.ReadFile(filepath.Join(tempDir, relP
ath)) | |
74 So(err, ShouldBeNil) | |
75 So(string(body), ShouldEqual, data) | |
76 } | |
77 | |
78 mockClock(time.Now()) | |
79 mockResumableUpload() | |
80 | |
81 Convey("EnsurePackages full flow", func() { | |
82 // Prepare a bunch of packages. | |
83 a1 := buildInstanceInMemory("pkg/a", []File{makeTestFile
("file a 1", "test data", false)}) | |
84 defer a1.Close() | |
85 a2 := buildInstanceInMemory("pkg/a", []File{makeTestFile
("file a 2", "test data", false)}) | |
86 defer a2.Close() | |
87 b := buildInstanceInMemory("pkg/b", []File{makeTestFile(
"file b", "test data", false)}) | |
88 defer b.Close() | |
89 | |
90 // Calls EnsurePackages mocking fetch backend first. | |
91 callEnsure := func(instances []PackageInstance) error { | |
92 states := []PackageState{} | |
93 for _, i := range instances { | |
94 states = append(states, PackageState{ | |
95 PackageName: i.PackageName(), | |
96 InstanceID: i.InstanceID(), | |
97 }) | |
98 } | |
99 service := mockFetchBackend(instances) | |
100 return EnsurePackages(EnsurePackagesOptions{ | |
101 ClientFactory: func() (*http.Client, err
or) { return service.client, nil }, | |
102 Root: tempDir, | |
103 Packages: states, | |
104 }) | |
105 } | |
106 | |
107 // Noop run on top of empty directory. | |
108 err := callEnsure(nil) | |
109 So(err, ShouldBeNil) | |
110 | |
111 // Specify same package twice. | |
112 err = callEnsure([]PackageInstance{a1, a2}) | |
113 So(err, ShouldNotBeNil) | |
114 | |
115 // Install a1 into a site root. | |
116 err = callEnsure([]PackageInstance{a1}) | |
117 So(err, ShouldBeNil) | |
118 assertFile("file a 1", "test data") | |
119 deployed, err := FindDeployed(tempDir) | |
120 So(err, ShouldBeNil) | |
121 So(deployed, ShouldResemble, []PackageState{ | |
122 PackageState{ | |
123 PackageName: a1.PackageName(), | |
124 InstanceID: a1.InstanceID(), | |
125 }, | |
126 }) | |
127 | |
128 // Noop run. | |
129 err = callEnsure([]PackageInstance{a1}) | |
130 So(err, ShouldBeNil) | |
131 assertFile("file a 1", "test data") | |
132 deployed, err = FindDeployed(tempDir) | |
133 So(err, ShouldBeNil) | |
134 So(deployed, ShouldResemble, []PackageState{ | |
135 PackageState{ | |
136 PackageName: a1.PackageName(), | |
137 InstanceID: a1.InstanceID(), | |
138 }, | |
139 }) | |
140 | |
141 // Upgrade a1 to a2. | |
142 err = callEnsure([]PackageInstance{a2}) | |
143 So(err, ShouldBeNil) | |
144 assertFile("file a 2", "test data") | |
145 deployed, err = FindDeployed(tempDir) | |
146 So(err, ShouldBeNil) | |
147 So(deployed, ShouldResemble, []PackageState{ | |
148 PackageState{ | |
149 PackageName: a2.PackageName(), | |
150 InstanceID: a2.InstanceID(), | |
151 }, | |
152 }) | |
153 | |
154 // Remove a2 and install b. | |
155 err = callEnsure([]PackageInstance{b}) | |
156 So(err, ShouldBeNil) | |
157 assertFile("file b", "test data") | |
158 deployed, err = FindDeployed(tempDir) | |
159 So(err, ShouldBeNil) | |
160 So(deployed, ShouldResemble, []PackageState{ | |
161 PackageState{ | |
162 PackageName: b.PackageName(), | |
163 InstanceID: b.InstanceID(), | |
164 }, | |
165 }) | |
166 | |
167 // Remove b. | |
168 err = callEnsure(nil) | |
169 So(err, ShouldBeNil) | |
170 deployed, err = FindDeployed(tempDir) | |
171 So(err, ShouldBeNil) | |
172 So(deployed, ShouldResemble, []PackageState{}) | |
173 | |
174 // Install a1 and b. | |
175 err = callEnsure([]PackageInstance{a1, b}) | |
176 So(err, ShouldBeNil) | |
177 assertFile("file a 1", "test data") | |
178 assertFile("file b", "test data") | |
179 deployed, err = FindDeployed(tempDir) | |
180 So(err, ShouldBeNil) | |
181 So(deployed, ShouldResemble, []PackageState{ | |
182 PackageState{ | |
183 PackageName: a1.PackageName(), | |
184 InstanceID: a1.InstanceID(), | |
185 }, | |
186 PackageState{ | |
187 PackageName: b.PackageName(), | |
188 InstanceID: b.InstanceID(), | |
189 }, | |
190 }) | |
191 }) | |
192 }) | |
193 } | |
OLD | NEW |