Skip to content

Commit

Permalink
fix: fix unit tests that fail in local environment (#1292)
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin-li authored Feb 1, 2024
1 parent 5225247 commit 45c0f81
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 96 deletions.
23 changes: 4 additions & 19 deletions pkg/controllers/store_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package controllers
import (
"context"
"os"
"path/filepath"
"strings"
"testing"

configv1beta1 "github.com/deislabs/ratify/api/v1beta1"
"github.com/deislabs/ratify/pkg/referrerstore"
"github.com/deislabs/ratify/pkg/utils"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -33,7 +33,7 @@ const sampleName = "sample"

func TestStoreAdd_EmptyParameter(t *testing.T) {
resetStoreMap()
dirPath, err := createPlugin(sampleName)
dirPath, err := utils.CreatePlugin(sampleName)
if err != nil {
t.Fatalf("createPlugin() expected no error, actual %v", err)
}
Expand All @@ -57,7 +57,7 @@ func TestStoreAdd_WithParameters(t *testing.T) {
if len(StoreMap) != 0 {
t.Fatalf("Store map expected size 0, actual %v", len(StoreMap))
}
dirPath, err := createPlugin(sampleName)
dirPath, err := utils.CreatePlugin(sampleName)
if err != nil {
t.Fatalf("createPlugin() expected no error, actual %v", err)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestStoreAddOrReplace_PluginNotFound(t *testing.T) {

func TestStore_UpdateAndDelete(t *testing.T) {
resetStoreMap()
dirPath, err := createPlugin(sampleName)
dirPath, err := utils.CreatePlugin(sampleName)
if err != nil {
t.Fatalf("createPlugin() expected no error, actual %v", err)
}
Expand Down Expand Up @@ -186,18 +186,3 @@ func getInvalidStoreSpec() configv1beta1.StoreSpec {
Address: "test/path",
}
}

func createPlugin(pluginName string) (string, error) {
tempDir, err := os.MkdirTemp("", "directory")
if err != nil {
return "", err
}

fullName := filepath.Join(tempDir, pluginName)
file, err := os.Create(fullName)
if err != nil {
return "", err
}
defer file.Close()
return tempDir, nil
}
6 changes: 3 additions & 3 deletions pkg/controllers/verifier_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestMain(m *testing.M) {

func TestVerifierAdd_EmptyParameter(t *testing.T) {
resetVerifierMap()
dirPath, err := createPlugin(sampleName)
dirPath, err := utils.CreatePlugin(sampleName)
if err != nil {
t.Fatalf("createPlugin() expected no error, actual %v", err)
}
Expand All @@ -67,7 +67,7 @@ func TestVerifierAdd_WithParameters(t *testing.T) {
t.Fatalf("Verifier map expected size 0, actual %v", len(VerifierMap))
}

dirPath, err := createPlugin(licenseChecker)
dirPath, err := utils.CreatePlugin(licenseChecker)
if err != nil {
t.Fatalf("createPlugin() expected no error, actual %v", err)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestVerifierAddOrReplace_PluginNotFound(t *testing.T) {

func TestVerifier_UpdateAndDelete(t *testing.T) {
resetVerifierMap()
dirPath, err := createPlugin(licenseChecker)
dirPath, err := utils.CreatePlugin(licenseChecker)
if err != nil {
t.Fatalf("createPlugin() expected no error, actual %v", err)
}
Expand Down
46 changes: 32 additions & 14 deletions pkg/referrerstore/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ import (
"errors"
"os"
"path"
"path/filepath"
"testing"

"github.com/deislabs/ratify/pkg/featureflag"
"github.com/deislabs/ratify/pkg/referrerstore"
"github.com/deislabs/ratify/pkg/referrerstore/config"
"github.com/deislabs/ratify/pkg/referrerstore/mocks"
"github.com/deislabs/ratify/pkg/referrerstore/plugin"
"github.com/deislabs/ratify/pkg/utils"
)

const (
testStore = "testStore"
sampleName = "sample"
pluginStoreName = "plugin-store"
)

type TestStoreFactory struct{}
Expand All @@ -36,18 +42,24 @@ func (f *TestStoreFactory) Create(_ string, _ config.StorePluginConfig) (referre
}

func TestCreateStoresFromConfig_BuiltInStores_ReturnsExpected(t *testing.T) {
dirPath, err := utils.CreatePlugin(testStore)
if err != nil {
t.Fatalf("createPlugin() expected no error, actual %v", err)
}
defer os.RemoveAll(dirPath)

builtInStores = map[string]StoreFactory{
"testStore": &TestStoreFactory{},
testStore: &TestStoreFactory{},
}

storeConfig := map[string]interface{}{
"name": "testStore",
"name": testStore,
}
storesConfig := config.StoresConfig{
Stores: []config.StorePluginConfig{storeConfig},
}

stores, err := CreateStoresFromConfig(storesConfig, getReferrerstorePluginsDir())
stores, err := CreateStoresFromConfig(storesConfig, dirPath)

if err != nil {
t.Fatalf("create stores failed with err %v", err)
Expand All @@ -67,14 +79,20 @@ func TestCreateStoresFromConfig_BuiltInStores_ReturnsExpected(t *testing.T) {
}

func TestCreateStoresFromConfig_PluginStores_ReturnsExpected(t *testing.T) {
dirPath, err := utils.CreatePlugin(sampleName)
if err != nil {
t.Fatalf("createPlugin() expected no error, actual %v", err)
}
defer os.RemoveAll(dirPath)

storeConfig := map[string]interface{}{
"name": "sample",
"name": sampleName,
}
storesConfig := config.StoresConfig{
Stores: []config.StorePluginConfig{storeConfig},
}

stores, err := CreateStoresFromConfig(storesConfig, getReferrerstorePluginsDir())
stores, err := CreateStoresFromConfig(storesConfig, dirPath)

if err != nil {
t.Fatalf("create stores failed with err %v", err)
Expand Down Expand Up @@ -114,16 +132,22 @@ func TestCreateStoresFromConfig_DynamicPluginStores_ReturnsExpected(t *testing.T
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
storeConfig := map[string]interface{}{
"name": "plugin-store",
"name": pluginStoreName,
"source": map[string]interface{}{
"artifact": tc.artifact,
},
}

dirPath, err := utils.CreatePlugin(pluginStoreName)
if err != nil {
t.Fatalf("createPlugin() expected no error, actual %v", err)
}
defer os.RemoveAll(dirPath)

storesConfig := config.StoresConfig{
Stores: []config.StorePluginConfig{storeConfig},
}
stores, err := CreateStoresFromConfig(storesConfig, getReferrerstorePluginsDir())
stores, err := CreateStoresFromConfig(storesConfig, dirPath)

if err != nil {
t.Fatalf("create stores failed with err %v", err)
Expand All @@ -148,9 +172,3 @@ func TestCreateStoresFromConfig_DynamicPluginStores_ReturnsExpected(t *testing.T
})
}
}

func getReferrerstorePluginsDir() string {
workingDir, _ := os.Getwd()
pluginDir := filepath.Clean(filepath.Join(workingDir, "../../../", "./bin/plugins/referrerstore/"))
return pluginDir
}
60 changes: 35 additions & 25 deletions pkg/referrerstore/plugin/skel/skel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

Expand All @@ -28,13 +27,31 @@ import (
"github.com/deislabs/ratify/pkg/referrerstore"
"github.com/deislabs/ratify/pkg/referrerstore/plugin"
"github.com/deislabs/ratify/pkg/referrerstore/types"
"github.com/deislabs/ratify/pkg/utils"
"github.com/opencontainers/go-digest"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

var (
testStdinData = fmt.Sprintf(`{ "name":"skel-test-case", "some": "config","pluginBinDirs": ["%s"]}`, getPluginsDir())
)
const skelPluginName = "skel-test-case"

var dirPath string
var testStdinData string

func TestMain(m *testing.M) {
setup()
code := m.Run()
teardown()
os.Exit(code)
}

func setup() {
dirPath, _ = utils.CreatePlugin(skelPluginName)
testStdinData = fmt.Sprintf(`{ "name":"skel-test-case", "some": "config","pluginBinDirs": ["%s"]}`, dirPath)
}

func teardown() {
os.RemoveAll(dirPath)
}

func TestPluginMain_GetBlobContent_ReturnsExpected(t *testing.T) {
getBlobContent := func(args *CmdArgs, subjectReference common.Reference, digest digest.Digest) ([]byte, error) {
Expand All @@ -57,8 +74,7 @@ func TestPluginMain_GetBlobContent_ReturnsExpected(t *testing.T) {
Stderr: stderr,
}

err := pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
if err != nil {
if err := pluginContext.pluginMainCore("", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"}); err != nil {
t.Fatalf("plugin execution failed %v", err)
}

Expand Down Expand Up @@ -92,7 +108,7 @@ func TestPluginMain_GetReferenceManifest_ReturnsExpected(t *testing.T) {
Stderr: stderr,
}

err := pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, nil, getReferenceManifest, nil, []string{"1.0.0"})
err := pluginContext.pluginMainCore("", "1.0.0", nil, nil, getReferenceManifest, nil, []string{"1.0.0"})
if err != nil {
t.Fatalf("plugin execution failed %v", err)
}
Expand Down Expand Up @@ -132,7 +148,7 @@ func TestPluginMain_ListReferrers_ReturnsExpected(t *testing.T) {
Stderr: stderr,
}

err := pluginContext.pluginMainCore("skel-test-case", "1.0.0", listReferrers, nil, nil, nil, []string{"1.0.0"})
err := pluginContext.pluginMainCore("", "1.0.0", listReferrers, nil, nil, nil, []string{"1.0.0"})
if err != nil {
t.Fatalf("plugin execution failed %v", err)
}
Expand Down Expand Up @@ -165,7 +181,7 @@ func TestPluginMain_GetSubjectDesc_ReturnsExpected(t *testing.T) {
Stderr: stderr,
}

err := pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, nil, nil, getSubjectDesc, []string{"1.0.0"})
err := pluginContext.pluginMainCore("", "1.0.0", nil, nil, nil, getSubjectDesc, []string{"1.0.0"})
if err != nil {
t.Fatalf("plugin execution failed %v", err)
}
Expand Down Expand Up @@ -196,22 +212,22 @@ func TestPluginMain_ErrorCases(t *testing.T) {
Stderr: stderr,
}

err := pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
err := pluginContext.pluginMainCore("", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
if err == nil || err.Code != types.ErrMissingEnvironmentVariables {
t.Fatalf("plugin execution expected to fail with error code %d", types.ErrMissingEnvironmentVariables)
}

environment[plugin.VersionEnvKey] = "1.0.0"
environment[plugin.SubjectEnvKey] = "localhost&300"

err = pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
err = pluginContext.pluginMainCore("", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
if err == nil || err.Code != types.ErrArgsParsingFailure {
t.Fatalf("plugin execution expected to fail with error code %d for invalid subject", types.ErrArgsParsingFailure)
}

environment[plugin.SubjectEnvKey] = "localhost:5000/net-monitor:v1@sha256:a0fc570a245b09ed752c42d600ee3bb5b4f77bbd70d8898780b7ab43454530eb"
environment[plugin.VersionEnvKey] = "2.0.0"
err = pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
err = pluginContext.pluginMainCore("", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
if err == nil || err.Code != types.ErrVersionNotSupported {
t.Fatalf("plugin execution expected to fail with error code %d for unsupported version", types.ErrVersionNotSupported)
}
Expand All @@ -220,30 +236,30 @@ func TestPluginMain_ErrorCases(t *testing.T) {

stdinData = ` "name":"skel-test-case", "some": "config" }`
pluginContext.Stdin = strings.NewReader(stdinData)
err = pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
err = pluginContext.pluginMainCore("", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
if err == nil || err.Code != types.ErrConfigParsingFailure {
t.Fatalf("plugin execution expected to fail with error code %d for invalid config, actual error: %s", types.ErrConfigParsingFailure, err)
}

stdinData = ` {"some": "config" }`
pluginContext.Stdin = strings.NewReader(stdinData)
err = pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
err = pluginContext.pluginMainCore("", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
if err == nil || err.Code != types.ErrInvalidStoreConfig {
t.Fatalf("plugin execution expected to fail with error code %d for missing store name", types.ErrInvalidStoreConfig)
}

environment[plugin.CommandEnvKey] = "unknown"
stdinData = testStdinData
pluginContext.Stdin = strings.NewReader(stdinData)
err = pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
err = pluginContext.pluginMainCore("", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
if err == nil || err.Code != types.ErrUnknownCommand {
t.Fatalf("plugin execution expected to fail with error code %d for invalid command", types.ErrUnknownCommand)
}

environment[plugin.CommandEnvKey] = plugin.GetBlobContentCommand
stdinData = testStdinData
pluginContext.Stdin = strings.NewReader(stdinData)
err = pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
err = pluginContext.pluginMainCore("", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
if err == nil || err.Code != types.ErrPluginCmdFailure {
t.Fatalf("plugin execution expected to fail with error code %d for cmd failure", types.ErrPluginCmdFailure)
}
Expand All @@ -270,15 +286,15 @@ func TestPluginMain_GetBlobContent_ErrorCases(t *testing.T) {
Stderr: stderr,
}

err := pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
err := pluginContext.pluginMainCore("", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
if err == nil || err.Code != types.ErrArgsParsingFailure {
t.Fatalf("plugin execution expected to fail with error code %d for invalid arg", types.ErrArgsParsingFailure)
}

stdinData = testStdinData
pluginContext.Stdin = strings.NewReader(stdinData)
environment[plugin.ArgsEnvKey] = "digest=sha256a0fc570a245b09ed752c42d600ee3bb5b4f77bbd70d8898780b7ab43454530eb"
err = pluginContext.pluginMainCore("skel-test-case", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
err = pluginContext.pluginMainCore("", "1.0.0", nil, getBlobContent, nil, nil, []string{"1.0.0"})
if err == nil || err.Code != types.ErrArgsParsingFailure {
t.Fatalf("plugin execution expected to fail with error code %d for invalid digest", types.ErrArgsParsingFailure)
}
Expand Down Expand Up @@ -313,14 +329,8 @@ func TestPluginMain_ListReferrers_ErrorCases(t *testing.T) {
Stderr: stderr,
}

err := pluginContext.pluginMainCore("skel-test-case", "1.0.0", listReferrers, nil, nil, nil, []string{"1.0.0"})
err := pluginContext.pluginMainCore("", "1.0.0", listReferrers, nil, nil, nil, []string{"1.0.0"})
if err == nil || err.Code != types.ErrArgsParsingFailure {
t.Fatalf("plugin execution expected to fail with error code %d for invalid arg", types.ErrArgsParsingFailure)
}
}

func getPluginsDir() string {
workingDir, _ := os.Getwd()
pluginDir := filepath.Clean(filepath.Join(workingDir, "../../../../", "./bin/plugins/referrerstore/"))
return pluginDir
}
Loading

0 comments on commit 45c0f81

Please sign in to comment.