| OLD | NEW |
| 1 // Copyright 2014 The LUCI Authors. All rights reserved. | 1 // Copyright 2014 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package local | 5 package local |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "encoding/json" | 9 "encoding/json" |
| 10 "io" | 10 "io" |
| 11 "io/ioutil" | 11 "io/ioutil" |
| 12 "os" | 12 "os" |
| 13 "strings" |
| 13 "testing" | 14 "testing" |
| 14 | 15 |
| 15 "golang.org/x/net/context" | 16 "golang.org/x/net/context" |
| 16 | 17 |
| 17 . "github.com/luci/luci-go/cipd/client/cipd/common" | 18 . "github.com/luci/luci-go/cipd/client/cipd/common" |
| 18 . "github.com/smartystreets/goconvey/convey" | 19 . "github.com/smartystreets/goconvey/convey" |
| 19 ) | 20 ) |
| 20 | 21 |
| 21 func normalizeJSON(s string) (string, error) { | 22 func normalizeJSON(s string) (string, error) { |
| 22 // Round trip through default json marshaller to normalize indentation. | 23 // Round trip through default json marshaller to normalize indentation. |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 So(err, ShouldBeNil) | 137 So(err, ShouldBeNil) |
| 137 }) | 138 }) |
| 138 | 139 |
| 139 Convey("ExtractInstance works", t, func() { | 140 Convey("ExtractInstance works", t, func() { |
| 140 // Add a bunch of files to a package. | 141 // Add a bunch of files to a package. |
| 141 out := bytes.Buffer{} | 142 out := bytes.Buffer{} |
| 142 err := BuildInstance(ctx, BuildInstanceOptions{ | 143 err := BuildInstance(ctx, BuildInstanceOptions{ |
| 143 Input: []File{ | 144 Input: []File{ |
| 144 NewTestFile("testing/qwerty", "12345", false), | 145 NewTestFile("testing/qwerty", "12345", false), |
| 145 NewTestFile("abc", "duh", true), | 146 NewTestFile("abc", "duh", true), |
| 147 NewTestFile("bad_dir/pkg/0/description.json", "{
}", false), |
| 146 NewTestSymlink("rel_symlink", "abc"), | 148 NewTestSymlink("rel_symlink", "abc"), |
| 147 NewTestSymlink("abs_symlink", "/abc/def"), | 149 NewTestSymlink("abs_symlink", "/abc/def"), |
| 148 }, | 150 }, |
| 149 Output: &out, | 151 Output: &out, |
| 150 PackageName: "testing", | 152 PackageName: "testing", |
| 151 VersionFile: "subpath/version.json", | 153 VersionFile: "subpath/version.json", |
| 152 CompressionLevel: 5, | 154 CompressionLevel: 5, |
| 153 }) | 155 }) |
| 154 So(err, ShouldBeNil) | 156 So(err, ShouldBeNil) |
| 155 | 157 |
| 156 // Extract files. | 158 // Extract files. |
| 157 inst, err := OpenInstance(ctx, bytes.NewReader(out.Bytes()), "") | 159 inst, err := OpenInstance(ctx, bytes.NewReader(out.Bytes()), "") |
| 158 if inst != nil { | 160 if inst != nil { |
| 159 defer inst.Close() | 161 defer inst.Close() |
| 160 } | 162 } |
| 161 So(err, ShouldBeNil) | 163 So(err, ShouldBeNil) |
| 162 dest := &testDestination{} | 164 dest := &testDestination{} |
| 163 » » err = ExtractInstance(ctx, inst, dest) | 165 » » err = ExtractInstance(ctx, inst, dest, func(f File) bool { |
| 166 » » » return strings.HasPrefix(f.Name(), "bad_dir/") |
| 167 » » }) |
| 164 So(err, ShouldBeNil) | 168 So(err, ShouldBeNil) |
| 165 So(dest.beginCalls, ShouldEqual, 1) | 169 So(dest.beginCalls, ShouldEqual, 1) |
| 166 So(dest.endCalls, ShouldEqual, 1) | 170 So(dest.endCalls, ShouldEqual, 1) |
| 167 | 171 |
| 168 // Verify file list, file data and flags are correct. | 172 // Verify file list, file data and flags are correct. |
| 169 names := []string{} | 173 names := []string{} |
| 170 for _, f := range dest.files { | 174 for _, f := range dest.files { |
| 171 names = append(names, f.name) | 175 names = append(names, f.name) |
| 172 } | 176 } |
| 173 So(names, ShouldResemble, []string{ | 177 So(names, ShouldResemble, []string{ |
| 174 "testing/qwerty", | 178 "testing/qwerty", |
| 175 "abc", | 179 "abc", |
| 176 "rel_symlink", | 180 "rel_symlink", |
| 177 "abs_symlink", | 181 "abs_symlink", |
| 178 "subpath/version.json", | 182 "subpath/version.json", |
| 179 ".cipdpkg/manifest.json", | 183 ".cipdpkg/manifest.json", |
| 180 }) | 184 }) |
| 181 So(string(dest.files[0].Bytes()), ShouldEqual, "12345") | 185 So(string(dest.files[0].Bytes()), ShouldEqual, "12345") |
| 182 So(dest.files[1].executable, ShouldBeTrue) | 186 So(dest.files[1].executable, ShouldBeTrue) |
| 183 So(dest.files[2].symlinkTarget, ShouldEqual, "abc") | 187 So(dest.files[2].symlinkTarget, ShouldEqual, "abc") |
| 184 So(dest.files[3].symlinkTarget, ShouldEqual, "/abc/def") | 188 So(dest.files[3].symlinkTarget, ShouldEqual, "/abc/def") |
| 185 | 189 |
| 186 // Verify version file is correct. | 190 // Verify version file is correct. |
| 187 goodVersionFile := `{ | 191 goodVersionFile := `{ |
| 188 » » » "instance_id": "dd08e20d0e436c5c778ccbd87c99a05182dc5558
", | 192 » » » "instance_id": "5503734d74bc46f2b4d59c23bf4b1293696861a9
", |
| 189 "package_name": "testing" | 193 "package_name": "testing" |
| 190 }` | 194 }` |
| 191 So(dest.files[4].name, ShouldEqual, "subpath/version.json") | 195 So(dest.files[4].name, ShouldEqual, "subpath/version.json") |
| 192 So(string(dest.files[4].Bytes()), shouldBeSameJSONDict, goodVers
ionFile) | 196 So(string(dest.files[4].Bytes()), shouldBeSameJSONDict, goodVers
ionFile) |
| 193 | 197 |
| 194 // Verify manifest file is correct. | 198 // Verify manifest file is correct. |
| 195 goodManifest := `{ | 199 goodManifest := `{ |
| 196 "format_version": "1", | 200 "format_version": "1", |
| 197 "package_name": "testing", | 201 "package_name": "testing", |
| 198 "version_file": "subpath/version.json", | 202 "version_file": "subpath/version.json", |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 symlinkTarget: target, | 268 symlinkTarget: target, |
| 265 } | 269 } |
| 266 d.files = append(d.files, f) | 270 d.files = append(d.files, f) |
| 267 return nil | 271 return nil |
| 268 } | 272 } |
| 269 | 273 |
| 270 func (d *testDestination) End(ctx context.Context, success bool) error { | 274 func (d *testDestination) End(ctx context.Context, success bool) error { |
| 271 d.endCalls++ | 275 d.endCalls++ |
| 272 return nil | 276 return nil |
| 273 } | 277 } |
| OLD | NEW |