airshipctl/pkg/phase/executors/clusterctl_test.go
bijayasharma 9d4d0cf863 Phase Validation: clusterctl package changes to support Phase Validation sub command
Relates-To: #330

Change-Id: I1b9ece114934a05245898df2088d80fe2f1c69c5
2021-03-02 13:20:45 +00:00

304 lines
8.7 KiB
Go
Executable File

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package executors_test
import (
"bytes"
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"opendev.org/airship/airshipctl/pkg/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/cluster/clustermap"
"opendev.org/airship/airshipctl/pkg/document"
"opendev.org/airship/airshipctl/pkg/events"
"opendev.org/airship/airshipctl/pkg/fs"
"opendev.org/airship/airshipctl/pkg/k8s/kubeconfig"
"opendev.org/airship/airshipctl/pkg/phase/executors"
"opendev.org/airship/airshipctl/pkg/phase/ifc"
testfs "opendev.org/airship/airshipctl/testutil/fs"
)
var (
executorConfigTmpl = `
apiVersion: airshipit.org/v1alpha1
kind: Clusterctl
metadata:
name: clusterctl-v1
action: %s
init-options:
core-provider: "cluster-api:v0.3.2"
kubeConfigRef:
apiVersion: airshipit.org/v1alpha1
kind: KubeConfig
name: sample-name
providers:
- name: "cluster-api"
type: "CoreProvider"
versions:
v0.3.2: functions/capi/infrastructure/v0.3.2`
executorConfigTmplGood = `
apiVersion: airshipit.org/v1alpha1
kind: Clusterctl
metadata:
name: clusterctl-v1
action: %s
init-options:
core-provider: "cluster-api:v0.3.2"
kubeConfigRef:
apiVersion: airshipit.org/v1alpha1
kind: KubeConfig
name: sample-name
namespace: some-namespace
move-options:
namespace: some-namespace
providers:
- name: "cluster-api"
type: "CoreProvider"
versions:
v0.3.3: manifests/function/capi/v0.3.3`
executorConfigTmplBad = `
apiVersion: airshipit.org/v1alpha1
kind: Clusterctl
metadata:
name: clusterctl-v1
action: %s
somefield: %s
providers:
- name: "cluster-api"
type: "CoreProvider"
versions:
v0.3.2: functions/capi/infrastructure/v0.3.2`
renderedDocs = `---
apiVersion: v1
kind: Namespace
metadata:
labels:
cluster.x-k8s.io/provider: cluster-api
clusterctl.cluster.x-k8s.io: ""
control-plane: controller-manager
name: version-two
...
`
)
func TestNewClusterctlExecutor(t *testing.T) {
sampleCfgDoc := executorDoc(t, fmt.Sprintf(executorConfigTmplGood, "init"))
testCases := []struct {
name string
helper ifc.Helper
expectedErr error
}{
{
name: "New Clusterctl Executor",
helper: makeDefaultHelper(t, "../../clusterctl/client/testdata", defaultMetadataPath),
},
}
for _, test := range testCases {
tt := test
t.Run(tt.name, func(t *testing.T) {
_, actualErr := executors.NewClusterctlExecutor(ifc.ExecutorConfig{
ExecutorDocument: sampleCfgDoc,
Helper: tt.helper,
})
assert.Equal(t, tt.expectedErr, actualErr)
})
}
}
func TestClusterctlExecutorRun(t *testing.T) {
errTmpFile := errors.New("TmpFile error")
testCases := []struct {
name string
cfgDoc document.Document
fs fs.FileSystem
bundlePath string
expectedEvt []events.Event
clusterMap clustermap.ClusterMap
}{
{
name: "Error unknown action",
cfgDoc: executorDoc(t, fmt.Sprintf(executorConfigTmplGood, "someAction")),
bundlePath: "testdata/executor_init",
expectedEvt: []events.Event{
wrapError(executors.ErrUnknownExecutorAction{Action: "someAction", ExecutorName: "clusterctl"}),
},
clusterMap: clustermap.NewClusterMap(v1alpha1.DefaultClusterMap()),
},
{
name: "Error temporary file",
cfgDoc: executorDoc(t, fmt.Sprintf(executorConfigTmplGood, "init")),
fs: testfs.MockFileSystem{
MockTempFile: func(string, string) (fs.File, error) {
return nil, errTmpFile
},
},
bundlePath: "testdata/executor_init",
expectedEvt: []events.Event{
events.NewEvent().WithClusterctlEvent(events.ClusterctlEvent{
Operation: events.ClusterctlInitStart,
}),
wrapError(errTmpFile),
},
clusterMap: clustermap.NewClusterMap(v1alpha1.DefaultClusterMap()),
},
{
name: "Regular Run init",
cfgDoc: executorDoc(t, fmt.Sprintf(executorConfigTmplGood, "init")),
fs: testfs.MockFileSystem{
MockTempFile: func(string, string) (fs.File, error) {
return testfs.TestFile{
MockName: func() string { return "filename" },
MockWrite: func() (int, error) { return 0, nil },
MockClose: func() error { return nil },
}, nil
},
MockRemoveAll: func() error { return nil },
},
bundlePath: "testdata/executor_init",
clusterMap: clustermap.NewClusterMap(v1alpha1.DefaultClusterMap()),
expectedEvt: []events.Event{
events.NewEvent().WithClusterctlEvent(events.ClusterctlEvent{
Operation: events.ClusterctlInitStart,
}),
events.NewEvent().WithClusterctlEvent(events.ClusterctlEvent{
Operation: events.ClusterctlInitEnd,
}),
},
},
// TODO add move tests here
}
for _, test := range testCases {
tt := test
t.Run(tt.name, func(t *testing.T) {
kubeCfg := kubeconfig.NewKubeConfig(
kubeconfig.FromByte([]byte("someKubeConfig")),
kubeconfig.InjectFileSystem(tt.fs),
)
executor, err := executors.NewClusterctlExecutor(
ifc.ExecutorConfig{
ExecutorDocument: tt.cfgDoc,
Helper: makeDefaultHelper(t, "../../clusterctl/client/testdata", defaultMetadataPath),
KubeConfig: kubeCfg,
ClusterMap: tt.clusterMap,
})
require.NoError(t, err)
ch := make(chan events.Event)
go executor.Run(ch, ifc.RunOptions{DryRun: true})
var actualEvt []events.Event
for evt := range ch {
// Skip timmestamp for comparison
evt.Timestamp = time.Time{}
if evt.Type == events.ClusterctlType {
// Set message to empty string, so it's not compared
evt.ClusterctlEvent.Message = ""
}
actualEvt = append(actualEvt, evt)
}
for i := range tt.expectedEvt {
// Skip timmestamp for comparison
tt.expectedEvt[i].Timestamp = time.Time{}
}
assert.Equal(t, tt.expectedEvt, actualEvt)
})
}
}
func TestClusterctlExecutorValidate(t *testing.T) {
testCases := []struct {
name string
actionType string
bundlePath string
executorConfigTmpl string
expectedErrString string
}{
{
name: "Success init action",
actionType: "init",
executorConfigTmpl: executorConfigTmplGood,
},
{
name: "Success move action",
actionType: "move",
executorConfigTmpl: executorConfigTmplGood,
},
{
name: "Error any other action",
actionType: "any",
executorConfigTmpl: executorConfigTmplGood,
expectedErrString: "unknown action type 'any'",
},
{
name: "Error empty action",
actionType: "",
executorConfigTmpl: executorConfigTmplGood,
expectedErrString: "invalid phase: ClusterctlExecutor.Action is empty",
},
{
name: "Error empty init option",
actionType: "init",
executorConfigTmpl: executorConfigTmplBad,
expectedErrString: "invalid phase: ClusterctlExecutor.InitOptions.CoreProvider is empty",
},
{
name: "Error empty move option",
actionType: "move",
executorConfigTmpl: executorConfigTmplBad,
expectedErrString: "invalid phase: ClusterctlExecutor.MoveOptions.Namespace is empty",
},
}
for _, test := range testCases {
tt := test
t.Run(tt.name, func(t *testing.T) {
sampleCfgDoc := executorDoc(t, fmt.Sprintf(test.executorConfigTmpl, test.actionType, test.actionType))
executor, err := executors.NewClusterctlExecutor(
ifc.ExecutorConfig{
ExecutorDocument: sampleCfgDoc,
Helper: makeDefaultHelper(t, "../../clusterctl/client/testdata", defaultMetadataPath),
})
require.NoError(t, err)
err = executor.Validate()
if test.expectedErrString != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), test.expectedErrString)
} else {
require.NoError(t, err)
}
})
}
}
func TestClusterctlExecutorRender(t *testing.T) {
sampleCfgDoc := executorDoc(t, fmt.Sprintf(executorConfigTmpl, "init"))
executor, err := executors.NewClusterctlExecutor(
ifc.ExecutorConfig{
ExecutorDocument: sampleCfgDoc,
Helper: makeDefaultHelper(t, "../../clusterctl/client/testdata", defaultMetadataPath),
})
require.NoError(t, err)
actualOut := &bytes.Buffer{}
actualErr := executor.Render(actualOut, ifc.RenderOptions{})
assert.Equal(t, renderedDocs, actualOut.String())
assert.NoError(t, actualErr)
}