2019-12-04 17:19:06 -05:00
|
|
|
/*
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package config_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
cmd "opendev.org/airship/airshipctl/cmd/config"
|
|
|
|
"opendev.org/airship/airshipctl/pkg/config"
|
|
|
|
"opendev.org/airship/airshipctl/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
fooContext = "ContextFoo"
|
|
|
|
barContext = "ContextBar"
|
|
|
|
bazContext = "ContextBaz"
|
|
|
|
missingContext = "contextMissing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetContextCmd(t *testing.T) {
|
2020-08-28 18:32:33 -05:00
|
|
|
settings := func() (*config.Config, error) {
|
|
|
|
return &config.Config{
|
2020-03-23 14:41:08 -05:00
|
|
|
Contexts: map[string]*config.Context{
|
|
|
|
fooContext: getNamedTestContext(fooContext),
|
|
|
|
barContext: getNamedTestContext(barContext),
|
|
|
|
bazContext: getNamedTestContext(bazContext),
|
|
|
|
},
|
|
|
|
CurrentContext: bazContext,
|
2020-08-28 18:32:33 -05:00
|
|
|
}, nil
|
2019-12-04 17:19:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
cmdTests := []*testutil.CmdTest{
|
|
|
|
{
|
|
|
|
Name: "get-context",
|
|
|
|
CmdLine: fmt.Sprintf("%s", fooContext),
|
2020-04-15 16:59:49 -05:00
|
|
|
Cmd: cmd.NewGetContextCommand(settings),
|
2019-12-04 17:19:06 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "get-all-contexts",
|
2021-04-16 19:03:54 +05:30
|
|
|
CmdLine: fmt.Sprintf("%s", barContext),
|
2020-04-15 16:59:49 -05:00
|
|
|
Cmd: cmd.NewGetContextCommand(settings),
|
2019-12-04 17:19:06 -05:00
|
|
|
},
|
|
|
|
// This is not implemented yet
|
|
|
|
{
|
|
|
|
Name: "get-multiple-contexts",
|
|
|
|
CmdLine: fmt.Sprintf("%s %s", fooContext, barContext),
|
2020-04-15 16:59:49 -05:00
|
|
|
Cmd: cmd.NewGetContextCommand(settings),
|
2021-04-16 19:03:54 +05:30
|
|
|
Error: fmt.Errorf("accepts at most 1 arg(s), received 2"),
|
2019-12-04 17:19:06 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Name: "missing",
|
|
|
|
CmdLine: fmt.Sprintf("%s", missingContext),
|
2020-04-15 16:59:49 -05:00
|
|
|
Cmd: cmd.NewGetContextCommand(settings),
|
2021-01-21 15:07:20 -06:00
|
|
|
Error: fmt.Errorf("missing configuration: context with name '%s'",
|
2020-05-20 11:04:55 -05:00
|
|
|
missingContext),
|
2019-12-04 17:19:06 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "get-current-context",
|
2020-04-15 16:59:49 -05:00
|
|
|
CmdLine: "--current",
|
|
|
|
Cmd: cmd.NewGetContextCommand(settings),
|
2019-12-04 17:19:06 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range cmdTests {
|
|
|
|
testutil.RunTest(t, tt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoContextsGetContextCmd(t *testing.T) {
|
2020-08-28 18:32:33 -05:00
|
|
|
settings := func() (*config.Config, error) { return new(config.Config), nil }
|
2019-12-04 17:19:06 -05:00
|
|
|
cmdTest := &testutil.CmdTest{
|
|
|
|
Name: "no-contexts",
|
|
|
|
CmdLine: "",
|
2020-04-15 16:59:49 -05:00
|
|
|
Cmd: cmd.NewGetContextCommand(settings),
|
2019-12-04 17:19:06 -05:00
|
|
|
}
|
|
|
|
testutil.RunTest(t, cmdTest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNamedTestContext(contextName string) *config.Context {
|
|
|
|
newContext := &config.Context{
|
2021-03-24 14:34:11 -05:00
|
|
|
Manifest: fmt.Sprintf("Manifest_%s", contextName),
|
2019-12-04 17:19:06 -05:00
|
|
|
}
|
|
|
|
return newContext
|
|
|
|
}
|