Phase Validation: k8s applier package changes to support Phase Validation sub command

Relates-To: #330

Change-Id: I1503bcef1bc9d91d6897065a7c4418c1aa6181b6
This commit is contained in:
Niharika Bhavaraju 2021-01-15 11:51:38 -05:00 committed by Bijaya Sharma
parent abc8315828
commit 90c0116b7f
4 changed files with 83 additions and 2 deletions

View File

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}
})
}
}