Add NewTestBundle function to testutils package

This commit adds small helper function in testutils module, that would
help with testing modules that relay on bundle interface. The function
will take directory with testdata as a parameter, and load all files in
it into its fakefile system, allowing bundle to get those files and
render required yaml/kustomize files storing them as airship documents
in the returned bundle.

Change-Id: Ib55180cc720c42cab77626601d9ec0d6151b8454
This commit is contained in:
Kostiantyn Kalynovskyi 2019-10-30 12:35:40 -05:00 committed by Kostyantyn Kalynovskyi
parent 3bf54274c3
commit 3ca3a34fbf
2 changed files with 17 additions and 23 deletions

View File

@ -9,18 +9,12 @@ import (
"sigs.k8s.io/kustomize/v3/pkg/gvk"
"sigs.k8s.io/kustomize/v3/pkg/types"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/testutil"
)
func TestNewBundle(t *testing.T) {
fSys := testutil.SetupTestFs(t, "testdata")
bundle, err := document.NewBundle(fSys, "/", "/")
if err != nil {
t.Fatalf("Building Bundle Failed: %v", err)
}
bundle := testutil.NewTestBundle(t, "testdata")
require := require.New(t)
require.NotNil(bundle)
@ -28,12 +22,7 @@ func TestNewBundle(t *testing.T) {
func TestBundleDocumentFiltering(t *testing.T) {
fSys := testutil.SetupTestFs(t, "testdata")
bundle, err := document.NewBundle(fSys, "/", "/")
if err != nil {
t.Fatalf("Building Bundle Failed: %v", err)
}
bundle := testutil.NewTestBundle(t, "testdata")
assert := assert.New(t)
t.Run("GetKustomizeResourceMap", func(t *testing.T) {

View File

@ -5,7 +5,10 @@ import (
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/v3/pkg/fs"
"opendev.org/airship/airshipctl/pkg/document"
)
// SetupTestFs help manufacture a fake file system for testing purposes. It
@ -13,26 +16,28 @@ import (
// to the tests themselves, and will write each of those files (preserving
// names) to an in-memory file system and return that fs
func SetupTestFs(t *testing.T, fixtureDir string) fs.FileSystem {
t.Helper()
x := fs.MakeFakeFS()
files, err := ioutil.ReadDir(fixtureDir)
if err != nil {
t.Fatalf("Unable to read fixture directory %s: %v", fixtureDir, err)
}
require.NoError(t, err, "Failed to read fixture directory, setting up testfs failed")
for _, file := range files {
fileName := file.Name()
filePath := filepath.Join(fixtureDir, fileName)
fileBytes, err := ioutil.ReadFile(filePath)
if err != nil {
t.Fatalf("Error reading fixture %s: %v", filePath, err)
}
// nolint: errcheck
require.NoError(t, err, "Failed to read file, setting up testfs failed")
err = x.WriteFile(filepath.Join("/", file.Name()), fileBytes)
if err != nil {
t.Fatalf("Error writing fixture %s: %v", filePath, err)
}
require.NoError(t, err, "Failed to write file, setting up testfs failed")
}
return x
}
// NewTestBundle helps to create a new bundle with FakeFs containing documents from fixtureDir
func NewTestBundle(t *testing.T, fixtureDir string) document.Bundle {
t.Helper()
b, err := document.NewBundle(SetupTestFs(t, fixtureDir), "/", "/")
require.NoError(t, err, "Failed to build a bundle, setting up TestBundle failed")
return b
}