Skip to content

Commit

Permalink
test: add e2e specs for oras blob fetch (#699)
Browse files Browse the repository at this point in the history
Resolves #573

Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah committed Dec 14, 2022
1 parent 239e145 commit 106d51a
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 20 deletions.
30 changes: 10 additions & 20 deletions cmd/oras/internal/option/pretty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ limitations under the License.
package option

import (
"encoding/json"
"io"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/pflag"
)

Expand All @@ -38,16 +35,8 @@ func TestPretty_ApplyFlags(t *testing.T) {

func TestPretty_Output(t *testing.T) {
// generate test content
blob := []byte("hello world")
desc := ocispec.Descriptor{
MediaType: "test",
Digest: digest.FromBytes(blob),
Size: int64(len(blob)),
}
want, err := json.Marshal(desc)
if err != nil {
t.Fatal("error calling json.Marshal(), error =", err)
}
raw := []byte("{\"mediaType\":\"test\",\"digest\":\"sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9\",\"size\":11}")
prettified := []byte("{\n \"mediaType\": \"test\",\n \"digest\": \"sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9\",\n \"size\": 11\n}\n")

tempDir := t.TempDir()
fileName := "test.txt"
Expand All @@ -58,11 +47,11 @@ func TestPretty_Output(t *testing.T) {
}
defer fp.Close()

// outputs unprettified content
// test unprettified content
opts := Pretty{
pretty: false,
}
err = opts.Output(fp, want)
err = opts.Output(fp, raw)
if err != nil {
t.Fatal("Pretty.Output() error =", err)
}
Expand All @@ -73,8 +62,8 @@ func TestPretty_Output(t *testing.T) {
if err != nil {
t.Fatal("error calling io.ReadAll(), error =", err)
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("Pretty.Output() got %v, want %v", got, want)
if !reflect.DeepEqual(got, raw) {
t.Fatalf("Pretty.Output() got %v, want %v", got, raw)
}

// remove all content in the file
Expand All @@ -85,11 +74,11 @@ func TestPretty_Output(t *testing.T) {
t.Fatal("error calling File.Seek(), error =", err)
}

// outputs prettified content
// test prettified content
opts = Pretty{
pretty: true,
}
err = opts.Output(fp, want)
err = opts.Output(fp, raw)
if err != nil {
t.Fatal("Pretty.Output() error =", err)
}
Expand All @@ -100,7 +89,8 @@ func TestPretty_Output(t *testing.T) {
if err != nil {
t.Fatal("error calling io.ReadAll(), error =", err)
}
if reflect.DeepEqual(got, want) {

if !reflect.DeepEqual(got, prettified) {
t.Fatalf("Pretty.Output() failed to prettified the content: %v", got)
}
}
109 changes: 109 additions & 0 deletions test/e2e/suite/command/blob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
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 (
"os"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
. "oras.land/oras/test/e2e/internal/utils"
)

var _ = Describe("ORAS beginners:", func() {
When("running blob command", func() {
runAndShowPreviewInHelp([]string{"blob"})

When("running `blob fetch`", func() {
runAndShowPreviewInHelp([]string{"blob", "fetch"}, preview_desc, example_desc)

It("should call sub-commands with aliases", func() {
ORAS("blob", "get", "--help").
MatchKeyWords("[Preview] Fetch", preview_desc, example_desc).
Exec()
})
It("should have flag for prettifying JSON output", func() {
ORAS("blob", "get", "--help").
MatchKeyWords("--pretty", "prettify JSON").
Exec()
})

It("should fail if neither output path nor descriptor flag are not provided", func() {
ORAS("blob", "fetch", Reference(Host, repo, "sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae")).
WithFailureCheck().Exec()
})

It("should fail if no digest provided", func() {
ORAS("blob", "fetch", Reference(Host, repo, "")).
WithFailureCheck().Exec()
})

It("should fail if provided digest doesn't existed", func() {
ORAS("blob", "fetch", Reference(Host, repo, "sha256:2aaa2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a")).
WithFailureCheck().Exec()
})

It("should fail if output path points to stdout and descriptor flag is provided", func() {
ORAS("blob", "fetch", Reference(Host, repo, ""), "--descriptor", "--output", "-").
WithFailureCheck().Exec()
})

It("should fail if no reference is provided", func() {
ORAS("blob", "fetch").WithFailureCheck().Exec()
})
})
})
})

var _ = Describe("Common registry users:", func() {
var blobDigest = "sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
var blobContent = "foo"
var blobDescriptor = `{"mediaType":"application/octet-stream","digest":"sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae","size":3}`

When("running `blob fetch`", func() {
It("should fetch blob descriptor ", func() {
ORAS("blob", "fetch", Reference(Host, repo, blobDigest), "--descriptor").
MatchContent(blobDescriptor).Exec()
})
It("should fetch blob content and output to stdout", func() {
ORAS("blob", "fetch", Reference(Host, repo, blobDigest), "--output", "-").
MatchContent(blobContent).Exec()
})
It("should fetch blob content and output to a file", func() {
tempDir := GinkgoT().TempDir()
contentPath := filepath.Join(tempDir, "fetched")
ORAS("blob", "fetch", Reference(Host, repo, blobDigest), "--output", contentPath).
WithWorkDir(tempDir).Exec()
Expect(contentPath).Should(BeAnExistingFile())
f, err := os.Open(contentPath)
Expect(err).ShouldNot(HaveOccurred())
defer f.Close()
Eventually(gbytes.BufferReader(f)).Should(gbytes.Say(blobContent))
})
It("should fetch blob descriptor and output content to a file", func() {
tempDir := GinkgoT().TempDir()
contentPath := filepath.Join(tempDir, "fetched")
ORAS("blob", "fetch", Reference(Host, repo, blobDigest), "--output", contentPath, "--descriptor").
MatchContent(blobDescriptor).
WithWorkDir(tempDir).Exec()
Expect(contentPath).Should(BeAnExistingFile())
f, err := os.Open(contentPath)
Expect(err).ShouldNot(HaveOccurred())
defer f.Close()
Eventually(gbytes.BufferReader(f)).Should(gbytes.Say(blobContent))
})
})
})

0 comments on commit 106d51a

Please sign in to comment.