From 90c0116b7f049aecc463f51c89eed7ccd8e33af9 Mon Sep 17 00:00:00 2001 From: Niharika Bhavaraju Date: Fri, 15 Jan 2021 11:51:38 -0500 Subject: [PATCH] Phase Validation: k8s applier package changes to support Phase Validation sub command Relates-To: #330 Change-Id: I1503bcef1bc9d91d6897065a7c4418c1aa6181b6 --- .../testdata/no_bundle/kustomization.yaml | 0 pkg/phase/executors/errors.go | 17 ++++++ pkg/phase/executors/k8s_applier.go | 14 ++++- pkg/phase/executors/k8s_applier_test.go | 54 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 pkg/k8s/applier/testdata/no_bundle/kustomization.yaml diff --git a/pkg/k8s/applier/testdata/no_bundle/kustomization.yaml b/pkg/k8s/applier/testdata/no_bundle/kustomization.yaml new file mode 100644 index 000000000..e69de29bb diff --git a/pkg/phase/executors/errors.go b/pkg/phase/executors/errors.go index 6058c924d..00f2beefd 100755 --- a/pkg/phase/executors/errors.go +++ b/pkg/phase/executors/errors.go @@ -57,3 +57,20 @@ type ErrUnableParseProvider struct { func (e ErrUnableParseProvider) Error() string { return fmt.Sprintf("unable to parse name and version of '%s' type, '%s' provider", e.ProviderType, e.Provider) } + +// ErrNilExecutorDoc returned when the executor document is nil +type ErrNilExecutorDoc struct { +} + +func (e ErrNilExecutorDoc) Error() string { + return "executor document is nil" +} + +// ErrInvalidPhase is returned if the phase is invalid +type ErrInvalidPhase struct { + Reason string +} + +func (e ErrInvalidPhase) Error() string { + return fmt.Sprintf("invalid phase: %s", e.Reason) +} diff --git a/pkg/phase/executors/k8s_applier.go b/pkg/phase/executors/k8s_applier.go index 720bbef45..53083e739 100644 --- a/pkg/phase/executors/k8s_applier.go +++ b/pkg/phase/executors/k8s_applier.go @@ -23,7 +23,6 @@ import ( airshipv1 "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/errors" "opendev.org/airship/airshipctl/pkg/events" k8sapplier "opendev.org/airship/airshipctl/pkg/k8s/applier" "opendev.org/airship/airshipctl/pkg/k8s/kubeconfig" @@ -125,7 +124,18 @@ func (e *KubeApplierExecutor) prepareApplier(ch chan events.Event) (*k8sapplier. // Validate document set func (e *KubeApplierExecutor) Validate() error { - return errors.ErrNotImplemented{} + if e.BundleName == "" { + return ErrInvalidPhase{Reason: "k8s applier BundleName is empty"} + } + docs, err := e.ExecutorBundle.GetAllDocuments() + if err != nil { + return err + } + if len(docs) == 0 { + return ErrInvalidPhase{Reason: "no executor documents in the bundle"} + } + // TODO: need to find if any other validation needs to be added + return nil } // Render document set diff --git a/pkg/phase/executors/k8s_applier_test.go b/pkg/phase/executors/k8s_applier_test.go index 614d79ac4..4117b8525 100644 --- a/pkg/phase/executors/k8s_applier_test.go +++ b/pkg/phase/executors/k8s_applier_test.go @@ -254,3 +254,57 @@ func testKubeconfig(stringData string) kubeconfig.Interface { }, )) } + +func TestKubeApplierExecutor_Validate(t *testing.T) { + type fields struct { + BundleName string + path string + } + tests := []struct { + name string + fields fields + wantErr bool + }{ + { + name: "Error empty BundleName", + fields: fields{ + path: "../../k8s/applier/testdata/source_bundle", + }, + wantErr: true, + }, + { + name: "Error no documents", + fields: fields{BundleName: "some name", + path: "../../k8s/applier/testdata/no_bundle", + }, + wantErr: true, + }, + { + name: "Success case", + fields: fields{BundleName: "some name", + path: "../../k8s/applier/testdata/source_bundle", + }, + wantErr: false, + }, + } + for _, test := range tests { + tt := test + t.Run(tt.name, func(t *testing.T) { + execDoc, err := document.NewDocumentFromBytes([]byte(ValidExecutorDoc)) + require.NoError(t, err) + require.NotNil(t, execDoc) + + e, err := executors.NewKubeApplierExecutor(ifc.ExecutorConfig{ + BundleFactory: testBundleFactory(tt.fields.path), + PhaseName: tt.fields.BundleName, + ExecutorDocument: execDoc, + }) + require.NoError(t, err) + require.NotNil(t, e) + + if err := e.Validate(); (err != nil) != tt.wantErr { + t.Errorf("KubeApplierExecutor.Validate() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}