From 16bc0cc81cb4559d60b960218803b0b6baea0a57 Mon Sep 17 00:00:00 2001 From: Alan Meadows Date: Wed, 11 Mar 2020 16:16:57 -0700 Subject: [PATCH] Properly locate tests within a config_test pkg for pkg/config and cmd/config This relocates tests that were improperly placed within the config pkg for both the pkg/config and cmd/config packages. These were causing testutil to be imported outside of tests causing a cyclic import issue when trying to import the config pkg within the document pkg. This patchset moves the tests as well as refactoring several of them where they were leveraging private attributes which cannot be accessed as an external test pkg. Closes: #109 Relates-To: #107 Change-Id: Ib1bf2aff020599ba0b3f5a7fd7fadf737b8c86b8 --- cmd/config/config_test.go | 11 +- cmd/config/set_authinfo_test.go | 15 +- cmd/config/set_cluster_test.go | 24 +-- cmd/config/set_context_test.go | 13 +- cmd/config/use_context_test.go | 3 +- cmd/document/pull_test.go | 2 +- pkg/config/cmds_test.go | 143 +++++++++--------- pkg/config/config.go | 4 + pkg/config/config_test.go | 72 ++++----- pkg/config/repo_test.go | 6 +- pkg/document/pull/pull_test.go | 2 +- pkg/remote/remote_direct_test.go | 3 +- .../test_utils.go => testutil/testconfig.go | 113 ++++++++------ 13 files changed, 222 insertions(+), 189 deletions(-) rename pkg/config/test_utils.go => testutil/testconfig.go (84%) diff --git a/cmd/config/config_test.go b/cmd/config/config_test.go index 50fdec507..12429b125 100644 --- a/cmd/config/config_test.go +++ b/cmd/config/config_test.go @@ -14,29 +14,26 @@ See the License for the specific language governing permissions and limitations under the License. */ -package config +package config_test import ( "testing" + cmd "opendev.org/airship/airshipctl/cmd/config" "opendev.org/airship/airshipctl/testutil" ) -const ( - testClusterName = "testCluster" -) - func TestConfig(t *testing.T) { cmdTests := []*testutil.CmdTest{ { Name: "config-cmd-with-defaults", CmdLine: "", - Cmd: NewConfigCommand(nil), + Cmd: cmd.NewConfigCommand(nil), }, { Name: "config-cmd-with-help", CmdLine: "--help", - Cmd: NewConfigCommand(nil), + Cmd: cmd.NewConfigCommand(nil), }, } diff --git a/cmd/config/set_authinfo_test.go b/cmd/config/set_authinfo_test.go index 0028d5c00..71989811a 100644 --- a/cmd/config/set_authinfo_test.go +++ b/cmd/config/set_authinfo_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package config +package config_test import ( "fmt" @@ -26,6 +26,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + cmd "opendev.org/airship/airshipctl/cmd/config" "opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/testutil" @@ -51,18 +52,18 @@ func TestConfigSetAuthInfo(t *testing.T) { { Name: "config-cmd-set-authinfo-with-help", CmdLine: "--help", - Cmd: NewCmdConfigSetAuthInfo(nil), + Cmd: cmd.NewCmdConfigSetAuthInfo(nil), }, { Name: "config-cmd-set-authinfo-too-many-args", CmdLine: "arg1 arg2", - Cmd: NewCmdConfigSetAuthInfo(nil), + Cmd: cmd.NewCmdConfigSetAuthInfo(nil), Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 2), }, { Name: "config-cmd-set-authinfo-too-few-args", CmdLine: "", - Cmd: NewCmdConfigSetAuthInfo(nil), + Cmd: cmd.NewCmdConfigSetAuthInfo(nil), Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 0), }, } @@ -76,7 +77,7 @@ func TestConfigSetAuthInfo(t *testing.T) { // Each of these config objects are associated with real files. Those files can be // cleaned up by calling cleanup func initInputConfig(t *testing.T) (given *config.Config, cleanup func(*testing.T)) { - given, givenCleanup := config.InitConfig(t) + given, givenCleanup := testutil.InitConfig(t) kubeAuthInfo := kubeconfig.NewAuthInfo() kubeAuthInfo.Username = testUsername kubeAuthInfo.Password = testPassword @@ -89,7 +90,7 @@ func initInputConfig(t *testing.T) (given *config.Config, cleanup func(*testing. func (test setAuthInfoTest) run(t *testing.T) { settings := &environment.AirshipCTLSettings{} settings.SetConfig(test.inputConfig) - test.cmdTest.Cmd = NewCmdConfigSetAuthInfo(settings) + test.cmdTest.Cmd = cmd.NewCmdConfigSetAuthInfo(settings) testutil.RunTest(t, test.cmdTest) afterRunConf := settings.Config() @@ -106,7 +107,7 @@ func (test setAuthInfoTest) run(t *testing.T) { } func TestSetAuthInfo(t *testing.T) { - given, cleanup := config.InitConfig(t) + given, cleanup := testutil.InitConfig(t) defer cleanup(t) input, cleanupInput := initInputConfig(t) diff --git a/cmd/config/set_cluster_test.go b/cmd/config/set_cluster_test.go index 1dbecafa6..f29bb7b07 100644 --- a/cmd/config/set_cluster_test.go +++ b/cmd/config/set_cluster_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package config +package config_test import ( "bytes" @@ -27,8 +27,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + cmd "opendev.org/airship/airshipctl/cmd/config" "opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/environment" + "opendev.org/airship/airshipctl/testutil" ) type setClusterTest struct { @@ -45,7 +47,7 @@ const ( ) func TestSetClusterWithCAFile(t *testing.T) { - given, cleanupGiven := config.InitConfig(t) + given, cleanupGiven := testutil.InitConfig(t) defer cleanupGiven(t) certFile := "../../pkg/config/testdata/ca.crt" @@ -53,7 +55,7 @@ func TestSetClusterWithCAFile(t *testing.T) { tname := testCluster tctype := config.Ephemeral - expected, cleanupExpected := config.InitConfig(t) + expected, cleanupExpected := testutil.InitConfig(t) defer cleanupExpected(t) expected.Clusters[tname] = config.NewClusterPurpose() @@ -83,7 +85,7 @@ func TestSetClusterWithCAFile(t *testing.T) { test.run(t) } func TestSetClusterWithCAFileData(t *testing.T) { - given, cleanupGiven := config.InitConfig(t) + given, cleanupGiven := testutil.InitConfig(t) defer cleanupGiven(t) certFile := "../../pkg/config/testdata/ca.crt" @@ -91,7 +93,7 @@ func TestSetClusterWithCAFileData(t *testing.T) { tname := testCluster tctype := config.Ephemeral - expected, cleanupExpected := config.InitConfig(t) + expected, cleanupExpected := testutil.InitConfig(t) defer cleanupExpected(t) expected.Clusters[tname] = config.NewClusterPurpose() @@ -125,13 +127,13 @@ func TestSetClusterWithCAFileData(t *testing.T) { } func TestSetCluster(t *testing.T) { - given, cleanupGiven := config.InitConfig(t) + given, cleanupGiven := testutil.InitConfig(t) defer cleanupGiven(t) tname := testCluster tctype := config.Ephemeral - expected, cleanupExpected := config.InitConfig(t) + expected, cleanupExpected := testutil.InitConfig(t) defer cleanupExpected(t) expected.Clusters[tname] = config.NewClusterPurpose() @@ -161,10 +163,10 @@ func TestSetCluster(t *testing.T) { } func TestModifyCluster(t *testing.T) { - tname := testClusterName + tname := "testCluster" tctype := config.Ephemeral - given, cleanupGiven := config.InitConfig(t) + given, cleanupGiven := testutil.InitConfig(t) defer cleanupGiven(t) given.Clusters[tname] = config.NewClusterPurpose() @@ -177,7 +179,7 @@ func TestModifyCluster(t *testing.T) { given.KubeConfig().Clusters[clusterName.Name()] = cluster given.Clusters[tname].ClusterTypes[tctype].SetKubeCluster(cluster) - expected, cleanupExpected := config.InitConfig(t) + expected, cleanupExpected := testutil.InitConfig(t) defer cleanupExpected(t) expected.Clusters[tname] = config.NewClusterPurpose() @@ -209,7 +211,7 @@ func (test setClusterTest) run(t *testing.T) { buf := bytes.NewBuffer([]byte{}) - cmd := NewCmdConfigSetCluster(settings) + cmd := cmd.NewCmdConfigSetCluster(settings) cmd.SetOut(buf) cmd.SetArgs(test.args) err := cmd.Flags().Parse(test.flags) diff --git a/cmd/config/set_context_test.go b/cmd/config/set_context_test.go index 5b0463306..b1e2f52dd 100644 --- a/cmd/config/set_context_test.go +++ b/cmd/config/set_context_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package config +package config_test import ( "fmt" @@ -24,6 +24,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + cmd "opendev.org/airship/airshipctl/cmd/config" "opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/testutil" @@ -48,17 +49,17 @@ func TestConfigSetContext(t *testing.T) { { Name: "config-cmd-set-context-with-help", CmdLine: "--help", - Cmd: NewCmdConfigSetContext(nil), + Cmd: cmd.NewCmdConfigSetContext(nil), }, { Name: "config-cmd-set-context-no-flags", CmdLine: "context", - Cmd: NewCmdConfigSetContext(nil), + Cmd: cmd.NewCmdConfigSetContext(nil), }, { Name: "config-cmd-set-context-too-many-args", CmdLine: "arg1 arg2", - Cmd: NewCmdConfigSetContext(nil), + Cmd: cmd.NewCmdConfigSetContext(nil), Error: fmt.Errorf("accepts at most %d arg(s), received %d", 1, 2), }, } @@ -69,7 +70,7 @@ func TestConfigSetContext(t *testing.T) { } func TestSetContext(t *testing.T) { - given, cleanupGiven := config.InitConfig(t) + given, cleanupGiven := testutil.InitConfig(t) defer cleanupGiven(t) tests := []struct { @@ -129,7 +130,7 @@ func (test setContextTest) run(t *testing.T) { settings := &environment.AirshipCTLSettings{} settings.SetConfig(test.givenConfig) - test.cmdTest.Cmd = NewCmdConfigSetContext(settings) + test.cmdTest.Cmd = cmd.NewCmdConfigSetContext(settings) testutil.RunTest(t, test.cmdTest) afterRunConf := settings.Config() diff --git a/cmd/config/use_context_test.go b/cmd/config/use_context_test.go index 415a55133..46f9f61b4 100644 --- a/cmd/config/use_context_test.go +++ b/cmd/config/use_context_test.go @@ -21,13 +21,12 @@ import ( "testing" cmd "opendev.org/airship/airshipctl/cmd/config" - "opendev.org/airship/airshipctl/pkg/config" "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/testutil" ) func TestConfigUseContext(t *testing.T) { - conf := config.DummyConfig() + conf := testutil.DummyConfig() settings := &environment.AirshipCTLSettings{} settings.SetConfig(conf) diff --git a/cmd/document/pull_test.go b/cmd/document/pull_test.go index c16d5cb2b..a5f18d478 100644 --- a/cmd/document/pull_test.go +++ b/cmd/document/pull_test.go @@ -14,7 +14,7 @@ import ( func getDummyAirshipSettings(t *testing.T) *environment.AirshipCTLSettings { settings := new(environment.AirshipCTLSettings) - conf := config.DummyConfig() + conf := testutil.DummyConfig() mfst := conf.Manifests["dummy_manifest"] err := fixtures.Init() diff --git a/pkg/config/cmds_test.go b/pkg/config/cmds_test.go index d55c6b831..1904196c9 100644 --- a/pkg/config/cmds_test.go +++ b/pkg/config/cmds_test.go @@ -14,58 +14,63 @@ See the License for the specific language governing permissions and limitations under the License. */ -package config +package config_test import ( "bytes" "testing" "github.com/stretchr/testify/assert" + + "opendev.org/airship/airshipctl/pkg/config" + "opendev.org/airship/airshipctl/testutil" ) func TestRunGetAuthInfo(t *testing.T) { t.Run("testNonExistentAuthInfo", func(t *testing.T) { - conf := DummyConfig() - dummyAuthInfoOptions := DummyAuthInfoOptions() + conf := testutil.DummyConfig() + dummyAuthInfoOptions := testutil.DummyAuthInfoOptions() dummyAuthInfoOptions.Name = "nonexistent_user" output := new(bytes.Buffer) - err := RunGetAuthInfo(dummyAuthInfoOptions, output, conf) + err := config.RunGetAuthInfo(dummyAuthInfoOptions, output, conf) assert.Error(t, err) }) t.Run("testSingleAuthInfo", func(t *testing.T) { - conf := DummyConfig() - dummyAuthInfoOptions := DummyAuthInfoOptions() + conf := testutil.DummyConfig() + dummyAuthInfoOptions := testutil.DummyAuthInfoOptions() output := new(bytes.Buffer) - err := RunGetAuthInfo(dummyAuthInfoOptions, output, conf) + err := config.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() + conf := testutil.DummyConfig() + secondAuthInfo := testutil.DummyAuthInfo() secondUserName := "second_user" - secondAuthInfo.authInfo.Username = secondUserName + newKubeAuthInfo := testutil.DummyKubeAuthInfo() + newKubeAuthInfo.Username = secondUserName + secondAuthInfo.SetKubeAuthInfo(newKubeAuthInfo) conf.AuthInfos[secondUserName] = secondAuthInfo - dummyAuthInfoOptions := DummyAuthInfoOptions() + dummyAuthInfoOptions := testutil.DummyAuthInfoOptions() dummyAuthInfoOptions.Name = "" output := new(bytes.Buffer) - err := RunGetAuthInfo(dummyAuthInfoOptions, output, conf) + err := config.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() + conf := testutil.DummyConfig() + dummyAuthInfoOptions := testutil.DummyAuthInfoOptions() dummyAuthInfoOptions.Name = "" delete(conf.AuthInfos, "dummy_user") output := new(bytes.Buffer) - err := RunGetAuthInfo(dummyAuthInfoOptions, output, conf) + err := config.RunGetAuthInfo(dummyAuthInfoOptions, output, conf) expectedMessage := "No User credentials found in the configuration.\n" assert.NoError(t, err) assert.Equal(t, expectedMessage, output.String()) @@ -76,34 +81,34 @@ func TestRunGetCluster(t *testing.T) { const dummyClusterEphemeralName = "dummy_cluster_ephemeral" t.Run("testNonExistentCluster", func(t *testing.T) { - conf := DummyConfig() - dummyClusterOptions := DummyClusterOptions() + conf := testutil.DummyConfig() + dummyClusterOptions := testutil.DummyClusterOptions() dummyClusterOptions.Name = "nonexistent_cluster" output := new(bytes.Buffer) - err := RunGetCluster(dummyClusterOptions, output, conf) + err := config.RunGetCluster(dummyClusterOptions, output, conf) assert.Error(t, err) }) t.Run("testSingleCluster", func(t *testing.T) { - conf := DummyConfig() - dummyClusterOptions := DummyClusterOptions() + conf := testutil.DummyConfig() + dummyClusterOptions := testutil.DummyClusterOptions() output := new(bytes.Buffer) - err := RunGetCluster(dummyClusterOptions, output, conf) - expectedCluster := DummyCluster() + err := config.RunGetCluster(dummyClusterOptions, output, conf) + expectedCluster := testutil.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() + conf := testutil.DummyConfig() + dummyClusterOptions := testutil.DummyClusterOptions() dummyClusterOptions.Name = "" output := new(bytes.Buffer) - err := RunGetCluster(dummyClusterOptions, output, conf) + err := config.RunGetCluster(dummyClusterOptions, output, conf) - expectedClusterTarget := DummyCluster() - expectedClusterEphemeral := DummyCluster() + expectedClusterTarget := testutil.DummyCluster() + expectedClusterEphemeral := testutil.DummyCluster() expectedClusterEphemeral.NameInKubeconf = dummyClusterEphemeralName expectedOutput := expectedClusterEphemeral.PrettyString() + "\n" + expectedClusterTarget.PrettyString() + "\n" @@ -112,12 +117,12 @@ func TestRunGetCluster(t *testing.T) { }) t.Run("testNoClusters", func(t *testing.T) { - conf := DummyConfig() - dummyClusterOptions := DummyClusterOptions() + conf := testutil.DummyConfig() + dummyClusterOptions := testutil.DummyClusterOptions() dummyClusterOptions.Name = "" delete(conf.Clusters, "dummy_cluster") output := new(bytes.Buffer) - err := RunGetCluster(dummyClusterOptions, output, conf) + err := config.RunGetCluster(dummyClusterOptions, output, conf) expectedMessage := "No clusters found in the configuration.\n" assert.NoError(t, err) assert.Equal(t, expectedMessage, output.String()) @@ -126,45 +131,45 @@ func TestRunGetCluster(t *testing.T) { func TestRunGetContext(t *testing.T) { t.Run("testNonExistentContext", func(t *testing.T) { - conf := DummyConfig() - dummyContextOptions := DummyContextOptions() + conf := testutil.DummyConfig() + dummyContextOptions := testutil.DummyContextOptions() dummyContextOptions.Name = "nonexistent_context" output := new(bytes.Buffer) - err := RunGetContext(dummyContextOptions, output, conf) + err := config.RunGetContext(dummyContextOptions, output, conf) assert.Error(t, err) }) t.Run("testSingleContext", func(t *testing.T) { - conf := DummyConfig() - dummyContextOptions := DummyContextOptions() + conf := testutil.DummyConfig() + dummyContextOptions := testutil.DummyContextOptions() output := new(bytes.Buffer) - err := RunGetContext(dummyContextOptions, output, conf) + err := config.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() + conf := testutil.DummyConfig() + newCtx := testutil.DummyContext() newCtx.NameInKubeconf = "second_context" conf.Contexts["second_context"] = newCtx - dummyContextOptions := DummyContextOptions() + dummyContextOptions := testutil.DummyContextOptions() dummyContextOptions.Name = "" output := new(bytes.Buffer) - err := RunGetContext(dummyContextOptions, output, conf) + err := config.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() + conf := testutil.DummyConfig() delete(conf.Contexts, "dummy_context") - dummyContextOptions := DummyContextOptions() + dummyContextOptions := testutil.DummyContextOptions() dummyContextOptions.Name = "" output := new(bytes.Buffer) - err := RunGetContext(dummyContextOptions, output, conf) + err := config.RunGetContext(dummyContextOptions, output, conf) expectedOutput := "No Contexts found in the configuration.\n" assert.NoError(t, err) assert.Equal(t, expectedOutput, output.String()) @@ -173,91 +178,93 @@ func TestRunGetContext(t *testing.T) { func TestRunSetAuthInfo(t *testing.T) { t.Run("testAddAuthInfo", func(t *testing.T) { - conf := DummyConfig() - dummyAuthInfoOptions := DummyAuthInfoOptions() + conf := testutil.DummyConfig() + dummyAuthInfoOptions := testutil.DummyAuthInfoOptions() dummyAuthInfoOptions.Name = "second_user" dummyAuthInfoOptions.Token = "" - modified, err := RunSetAuthInfo(dummyAuthInfoOptions, conf, false) + modified, err := config.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() + conf := testutil.DummyConfig() + dummyAuthInfoOptions := testutil.DummyAuthInfoOptions() dummyAuthInfoOptions.Name = "dummy_user" dummyAuthInfoOptions.Password = "testpassword123" dummyAuthInfoOptions.Token = "" - modified, err := RunSetAuthInfo(dummyAuthInfoOptions, conf, false) + modified, err := config.RunSetAuthInfo(dummyAuthInfoOptions, conf, false) assert.NoError(t, err) assert.True(t, modified) - assert.Equal(t, dummyAuthInfoOptions.Password, conf.AuthInfos["dummy_user"].authInfo.Password) + authInfo, err := conf.GetAuthInfo("dummy_user") + assert.NoError(t, err) + assert.Equal(t, dummyAuthInfoOptions.Password, authInfo.KubeAuthInfo().Password) }) } func TestRunSetCluster(t *testing.T) { t.Run("testAddCluster", func(t *testing.T) { - conf := DummyConfig() - dummyClusterOptions := DummyClusterOptions() + conf := testutil.DummyConfig() + dummyClusterOptions := testutil.DummyClusterOptions() dummyClusterOptions.Name = "second_cluster" - modified, err := RunSetCluster(dummyClusterOptions, conf, false) + modified, err := config.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() + conf := testutil.DummyConfig() + dummyClusterOptions := testutil.DummyClusterOptions() dummyClusterOptions.Server = "http://123.45.67.890" - modified, err := RunSetCluster(dummyClusterOptions, conf, false) + modified, err := config.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"].cluster.Server) + conf.Clusters["dummy_cluster"].ClusterTypes["ephemeral"].KubeCluster().Server) }) } func TestRunSetContext(t *testing.T) { t.Run("testAddContext", func(t *testing.T) { - conf := DummyConfig() - dummyContextOptions := DummyContextOptions() + conf := testutil.DummyConfig() + dummyContextOptions := testutil.DummyContextOptions() dummyContextOptions.Name = "second_context" - modified, err := RunSetContext(dummyContextOptions, conf, false) + modified, err := config.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() + conf := testutil.DummyConfig() + dummyContextOptions := testutil.DummyContextOptions() dummyContextOptions.Namespace = "new_namespace" - modified, err := RunSetContext(dummyContextOptions, conf, false) + modified, err := config.RunSetContext(dummyContextOptions, conf, false) assert.NoError(t, err) assert.True(t, modified) - assert.Equal(t, "new_namespace", conf.Contexts["dummy_context"].context.Namespace) + assert.Equal(t, "new_namespace", conf.Contexts["dummy_context"].KubeContext().Namespace) }) } func TestRunUseContext(t *testing.T) { t.Run("testUseContext", func(t *testing.T) { - conf := DummyConfig() - err := RunUseContext("dummy_context", conf) + conf := testutil.DummyConfig() + err := config.RunUseContext("dummy_context", conf) assert.Nil(t, err) }) t.Run("testUseContextDoesNotExist", func(t *testing.T) { - conf := NewConfig() - err := RunUseContext("foo", conf) + conf := config.NewConfig() + err := config.RunUseContext("foo", conf) assert.Error(t, err) }) } diff --git a/pkg/config/config.go b/pkg/config/config.go index 54f72eca7..d01badeff 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -416,6 +416,10 @@ func (c *Config) KubeConfig() *clientcmdapi.Config { return c.kubeConfig } +func (c *Config) SetKubeConfig(kubeConfig *clientcmdapi.Config) { + c.kubeConfig = kubeConfig +} + // Get A Cluster func (c *Config) GetCluster(cName, cType string) (*Cluster, error) { _, exists := c.Clusters[cName] diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 1793a3f6c..681a8b465 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -39,47 +39,47 @@ func TestString(t *testing.T) { }{ { name: "config", - stringer: config.DummyConfig(), + stringer: testutil.DummyConfig(), }, { name: "context", - stringer: config.DummyContext(), + stringer: testutil.DummyContext(), }, { name: "cluster", - stringer: config.DummyCluster(), + stringer: testutil.DummyCluster(), }, { name: "authinfo", - stringer: config.DummyAuthInfo(), + stringer: testutil.DummyAuthInfo(), }, { name: "manifest", - stringer: config.DummyManifest(), + stringer: testutil.DummyManifest(), }, { name: "modules", - stringer: config.DummyModules(), + stringer: testutil.DummyModules(), }, { name: "repository", - stringer: config.DummyRepository(), + stringer: testutil.DummyRepository(), }, { name: "repo-auth", - stringer: config.DummyRepoAuth(), + stringer: testutil.DummyRepoAuth(), }, { name: "repo-checkout", - stringer: config.DummyRepoCheckout(), + stringer: testutil.DummyRepoCheckout(), }, { name: "bootstrap", - stringer: config.DummyBootstrap(), + stringer: testutil.DummyBootstrap(), }, { name: "bootstrap", - stringer: config.DummyBootstrap(), + stringer: testutil.DummyBootstrap(), }, { name: "builder", @@ -116,7 +116,7 @@ func TestPrettyString(t *testing.T) { data, err := fSys.ReadFile("/prettycluster-string.yaml") require.NoError(t, err) - cluster := config.DummyCluster() + cluster := testutil.DummyCluster() assert.EqualValues(t, cluster.PrettyString(), string(data)) } @@ -230,7 +230,7 @@ func TestEqual(t *testing.T) { } func TestLoadConfig(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) assert.Len(t, conf.Clusters, 5) @@ -241,7 +241,7 @@ func TestLoadConfig(t *testing.T) { } func TestPersistConfig(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) err := conf.PersistConfig() @@ -370,7 +370,7 @@ func TestEnsureComplete(t *testing.T) { } func TestPurge(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) // Store it @@ -392,7 +392,7 @@ func TestPurge(t *testing.T) { } func TestKClusterString(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) kClusters := conf.KubeConfig().Clusters @@ -402,7 +402,7 @@ func TestKClusterString(t *testing.T) { assert.EqualValues(t, config.KClusterString(nil), "null\n") } func TestKContextString(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) kContexts := conf.KubeConfig().Contexts @@ -412,7 +412,7 @@ func TestKContextString(t *testing.T) { assert.EqualValues(t, config.KClusterString(nil), "null\n") } func TestKAuthInfoString(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) kAuthInfos := conf.KubeConfig().AuthInfos @@ -445,7 +445,7 @@ func TestValidClusterTypeFail(t *testing.T) { assert.Error(t, err) } func TestGetCluster(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) cluster, err := conf.GetCluster("def", config.Ephemeral) @@ -465,10 +465,10 @@ func TestGetCluster(t *testing.T) { } func TestAddCluster(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) - co := config.DummyClusterOptions() + co := testutil.DummyClusterOptions() cluster, err := conf.AddCluster(co) require.NoError(t, err) @@ -476,10 +476,10 @@ func TestAddCluster(t *testing.T) { } func TestModifyCluster(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) - co := config.DummyClusterOptions() + co := testutil.DummyClusterOptions() cluster, err := conf.AddCluster(co) require.NoError(t, err) @@ -498,7 +498,7 @@ func TestModifyCluster(t *testing.T) { } func TestGetClusters(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) clusters := conf.GetClusters() @@ -506,7 +506,7 @@ func TestGetClusters(t *testing.T) { } func TestGetContexts(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) contexts := conf.GetContexts() @@ -514,7 +514,7 @@ func TestGetContexts(t *testing.T) { } func TestGetContext(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) context, err := conf.GetContext("def_ephemeral") @@ -530,19 +530,19 @@ func TestGetContext(t *testing.T) { } func TestAddContext(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) - co := config.DummyContextOptions() + co := testutil.DummyContextOptions() context := conf.AddContext(co) assert.EqualValues(t, conf.Contexts[co.Name], context) } func TestModifyContext(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) - co := config.DummyContextOptions() + co := testutil.DummyContextOptions() context := conf.AddContext(co) co.Namespace += stringDelta @@ -560,7 +560,7 @@ func TestModifyContext(t *testing.T) { // AuthInfo Related func TestGetAuthInfos(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) authinfos := conf.GetAuthInfos() @@ -568,7 +568,7 @@ func TestGetAuthInfos(t *testing.T) { } func TestGetAuthInfo(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) authinfo, err := conf.GetAuthInfo("def-user") @@ -583,19 +583,19 @@ func TestGetAuthInfo(t *testing.T) { } func TestAddAuthInfo(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) - co := config.DummyAuthInfoOptions() + co := testutil.DummyAuthInfoOptions() authinfo := conf.AddAuthInfo(co) assert.EqualValues(t, conf.AuthInfos[co.Name], authinfo) } func TestModifyAuthInfo(t *testing.T) { - conf, cleanup := config.InitConfig(t) + conf, cleanup := testutil.InitConfig(t) defer cleanup(t) - co := config.DummyAuthInfoOptions() + co := testutil.DummyAuthInfoOptions() authinfo := conf.AddAuthInfo(co) co.Username += stringDelta diff --git a/pkg/config/repo_test.go b/pkg/config/repo_test.go index 8f83c969c..150bb8c13 100644 --- a/pkg/config/repo_test.go +++ b/pkg/config/repo_test.go @@ -1,4 +1,4 @@ -package config +package config_test import ( "testing" @@ -7,6 +7,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "opendev.org/airship/airshipctl/pkg/config" ) const ( @@ -147,7 +149,7 @@ type TestCase struct { } type TestRepos struct { - TestData map[string]*Repository `json:"test-data"` + TestData map[string]*config.Repository `json:"test-data"` } func TestToCheckout(t *testing.T) { diff --git a/pkg/document/pull/pull_test.go b/pkg/document/pull/pull_test.go index bf3006678..86f722b62 100644 --- a/pkg/document/pull/pull_test.go +++ b/pkg/document/pull/pull_test.go @@ -23,7 +23,7 @@ func getDummyPullSettings() *Settings { mockPullSettings := &Settings{ AirshipCTLSettings: new(environment.AirshipCTLSettings), } - mockConf := config.DummyConfig() + mockConf := testutil.DummyConfig() mockPullSettings.AirshipCTLSettings.SetConfig(mockConf) return mockPullSettings } diff --git a/pkg/remote/remote_direct_test.go b/pkg/remote/remote_direct_test.go index 387df6c96..0654073bb 100644 --- a/pkg/remote/remote_direct_test.go +++ b/pkg/remote/remote_direct_test.go @@ -10,11 +10,12 @@ import ( "opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/remote/redfish" + "opendev.org/airship/airshipctl/testutil" ) func initSettings(t *testing.T, rd *config.RemoteDirect, testdata string) *environment.AirshipCTLSettings { settings := &environment.AirshipCTLSettings{} - settings.SetConfig(config.DummyConfig()) + settings.SetConfig(testutil.DummyConfig()) bi, err := settings.Config().CurrentContextBootstrapInfo() require.NoError(t, err) bi.RemoteDirect = rd diff --git a/pkg/config/test_utils.go b/testutil/testconfig.go similarity index 84% rename from pkg/config/test_utils.go rename to testutil/testconfig.go index cbb7b9d0c..4c959eb9a 100644 --- a/pkg/config/test_utils.go +++ b/testutil/testconfig.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package config +package testutil import ( "io/ioutil" @@ -25,39 +25,42 @@ import ( "github.com/stretchr/testify/require" - "opendev.org/airship/airshipctl/testutil" + "opendev.org/airship/airshipctl/pkg/config" ) +// types cloned directory from pkg/config/types to prevent circular import + // DummyConfig used by tests, to initialize min set of data -func DummyConfig() *Config { - conf := &Config{ - Kind: AirshipConfigKind, - APIVersion: AirshipConfigAPIVersion, - Clusters: map[string]*ClusterPurpose{ +func DummyConfig() *config.Config { + conf := &config.Config{ + Kind: config.AirshipConfigKind, + APIVersion: config.AirshipConfigAPIVersion, + Clusters: map[string]*config.ClusterPurpose{ "dummy_cluster": DummyClusterPurpose(), }, - AuthInfos: map[string]*AuthInfo{ + AuthInfos: map[string]*config.AuthInfo{ "dummy_user": DummyAuthInfo(), }, - Contexts: map[string]*Context{ + Contexts: map[string]*config.Context{ "dummy_context": DummyContext(), }, - Manifests: map[string]*Manifest{ + Manifests: map[string]*config.Manifest{ "dummy_manifest": DummyManifest(), }, ModulesConfig: DummyModules(), CurrentContext: "dummy_context", - kubeConfig: kubeconfig.NewConfig(), } + conf.SetKubeConfig(kubeconfig.NewConfig()) + dummyCluster := conf.Clusters["dummy_cluster"] - conf.KubeConfig().Clusters["dummy_cluster_target"] = dummyCluster.ClusterTypes[Target].KubeCluster() - conf.KubeConfig().Clusters["dummy_cluster_ephemeral"] = dummyCluster.ClusterTypes[Ephemeral].KubeCluster() + conf.KubeConfig().Clusters["dummy_cluster_target"] = dummyCluster.ClusterTypes[config.Target].KubeCluster() + conf.KubeConfig().Clusters["dummy_cluster_ephemeral"] = dummyCluster.ClusterTypes[config.Ephemeral].KubeCluster() return conf } // DummyContext , utility function used for tests -func DummyContext() *Context { - c := NewContext() +func DummyContext() *config.Context { + c := config.NewContext() c.NameInKubeconf = "dummy_cluster_ephemeral" c.Manifest = "dummy_manifest" context := kubeconfig.NewContext() @@ -70,8 +73,8 @@ func DummyContext() *Context { } // DummyCluster, utility function used for tests -func DummyCluster() *Cluster { - c := NewCluster() +func DummyCluster() *config.Cluster { + c := config.NewCluster() cluster := kubeconfig.NewCluster() cluster.Server = "http://dummy.server" @@ -84,44 +87,48 @@ func DummyCluster() *Cluster { } // DummyManifest , utility function used for tests -func DummyManifest() *Manifest { - m := NewManifest() +func DummyManifest() *config.Manifest { + m := config.NewManifest() // Repositories is the map of repository adddressable by a name m.Repository = DummyRepository() m.TargetPath = "/var/tmp/" return m } -func DummyRepository() *Repository { - return &Repository{ +// DummyRepository, utility function used for tests +func DummyRepository() *config.Repository { + return &config.Repository{ URLString: "http://dummy.url.com", - CheckoutOptions: &RepoCheckout{ + CheckoutOptions: &config.RepoCheckout{ Tag: "v1.0.1", ForceCheckout: false, }, - Auth: &RepoAuth{ + Auth: &config.RepoAuth{ Type: "ssh-key", KeyPath: "testdata/test-key.pem", }, } } -func DummyRepoAuth() *RepoAuth { - return &RepoAuth{ +// DummyRepoAuth , utility function used for tests +func DummyRepoAuth() *config.RepoAuth { + return &config.RepoAuth{ Type: "ssh-key", KeyPath: "testdata/test-key.pem", } } -func DummyRepoCheckout() *RepoCheckout { - return &RepoCheckout{ +// DummyRepoCheckout, utility function used for checks +func DummyRepoCheckout() *config.RepoCheckout { + return &config.RepoCheckout{ Tag: "v1.0.1", ForceCheckout: false, } } -func DummyAuthInfo() *AuthInfo { - a := NewAuthInfo() +// DummyAuthInfo , utility function used for tests +func DummyAuthInfo() *config.AuthInfo { + a := config.NewAuthInfo() authinfo := kubeconfig.NewAuthInfo() authinfo.Username = "dummy_username" authinfo.Password = "dummy_password" @@ -132,15 +139,27 @@ func DummyAuthInfo() *AuthInfo { return a } -func DummyModules() *Modules { - m := NewModules() +// DummyKubeAuthInfo , utility function used for tests +func DummyKubeAuthInfo() *kubeconfig.AuthInfo { + authinfo := kubeconfig.NewAuthInfo() + authinfo.Username = "dummy_username" + authinfo.Password = "dummy_password" + authinfo.ClientCertificate = "dummy_certificate" + authinfo.ClientKey = "dummy_key" + authinfo.Token = "dummy_token" + return authinfo +} + +// DummyModules , utility function used for tests +func DummyModules() *config.Modules { + m := config.NewModules() m.BootstrapInfo["dummy_bootstrap_config"] = DummyBootstrap() return m } // DummyClusterPurpose , utility function used for tests -func DummyClusterPurpose() *ClusterPurpose { - cp := NewClusterPurpose() +func DummyClusterPurpose() *config.ClusterPurpose { + cp := config.NewClusterPurpose() cp.ClusterTypes["ephemeral"] = DummyCluster() cp.ClusterTypes["ephemeral"].NameInKubeconf = "dummy_cluster_ephemeral" cp.ClusterTypes["target"] = DummyCluster() @@ -152,9 +171,9 @@ func DummyClusterPurpose() *ClusterPurpose { // The returned config object will be associated with real files stored in a // directory in the user's temporary file storage // This directory can be cleaned up by calling the returned "cleanup" function -func InitConfig(t *testing.T) (conf *Config, cleanup func(*testing.T)) { +func InitConfig(t *testing.T) (conf *config.Config, cleanup func(*testing.T)) { t.Helper() - testDir, cleanup := testutil.TempDir(t, "airship-test") + testDir, cleanup := TempDir(t, "airship-test") configPath := filepath.Join(testDir, "config") err := ioutil.WriteFile(configPath, []byte(testConfigYAML), 0666) @@ -164,7 +183,7 @@ func InitConfig(t *testing.T) (conf *Config, cleanup func(*testing.T)) { err = ioutil.WriteFile(kubeConfigPath, []byte(testKubeConfigYAML), 0666) require.NoError(t, err) - conf = NewConfig() + conf = config.NewConfig() err = conf.LoadConfig(configPath, kubeConfigPath) require.NoError(t, err) @@ -172,10 +191,10 @@ func InitConfig(t *testing.T) (conf *Config, cleanup func(*testing.T)) { return conf, cleanup } -func DummyClusterOptions() *ClusterOptions { - co := &ClusterOptions{} +func DummyClusterOptions() *config.ClusterOptions { + co := &config.ClusterOptions{} co.Name = "dummy_cluster" - co.ClusterType = Ephemeral + co.ClusterType = config.Ephemeral co.Server = "http://1.1.1.1" co.InsecureSkipTLSVerify = false co.CertificateAuthority = "" @@ -184,8 +203,8 @@ func DummyClusterOptions() *ClusterOptions { return co } -func DummyContextOptions() *ContextOptions { - co := &ContextOptions{} +func DummyContextOptions() *config.ContextOptions { + co := &config.ContextOptions{} co.Name = "dummy_context" co.Manifest = "dummy_manifest" co.AuthInfo = "dummy_user" @@ -195,8 +214,8 @@ func DummyContextOptions() *ContextOptions { return co } -func DummyAuthInfoOptions() *AuthInfoOptions { - authinfo := &AuthInfoOptions{} +func DummyAuthInfoOptions() *config.AuthInfoOptions { + authinfo := &config.AuthInfoOptions{} authinfo.Username = "dummy_username" authinfo.Password = "dummy_password" authinfo.ClientCertificate = "dummy_certificate" @@ -205,14 +224,14 @@ func DummyAuthInfoOptions() *AuthInfoOptions { return authinfo } -func DummyBootstrap() *Bootstrap { - bs := &Bootstrap{} - cont := Container{ +func DummyBootstrap() *config.Bootstrap { + bs := &config.Bootstrap{} + cont := config.Container{ Volume: "/dummy:dummy", Image: "dummy_image:dummy_tag", ContainerRuntime: "docker", } - builder := Builder{ + builder := config.Builder{ UserDataFileName: "user-data", NetworkConfigFileName: "netconfig", OutputMetadataFileName: "output-metadata.yaml",