[#27] Move execution logic from cmd to pkg
Moves all execution logic from cmd/config to pkg/config. Adds tests for moved execution logic. Change-Id: I6381c9fe9eeba938e855bf7d7ea52cd22a5c08b6
This commit is contained in:
parent
dae73126e9
commit
69b37c15b8
@ -17,9 +17,6 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"opendev.org/airship/airshipctl/pkg/config"
|
||||
@ -49,41 +46,9 @@ func NewCmdConfigGetAuthInfo(rootSettings *environment.AirshipCTLSettings) *cobr
|
||||
if len(args) == 1 {
|
||||
theAuthInfo.Name = args[0]
|
||||
}
|
||||
return runGetAuthInfo(theAuthInfo, cmd.OutOrStdout(), rootSettings.Config())
|
||||
return config.RunGetAuthInfo(theAuthInfo, cmd.OutOrStdout(), rootSettings.Config())
|
||||
},
|
||||
}
|
||||
|
||||
return getauthinfocmd
|
||||
}
|
||||
|
||||
// runGetAuthInfo performs the execution of 'config get-credentials' sub command
|
||||
func runGetAuthInfo(o *config.AuthInfoOptions, out io.Writer, airconfig *config.Config) error {
|
||||
if o.Name == "" {
|
||||
return getAuthInfos(out, airconfig)
|
||||
}
|
||||
return getAuthInfo(o, out, airconfig)
|
||||
}
|
||||
|
||||
func getAuthInfo(o *config.AuthInfoOptions, out io.Writer, airconfig *config.Config) error {
|
||||
cName := o.Name
|
||||
authinfo, err := airconfig.GetAuthInfo(cName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(out, authinfo)
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAuthInfos(out io.Writer, airconfig *config.Config) error {
|
||||
authinfos, err := airconfig.GetAuthInfos()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(authinfos) == 0 {
|
||||
fmt.Fprintln(out, "No User credentials found in the configuration.")
|
||||
}
|
||||
for _, authinfo := range authinfos {
|
||||
fmt.Fprintln(out, authinfo)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -18,7 +18,6 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@ -48,7 +47,13 @@ func NewCmdConfigGetCluster(rootSettings *environment.AirshipCTLSettings) *cobra
|
||||
if len(args) == 1 {
|
||||
theCluster.Name = args[0]
|
||||
}
|
||||
return runGetCluster(theCluster, cmd.OutOrStdout(), rootSettings)
|
||||
|
||||
err := validate(theCluster)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return config.RunGetCluster(theCluster, cmd.OutOrStdout(), rootSettings.Config())
|
||||
},
|
||||
}
|
||||
|
||||
@ -62,47 +67,6 @@ func gcInitFlags(o *config.ClusterOptions, getclustercmd *cobra.Command) {
|
||||
config.FlagClusterType+" for the cluster entry in airshipctl config")
|
||||
}
|
||||
|
||||
// runGetCluster performs the execution of 'config get-cluster' sub command
|
||||
func runGetCluster(o *config.ClusterOptions, out io.Writer, rootSettings *environment.AirshipCTLSettings) error {
|
||||
err := validate(o)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if o.Name == "" {
|
||||
return getClusters(out, rootSettings)
|
||||
}
|
||||
return getCluster(o.Name, o.ClusterType, out, rootSettings)
|
||||
}
|
||||
|
||||
func getCluster(cName, cType string,
|
||||
out io.Writer, rootSettings *environment.AirshipCTLSettings) error {
|
||||
airconfig := rootSettings.Config()
|
||||
cluster, err := airconfig.GetCluster(cName, cType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "%s", cluster.PrettyString())
|
||||
return nil
|
||||
}
|
||||
|
||||
func getClusters(out io.Writer, rootSettings *environment.AirshipCTLSettings) error {
|
||||
airconfig := rootSettings.Config()
|
||||
clusters, err := airconfig.GetClusters()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(clusters) == 0 {
|
||||
fmt.Fprintln(out, "No clusters found in the configuration.")
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, cluster := range clusters {
|
||||
fmt.Fprintf(out, "%s\n", cluster.PrettyString())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validate(o *config.ClusterOptions) error {
|
||||
// Only an error if asking for a specific cluster
|
||||
if len(o.Name) == 0 {
|
||||
|
@ -18,7 +18,6 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@ -55,7 +54,7 @@ func NewCmdConfigGetContext(rootSettings *environment.AirshipCTLSettings) *cobra
|
||||
if len(args) == 1 {
|
||||
theContext.Name = args[0]
|
||||
}
|
||||
return runGetContext(theContext, cmd.OutOrStdout(), rootSettings.Config())
|
||||
return config.RunGetContext(theContext, cmd.OutOrStdout(), rootSettings.Config())
|
||||
},
|
||||
}
|
||||
|
||||
@ -68,38 +67,3 @@ func gctxInitFlags(o *config.ContextOptions, getcontextcmd *cobra.Command) {
|
||||
getcontextcmd.Flags().BoolVar(&o.CurrentContext, config.FlagCurrentContext, false,
|
||||
config.FlagCurrentContext+" to retrieve the current context entry in airshipctl config")
|
||||
}
|
||||
|
||||
// runGetContext performs the execution of 'config get-Context' sub command
|
||||
func runGetContext(o *config.ContextOptions, out io.Writer, airconfig *config.Config) error {
|
||||
if o.Name == "" && !o.CurrentContext {
|
||||
return getContexts(out, airconfig)
|
||||
}
|
||||
return getContext(o, out, airconfig)
|
||||
}
|
||||
|
||||
func getContext(o *config.ContextOptions, out io.Writer, airconfig *config.Config) error {
|
||||
cName := o.Name
|
||||
if o.CurrentContext {
|
||||
cName = airconfig.CurrentContext
|
||||
}
|
||||
context, err := airconfig.GetContext(cName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "%s", context.PrettyString())
|
||||
return nil
|
||||
}
|
||||
|
||||
func getContexts(out io.Writer, airconfig *config.Config) error {
|
||||
contexts, err := airconfig.GetContexts()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(contexts) == 0 {
|
||||
fmt.Fprintln(out, "No Contexts found in the configuration.")
|
||||
}
|
||||
for _, context := range contexts {
|
||||
fmt.Fprintf(out, "%s", context.PrettyString())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@ -78,7 +77,7 @@ func NewCmdConfigSetAuthInfo(rootSettings *environment.AirshipCTLSettings) *cobr
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
theAuthInfo.Name = args[0]
|
||||
modified, err := runSetAuthInfo(theAuthInfo, rootSettings.Config())
|
||||
modified, err := config.RunSetAuthInfo(theAuthInfo, rootSettings.Config(), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -127,36 +126,3 @@ func suInitFlags(o *config.AuthInfoOptions, setauthinfo *cobra.Command) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runSetAuthInfo(o *config.AuthInfoOptions, airconfig *config.Config) (bool, error) {
|
||||
authinfoWasModified := false
|
||||
err := o.Validate()
|
||||
if err != nil {
|
||||
return authinfoWasModified, err
|
||||
}
|
||||
|
||||
authinfoIWant := o.Name
|
||||
authinfo, err := airconfig.GetAuthInfo(authinfoIWant)
|
||||
if err != nil {
|
||||
var cerr config.ErrMissingConfig
|
||||
if !errors.As(err, &cerr) {
|
||||
// An error occurred, but it wasn't a "missing" config error.
|
||||
return authinfoWasModified, err
|
||||
}
|
||||
|
||||
// authinfo didn't exist, create it
|
||||
// ignoring the returned added authinfo
|
||||
airconfig.AddAuthInfo(o)
|
||||
} else {
|
||||
// AuthInfo exists, lets update
|
||||
airconfig.ModifyAuthInfo(authinfo, o)
|
||||
authinfoWasModified = true
|
||||
}
|
||||
// Update configuration file just in time persistence approach
|
||||
if err := airconfig.PersistConfig(); err != nil {
|
||||
// Error that it didnt persist the changes
|
||||
return authinfoWasModified, config.ErrConfigFailed{}
|
||||
}
|
||||
|
||||
return authinfoWasModified, nil
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@ -68,7 +67,7 @@ func NewCmdConfigSetCluster(rootSettings *environment.AirshipCTLSettings) *cobra
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
theCluster.Name = cmd.Flags().Args()[0]
|
||||
modified, err := runSetCluster(theCluster, rootSettings)
|
||||
modified, err := config.RunSetCluster(theCluster, rootSettings.Config(), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -107,46 +106,3 @@ func scInitFlags(o *config.ClusterOptions, setclustercmd *cobra.Command) {
|
||||
setclustercmd.Flags().BoolVar(&o.EmbedCAData, config.FlagEmbedCerts, false,
|
||||
config.FlagEmbedCerts+" for the cluster entry in airshipctl config")
|
||||
}
|
||||
|
||||
func runSetCluster(o *config.ClusterOptions, rootSettings *environment.AirshipCTLSettings) (bool, error) {
|
||||
clusterWasModified := false
|
||||
err := o.Validate()
|
||||
if err != nil {
|
||||
return clusterWasModified, err
|
||||
}
|
||||
|
||||
airconfig := rootSettings.Config()
|
||||
cluster, err := airconfig.GetCluster(o.Name, o.ClusterType)
|
||||
if err != nil {
|
||||
var cerr config.ErrMissingConfig
|
||||
if !errors.As(err, &cerr) {
|
||||
// An error occurred, but it wasn't a "missing" config error.
|
||||
return clusterWasModified, err
|
||||
}
|
||||
|
||||
// Cluster didn't exist, create it
|
||||
_, err := airconfig.AddCluster(o)
|
||||
if err != nil {
|
||||
return clusterWasModified, err
|
||||
}
|
||||
clusterWasModified = false
|
||||
} else {
|
||||
// Cluster exists, lets update
|
||||
_, err := airconfig.ModifyCluster(cluster, o)
|
||||
if err != nil {
|
||||
return clusterWasModified, err
|
||||
}
|
||||
clusterWasModified = true
|
||||
}
|
||||
|
||||
// Update configuration file
|
||||
// Just in time persistence approach
|
||||
if err := airconfig.PersistConfig(); err != nil {
|
||||
// Some warning here , that it didnt persist the changes because of this
|
||||
// Or should we float this up
|
||||
// What would it mean? No value.
|
||||
return clusterWasModified, err
|
||||
}
|
||||
|
||||
return clusterWasModified, nil
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@ -58,7 +57,7 @@ func NewCmdConfigSetContext(rootSettings *environment.AirshipCTLSettings) *cobra
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
theContext.Name = cmd.Flags().Args()[0]
|
||||
modified, err := runSetContext(theContext, rootSettings.Config())
|
||||
modified, err := config.RunSetContext(theContext, rootSettings.Config(), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -94,45 +93,3 @@ func sctxInitFlags(o *config.ContextOptions, setcontextcmd *cobra.Command) {
|
||||
setcontextcmd.Flags().StringVar(&o.ClusterType, config.FlagClusterType, "",
|
||||
config.FlagClusterType+" for the context entry in airshipctl config")
|
||||
}
|
||||
|
||||
func runSetContext(o *config.ContextOptions, airconfig *config.Config) (bool, error) {
|
||||
contextWasModified := false
|
||||
err := o.Validate()
|
||||
if err != nil {
|
||||
return contextWasModified, err
|
||||
}
|
||||
|
||||
contextIWant := o.Name
|
||||
context, err := airconfig.GetContext(contextIWant)
|
||||
if err != nil {
|
||||
var cerr config.ErrMissingConfig
|
||||
if !errors.As(err, &cerr) {
|
||||
// An error occurred, but it wasn't a "missing" config error.
|
||||
return contextWasModified, err
|
||||
}
|
||||
|
||||
if o.CurrentContext {
|
||||
return contextWasModified, config.ErrMissingConfig{}
|
||||
}
|
||||
// context didn't exist, create it
|
||||
// ignoring the returned added context
|
||||
airconfig.AddContext(o)
|
||||
} else {
|
||||
// Found the desired Current Context
|
||||
// Lets update it and be done.
|
||||
if o.CurrentContext {
|
||||
airconfig.CurrentContext = o.Name
|
||||
} else {
|
||||
// Context exists, lets update
|
||||
airconfig.ModifyContext(context, o)
|
||||
}
|
||||
contextWasModified = true
|
||||
}
|
||||
// Update configuration file just in time persistence approach
|
||||
if err := airconfig.PersistConfig(); err != nil {
|
||||
// Error that it didnt persist the changes
|
||||
return contextWasModified, config.ErrConfigFailed{}
|
||||
}
|
||||
|
||||
return contextWasModified, nil
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ package config
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
@ -87,3 +88,227 @@ func (o *AuthInfoOptions) Validate() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// runGetAuthInfo performs the execution of 'config get-credentials' sub command
|
||||
func RunGetAuthInfo(o *AuthInfoOptions, out io.Writer, airconfig *Config) error {
|
||||
if o.Name == "" {
|
||||
return getAuthInfos(out, airconfig)
|
||||
}
|
||||
return getAuthInfo(o, out, airconfig)
|
||||
}
|
||||
|
||||
func getAuthInfo(o *AuthInfoOptions, out io.Writer, airconfig *Config) error {
|
||||
cName := o.Name
|
||||
authinfo, err := airconfig.GetAuthInfo(cName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(out, authinfo)
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAuthInfos(out io.Writer, airconfig *Config) error {
|
||||
authinfos, err := airconfig.GetAuthInfos()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(authinfos) == 0 {
|
||||
fmt.Fprintln(out, "No User credentials found in the configuration.")
|
||||
}
|
||||
for _, authinfo := range authinfos {
|
||||
fmt.Fprintln(out, authinfo)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// runGetCluster performs the execution of 'config get-cluster' sub command
|
||||
func RunGetCluster(o *ClusterOptions, out io.Writer, airconfig *Config) error {
|
||||
if o.Name == "" {
|
||||
return getClusters(out, airconfig)
|
||||
}
|
||||
return getCluster(o.Name, o.ClusterType, out, airconfig)
|
||||
}
|
||||
|
||||
func getCluster(cName, cType string,
|
||||
out io.Writer, airconfig *Config) error {
|
||||
cluster, err := airconfig.GetCluster(cName, cType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "%s", cluster.PrettyString())
|
||||
return nil
|
||||
}
|
||||
|
||||
func getClusters(out io.Writer, airconfig *Config) error {
|
||||
clusters, err := airconfig.GetClusters()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(clusters) == 0 {
|
||||
fmt.Fprintln(out, "No clusters found in the configuration.")
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, cluster := range clusters {
|
||||
fmt.Fprintf(out, "%s\n", cluster.PrettyString())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// runGetContext performs the execution of 'config get-Context' sub command
|
||||
func RunGetContext(o *ContextOptions, out io.Writer, airconfig *Config) error {
|
||||
if o.Name == "" && !o.CurrentContext {
|
||||
return getContexts(out, airconfig)
|
||||
}
|
||||
return getContext(o, out, airconfig)
|
||||
}
|
||||
|
||||
func getContext(o *ContextOptions, out io.Writer, airconfig *Config) error {
|
||||
cName := o.Name
|
||||
if o.CurrentContext {
|
||||
cName = airconfig.CurrentContext
|
||||
}
|
||||
context, err := airconfig.GetContext(cName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(out, "%s", context.PrettyString())
|
||||
return nil
|
||||
}
|
||||
|
||||
func getContexts(out io.Writer, airconfig *Config) error {
|
||||
contexts, err := airconfig.GetContexts()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(contexts) == 0 {
|
||||
fmt.Fprintln(out, "No Contexts found in the configuration.")
|
||||
}
|
||||
for _, context := range contexts {
|
||||
fmt.Fprintf(out, "%s", context.PrettyString())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunSetAuthInfo(o *AuthInfoOptions, airconfig *Config, writeToStorage bool) (bool, error) {
|
||||
authinfoWasModified := false
|
||||
err := o.Validate()
|
||||
if err != nil {
|
||||
return authinfoWasModified, err
|
||||
}
|
||||
|
||||
authinfoIWant := o.Name
|
||||
authinfo, err := airconfig.GetAuthInfo(authinfoIWant)
|
||||
if err != nil {
|
||||
var cerr ErrMissingConfig
|
||||
if !errors.As(err, &cerr) {
|
||||
// An error occurred, but it wasn't a "missing" config error.
|
||||
return authinfoWasModified, err
|
||||
}
|
||||
|
||||
// authinfo didn't exist, create it
|
||||
// ignoring the returned added authinfo
|
||||
airconfig.AddAuthInfo(o)
|
||||
} else {
|
||||
// AuthInfo exists, lets update
|
||||
airconfig.ModifyAuthInfo(authinfo, o)
|
||||
authinfoWasModified = true
|
||||
}
|
||||
// Update configuration file just in time persistence approach
|
||||
if writeToStorage {
|
||||
if err := airconfig.PersistConfig(); err != nil {
|
||||
// Error that it didnt persist the changes
|
||||
return authinfoWasModified, ErrConfigFailed{}
|
||||
}
|
||||
}
|
||||
|
||||
return authinfoWasModified, nil
|
||||
}
|
||||
|
||||
func RunSetCluster(o *ClusterOptions, airconfig *Config, writeToStorage bool) (bool, error) {
|
||||
clusterWasModified := false
|
||||
err := o.Validate()
|
||||
if err != nil {
|
||||
return clusterWasModified, err
|
||||
}
|
||||
|
||||
cluster, err := airconfig.GetCluster(o.Name, o.ClusterType)
|
||||
if err != nil {
|
||||
var cerr ErrMissingConfig
|
||||
if !errors.As(err, &cerr) {
|
||||
// An error occurred, but it wasn't a "missing" config error.
|
||||
return clusterWasModified, err
|
||||
}
|
||||
|
||||
// Cluster didn't exist, create it
|
||||
_, err := airconfig.AddCluster(o)
|
||||
if err != nil {
|
||||
return clusterWasModified, err
|
||||
}
|
||||
clusterWasModified = false
|
||||
} else {
|
||||
// Cluster exists, lets update
|
||||
_, err := airconfig.ModifyCluster(cluster, o)
|
||||
if err != nil {
|
||||
return clusterWasModified, err
|
||||
}
|
||||
clusterWasModified = true
|
||||
}
|
||||
|
||||
// Update configuration file
|
||||
// Just in time persistence approach
|
||||
if writeToStorage {
|
||||
if err := airconfig.PersistConfig(); err != nil {
|
||||
// Some warning here , that it didnt persist the changes because of this
|
||||
// Or should we float this up
|
||||
// What would it mean? No value.
|
||||
return clusterWasModified, err
|
||||
}
|
||||
}
|
||||
|
||||
return clusterWasModified, nil
|
||||
}
|
||||
|
||||
func RunSetContext(o *ContextOptions, airconfig *Config, writeToStorage bool) (bool, error) {
|
||||
contextWasModified := false
|
||||
err := o.Validate()
|
||||
if err != nil {
|
||||
return contextWasModified, err
|
||||
}
|
||||
|
||||
contextIWant := o.Name
|
||||
context, err := airconfig.GetContext(contextIWant)
|
||||
if err != nil {
|
||||
var cerr ErrMissingConfig
|
||||
if !errors.As(err, &cerr) {
|
||||
// An error occurred, but it wasn't a "missing" config error.
|
||||
return contextWasModified, err
|
||||
}
|
||||
|
||||
if o.CurrentContext {
|
||||
return contextWasModified, ErrMissingConfig{}
|
||||
}
|
||||
// context didn't exist, create it
|
||||
// ignoring the returned added context
|
||||
airconfig.AddContext(o)
|
||||
} else {
|
||||
// Found the desired Current Context
|
||||
// Lets update it and be done.
|
||||
if o.CurrentContext {
|
||||
airconfig.CurrentContext = o.Name
|
||||
} else {
|
||||
// Context exists, lets update
|
||||
airconfig.ModifyContext(context, o)
|
||||
}
|
||||
contextWasModified = true
|
||||
}
|
||||
// Update configuration file just in time persistence approach
|
||||
if writeToStorage {
|
||||
if err := airconfig.PersistConfig(); err != nil {
|
||||
// Error that it didnt persist the changes
|
||||
return contextWasModified, ErrConfigFailed{}
|
||||
}
|
||||
}
|
||||
|
||||
return contextWasModified, nil
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -80,3 +81,228 @@ func TestValidateAuthInfo(t *testing.T) {
|
||||
err = co.Validate()
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRunGetAuthInfo(t *testing.T) {
|
||||
t.Run("testNonExistentAuthInfo", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyAuthInfoOptions := DummyAuthInfoOptions()
|
||||
dummyAuthInfoOptions.Name = "nonexistent_user"
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetAuthInfo(dummyAuthInfoOptions, output, conf)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("testSingleAuthInfo", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyAuthInfoOptions := DummyAuthInfoOptions()
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetAuthInfo(dummyAuthInfoOptions, output, conf)
|
||||
expectedOutput := conf.AuthInfos["dummy_user"].String() + "\n"
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedOutput, output.String())
|
||||
})
|
||||
|
||||
t.Run("testAllAuthInfo", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
secondAuthInfo := DummyAuthInfo()
|
||||
secondUserName := "second_user"
|
||||
secondAuthInfo.kAuthInfo.Username = secondUserName
|
||||
conf.AuthInfos[secondUserName] = secondAuthInfo
|
||||
|
||||
dummyAuthInfoOptions := DummyAuthInfoOptions()
|
||||
dummyAuthInfoOptions.Name = ""
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetAuthInfo(dummyAuthInfoOptions, output, conf)
|
||||
expectedOutput := conf.AuthInfos["dummy_user"].String() + "\n" + conf.AuthInfos[secondUserName].String() + "\n"
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedOutput, output.String())
|
||||
})
|
||||
|
||||
t.Run("testNoAuthInfos", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyAuthInfoOptions := DummyAuthInfoOptions()
|
||||
dummyAuthInfoOptions.Name = ""
|
||||
delete(conf.AuthInfos, "dummy_user")
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetAuthInfo(dummyAuthInfoOptions, output, conf)
|
||||
expectedMessage := "No User credentials found in the configuration.\n"
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedMessage, output.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunGetCluster(t *testing.T) {
|
||||
const dummyClusterEphemeralName = "dummy_cluster_ephemeral"
|
||||
|
||||
t.Run("testNonExistentCluster", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyClusterOptions := DummyClusterOptions()
|
||||
dummyClusterOptions.Name = "nonexistent_cluster"
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetCluster(dummyClusterOptions, output, conf)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("testSingleCluster", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyClusterOptions := DummyClusterOptions()
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetCluster(dummyClusterOptions, output, conf)
|
||||
expectedCluster := DummyCluster()
|
||||
expectedCluster.NameInKubeconf = dummyClusterEphemeralName
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedCluster.PrettyString(), output.String())
|
||||
})
|
||||
|
||||
t.Run("testAllClusters", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyClusterOptions := DummyClusterOptions()
|
||||
dummyClusterOptions.Name = ""
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetCluster(dummyClusterOptions, output, conf)
|
||||
|
||||
expectedClusterTarget := DummyCluster()
|
||||
expectedClusterEphemeral := DummyCluster()
|
||||
expectedClusterEphemeral.NameInKubeconf = dummyClusterEphemeralName
|
||||
expectedOutput := expectedClusterEphemeral.PrettyString() + "\n" + expectedClusterTarget.PrettyString() + "\n"
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedOutput, output.String())
|
||||
})
|
||||
|
||||
t.Run("testNoClusters", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyClusterOptions := DummyClusterOptions()
|
||||
dummyClusterOptions.Name = ""
|
||||
delete(conf.Clusters, "dummy_cluster")
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetCluster(dummyClusterOptions, output, conf)
|
||||
expectedMessage := "No clusters found in the configuration.\n"
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedMessage, output.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunGetContext(t *testing.T) {
|
||||
t.Run("testNonExistentContext", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyContextOptions := DummyContextOptions()
|
||||
dummyContextOptions.Name = "nonexistent_context"
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetContext(dummyContextOptions, output, conf)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("testSingleContext", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyContextOptions := DummyContextOptions()
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetContext(dummyContextOptions, output, conf)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, conf.Contexts["dummy_context"].PrettyString(), output.String())
|
||||
})
|
||||
|
||||
t.Run("testAllContext", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
newCtx := DummyContext()
|
||||
newCtx.NameInKubeconf = "second_context"
|
||||
conf.Contexts["second_context"] = newCtx
|
||||
|
||||
dummyContextOptions := DummyContextOptions()
|
||||
dummyContextOptions.Name = ""
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetContext(dummyContextOptions, output, conf)
|
||||
expectedOutput := conf.Contexts["dummy_context"].PrettyString() + conf.Contexts["second_context"].PrettyString()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedOutput, output.String())
|
||||
})
|
||||
|
||||
t.Run("testNoContexts", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
delete(conf.Contexts, "dummy_context")
|
||||
dummyContextOptions := DummyContextOptions()
|
||||
dummyContextOptions.Name = ""
|
||||
output := new(bytes.Buffer)
|
||||
err := RunGetContext(dummyContextOptions, output, conf)
|
||||
expectedOutput := "No Contexts found in the configuration.\n"
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expectedOutput, output.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunSetAuthInfo(t *testing.T) {
|
||||
t.Run("testAddAuthInfo", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyAuthInfoOptions := DummyAuthInfoOptions()
|
||||
dummyAuthInfoOptions.Name = "second_user"
|
||||
dummyAuthInfoOptions.Token = ""
|
||||
|
||||
modified, err := RunSetAuthInfo(dummyAuthInfoOptions, conf, false)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, modified)
|
||||
assert.Contains(t, conf.AuthInfos, "second_user")
|
||||
})
|
||||
|
||||
t.Run("testModifyAuthInfo", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyAuthInfoOptions := DummyAuthInfoOptions()
|
||||
dummyAuthInfoOptions.Name = "dummy_user"
|
||||
dummyAuthInfoOptions.Password = "testpassword123"
|
||||
dummyAuthInfoOptions.Token = ""
|
||||
|
||||
modified, err := RunSetAuthInfo(dummyAuthInfoOptions, conf, false)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, modified)
|
||||
assert.Equal(t, dummyAuthInfoOptions.Password, conf.AuthInfos["dummy_user"].kAuthInfo.Password)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunSetCluster(t *testing.T) {
|
||||
t.Run("testAddCluster", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyClusterOptions := DummyClusterOptions()
|
||||
dummyClusterOptions.Name = "second_cluster"
|
||||
|
||||
modified, err := RunSetCluster(dummyClusterOptions, conf, false)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, modified)
|
||||
assert.Contains(t, conf.Clusters, "second_cluster")
|
||||
})
|
||||
|
||||
t.Run("testModifyCluster", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyClusterOptions := DummyClusterOptions()
|
||||
dummyClusterOptions.Server = "http://123.45.67.890"
|
||||
|
||||
modified, err := RunSetCluster(dummyClusterOptions, conf, false)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, modified)
|
||||
assert.Equal(
|
||||
t, "http://123.45.67.890",
|
||||
conf.Clusters["dummy_cluster"].ClusterTypes["ephemeral"].kCluster.Server)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunSetContext(t *testing.T) {
|
||||
t.Run("testAddContext", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyContextOptions := DummyContextOptions()
|
||||
dummyContextOptions.Name = "second_context"
|
||||
|
||||
modified, err := RunSetContext(dummyContextOptions, conf, false)
|
||||
assert.NoError(t, err)
|
||||
assert.False(t, modified)
|
||||
assert.Contains(t, conf.Contexts, "second_context")
|
||||
})
|
||||
|
||||
t.Run("testModifyContext", func(t *testing.T) {
|
||||
conf := DummyConfig()
|
||||
dummyContextOptions := DummyContextOptions()
|
||||
dummyContextOptions.Namespace = "new_namespace"
|
||||
|
||||
modified, err := RunSetContext(dummyContextOptions, conf, false)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, modified)
|
||||
assert.Equal(t, "new_namespace", conf.Contexts["dummy_context"].kContext.Namespace)
|
||||
})
|
||||
}
|
||||
|
@ -152,7 +152,7 @@ func InitConfig(t *testing.T) *Config {
|
||||
|
||||
func DummyClusterOptions() *ClusterOptions {
|
||||
co := &ClusterOptions{}
|
||||
co.Name = "dummy_Cluster"
|
||||
co.Name = "dummy_cluster"
|
||||
co.ClusterType = Ephemeral
|
||||
co.Server = "http://1.1.1.1"
|
||||
co.InsecureSkipTLSVerify = false
|
||||
|
Loading…
x
Reference in New Issue
Block a user