Ian Howell ea9fba7278 Add the dynamic kubernetes client
The dynamic client will be needed to interact with any custom resource
that airshipctl doesn't know about. It will be required for checking the
health of a cluster, as well as any other operations that may need to be
performed on generic objects.

This also adds the pkg/k8s/client/fake package, which can be used to
create a mock instance of a client for use in unit tests.

Change-Id: Ia331ff4875a067045f6f9245daee109126fb1d33
Relates-To: #73
Relates-To: #20
2020-03-23 16:24:04 -05:00

106 lines
2.5 KiB
Go

package initinfra_test
import (
"errors"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"opendev.org/airship/airshipctl/pkg/cluster/initinfra"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/k8s/client"
"opendev.org/airship/airshipctl/pkg/k8s/client/fake"
"opendev.org/airship/airshipctl/pkg/k8s/kubectl"
"opendev.org/airship/airshipctl/testutil/k8sutils"
)
const (
kubeconfigPath = "testdata/kubeconfig.yaml"
filenameRC = "testdata/primary/site/test-site/ephemeral/initinfra/replicationcontroller.yaml"
airshipConfigFile = "testdata/config.yaml"
)
var (
DynamicClientError = errors.New("DynamicClientError")
)
func TestNewInfra(t *testing.T) {
rs := makeNewFakeRootSettings(t, kubeconfigPath, airshipConfigFile)
infra := initinfra.NewInfra(rs)
assert.NotNil(t, infra.RootSettings)
}
func TestDeploy(t *testing.T) {
rs := makeNewFakeRootSettings(t, kubeconfigPath, airshipConfigFile)
tf := k8sutils.NewFakeFactoryForRC(t, filenameRC)
defer tf.Cleanup()
infra := initinfra.NewInfra(rs)
infra.ClusterType = "ephemeral"
infra.DryRun = true
infra.FileSystem = document.NewDocumentFs()
kctl := kubectl.NewKubectl(tf)
tc := fake.Client{
MockKubectl: func() kubectl.Interface { return kctl },
}
tests := []struct {
theInfra *initinfra.Infra
client client.Interface
prune bool
expectedError error
}{
{
client: fake.Client{
MockKubectl: func() kubectl.Interface {
return kubectl.NewKubectl(k8sutils.
NewMockKubectlFactory().
WithDynamicClientByError(nil, DynamicClientError))
},
},
expectedError: DynamicClientError,
},
{
expectedError: nil,
prune: false,
client: tc,
},
{
expectedError: nil,
prune: true,
client: tc,
},
}
for _, test := range tests {
infra.Prune = test.prune
infra.Client = test.client
actualErr := infra.Deploy()
assert.Equal(t, test.expectedError, actualErr)
}
}
// MakeNewFakeRootSettings takes kubeconfig path and directory path to fixture dir as argument.
func makeNewFakeRootSettings(t *testing.T, kp string, dir string) *environment.AirshipCTLSettings {
t.Helper()
rs := &environment.AirshipCTLSettings{}
akp, err := filepath.Abs(kp)
require.NoError(t, err)
adir, err := filepath.Abs(dir)
require.NoError(t, err)
rs.SetAirshipConfigPath(adir)
rs.SetKubeConfigPath(akp)
rs.InitConfig()
return rs
}