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

Side by Side Diff: go/src/infra/tools/cipd/reader_test.go

Issue 1129043003: cipd: Refactor client to make it more readable. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 5 years, 7 months 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 | « go/src/infra/tools/cipd/reader.go ('k') | go/src/infra/tools/cipd/remote.go » ('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 // 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"
10 "io/ioutil"
11 "os"
12 "testing"
13
14 . "github.com/smartystreets/goconvey/convey"
15 )
16
17 func TestPackageReading(t *testing.T) {
18 goodManifest := `{
19 "format_version": "1",
20 "package_name": "testing"
21 }`
22
23 Convey("Open empty package works", t, func() {
24 // Build an empty package.
25 out := bytes.Buffer{}
26 err := BuildInstance(BuildInstanceOptions{
27 Output: &out,
28 PackageName: "testing",
29 })
30 So(err, ShouldBeNil)
31
32 // Open it.
33 inst, err := OpenInstance(bytes.NewReader(out.Bytes()), "")
34 if inst != nil {
35 defer inst.Close()
36 }
37 So(inst, ShouldNotBeNil)
38 So(err, ShouldBeNil)
39 So(inst.PackageName(), ShouldEqual, "testing")
40 So(inst.InstanceID(), ShouldEqual, "23f2c4900785ac8faa2f38e47392 5b840e574ccc")
41 So(len(inst.Files()), ShouldEqual, 1)
42
43 // Contains single manifest file.
44 f := inst.Files()[0]
45 So(f.Name(), ShouldEqual, ".cipdpkg/manifest.json")
46 So(f.Size(), ShouldEqual, uint64(len(goodManifest)))
47 So(f.Executable(), ShouldBeFalse)
48 r, err := f.Open()
49 if r != nil {
50 defer r.Close()
51 }
52 So(err, ShouldBeNil)
53 manifest, err := ioutil.ReadAll(r)
54 So(err, ShouldBeNil)
55 So(string(manifest), ShouldEqual, goodManifest)
56 })
57
58 Convey("Open empty package with unexpected instance ID", t, func() {
59 // Build an empty package.
60 out := bytes.Buffer{}
61 err := BuildInstance(BuildInstanceOptions{
62 Output: &out,
63 PackageName: "testing",
64 })
65 So(err, ShouldBeNil)
66
67 // Attempt to open it, providing correct instance ID, should wor k.
68 source := bytes.NewReader(out.Bytes())
69 inst, err := OpenInstance(source, "23f2c4900785ac8faa2f38e473925 b840e574ccc")
70 So(err, ShouldBeNil)
71 So(inst, ShouldNotBeNil)
72 inst.Close()
73
74 // Attempt to open it, providing incorrect instance ID.
75 inst, err = OpenInstance(source, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaa")
76 So(err, ShouldNotBeNil)
77 So(inst, ShouldBeNil)
78 })
79
80 Convey("OpenInstanceFile works", t, func() {
81 // Open temp file.
82 tempFile, err := ioutil.TempFile("", "cipdtest")
83 So(err, ShouldBeNil)
84 tempFilePath := tempFile.Name()
85 defer os.Remove(tempFilePath)
86
87 // Write empty package to it.
88 err = BuildInstance(BuildInstanceOptions{
89 Output: tempFile,
90 PackageName: "testing",
91 })
92 So(err, ShouldBeNil)
93 tempFile.Close()
94
95 // Read the package.
96 inst, err := OpenInstanceFile(tempFilePath, "")
97 if inst != nil {
98 defer inst.Close()
99 }
100 So(inst, ShouldNotBeNil)
101 So(err, ShouldBeNil)
102 })
103
104 Convey("ExtractInstance works", t, func() {
105 // Add a bunch of files to a package.
106 out := bytes.Buffer{}
107 err := BuildInstance(BuildInstanceOptions{
108 Input: []File{
109 makeTestFile("testing/qwerty", "12345", false),
110 makeTestFile("abc", "duh", true),
111 makeTestSymlink("rel_symlink", "abc"),
112 makeTestSymlink("abs_symlink", "/abc/def"),
113 },
114 Output: &out,
115 PackageName: "testing",
116 })
117 So(err, ShouldBeNil)
118
119 // Extract files.
120 inst, err := OpenInstance(bytes.NewReader(out.Bytes()), "")
121 if inst != nil {
122 defer inst.Close()
123 }
124 So(err, ShouldBeNil)
125 dest := &testDestination{}
126 err = ExtractInstance(inst, dest)
127 So(err, ShouldBeNil)
128 So(dest.beginCalls, ShouldEqual, 1)
129 So(dest.endCalls, ShouldEqual, 1)
130
131 // Verify file list, file data and flags are correct.
132 names := []string{}
133 for _, f := range dest.files {
134 names = append(names, f.name)
135 }
136 So(names, ShouldResemble, []string{
137 "testing/qwerty",
138 "abc",
139 "rel_symlink",
140 "abs_symlink",
141 ".cipdpkg/manifest.json",
142 })
143 So(string(dest.files[0].Bytes()), ShouldEqual, "12345")
144 So(dest.files[1].executable, ShouldBeTrue)
145 So(dest.files[2].symlinkTarget, ShouldEqual, "abc")
146 So(dest.files[3].symlinkTarget, ShouldEqual, "/abc/def")
147 })
148 }
149
150 ////////////////////////////////////////////////////////////////////////////////
151
152 type testDestination struct {
153 beginCalls int
154 endCalls int
155 files []*testDestinationFile
156 }
157
158 type testDestinationFile struct {
159 bytes.Buffer
160 name string
161 executable bool
162 symlinkTarget string
163 }
164
165 func (d *testDestinationFile) Close() error { return nil }
166
167 func (d *testDestination) Begin() error {
168 d.beginCalls++
169 return nil
170 }
171
172 func (d *testDestination) CreateFile(name string, executable bool) (io.WriteClos er, error) {
173 f := &testDestinationFile{
174 name: name,
175 executable: executable,
176 }
177 d.files = append(d.files, f)
178 return f, nil
179 }
180
181 func (d *testDestination) CreateSymlink(name string, target string) error {
182 f := &testDestinationFile{
183 name: name,
184 symlinkTarget: target,
185 }
186 d.files = append(d.files, f)
187 return nil
188 }
189
190 func (d *testDestination) End(success bool) error {
191 d.endCalls++
192 return nil
193 }
OLDNEW
« no previous file with comments | « go/src/infra/tools/cipd/reader.go ('k') | go/src/infra/tools/cipd/remote.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698