1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
/*
Copyright The ORAS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package command
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
. "github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"oras.land/oras/test/e2e/internal/testdata/artifact/blob"
"oras.land/oras/test/e2e/internal/testdata/artifact/config"
"oras.land/oras/test/e2e/internal/testdata/artifact/empty"
"oras.land/oras/test/e2e/internal/testdata/artifact/unnamed"
"oras.land/oras/test/e2e/internal/testdata/feature"
"oras.land/oras/test/e2e/internal/testdata/foobar"
"oras.land/oras/test/e2e/internal/testdata/multi_arch"
. "oras.land/oras/test/e2e/internal/utils"
)
var _ = Describe("ORAS beginners:", func() {
When("running pull command", func() {
It("should show help description with feature flags", func() {
out := ORAS("pull", "--help").MatchKeyWords(ExampleDesc).Exec().Out
gomega.Expect(out).Should(gbytes.Say("--oci-layout-path string\\s+%s", regexp.QuoteMeta(feature.Experimental.Mark)))
})
It("should not show --verbose in help doc", func() {
out := ORAS("push", "--help").MatchKeyWords(ExampleDesc).Exec().Out
gomega.Expect(out).ShouldNot(gbytes.Say("--verbose"))
})
It("should show text as default format type in help doc", func() {
MatchDefaultFlagValue("format", "text", "pull")
})
It("should show deprecation message and print unnamed status output for --verbose", func() {
tempDir := PrepareTempFiles()
ref := RegistryRef(ZOTHost, ImageRepo, foobar.Tag)
stateKeys := append(foobar.ImageLayerStateKeys, foobar.ManifestStateKey)
ORAS("pull", ref, "--verbose").
WithWorkDir(tempDir).
MatchErrKeyWords(feature.DeprecationMessageVerboseFlag).
MatchStatus(stateKeys, true, len(stateKeys)).
Exec()
})
It("should show deprecation message and should NOT print unnamed status output for --verbose=false", func() {
tempDir := PrepareTempFiles()
ref := RegistryRef(ZOTHost, ImageRepo, foobar.Tag)
stateKeys := foobar.ImageLayerStateKeys
out := ORAS("pull", ref, "--verbose=false").
WithWorkDir(tempDir).
MatchErrKeyWords(feature.DeprecationMessageVerboseFlag).
MatchStatus(stateKeys, false, len(stateKeys)).
Exec().Out
// should not print status output for unnamed blobs
gomega.Expect(out).ShouldNot(gbytes.Say("application/vnd.oci.image.manifest.v1+json"))
})
hintMsg := func(reference string) string {
return fmt.Sprintf("Skipped pulling layers without file name in \"org.opencontainers.image.title\"\nUse 'oras copy %s --to-oci-layout <layout-dir>' to pull all layers.\n", reference)
}
It("should show hint for unnamed layer", func() {
tempDir := PrepareTempFiles()
ref := RegistryRef(ZOTHost, ArtifactRepo, unnamed.Tag)
out := ORAS("pull", ref).WithWorkDir(tempDir).Exec().Out
gomega.Expect(out).Should(gbytes.Say(hintMsg(ref)))
})
It("should not show hint for unnamed config blob", func() {
tempDir := PrepareTempFiles()
ref := RegistryRef(ZOTHost, ImageRepo, foobar.Tag)
out := ORAS("pull", ref).WithWorkDir(tempDir).Exec().Out
gomega.Expect(out).ShouldNot(gbytes.Say(hintMsg(ref)))
})
It("should not show hint for empty layer", func() {
tempDir := PrepareTempFiles()
ref := RegistryRef(ZOTHost, ArtifactRepo, empty.Tag)
out := ORAS("pull", ref).WithWorkDir(tempDir).Exec().Out
gomega.Expect(out).ShouldNot(gbytes.Say(hintMsg(ref)))
})
It("should not show hint for json output", func() {
tempDir := PrepareTempFiles()
ref := RegistryRef(ZOTHost, ArtifactRepo, unnamed.Tag)
out := ORAS("pull", ref, "--format", "json").WithWorkDir(tempDir).Exec().Out
gomega.Expect(out).ShouldNot(gbytes.Say(hintMsg(ref)))
})
It("should not show hint for go template output", func() {
tempDir := PrepareTempFiles()
ref := RegistryRef(ZOTHost, ArtifactRepo, unnamed.Tag)
out := ORAS("pull", ref, "--format", "go-template={{.}}").WithWorkDir(tempDir).Exec().Out
gomega.Expect(out).ShouldNot(gbytes.Say(hintMsg(ref)))
})
It("should fail if template is specified without go template format", func() {
tempDir := PrepareTempFiles()
ref := RegistryRef(ZOTHost, ArtifactRepo, foobar.Tag)
ORAS("pull", ref, "--format", "json", "--template", "{{.}}").
WithWorkDir(tempDir).
ExpectFailure().
MatchErrKeyWords("--template must be used with --format go-template").
Exec()
ORAS("pull", ref, "--template", "{{.}}").
WithWorkDir(tempDir).
ExpectFailure().
MatchErrKeyWords("--template must be used with --format go-template").
Exec()
})
It("should fail if template format is invalid", func() {
tempDir := PrepareTempFiles()
invalidPrompt := "invalid format type"
ref := RegistryRef(ZOTHost, ArtifactRepo, foobar.Tag)
ORAS("pull", ref, "--format", "json", "--format", "=").
WithWorkDir(tempDir).
ExpectFailure().
MatchErrKeyWords(invalidPrompt).
Exec()
ORAS("pull", ref, "--format", "json", "--format", "={{.}}").
WithWorkDir(tempDir).
ExpectFailure().
MatchErrKeyWords(invalidPrompt).
Exec()
noTemplatePrompt := "format specified but no template given"
ORAS("pull", ref, "--format", "json", "--format", "go-template=").
WithWorkDir(tempDir).
ExpectFailure().
MatchErrKeyWords(noTemplatePrompt).
Exec()
ORAS("pull", ref, "--format", "json", "--format", "go-template").
WithWorkDir(tempDir).
ExpectFailure().
MatchErrKeyWords(noTemplatePrompt).
Exec()
})
It("should fail and show detailed error description if no argument provided", func() {
err := ORAS("pull").ExpectFailure().Exec().Err
Expect(err).Should(gbytes.Say("Error"))
Expect(err).Should(gbytes.Say("\nUsage: oras pull"))
Expect(err).Should(gbytes.Say("\n"))
Expect(err).Should(gbytes.Say(`Run "oras pull -h"`))
})
It("should fail if password is wrong with registry error prefix", func() {
ORAS("pull", RegistryRef(ZOTHost, ArtifactRepo, empty.Tag), "-u", Username, "-p", "???").
MatchErrKeyWords(RegistryErrorPrefix).ExpectFailure().Exec()
})
It("should fail if artifact is not found with registry error prefix", func() {
ORAS("pull", RegistryRef(ZOTHost, ArtifactRepo, InvalidTag)).
MatchErrKeyWords(RegistryErrorPrefix).ExpectFailure().Exec()
})
It("should fail if artifact is not found from OCI layout", func() {
root := PrepareTempOCI(ArtifactRepo)
ORAS("pull", Flags.Layout, LayoutRef(root, InvalidTag)).
MatchErrKeyWords("Error: ").ExpectFailure().Exec()
})
It("should fail if manifest config reference is invalid", func() {
root := PrepareTempOCI(ImageRepo)
ORAS("pull", Flags.Layout, LayoutRef(root, foobar.Tag), "--config", ":").
MatchErrKeyWords("Error: ").ExpectFailure().Exec()
})
It("should fail to pull layers outside of working directory", func() {
// prepare
pushRoot := GinkgoT().TempDir()
Expect(CopyTestFiles(pushRoot)).ShouldNot(HaveOccurred())
tag := "pushed"
ORAS("push", Flags.Layout, LayoutRef(pushRoot, tag), filepath.Join(pushRoot, foobar.FileConfigName), "--disable-path-validation").WithWorkDir(pushRoot).Exec()
// test
pullRoot := GinkgoT().TempDir()
ORAS("pull", Flags.Layout, LayoutRef(pushRoot, tag), "-o", "pulled").
WithDescription(pullRoot).
ExpectFailure().
MatchErrKeyWords("Error: ", "--allow-path-traversal").Exec()
})
})
})
var _ = Describe("OCI spec 1.1 registry users:", func() {
When("pulling images from remote registry", func() {
var (
configName = "test.config"
)
It("should pull all files in an image to a target folder", func() {
pullRoot := "pulled"
tempDir := PrepareTempFiles()
stateKeys := append(foobar.ImageLayerStateKeys, foobar.ManifestStateKey, foobar.ImageConfigStateKey(configName))
ORAS("pull", RegistryRef(ZOTHost, ImageRepo, foobar.Tag), "--config", configName, "-o", pullRoot).
MatchStatus(stateKeys, true, len(stateKeys)).
WithWorkDir(tempDir).Exec()
// check config
configPath := filepath.Join(tempDir, pullRoot, configName)
Expect(configPath).Should(BeAnExistingFile())
f, err := os.Open(configPath)
Expect(err).ShouldNot(HaveOccurred())
defer f.Close()
Eventually(gbytes.BufferReader(f)).Should(gbytes.Say("{}"))
for _, f := range foobar.ImageLayerNames {
// check layers
Binary("diff", filepath.Join(tempDir, "foobar", f), filepath.Join(pullRoot, f)).
WithWorkDir(tempDir).Exec()
}
ORAS("pull", RegistryRef(ZOTHost, ImageRepo, foobar.Tag), "-o", pullRoot, "--keep-old-files").
ExpectFailure().
WithDescription("fail if overwrite old files are disabled")
})
It("should skip config if media type not matching", func() {
pullRoot := "pulled"
tempDir := PrepareTempFiles()
stateKeys := append(foobar.ImageLayerStateKeys, foobar.ManifestStateKey)
ORAS("pull", RegistryRef(ZOTHost, ImageRepo, foobar.Tag), "--config", fmt.Sprintf("%s:%s", configName, "???"), "-o", pullRoot).
MatchStatus(stateKeys, true, len(stateKeys)).
WithWorkDir(tempDir).Exec()
// check config
Expect(filepath.Join(pullRoot, configName)).ShouldNot(BeAnExistingFile())
for _, f := range foobar.ImageLayerNames {
// check layers
Binary("diff", filepath.Join(tempDir, "foobar", f), filepath.Join(pullRoot, f)).
WithWorkDir(tempDir).
WithDescription("should download identical file " + f).Exec()
}
})
It("should pull subject", func() {
tempDir := GinkgoT().TempDir()
stateKeys := append(append(
foobar.ImageLayerStateKeys,
foobar.ManifestStateKey),
foobar.ImageReferrersStateKeys...,
)
ORAS("pull", RegistryRef(ZOTHost, ArtifactRepo, foobar.SignatureImageReferrer.Digest.String()), "--include-subject").
MatchStatus(stateKeys, true, len(stateKeys)).
WithWorkDir(tempDir).Exec()
})
It("should pull and output downloaded file paths", func() {
tempDir := GinkgoT().TempDir()
var paths []string
for _, p := range foobar.ImageLayerNames {
paths = append(paths, filepath.Join(tempDir, p))
}
ORAS("pull", RegistryRef(ZOTHost, ArtifactRepo, foobar.Tag), "--format", "go-template={{range .files}}{{println .path}}{{end}}").
WithWorkDir(tempDir).MatchKeyWords(paths...).Exec()
})
It("should pull and output downloaded file paths using --template", func() {
tempDir := GinkgoT().TempDir()
var paths []string
for _, p := range foobar.ImageLayerNames {
paths = append(paths, filepath.Join(tempDir, p))
}
ORAS("pull", RegistryRef(ZOTHost, ArtifactRepo, foobar.Tag), "--format", "go-template", "--template", "{{range .files}}{{println .path}}{{end}}").
WithWorkDir(tempDir).MatchKeyWords(paths...).Exec()
})
It("should pull and output path in json", func() {
tempDir := GinkgoT().TempDir()
var paths []string
for _, p := range foobar.ImageLayerNames {
paths = append(paths, filepath.Join(tempDir, p))
}
out := ORAS("pull", RegistryRef(ZOTHost, ArtifactRepo, foobar.Tag), "--format", "json").
WithWorkDir(tempDir).MatchKeyWords(paths...).Exec().Out.Contents()
var parsed struct{}
err := json.Unmarshal(out, &parsed)
Expect(err).ShouldNot(HaveOccurred())
})
It("should show correct reference", func() {
tempDir := PrepareTempFiles()
ref := RegistryRef(ZOTHost, ArtifactRepo, foobar.Tag)
out := ORAS("pull", ref, "--format", "go-template={{.reference}}").WithWorkDir(tempDir).Exec().Out.Contents()
Expect(out).To(Equal([]byte("localhost:7000/command/artifacts@sha256:fd6ed2f36b5465244d5dc86cb4e7df0ab8a9d24adc57825099f522fe009a22bb")))
})
It("should pull specific platform", func() {
ORAS("pull", RegistryRef(ZOTHost, ImageRepo, "multi"), "--platform", "linux/amd64", "-o", GinkgoT().TempDir()).
MatchStatus(multi_arch.LinuxAMD64StateKeys, true, len(multi_arch.LinuxAMD64StateKeys)).Exec()
})
It("should pull an artifact with blob", func() {
pullRoot := GinkgoT().TempDir()
ORAS("pull", RegistryRef(ZOTHost, ArtifactRepo, blob.Tag), "-o", pullRoot).Exec()
Expect(filepath.Join(pullRoot, multi_arch.LayerName)).Should(BeAnExistingFile())
})
It("should pull an artifact with config", func() {
pullRoot := GinkgoT().TempDir()
ORAS("pull", RegistryRef(ZOTHost, ArtifactRepo, config.Tag), "-o", pullRoot).Exec()
Expect(filepath.Join(pullRoot, multi_arch.LayerName)).Should(BeAnExistingFile())
})
It("should copy an artifact with blob", func() {
repo := cpTestRepo("artifact-with-blob")
stateKeys := append(append(foobar.ImageLayerStateKeys, foobar.ManifestStateKey, foobar.ImageReferrerConfigStateKeys[0]), foobar.ImageReferrersStateKeys...)
src := RegistryRef(ZOTHost, ArtifactRepo, foobar.SignatureImageReferrer.Digest.String())
dst := RegistryRef(FallbackHost, repo, "")
ORAS("cp", "-r", src, dst).MatchStatus(stateKeys, true, len(stateKeys)).Exec()
CompareRef(src, RegistryRef(FallbackHost, repo, foobar.SignatureImageReferrer.Digest.String()))
ORAS("discover", "-o", "tree", RegistryRef(FallbackHost, repo, foobar.Digest)).
WithDescription("discover referrer via subject").MatchKeyWords(foobar.SignatureImageReferrer.Digest.String(), foobar.SBOMImageReferrer.Digest.String()).Exec()
})
})
})
var _ = Describe("OCI spec 1.0 registry users:", func() {
It("should pull all files in an image to a target folder", func() {
pullRoot := "pulled"
configName := "test.config"
tempDir := PrepareTempFiles()
stateKeys := append(foobar.ImageLayerStateKeys, foobar.ManifestStateKey, foobar.ImageConfigStateKey(configName))
ORAS("pull", RegistryRef(FallbackHost, ArtifactRepo, foobar.Tag), "--config", configName, "-o", pullRoot).
MatchStatus(stateKeys, true, len(stateKeys)).
WithWorkDir(tempDir).Exec()
// check config
configPath := filepath.Join(tempDir, pullRoot, configName)
Expect(configPath).Should(BeAnExistingFile())
f, err := os.Open(configPath)
Expect(err).ShouldNot(HaveOccurred())
defer f.Close()
Eventually(gbytes.BufferReader(f)).Should(gbytes.Say("{}"))
for _, f := range foobar.ImageLayerNames {
// check layers
Binary("diff", filepath.Join(tempDir, "foobar", f), filepath.Join(pullRoot, f)).
WithWorkDir(tempDir).Exec()
}
ORAS("pull", RegistryRef(FallbackHost, ArtifactRepo, foobar.Tag), "-o", pullRoot, "--keep-old-files").
ExpectFailure().
WithDescription("fail if overwrite old files are disabled")
})
It("should pull subject", func() {
tempDir := GinkgoT().TempDir()
stateKeys := append(append(
foobar.ImageLayerStateKeys,
foobar.ManifestStateKey),
foobar.ImageReferrersStateKeys...,
)
ORAS("pull", RegistryRef(FallbackHost, ArtifactRepo, foobar.SignatureImageReferrer.Digest.String()), "--include-subject").
MatchStatus(stateKeys, true, len(stateKeys)).
WithWorkDir(tempDir).Exec()
})
})
var _ = Describe("OCI image layout users:", func() {
When("pulling images", func() {
var (
configName = "test.config"
)
It("should pull all files in an image to a target folder", func() {
pullRoot := "pulled"
root := PrepareTempOCI(ImageRepo)
stateKeys := append(foobar.ImageLayerStateKeys, foobar.ManifestStateKey, foobar.ImageConfigStateKey(configName))
ORAS("pull", Flags.Layout, LayoutRef(root, foobar.Tag), "--config", configName, "-o", pullRoot).
MatchStatus(stateKeys, true, len(stateKeys)).
WithWorkDir(root).Exec()
// check config
configPath := filepath.Join(root, pullRoot, configName)
Expect(configPath).Should(BeAnExistingFile())
f, err := os.Open(configPath)
Expect(err).ShouldNot(HaveOccurred())
defer f.Close()
Eventually(gbytes.BufferReader(f)).Should(gbytes.Say("{}"))
for _, f := range foobar.ImageLayerNames {
// check layers
Binary("diff", filepath.Join(root, "foobar", f), filepath.Join(pullRoot, f)).
WithWorkDir(root).Exec()
}
ORAS("pull", Flags.Layout, LayoutRef(root, foobar.Tag), "-o", pullRoot, "--keep-old-files").
ExpectFailure().
WithDescription("fail if overwrite old files are disabled")
})
It("should skip config if media type does not match", func() {
pullRoot := "pulled"
root := PrepareTempOCI(ImageRepo)
stateKeys := append(foobar.ImageLayerStateKeys, foobar.ManifestStateKey)
ORAS("pull", Flags.Layout, LayoutRef(root, foobar.Tag), "--config", fmt.Sprintf("%s:%s", configName, "???"), "-o", pullRoot).
MatchStatus(stateKeys, true, len(stateKeys)).
WithWorkDir(root).Exec()
// check config
Expect(filepath.Join(pullRoot, configName)).ShouldNot(BeAnExistingFile())
for _, f := range foobar.ImageLayerNames {
// check layers
Binary("diff", filepath.Join(root, "foobar", f), filepath.Join(pullRoot, f)).
WithWorkDir(root).
WithDescription("should download identical file " + f).Exec()
}
})
It("should pull subject", func() {
root := PrepareTempOCI(ArtifactRepo)
stateKeys := append(append(
foobar.ImageLayerStateKeys,
foobar.ManifestStateKey),
foobar.ImageReferrersStateKeys...,
)
ORAS("pull", Flags.Layout, LayoutRef(root, foobar.SignatureImageReferrer.Digest.String()), "--include-subject").
MatchStatus(stateKeys, true, len(stateKeys)).
WithWorkDir(root).Exec()
})
It("should pull specific platform", func() {
root := PrepareTempOCI(ImageRepo)
ORAS("pull", Flags.Layout, LayoutRef(root, multi_arch.Tag), "--platform", "linux/amd64", "-o", root).
MatchStatus(multi_arch.LinuxAMD64StateKeys, true, len(multi_arch.LinuxAMD64StateKeys)).Exec()
})
})
})
var _ = Describe("OCI image spec v1.1.0-rc2 artifact users:", func() {
It("should pull all files in an image to a target folder", func() {
pullRoot := "pulled"
configName := "test.config"
tempDir := PrepareTempFiles()
stateKeys := append(foobar.ImageLayerStateKeys, foobar.ManifestStateKey, foobar.ImageConfigStateKey(configName))
ORAS("pull", RegistryRef(Host, ImageRepo, foobar.Tag), "--config", configName, "-o", pullRoot).
MatchStatus(stateKeys, true, len(stateKeys)).
WithWorkDir(tempDir).Exec()
// check config
configPath := filepath.Join(tempDir, pullRoot, configName)
Expect(configPath).Should(BeAnExistingFile())
f, err := os.Open(configPath)
Expect(err).ShouldNot(HaveOccurred())
defer f.Close()
Eventually(gbytes.BufferReader(f)).Should(gbytes.Say("{}"))
for _, f := range foobar.ImageLayerNames {
// check layers
Binary("diff", filepath.Join(tempDir, "foobar", f), filepath.Join(pullRoot, f)).
WithWorkDir(tempDir).Exec()
}
ORAS("pull", RegistryRef(Host, ImageRepo, foobar.Tag), "-o", pullRoot, "--keep-old-files").
ExpectFailure().
WithDescription("fail if overwrite old files are disabled")
})
It("should pull subject", func() {
tempDir := GinkgoT().TempDir()
stateKeys := append(append(
foobar.ImageLayerStateKeys,
foobar.ManifestStateKey),
foobar.ArtifactReferrerStateKeys...,
)
ORAS("pull", RegistryRef(Host, ArtifactRepo, foobar.SignatureArtifactReferrer.Digest.String()), "--include-subject").
MatchStatus(stateKeys, true, len(stateKeys)).
WithWorkDir(tempDir).Exec()
})
})
|