airshipctl/testutil/testdatafs.go
Matt McEuen e52ed80363 Update Kustomize integration to api/v0.3.1
This updates the Kustomize dependency for airshipctl to
api/v0.3.1, which is the oldest version which will support the
Replacement Transformer plugin.

Some changes were needed to accomodate the fact that various apis
that airshipctl was relying on were moved to kustomize-insternal
packages, namely:
- Integrated with the krusty.Kustomizer to drive kustomization
- Removed the custom plugin loader which leveraged the Unknown type
- Worked around NoFieldError becoming private, inc. removing a test

As a follow on we'll need to re-integrate plugin functionality somehow.

Also, in this release Kustomize has implemented support for the
"config.kubernetes.io/local-config" annotation, which we'd planned
to use to to avoid deploying some documents to the Kubernetes API.
It turns out the semantics are different than we anticipated;
Kustomize also fails to return these docs via document *selection*.
Therefore, this change reverts to an earlier approach which uses
a custom airshipit.org/deploy-k8s label.

Change-Id: I7022e12464ea7b6a3ca8609f99f3699bf8da0edd
2020-04-13 10:06:41 -05:00

69 lines
2.3 KiB
Go

package testutil
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
fs "sigs.k8s.io/kustomize/api/filesys"
"opendev.org/airship/airshipctl/pkg/document"
)
// SetupTestFs help manufacture a fake file system for testing purposes. It
// will iterate over the files in fixtureDir, which is a directory relative
// 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) document.FileSystem {
t.Helper()
x := &document.DocumentFs{FileSystem: fs.MakeFsInMemory()}
files, err := ioutil.ReadDir(fixtureDir)
require.NoErrorf(t, err, "Failed to read fixture directory %s", fixtureDir)
for _, file := range files {
fileName := file.Name()
filePath := filepath.Join(fixtureDir, fileName)
fileBytes, err := ioutil.ReadFile(filePath)
require.NoErrorf(t, err, "Failed to read file %s, setting up testfs failed", filePath)
err = x.WriteFile(filepath.Join("/", file.Name()), fileBytes)
require.NoErrorf(t, err, "Failed to write file %s, setting up testfs failed", filePath)
}
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
}
// CleanUpGitFixtures removes any temp directories created by the go-git test fixtures
func CleanUpGitFixtures(t *testing.T) {
if err := fixtures.Clean(); err != nil {
t.Logf("Could not clean up git fixtures: %v", err)
}
}
// TempDir creates a new temporary directory in the system's temporary file
// storage with a name beginning with prefix.
// It returns the path of the new directory and a function that can be used to
// easily clean up that directory
func TempDir(t *testing.T, prefix string) (path string, cleanup func(*testing.T)) {
path, err := ioutil.TempDir("", prefix)
require.NoError(t, err, "Failed to create a temporary directory")
return path, func(tt *testing.T) {
err := os.RemoveAll(path)
if err != nil {
t.Logf("Could not clean up temp directory %q: %v", path, err)
}
}
}