Merge "Phase Validation: pkg/phase change to add Phase validation sub command"

This commit is contained in:
Zuul 2021-03-02 01:36:37 +00:00 committed by Gerrit Code Review
commit 5a6213e80d
2 changed files with 128 additions and 0 deletions

View File

@ -274,3 +274,35 @@ func (c *ClusterListCommand) RunE() error {
} }
return nil 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()
}

View File

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