Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add utility for file matching #701

Merged
merged 8 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion test/e2e/internal/utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
Username = "hello"
Password = "oras-test"
AuthConfigPath = "test.config"
DefaultTimeout = 10 * time.Second
)

// ExecOption provides option used to execute a command.
Expand Down Expand Up @@ -61,7 +62,7 @@ func Binary(path string, args ...string) *ExecOption {
return &ExecOption{
binary: path,
args: args,
timeout: 10 * time.Second,
timeout: DefaultTimeout,
shouldFail: false,
}
}
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/internal/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (
"io/fs"
"os"
"path/filepath"
"regexp"
"time"

. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
)

var testFileRoot string
Expand Down Expand Up @@ -48,6 +53,16 @@ func CopyTestData(dstRoot string) error {
})
}

// MatchFile reads content from filepath, matches it with want with timeout.
func MatchFile(filepath string, want string, timeout time.Duration) {
Expect(filepath).To(BeAnExistingFile())
f, err := os.Open(filepath)
Expect(err).ToNot(HaveOccurred())
defer f.Close()
want = regexp.QuoteMeta(want)
Eventually(gbytes.BufferReader(f)).WithTimeout(timeout).Should(gbytes.Say(want))
}

func copyFile(srcFile, dstFile string) error {
to, err := os.Create(dstFile)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/suite/command/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ var _ = Describe("Common registry users:", func() {
fetchPath := filepath.Join(GinkgoT().TempDir(), "fetchedImage")
ORAS("manifest", "fetch", Reference(Host, repo, multiImage), "--output", fetchPath, "--descriptor").
MatchContent(descriptor_multi).Exec()
Binary("cat", fetchPath).
MatchContent(manifest_multi).Exec()
MatchFile(fetchPath, manifest_multi, DefaultTimeout)
})

It("should fetch manifest via tag with platform selection", func() {
Expand Down
7 changes: 3 additions & 4 deletions test/e2e/suite/command/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package command
import (
"bytes"
"fmt"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -84,10 +85,8 @@ var _ = Describe("Remote registry users:", func() {
ORAS("push", Reference(Host, repo, tag), files[1]+":"+layerType, "-v", "--export-manifest", exportPath).
MatchStatus(statusKeys, true, 2).
WithWorkDir(tempDir).Exec()
fetched := ORAS("manifest", "fetch", Reference(Host, repo, tag)).Exec().Out
Binary("cat", exportPath).
WithWorkDir(tempDir).
MatchTrimmedContent(string(fetched.Contents())).Exec()
fetched := ORAS("manifest", "fetch", Reference(Host, repo, tag)).Exec().Out.Contents()
MatchFile(filepath.Join(tempDir, exportPath), string(fetched), DefaultTimeout)
})

It("should push files with customized config file", func() {
Expand Down
7 changes: 3 additions & 4 deletions test/e2e/suite/scenario/oci_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ var _ = Describe("OCI image user:", Ordered, func() {
WithWorkDir(tempDir).
WithDescription("push files with manifest exported").Exec()

session := Binary("cat", manifestName).WithWorkDir(tempDir).Exec()
ORAS("manifest", "fetch", Reference(Host, repo, tag)).
MatchContent(string(session.Out.Contents())).
WithDescription("fetch pushed manifest content").Exec()
fetched := ORAS("manifest", "fetch", Reference(Host, repo, tag)).
WithDescription("fetch pushed manifest content").Exec().Out.Contents()
MatchFile(filepath.Join(tempDir, manifestName), string(fetched), DefaultTimeout)

pullRoot := "pulled"
ORAS("pull", Reference(Host, repo, tag), "-v", "--config", files[0], "-o", pullRoot).
Expand Down