Files
airshipctl/pkg/config/test_utils.go
jezogwza b2af034e57 airshipctl config (replace 686508)
This implementation creates named references between an airship
config file , and a user specified or system  default kubeconfig file

airshipconfig location can be specified via an envirnment variable
or via
--airshipconf string   Path to file for airshipctl configuration.
                       (default ".airship/config")

kubeconfig has to be explicitly stated using the argument below
--kubeconfig string    Path to kubeconfig associated with airshipctl
                       configuration. (default ".airship/kubeconfig")

if the argument is not specified a default empty kubeconfig will be
used using the default ".airship/kubeconfig"

All subcommands exposed via airshipctl config will update airship
config and airship related kubeconfig
when appropriate.

This patchset adds :

- Config Struct (type)
- config cmd and pkg
- get_cluster : List a specific name cluster or
                List all clusters if no name is provided.
- set-cluster : Create or Modify an existing cluster.

Review comment fixes as of Pathset 19
- Moved core functionality from cmd to pkg
- Encapsulate cmd needs in pck in nw files cmds, cmds_types and cmds_test .
  Expectation is that other functions will need func an structs there.
- added test for GetCluster
- Added GetCluster method to config object to be used by get_cluster command
- Change ClusterNames func as per review suggestion
- Change TestEmpty Cluster to avoid pointing to non test kubecnfig by default
- Change constant AirshipConfigFilePath to AirshipConfigDir
- Renamed config_utils to utils
- Added config cmd output tests
- Changes to settings_test.go to clean after itself.
- Created new pkg/config/testdata/GoldenString for struct data comparison values to avoid confusion
- Fix small get_cluster no name issue when empty config
- Fix issue when reconciling a cluster info that only exists in airship config and not in kubeconfig

Increased coverage to: SUCCESS: Test coverage is at 84.2%,
Started to move all testdata to a single place under pkg/config for now.

Change-Id: I7aae1f15afaebc99407f7fabccecf86ab0923bc3
2019-11-05 15:42:42 +00:00

157 lines
4.4 KiB
Go

/*
Copyright 2014 The Kubernetes Authors.
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
import (
"net/url"
"os"
"path/filepath"
"testing"
"k8s.io/client-go/tools/clientcmd"
kubeconfig "k8s.io/client-go/tools/clientcmd/api"
"github.com/stretchr/testify/assert"
)
const (
testDataDir = "../../pkg/config/testdata"
testAirshipConfig = "testconfig"
testAirshipConfigDir = ".testairship"
testMimeType = ".yaml"
)
// DummyConfig used by tests, to initialize min set of data
func DummyConfig() *Config {
conf := NewConfig()
// Make sure the .airship directory is created
//conf.ConfigFilePath()
conf.Clusters["dummy_cluster"] = DummyClusterPurpose()
conf.KubeConfig().Clusters["dummycluster_target"] = conf.Clusters["dummy_cluster"].ClusterTypes[Target].KubeCluster()
conf.KubeConfig().Clusters["dummycluster_ephemeral"] =
conf.Clusters["dummy_cluster"].ClusterTypes[Ephemeral].KubeCluster()
conf.AuthInfos["dummy_user"] = DummyAuthInfo()
conf.Contexts["dummy_context"] = DummyContext()
conf.Manifests["dummy_manifest"] = DummyManifest()
conf.ModulesConfig = DummyModules()
conf.CurrentContext = "dummy_context"
return conf
}
// DummyContext , utility function used for tests
func DummyContext() *Context {
c := NewContext()
c.NameInKubeconf = "dummy_cluster"
c.Manifest = "dummy_manifest"
return c
}
// DummyCluster, utility function used for tests
func DummyCluster() *Cluster {
c := NewCluster()
cluster := kubeconfig.NewCluster()
cluster.Server = "http://dummy.server"
cluster.InsecureSkipTLSVerify = false
cluster.CertificateAuthority = "dummy_ca"
c.SetKubeCluster(cluster)
c.NameInKubeconf = "dummycluster_target"
c.Bootstrap = "dummy_bootstrap"
return c
}
// DummyManifest , utility function used for tests
func DummyManifest() *Manifest {
m := NewManifest()
// Repositories is the map of repository adddressable by a name
m.Repositories["dummy"] = DummyRepository()
m.TargetPath = "/var/tmp/"
return m
}
func DummyRepository() *Repository {
url, _ := url.Parse("http://dummy.url.com")
return &Repository{
Url: url,
Username: "dummy_user",
TargetPath: "dummy_targetpath",
}
}
func DummyAuthInfo() *AuthInfo {
return NewAuthInfo()
}
func DummyModules() *Modules {
return &Modules{Dummy: "dummy-module"}
}
// DummyClusterPurpose , utility function used for tests
func DummyClusterPurpose() *ClusterPurpose {
cp := NewClusterPurpose()
cp.ClusterTypes["ephemeral"] = DummyCluster()
cp.ClusterTypes["ephemeral"].NameInKubeconf = "dummycluster_ephemeral"
cp.ClusterTypes["target"] = DummyCluster()
return cp
}
func InitConfigAt(t *testing.T, airConfigFile, kConfigFile string) *Config {
conf := NewConfig()
kubePathOptions := clientcmd.NewDefaultPathOptions()
kubePathOptions.GlobalFile = kConfigFile
err := conf.LoadConfig(airConfigFile, kubePathOptions)
assert.Nil(t, err)
return conf
}
func InitConfig(t *testing.T) *Config {
airConfigFile := filepath.Join(testDataDir, AirshipConfig+testMimeType)
kConfigFile := filepath.Join(testDataDir, AirshipKubeConfig+testMimeType)
return InitConfigAt(t, airConfigFile, kConfigFile)
}
func DefaultInitConfig(t *testing.T) *Config {
conf := InitConfig(t)
airConfigFile := filepath.Join(AirshipConfigDir, AirshipConfig)
kConfigFile := filepath.Join(AirshipConfigDir, AirshipKubeConfig)
conf.SetLoadedConfigPath(airConfigFile)
kubePathOptions := clientcmd.NewDefaultPathOptions()
kubePathOptions.GlobalFile = kConfigFile
conf.SetLoadedPathOptions(kubePathOptions)
return conf
}
func Clean(conf *Config) error {
configDir := filepath.Dir(conf.LoadedConfigPath())
err := os.RemoveAll(configDir)
if !os.IsNotExist(err) {
return err
}
return nil
}
func DummyClusterOptions() *ClusterOptions {
co := &ClusterOptions{}
co.Name = "dummy_Cluster"
co.ClusterType = Ephemeral
co.Server = "http://1.1.1.1"
co.InsecureSkipTLSVerify = false
co.CertificateAuthority = ""
co.EmbedCAData = false
return co
}