airshipctl/pkg/k8s/kubectl/kubectl_test.go
Kostiantyn Kalynovskyi d588c73e38 [#20] Add kubectl apply wrapper package
The wrapper is called ApplyAdapter and is a struct, that has Apply(..)
method and some setters that allow to control kubectl apply behaviour

Addapter is expected to be used through Apply(..) function, which takes
slice of document.Document interface objects, writes them out to
temporary file system, from where they are picked up by kubectl Apply
module, and delivered to kubernetes cluster. The decision to use
temporary file system is based on the fact, that in current state
kubectl project currently only works with actual files, and ignores
io.Streams object, that is part of ApplyOptions struct, so it is
currently the only way to use it.

Change-Id: Idc5d79794149c00198f420d76cf9aa3b5264946e
2020-02-20 20:58:31 +00:00

110 lines
3.0 KiB
Go

package kubectl_test
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/v3/pkg/fs"
"opendev.org/airship/airshipctl/pkg/k8s/kubectl"
k8sutils "opendev.org/airship/airshipctl/pkg/k8s/utils"
"opendev.org/airship/airshipctl/testutil"
k8stest "opendev.org/airship/airshipctl/testutil/k8sutils"
)
var (
kubeconfigPath = "testdata/kubeconfig.yaml"
fixtureDir = "testdata/"
writeOutError = errors.New("writeOutError")
TempFileError = errors.New("TempFileError")
)
type MockFileSystem struct {
MockRemoveAll func() error
MockTempFile func() (kubectl.File, error)
fs.FileSystem
}
func (fsys MockFileSystem) RemoveAll(name string) error { return fsys.MockRemoveAll() }
func (fsys MockFileSystem) TempFile(bufferDir string, prefix string) (kubectl.File, error) {
return fsys.MockTempFile()
}
type TestFile struct {
kubectl.File
MockName func() string
MockWrite func() (int, error)
MockClose func() error
}
func (f TestFile) Name() string { return f.MockName() }
func (f TestFile) Write([]byte) (int, error) { return f.MockWrite() }
func (f TestFile) Close() error { return f.MockClose() }
func TestNewKubectlFromKubeconfigPath(t *testing.T) {
f := k8sutils.FactoryFromKubeconfigPath(kubeconfigPath)
kctl := kubectl.NewKubectl(f).WithBufferDir("/tmp/.airship")
assert.NotNil(t, kctl.Factory)
assert.NotNil(t, kctl.FileSystem)
assert.NotNil(t, kctl.IOStreams)
}
func TestApply(t *testing.T) {
f := k8stest.NewFakeFactoryForRC(t, filenameRC)
defer f.Cleanup()
kctl := kubectl.NewKubectl(f).WithBufferDir("/tmp/.airship")
kctl.Factory = f
ao, err := kctl.ApplyOptions()
require.NoError(t, err, "failed to get documents from bundle")
ao.DryRun = true
b := testutil.NewTestBundle(t, fixtureDir)
docs, err := b.GetByAnnotation("airshipit.org/initinfra")
require.NoError(t, err, "failed to get documents from bundle")
tests := []struct {
name string
expectedErr error
fs kubectl.FileSystem
}{
{
expectedErr: nil,
fs: MockFileSystem{
MockRemoveAll: func() error { return nil },
MockTempFile: func() (kubectl.File, error) {
return TestFile{
MockName: func() string { return filenameRC },
MockWrite: func() (int, error) { return 0, nil },
MockClose: func() error { return nil },
}, nil
},
},
},
{
expectedErr: writeOutError,
fs: MockFileSystem{
MockTempFile: func() (kubectl.File, error) { return nil, writeOutError }},
},
{
expectedErr: TempFileError,
fs: MockFileSystem{
MockRemoveAll: func() error { return nil },
MockTempFile: func() (kubectl.File, error) {
return TestFile{
MockWrite: func() (int, error) { return 0, TempFileError },
MockName: func() string { return filenameRC },
MockClose: func() error { return nil },
}, nil
}},
},
}
for _, test := range tests {
kctl.FileSystem = test.fs
assert.Equal(t, kctl.Apply(docs, ao), test.expectedErr)
}
}