From e41a63ac8dcc5de553ccc02063bf10a34e6f6dd2 Mon Sep 17 00:00:00 2001 From: Niharika Bhavaraju Date: Wed, 18 Nov 2020 23:39:03 -0500 Subject: [PATCH] Phase Validation: pkg/phase change to add Phase validation sub command Related To #330 Change-Id: I649f0ed487d3469e5a51dac0a9fd234f686c4373 --- pkg/phase/command.go | 32 +++++++++++++ pkg/phase/command_test.go | 96 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) diff --git a/pkg/phase/command.go b/pkg/phase/command.go index 245ea0028..f1d24e5f7 100644 --- a/pkg/phase/command.go +++ b/pkg/phase/command.go @@ -274,3 +274,35 @@ func (c *ClusterListCommand) RunE() error { } return nil } + +// ValidateFlags options for phase validate command +type ValidateFlags struct { + PhaseID ifc.ID +} + +// ValidateCommand phase validate command +type ValidateCommand struct { + Options ValidateFlags + Factory config.Factory +} + +// RunE runs the phase validate command +func (c *ValidateCommand) RunE() error { + cfg, err := c.Factory() + if err != nil { + return err + } + + helper, err := NewHelper(cfg) + if err != nil { + return err + } + + client := NewClient(helper) + + phase, err := client.PhaseByID(c.Options.PhaseID) + if err != nil { + return err + } + return phase.Validate() +} diff --git a/pkg/phase/command_test.go b/pkg/phase/command_test.go index e25d06b26..dfbcd84b6 100644 --- a/pkg/phase/command_test.go +++ b/pkg/phase/command_test.go @@ -437,3 +437,99 @@ func TestClusterListCommand_RunE(t *testing.T) { }) } } + +func TestValidateCommand(t *testing.T) { + tests := []struct { + name string + errContains string + flags phase.ValidateFlags + factory config.Factory + }{ + { + name: "Error config factory", + factory: func() (*config.Config, error) { + return nil, fmt.Errorf(testFactoryErr) + }, + errContains: testFactoryErr, + }, + { + name: "Error new helper", + factory: func() (*config.Config, error) { + return &config.Config{ + CurrentContext: "does not exist", + Contexts: make(map[string]*config.Context), + }, nil + }, + errContains: testNewHelperErr, + }, + { + name: "Error phase by id", + factory: func() (*config.Config, error) { + conf := config.NewConfig() + conf.Manifests = map[string]*config.Manifest{ + "manifest": { + MetadataPath: "broken_metadata.yaml", + TargetPath: "testdata", + PhaseRepositoryName: config.DefaultTestPhaseRepo, + Repositories: map[string]*config.Repository{ + config.DefaultTestPhaseRepo: { + URLString: "", + }, + }, + }, + } + conf.CurrentContext = defaultCurrentContext + conf.Contexts = map[string]*config.Context{ + "context": { + Manifest: "manifest", + }, + } + return conf, nil + }, + errContains: testNoBundlePath, + }, + { + name: "success", + // flags: phase.ValidateFlags{PhaseID: } + factory: func() (*config.Config, error) { + conf := config.NewConfig() + conf.Manifests = map[string]*config.Manifest{ + "manifest": { + MetadataPath: "metadata.yaml", + TargetPath: "testdata", + PhaseRepositoryName: config.DefaultTestPhaseRepo, + Repositories: map[string]*config.Repository{ + config.DefaultTestPhaseRepo: { + URLString: "", + }, + }, + }, + } + conf.CurrentContext = defaultCurrentContext + conf.Contexts = map[string]*config.Context{ + "context": { + Manifest: "manifest", + }, + } + return conf, nil + }, + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + command := phase.ValidateCommand{ + Options: tt.flags, + Factory: tt.factory, + } + err := command.RunE() + if tt.errContains != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.errContains) + } else { + assert.NoError(t, err) + } + }) + } +}