This tests get-cluster with the pre-built CmdTest

This changes the unit tests in ./cmd/config/get_cluster.go by swapping
out the `run` function for the pre-built CmdTest. This matches the
conventions of the existing test bed.

Change-Id: I4cecd211fe7ab74a1151635f29bda578887f400a
This commit is contained in:
Ian Howell 2019-11-11 10:17:56 -06:00
parent ffcb533d39
commit 404760ce3e
10 changed files with 366 additions and 109 deletions

View File

@ -24,7 +24,6 @@ import (
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/log"
) )
var ( var (
@ -48,14 +47,11 @@ func NewCmdConfigGetCluster(rootSettings *environment.AirshipCTLSettings) *cobra
Short: "Display a specific cluster", Short: "Display a specific cluster",
Long: getClusterLong, Long: getClusterLong,
Example: getClusterExample, Example: getClusterExample,
Run: func(cmd *cobra.Command, args []string) { RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 { if len(args) == 1 {
theCluster.Name = args[0] theCluster.Name = args[0]
} }
err := runGetCluster(theCluster, cmd.OutOrStdout(), rootSettings) return runGetCluster(theCluster, cmd.OutOrStdout(), rootSettings)
if err != nil {
log.Fatal(err)
}
}, },
} }
@ -99,9 +95,11 @@ func getClusters(out io.Writer, rootSettings *environment.AirshipCTLSettings) er
if err != nil { if err != nil {
return err return err
} }
if clusters == nil { if len(clusters) == 0 {
fmt.Fprint(out, "No clusters found in the configuration.\n") fmt.Fprintln(out, "No clusters found in the configuration.")
return nil
} }
for _, cluster := range clusters { for _, cluster := range clusters {
fmt.Fprintf(out, "%s\n", cluster.PrettyString()) fmt.Fprintf(out, "%s\n", cluster.PrettyString())
} }

View File

@ -14,122 +14,132 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package config package config_test
import ( import (
"bytes" "fmt"
"path/filepath"
"testing" "testing"
"github.com/stretchr/testify/assert" kubeconfig "k8s.io/client-go/tools/clientcmd/api"
"github.com/stretchr/testify/require"
cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil"
) )
type getClusterTest struct { const (
config *config.Config ephemeralFlag = "--" + config.FlagClusterType + "=" + config.Ephemeral
args []string targetFlag = "--" + config.FlagClusterType + "=" + config.Target
flags []string
expected string
}
func TestGetCluster(t *testing.T) { fooCluster = "clusterFoo"
tname := "def" barCluster = "clusterBar"
tctype := config.Ephemeral bazCluster = "clusterBaz"
missingCluster = "clusterMissing"
)
conf := config.InitConfig(t) func TestGetClusterCmd(t *testing.T) {
conf := &config.Config{
// Retrieve one of the test Clusters: map[string]*config.ClusterPurpose{
theClusterIWant, err := conf.GetCluster(tname, tctype) fooCluster: {
require.NoError(t, err) ClusterTypes: map[string]*config.Cluster{
config.Ephemeral: getNamedTestCluster(fooCluster, config.Ephemeral),
err = conf.Purge() config.Target: getNamedTestCluster(fooCluster, config.Target),
require.NoError(t, err, "Unable to Purge before persisting the expected configuration") },
err = conf.PersistConfig() },
require.NoError(t, err, "Unable to Persist the expected configuration") barCluster: {
ClusterTypes: map[string]*config.Cluster{
test := getClusterTest{ config.Ephemeral: getNamedTestCluster(barCluster, config.Ephemeral),
config: conf, config.Target: getNamedTestCluster(barCluster, config.Target),
args: []string{tname}, },
flags: []string{ },
"--" + config.FlagClusterType + "=" + config.Ephemeral, bazCluster: {
ClusterTypes: map[string]*config.Cluster{
config.Ephemeral: getNamedTestCluster(bazCluster, config.Ephemeral),
config.Target: getNamedTestCluster(bazCluster, config.Target),
},
},
}, },
expected: theClusterIWant.PrettyString(),
} }
test.run(t)
}
func TestGetAllClusters(t *testing.T) {
conf := config.InitConfig(t)
testDir := filepath.Dir(conf.LoadedConfigPath())
kubeconfigPath := filepath.Join(testDir, "kubeconfig")
expected := `Cluster: def
ephemeral:
bootstrap-info: ""
cluster-kubeconf: def_ephemeral
LocationOfOrigin: ` + kubeconfigPath + `
insecure-skip-tls-verify: true
server: http://5.6.7.8
Cluster: def
target:
bootstrap-info: ""
cluster-kubeconf: def_target
LocationOfOrigin: ` + kubeconfigPath + `
insecure-skip-tls-verify: true
server: http://1.2.3.4
Cluster: onlyinkubeconf
target:
bootstrap-info: ""
cluster-kubeconf: onlyinkubeconf_target
LocationOfOrigin: ` + kubeconfigPath + `
insecure-skip-tls-verify: true
server: http://9.10.11.12
Cluster: wrongonlyinkubeconf
target:
bootstrap-info: ""
cluster-kubeconf: wrongonlyinkubeconf_target
LocationOfOrigin: ` + kubeconfigPath + `
certificate-authority: cert_file
server: ""
`
test := getClusterTest{
config: conf,
args: []string{},
flags: []string{},
expected: expected,
}
test.run(t)
}
func (test getClusterTest) run(t *testing.T) {
// Get the Environment
settings := &environment.AirshipCTLSettings{} settings := &environment.AirshipCTLSettings{}
settings.SetConfig(test.config) settings.SetConfig(conf)
buf := bytes.NewBuffer([]byte{})
cmd := NewCmdConfigGetCluster(settings)
cmd.SetOutput(buf)
cmd.SetArgs(test.args)
err := cmd.Flags().Parse(test.flags)
require.NoErrorf(t, err, "unexpected error flags args to command: %v, flags: %v", err, test.flags)
err = cmd.Execute() cmdTests := []*testutil.CmdTest{
require.NoError(t, err) {
if len(test.expected) != 0 { Name: "get-ephemeral",
assert.EqualValues(t, test.expected, buf.String()) CmdLine: fmt.Sprintf("%s %s", ephemeralFlag, fooCluster),
Cmd: cmd.NewCmdConfigGetCluster(settings),
},
{
Name: "get-target",
CmdLine: fmt.Sprintf("%s %s", targetFlag, fooCluster),
Cmd: cmd.NewCmdConfigGetCluster(settings),
},
// FIXME(howell): "airshipctl config get-cluster foo bar" will
// print *all* clusters, regardless of whether they are
// specified on the command line
// In this case, the bazCluster should not be included in the
// output, yet it is
{
Name: "get-multiple-ephemeral",
CmdLine: fmt.Sprintf("%s %s %s", ephemeralFlag, fooCluster, barCluster),
Cmd: cmd.NewCmdConfigGetCluster(settings),
},
{
Name: "get-multiple-target",
CmdLine: fmt.Sprintf("%s %s %s", targetFlag, fooCluster, barCluster),
Cmd: cmd.NewCmdConfigGetCluster(settings),
},
// FIXME(howell): "airshipctl config get-cluster
// --cluster-type=ephemeral" will print *all* clusters,
// regardless of whether they are ephemeral or target
{
Name: "get-all-ephemeral",
CmdLine: ephemeralFlag,
Cmd: cmd.NewCmdConfigGetCluster(settings),
},
{
Name: "get-all-target",
CmdLine: targetFlag,
Cmd: cmd.NewCmdConfigGetCluster(settings),
},
{
Name: "missing",
CmdLine: fmt.Sprintf("%s %s", targetFlag, missingCluster),
Cmd: cmd.NewCmdConfigGetCluster(settings),
Error: fmt.Errorf("Cluster clusterMissing information was not " +
"found in the configuration."),
},
}
for _, tt := range cmdTests {
testutil.RunTest(t, tt)
} }
} }
func TestNoClustersGetClusterCmd(t *testing.T) {
settings := &environment.AirshipCTLSettings{}
settings.SetConfig(&config.Config{})
cmdTest := &testutil.CmdTest{
Name: "no-clusters",
CmdLine: "",
Cmd: cmd.NewCmdConfigGetCluster(settings),
}
testutil.RunTest(t, cmdTest)
}
func getNamedTestCluster(clusterName, clusterType string) *config.Cluster {
kCluster := &kubeconfig.Cluster{
LocationOfOrigin: "",
InsecureSkipTLSVerify: true,
Server: "",
}
newCluster := &config.Cluster{NameInKubeconf: fmt.Sprintf("%s_%s", clusterName, clusterType)}
newCluster.SetKubeCluster(kCluster)
return newCluster
}

View File

@ -0,0 +1,54 @@
Cluster: clusterBar
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterBar_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBar
target:
bootstrap-info: ""
cluster-kubeconf: clusterBar_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBaz
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterBaz_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBaz
target:
bootstrap-info: ""
cluster-kubeconf: clusterBaz_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterFoo
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterFoo_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterFoo
target:
bootstrap-info: ""
cluster-kubeconf: clusterFoo_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""

View File

@ -0,0 +1,54 @@
Cluster: clusterBar
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterBar_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBar
target:
bootstrap-info: ""
cluster-kubeconf: clusterBar_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBaz
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterBaz_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBaz
target:
bootstrap-info: ""
cluster-kubeconf: clusterBaz_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterFoo
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterFoo_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterFoo
target:
bootstrap-info: ""
cluster-kubeconf: clusterFoo_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""

View File

@ -0,0 +1,8 @@
Cluster: clusterFoo
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterFoo_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""

View File

@ -0,0 +1,54 @@
Cluster: clusterBar
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterBar_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBar
target:
bootstrap-info: ""
cluster-kubeconf: clusterBar_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBaz
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterBaz_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBaz
target:
bootstrap-info: ""
cluster-kubeconf: clusterBaz_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterFoo
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterFoo_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterFoo
target:
bootstrap-info: ""
cluster-kubeconf: clusterFoo_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""

View File

@ -0,0 +1,54 @@
Cluster: clusterBar
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterBar_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBar
target:
bootstrap-info: ""
cluster-kubeconf: clusterBar_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBaz
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterBaz_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterBaz
target:
bootstrap-info: ""
cluster-kubeconf: clusterBaz_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterFoo
ephemeral:
bootstrap-info: ""
cluster-kubeconf: clusterFoo_ephemeral
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""
Cluster: clusterFoo
target:
bootstrap-info: ""
cluster-kubeconf: clusterFoo_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""

View File

@ -0,0 +1,8 @@
Cluster: clusterFoo
target:
bootstrap-info: ""
cluster-kubeconf: clusterFoo_target
LocationOfOrigin: ""
insecure-skip-tls-verify: true
server: ""

View File

@ -0,0 +1,16 @@
Error: Cluster clusterMissing information was not found in the configuration.
Usage:
get-cluster NAME [flags]
Examples:
# List all the clusters airshipctl knows about
airshipctl config get-cluster
# Display a specific cluster
airshipctl config get-cluster e2e --cluster-type=ephemeral
Flags:
--cluster-type string cluster-type for the cluster entry in airshipctl config
-h, --help help for get-cluster

View File

@ -0,0 +1 @@
No clusters found in the configuration.