зеркало из https://github.com/Azure/draft-classic.git
ref(cmd/draft): add test for config flag on init
This commit is contained in:
Родитель
43460de1ad
Коммит
3caecf0d0f
|
@ -57,14 +57,9 @@ func newInitCmd(out io.Writer, in io.Reader) *cobra.Command {
|
|||
// runInit initializes local config and installs Draft to Kubernetes Cluster
|
||||
func (i *initCmd) run() error {
|
||||
|
||||
pluginOverrides := []plugin.Builtin{}
|
||||
repoOverrides := []repo.Builtin{}
|
||||
if i.configFile != "" {
|
||||
var err error
|
||||
pluginOverrides, repoOverrides, err = parseConfigFile(i.configFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not parse config file: %s", err)
|
||||
}
|
||||
pluginOverrides, repoOverrides, err := i.parseConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !i.dryRun {
|
||||
|
@ -77,6 +72,20 @@ func (i *initCmd) run() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (i *initCmd) parseConfig() ([]plugin.Builtin, []repo.Builtin, error) {
|
||||
pluginOverrides := []plugin.Builtin{}
|
||||
repoOverrides := []repo.Builtin{}
|
||||
if i.configFile != "" {
|
||||
var err error
|
||||
pluginOverrides, repoOverrides, err = parseConfigFile(i.configFile)
|
||||
if err != nil {
|
||||
return pluginOverrides, repoOverrides, fmt.Errorf("Could not parse config file: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
return pluginOverrides, repoOverrides, nil
|
||||
}
|
||||
|
||||
func (i *initCmd) setupDraftHome(plugins []plugin.Builtin, repos []repo.Builtin) error {
|
||||
ensureFuncs := []func() error{
|
||||
i.ensureDirectories,
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/Azure/draft/pkg/draft/draftpath"
|
||||
)
|
||||
|
||||
func TestParseConfig(t *testing.T) {
|
||||
testCases := []struct {
|
||||
configFile string
|
||||
expectErr bool
|
||||
pluginCount int
|
||||
repoCount int
|
||||
}{
|
||||
{"", false, 0, 0},
|
||||
{"testdata/init/configFile.toml", false, 1, 1},
|
||||
{"testdata/init/malformedConfigFile.toml", true, 0, 0},
|
||||
{"testdata/init/missingConfigFile.toml", true, 0, 0},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
resetEnvVars := unsetEnvVars()
|
||||
tempHome, teardown := tempDir(t, "draft-init")
|
||||
defer func() {
|
||||
teardown()
|
||||
resetEnvVars()
|
||||
}()
|
||||
|
||||
cmd := &initCmd{
|
||||
home: draftpath.Home(tempHome),
|
||||
out: ioutil.Discard,
|
||||
configFile: tc.configFile,
|
||||
}
|
||||
|
||||
plugins, repos, err := cmd.parseConfig()
|
||||
if err != nil && !tc.expectErr {
|
||||
t.Errorf("Not expecting error but got error: %v", err)
|
||||
}
|
||||
if len(plugins) != tc.pluginCount {
|
||||
t.Errorf("Expected %v plugins, got %#v", tc.pluginCount, len(plugins))
|
||||
}
|
||||
if len(repos) != tc.repoCount {
|
||||
t.Errorf("Expected %v pack repos, got %#v", tc.repoCount, len(repos))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
[[plugin]]
|
||||
name = "plugin1"
|
||||
url = "a url"
|
||||
version = "1.0.0"
|
||||
|
||||
[[repo]]
|
||||
name = "pack-repo"
|
||||
url = "some url"
|
|
@ -0,0 +1,4 @@
|
|||
something random
|
||||
|
||||
[[nothing]]
|
||||
nonsense here
|
Загрузка…
Ссылка в новой задаче