Skip to content

Commit

Permalink
test: update to use oras-go copy
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
  • Loading branch information
JeyJeyGao committed Jan 4, 2023
1 parent db9d3b4 commit 38872f6
Show file tree
Hide file tree
Showing 20 changed files with 331 additions and 219 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test: vendor check-line-endings ## run unit tests
e2e: build ## build notation cli and run e2e test
NOTATION_BIN_PATH=`pwd`/bin/$(COMMANDS); \
cd ./test/e2e; \
./run.sh $$NOTATION_BIN_PATH
./run.sh zot $$NOTATION_BIN_PATH

.PHONY: clean
clean:
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ go 1.19
require (
github.com/onsi/ginkgo/v2 v2.3.0
github.com/onsi/gomega v1.22.1
oras.land/oras-go/v2 v2.0.0-rc.5
oras.land/oras-go/v2 v2.0.0-rc.6
)

require (
github.com/google/go-cmp v0.5.8 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
11 changes: 4 additions & 7 deletions test/e2e/go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/onsi/ginkgo/v2 v2.3.0 h1:kUMoxMoQG3ogk/QWyKh3zibV7BKZ+xBpWil1cTylVqc=
github.com/onsi/ginkgo/v2 v2.3.0/go.mod h1:Eew0uilEqZmIEZr8JrvYlvOM7Rr6xzTmMV8AyFNU9d0=
github.com/onsi/gomega v1.22.1 h1:pY8O4lBfsHKZHM/6nrxkhVPUznOlIu3quZcKP/M20KI=
Expand All @@ -13,17 +11,16 @@ github.com/opencontainers/image-spec v1.1.0-rc2 h1:2zx/Stx4Wc5pIPDvIxHXvXtQFW/7X
github.com/opencontainers/image-spec v1.1.0-rc2/go.mod h1:3OVijpioIKYWTqjiG0zfF6wvoJ4fAXGbjdZuI2NgsRQ=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
oras.land/oras-go/v2 v2.0.0-rc.5 h1:enT2ZMNo383bH3INm1/+mw4d09AaMbqx0BMhsgEDUSg=
oras.land/oras-go/v2 v2.0.0-rc.5/go.mod h1:YGHvWBGuqRlZgUyXUIoKsR3lcuCOb3DAtG0SEsEw1iY=
oras.land/oras-go/v2 v2.0.0-rc.6 h1:jGWysqm8flq+X0Vj8bZ6rkASAqTab5k18Mx9hEjFc8g=
oras.land/oras-go/v2 v2.0.0-rc.6/go.mod h1:iVExH1NxrccIxjsiq17L91WCZ4KIw6jVQyCLsZsu1gc=
17 changes: 8 additions & 9 deletions test/e2e/internal/notation/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func copyDir(src, dst string) error {
dstPath := filepath.Join(dst, relPath)

if d.IsDir() {
return os.MkdirAll(dstPath, os.ModePerm)
return os.MkdirAll(dstPath, 0731)
}
return copyFile(srcPath, dstPath)
})
Expand All @@ -36,6 +36,11 @@ func copyFile(src, dst string) error {
}
defer in.Close()

si, err := in.Stat()
if err != nil {
return err
}

out, err := os.Create(dst)
if err != nil {
return err
Expand All @@ -49,20 +54,14 @@ func copyFile(src, dst string) error {
if err := out.Sync(); err != nil {
return err
}

si, err := in.Stat()
if err != nil {
return err
}
return out.Chmod(si.Mode())
}

// saveJSON marshals the data and save to the given path.
func saveJSON(data any, path string) error {
b, err := json.Marshal(data)
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}

return os.WriteFile(path, b, 0644)
return json.NewEncoder(f).Encode(data)
}
75 changes: 50 additions & 25 deletions test/e2e/internal/notation/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,78 @@ import (
"github.com/notaryproject/notation/test/e2e/internal/utils"
)

// CoreTestFunc is the test function running in a VirtualHost.
//
// notation is an Executor isolated by $XDG_CONFIG_HOME.
// artifact is a generated artifact in a new repository.
// vhost is the VirtualHost instance.
type CoreTestFunc func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost)

// Host creates a virtualized notation testing host by modify
// the "XDG_CONFIG_HOME" environment variable of the Executor.
//
// options is the required testing environment options
// fn is the callback function containing the testing logic.
func Host(options []utils.HostOption, fn func(notation *utils.ExecOpts, artifact *Artifact, vhost *utils.VirtualHost)) {
// create a vhost
vhost, err := utils.NewVirtualHost(NotationBinPath, CreateNotationDirOption())
func Host(options []utils.HostOption, fn CoreTestFunc) {
// create a notation vhost
vhost, err := createNotationHost(NotationBinPath, options...)
if err != nil {
panic(err)
}

// set additional options
vhost.SetOption(options...)

// generate a repository with an artifact
artifact := GenerateArtifact()
artifact := GenerateArtifact("", "")

// run the main logic
fn(vhost.Executor, artifact, vhost)
}

// OldNotation create an old version notation ExecOpts in a VirtualHost
// for testing forward compatibility.
func OldNotation(options ...utils.HostOption) *utils.ExecOpts {
if len(options) == 0 {
options = BaseOptions()
}

// remove the generated repository and artifact
if err := artifact.Remove(); err != nil {
vhost, err := createNotationHost(NotationOldBinPath, options...)
if err != nil {
panic(err)
}

return vhost.Executor
}

// Opts is a grammar sugar to generate a list of HostOption
func createNotationHost(path string, options ...utils.HostOption) (*utils.VirtualHost, error) {
vhost, err := utils.NewVirtualHost(path, CreateNotationDirOption())
if err != nil {
return nil, err
}

// set additional options
vhost.SetOption(options...)
return vhost, nil
}

// Opts is a grammar sugar to generate a list of HostOption.
func Opts(options ...utils.HostOption) []utils.HostOption {
return options
}

// BaseOptions returns a list of base Options for a valid notation
// BaseOptions returns a list of base Options for a valid notation.
// testing environment.
func BaseOptions() []utils.HostOption {
return Opts(
AuthOption("", ""),
AddTestKeyOption(),
AddTestTrustStoreOption(),
AddKeyOption("e2e.key", "e2e.crt"),
AddTrustStoreOption("e2e", filepath.Join(NotationE2ELocalKeysDir, "e2e.crt")),
AddTrustPolicyOption("trustpolicy.json"),
)
}

// CreateNotationDirOption creates the notation directory in temp user dir.
func CreateNotationDirOption() utils.HostOption {
return func(vhost *utils.VirtualHost) error {
return os.MkdirAll(vhost.UserPath(NotationDirName), os.ModePerm)
return os.MkdirAll(vhost.AbsolutePath(NotationDirName), os.ModePerm)
}
}

Expand All @@ -71,19 +96,19 @@ func AuthOption(username, password string) utils.HostOption {
}
}

// AddTestKeyOption adds the test signingkeys.json, key and cert files to
// AddKeyOption adds the test signingkeys.json, key and cert files to
// the notation directory.
func AddTestKeyOption() utils.HostOption {
func AddKeyOption(keyName, certName string) utils.HostOption {
return func(vhost *utils.VirtualHost) error {
return AddTestKeyPairs(vhost.UserPath(NotationDirName))
return AddTestKeyPairs(vhost.AbsolutePath(NotationDirName), keyName, certName)
}
}

// AddTestTrustStoreOption added the test cert to the trust store.
func AddTestTrustStoreOption() utils.HostOption {
// AddTrustStoreOption added the test cert to the trust store.
func AddTrustStoreOption(namedstore string, srcCertPath string) utils.HostOption {
return func(vhost *utils.VirtualHost) error {
vhost.Executor.
Exec("cert", "add", "--type", "ca", "--store", "e2e", NotationE2ECertPath).
Exec("cert", "add", "--type", "ca", "--store", namedstore, srcCertPath).
MatchKeyWords("Successfully added following certificates")
return nil
}
Expand All @@ -94,16 +119,16 @@ func AddTrustPolicyOption(trustpolicyName string) utils.HostOption {
return func(vhost *utils.VirtualHost) error {
return copyFile(
filepath.Join(NotationE2ETrustPolicyDir, trustpolicyName),
vhost.UserPath(NotationDirName, NotationTrustPolicyName),
vhost.AbsolutePath(NotationDirName, TrustPolicyName),
)
}
}

// authEnv creates an auth info
// (By setting $NOTATION_USERNAME and $NOTATION_PASSWORD)
func authEnv(username, password string) map[string]string {
env := make(map[string]string)
env["NOTATION_USERNAME"] = username
env["NOTATION_PASSWORD"] = password
return env
return map[string]string{
"NOTATION_USERNAME": username,
"NOTATION_PASSWORD": password,
}
}
73 changes: 35 additions & 38 deletions test/e2e/internal/notation/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,38 @@ import (
)

const (
NotationDirName = "notation"
NotationTrustPolicyName = "trustpolicy.json"
NotationDirName = "notation"
TrustPolicyName = "trustpolicy.json"
TrustStoreDirName = "truststore"
TrustStoreTypeCA = "ca"
)

const (
envRegistryHost = "NOTATION_E2E_REGISTRY_HOST"
envRegistryUsername = "NOTATION_E2E_REGISTRY_USERNAME"
envRegistryPassword = "NOTATION_E2E_REGISTRY_PASSWORD"
envNotationBinPath = "NOTATION_E2E_BINARY_PATH"
envNotationE2EKeyPath = "NOTATION_E2E_KEY_PATH"
envNotationE2ECertPath = "NOTATION_E2E_CERT_PATH"
envNotationConfigPath = "NOTATION_E2E_CONFIG_PATH"
envOCILayoutPath = "NOTATION_E2E_OCI_LAYOUT_PATH"
envTestRepo = "NOTATION_E2E_TEST_REPO"
envTestTag = "NOTATION_E2E_TEST_TAG"
envRegistryStoragePath = "REGISTRY_STORAGE_PATH"
envKeyRegistryHost = "NOTATION_E2E_REGISTRY_HOST"
envKeyRegistryUsername = "NOTATION_E2E_REGISTRY_USERNAME"
envKeyRegistryPassword = "NOTATION_E2E_REGISTRY_PASSWORD"
envKeyNotationBinPath = "NOTATION_E2E_BINARY_PATH"
envKeyNotationOldBinPath = "NOTATION_E2E_OLD_BINARY_PATH"
envKeyNotationConfigPath = "NOTATION_E2E_CONFIG_PATH"
envKeyOCILayoutPath = "NOTATION_E2E_OCI_LAYOUT_PATH"
envKeyTestRepo = "NOTATION_E2E_TEST_REPO"
envKeyTestTag = "NOTATION_E2E_TEST_TAG"
)

var (
NotationBinPath string
NotationE2EKeyPath string
NotationE2ECertPath string
// NotationBinPath is the notation binary path.
NotationBinPath string
// NotationOldBinPath is the path of an old version notation binary for
// testing forward compatibility.
NotationOldBinPath string
NotationE2EConfigPath string
NotationE2ELocalKeysDir string
NotationE2ETrustPolicyDir string
)

var (
OCILayoutPath string
TestRepo string
TestRepoUri string
TestTag string
RegistryStoragePath string
)
Expand All @@ -50,41 +53,35 @@ func init() {
}

func setUpRegistry() {
setValue(envRegistryHost, &TestRegistry.Host)
fmt.Printf("Testing using registry host: %s\n", TestRegistry.Host)
setValue(envKeyRegistryHost, &TestRegistry.Host)
setValue(envKeyRegistryUsername, &TestRegistry.Username)
setValue(envKeyRegistryPassword, &TestRegistry.Password)

setValue(envRegistryUsername, &TestRegistry.Username)
fmt.Printf("Testing using registry username: %s\n", TestRegistry.Username)

setValue(envRegistryPassword, &TestRegistry.Password)
fmt.Printf("Testing using registry password: %s\n", TestRegistry.Password)
setPathValue(envKeyOCILayoutPath, &OCILayoutPath)
setValue(envKeyTestRepo, &TestRepoUri)
setValue(envKeyTestTag, &TestTag)

testImage := &Artifact{
Registry: &TestRegistry,
Repo: testRepo,
Tag: testTag,
Repo: TestRepoUri,
Tag: TestTag,
}

if err := testImage.Validate(); err != nil {
panic(fmt.Sprintf("E2E setup failed: %v", err))
panic(fmt.Sprintf("registry setup failed: %v", err))
}
}

func setUpNotationValues() {
// set Notation binary path
setPathValue(envNotationBinPath, &NotationBinPath)
setPathValue(envKeyNotationBinPath, &NotationBinPath)
setPathValue(envKeyNotationOldBinPath, &NotationOldBinPath)

// set Notation configuration paths
setPathValue(envNotationE2EKeyPath, &NotationE2EKeyPath)
setPathValue(envNotationE2ECertPath, &NotationE2ECertPath)
setPathValue(envNotationConfigPath, &NotationE2EConfigPath)
setPathValue(envKeyNotationConfigPath, &NotationE2EConfigPath)
NotationE2ETrustPolicyDir = filepath.Join(NotationE2EConfigPath, "trustpolicys")
NotationE2ELocalKeysDir = filepath.Join(NotationE2EConfigPath, LocalKeysDirName)

// set registry values
setPathValue(envRegistryStoragePath, &RegistryStoragePath)
setPathValue(envOCILayoutPath, &OCILayoutPath)
setValue(envTestRepo, &TestRepo)
setValue(envTestTag, &TestTag)
}

func setPathValue(envKey string, value *string) {
Expand All @@ -95,8 +92,8 @@ func setPathValue(envKey string, value *string) {
}

func setValue(envKey string, value *string) {
*value = os.Getenv(envKey)
if *value == "" {
if *value = os.Getenv(envKey); *value == "" {
panic(fmt.Sprintf("env %s is empty", envKey))
}
fmt.Printf("set test value $%s=%s\n", envKey, *value)
}
Loading

0 comments on commit 38872f6

Please sign in to comment.