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
This commit is contained in:
Alan Meadows 2020-03-11 16:16:57 -07:00
parent dc9de0114b
commit 16bc0cc81c
13 changed files with 222 additions and 189 deletions

View File

@ -14,29 +14,26 @@ 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 (
"testing" "testing"
cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
const (
testClusterName = "testCluster"
)
func TestConfig(t *testing.T) { func TestConfig(t *testing.T) {
cmdTests := []*testutil.CmdTest{ cmdTests := []*testutil.CmdTest{
{ {
Name: "config-cmd-with-defaults", Name: "config-cmd-with-defaults",
CmdLine: "", CmdLine: "",
Cmd: NewConfigCommand(nil), Cmd: cmd.NewConfigCommand(nil),
}, },
{ {
Name: "config-cmd-with-help", Name: "config-cmd-with-help",
CmdLine: "--help", CmdLine: "--help",
Cmd: NewConfigCommand(nil), Cmd: cmd.NewConfigCommand(nil),
}, },
} }

View File

@ -14,7 +14,7 @@ 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 (
"fmt" "fmt"
@ -26,6 +26,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "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" "opendev.org/airship/airshipctl/testutil"
@ -51,18 +52,18 @@ func TestConfigSetAuthInfo(t *testing.T) {
{ {
Name: "config-cmd-set-authinfo-with-help", Name: "config-cmd-set-authinfo-with-help",
CmdLine: "--help", CmdLine: "--help",
Cmd: NewCmdConfigSetAuthInfo(nil), Cmd: cmd.NewCmdConfigSetAuthInfo(nil),
}, },
{ {
Name: "config-cmd-set-authinfo-too-many-args", Name: "config-cmd-set-authinfo-too-many-args",
CmdLine: "arg1 arg2", CmdLine: "arg1 arg2",
Cmd: NewCmdConfigSetAuthInfo(nil), Cmd: cmd.NewCmdConfigSetAuthInfo(nil),
Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 2), Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 2),
}, },
{ {
Name: "config-cmd-set-authinfo-too-few-args", Name: "config-cmd-set-authinfo-too-few-args",
CmdLine: "", CmdLine: "",
Cmd: NewCmdConfigSetAuthInfo(nil), Cmd: cmd.NewCmdConfigSetAuthInfo(nil),
Error: fmt.Errorf("accepts %d arg(s), received %d", 1, 0), 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 // Each of these config objects are associated with real files. Those files can be
// cleaned up by calling cleanup // cleaned up by calling cleanup
func initInputConfig(t *testing.T) (given *config.Config, cleanup func(*testing.T)) { 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 := kubeconfig.NewAuthInfo()
kubeAuthInfo.Username = testUsername kubeAuthInfo.Username = testUsername
kubeAuthInfo.Password = testPassword 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) { func (test setAuthInfoTest) run(t *testing.T) {
settings := &environment.AirshipCTLSettings{} settings := &environment.AirshipCTLSettings{}
settings.SetConfig(test.inputConfig) settings.SetConfig(test.inputConfig)
test.cmdTest.Cmd = NewCmdConfigSetAuthInfo(settings) test.cmdTest.Cmd = cmd.NewCmdConfigSetAuthInfo(settings)
testutil.RunTest(t, test.cmdTest) testutil.RunTest(t, test.cmdTest)
afterRunConf := settings.Config() afterRunConf := settings.Config()
@ -106,7 +107,7 @@ func (test setAuthInfoTest) run(t *testing.T) {
} }
func TestSetAuthInfo(t *testing.T) { func TestSetAuthInfo(t *testing.T) {
given, cleanup := config.InitConfig(t) given, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
input, cleanupInput := initInputConfig(t) input, cleanupInput := initInputConfig(t)

View File

@ -14,7 +14,7 @@ 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" "bytes"
@ -27,8 +27,10 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "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 setClusterTest struct { type setClusterTest struct {
@ -45,7 +47,7 @@ const (
) )
func TestSetClusterWithCAFile(t *testing.T) { func TestSetClusterWithCAFile(t *testing.T) {
given, cleanupGiven := config.InitConfig(t) given, cleanupGiven := testutil.InitConfig(t)
defer cleanupGiven(t) defer cleanupGiven(t)
certFile := "../../pkg/config/testdata/ca.crt" certFile := "../../pkg/config/testdata/ca.crt"
@ -53,7 +55,7 @@ func TestSetClusterWithCAFile(t *testing.T) {
tname := testCluster tname := testCluster
tctype := config.Ephemeral tctype := config.Ephemeral
expected, cleanupExpected := config.InitConfig(t) expected, cleanupExpected := testutil.InitConfig(t)
defer cleanupExpected(t) defer cleanupExpected(t)
expected.Clusters[tname] = config.NewClusterPurpose() expected.Clusters[tname] = config.NewClusterPurpose()
@ -83,7 +85,7 @@ func TestSetClusterWithCAFile(t *testing.T) {
test.run(t) test.run(t)
} }
func TestSetClusterWithCAFileData(t *testing.T) { func TestSetClusterWithCAFileData(t *testing.T) {
given, cleanupGiven := config.InitConfig(t) given, cleanupGiven := testutil.InitConfig(t)
defer cleanupGiven(t) defer cleanupGiven(t)
certFile := "../../pkg/config/testdata/ca.crt" certFile := "../../pkg/config/testdata/ca.crt"
@ -91,7 +93,7 @@ func TestSetClusterWithCAFileData(t *testing.T) {
tname := testCluster tname := testCluster
tctype := config.Ephemeral tctype := config.Ephemeral
expected, cleanupExpected := config.InitConfig(t) expected, cleanupExpected := testutil.InitConfig(t)
defer cleanupExpected(t) defer cleanupExpected(t)
expected.Clusters[tname] = config.NewClusterPurpose() expected.Clusters[tname] = config.NewClusterPurpose()
@ -125,13 +127,13 @@ func TestSetClusterWithCAFileData(t *testing.T) {
} }
func TestSetCluster(t *testing.T) { func TestSetCluster(t *testing.T) {
given, cleanupGiven := config.InitConfig(t) given, cleanupGiven := testutil.InitConfig(t)
defer cleanupGiven(t) defer cleanupGiven(t)
tname := testCluster tname := testCluster
tctype := config.Ephemeral tctype := config.Ephemeral
expected, cleanupExpected := config.InitConfig(t) expected, cleanupExpected := testutil.InitConfig(t)
defer cleanupExpected(t) defer cleanupExpected(t)
expected.Clusters[tname] = config.NewClusterPurpose() expected.Clusters[tname] = config.NewClusterPurpose()
@ -161,10 +163,10 @@ func TestSetCluster(t *testing.T) {
} }
func TestModifyCluster(t *testing.T) { func TestModifyCluster(t *testing.T) {
tname := testClusterName tname := "testCluster"
tctype := config.Ephemeral tctype := config.Ephemeral
given, cleanupGiven := config.InitConfig(t) given, cleanupGiven := testutil.InitConfig(t)
defer cleanupGiven(t) defer cleanupGiven(t)
given.Clusters[tname] = config.NewClusterPurpose() given.Clusters[tname] = config.NewClusterPurpose()
@ -177,7 +179,7 @@ func TestModifyCluster(t *testing.T) {
given.KubeConfig().Clusters[clusterName.Name()] = cluster given.KubeConfig().Clusters[clusterName.Name()] = cluster
given.Clusters[tname].ClusterTypes[tctype].SetKubeCluster(cluster) given.Clusters[tname].ClusterTypes[tctype].SetKubeCluster(cluster)
expected, cleanupExpected := config.InitConfig(t) expected, cleanupExpected := testutil.InitConfig(t)
defer cleanupExpected(t) defer cleanupExpected(t)
expected.Clusters[tname] = config.NewClusterPurpose() expected.Clusters[tname] = config.NewClusterPurpose()
@ -209,7 +211,7 @@ func (test setClusterTest) run(t *testing.T) {
buf := bytes.NewBuffer([]byte{}) buf := bytes.NewBuffer([]byte{})
cmd := NewCmdConfigSetCluster(settings) cmd := cmd.NewCmdConfigSetCluster(settings)
cmd.SetOut(buf) cmd.SetOut(buf)
cmd.SetArgs(test.args) cmd.SetArgs(test.args)
err := cmd.Flags().Parse(test.flags) err := cmd.Flags().Parse(test.flags)

View File

@ -14,7 +14,7 @@ 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 (
"fmt" "fmt"
@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "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" "opendev.org/airship/airshipctl/testutil"
@ -48,17 +49,17 @@ func TestConfigSetContext(t *testing.T) {
{ {
Name: "config-cmd-set-context-with-help", Name: "config-cmd-set-context-with-help",
CmdLine: "--help", CmdLine: "--help",
Cmd: NewCmdConfigSetContext(nil), Cmd: cmd.NewCmdConfigSetContext(nil),
}, },
{ {
Name: "config-cmd-set-context-no-flags", Name: "config-cmd-set-context-no-flags",
CmdLine: "context", CmdLine: "context",
Cmd: NewCmdConfigSetContext(nil), Cmd: cmd.NewCmdConfigSetContext(nil),
}, },
{ {
Name: "config-cmd-set-context-too-many-args", Name: "config-cmd-set-context-too-many-args",
CmdLine: "arg1 arg2", CmdLine: "arg1 arg2",
Cmd: NewCmdConfigSetContext(nil), Cmd: cmd.NewCmdConfigSetContext(nil),
Error: fmt.Errorf("accepts at most %d arg(s), received %d", 1, 2), 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) { func TestSetContext(t *testing.T) {
given, cleanupGiven := config.InitConfig(t) given, cleanupGiven := testutil.InitConfig(t)
defer cleanupGiven(t) defer cleanupGiven(t)
tests := []struct { tests := []struct {
@ -129,7 +130,7 @@ func (test setContextTest) run(t *testing.T) {
settings := &environment.AirshipCTLSettings{} settings := &environment.AirshipCTLSettings{}
settings.SetConfig(test.givenConfig) settings.SetConfig(test.givenConfig)
test.cmdTest.Cmd = NewCmdConfigSetContext(settings) test.cmdTest.Cmd = cmd.NewCmdConfigSetContext(settings)
testutil.RunTest(t, test.cmdTest) testutil.RunTest(t, test.cmdTest)
afterRunConf := settings.Config() afterRunConf := settings.Config()

View File

@ -21,13 +21,12 @@ import (
"testing" "testing"
cmd "opendev.org/airship/airshipctl/cmd/config" cmd "opendev.org/airship/airshipctl/cmd/config"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/testutil" "opendev.org/airship/airshipctl/testutil"
) )
func TestConfigUseContext(t *testing.T) { func TestConfigUseContext(t *testing.T) {
conf := config.DummyConfig() conf := testutil.DummyConfig()
settings := &environment.AirshipCTLSettings{} settings := &environment.AirshipCTLSettings{}
settings.SetConfig(conf) settings.SetConfig(conf)

View File

@ -14,7 +14,7 @@ import (
func getDummyAirshipSettings(t *testing.T) *environment.AirshipCTLSettings { func getDummyAirshipSettings(t *testing.T) *environment.AirshipCTLSettings {
settings := new(environment.AirshipCTLSettings) settings := new(environment.AirshipCTLSettings)
conf := config.DummyConfig() conf := testutil.DummyConfig()
mfst := conf.Manifests["dummy_manifest"] mfst := conf.Manifests["dummy_manifest"]
err := fixtures.Init() err := fixtures.Init()

View File

@ -14,58 +14,63 @@ 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" "bytes"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"opendev.org/airship/airshipctl/pkg/config"
"opendev.org/airship/airshipctl/testutil"
) )
func TestRunGetAuthInfo(t *testing.T) { func TestRunGetAuthInfo(t *testing.T) {
t.Run("testNonExistentAuthInfo", func(t *testing.T) { t.Run("testNonExistentAuthInfo", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyAuthInfoOptions := DummyAuthInfoOptions() dummyAuthInfoOptions := testutil.DummyAuthInfoOptions()
dummyAuthInfoOptions.Name = "nonexistent_user" dummyAuthInfoOptions.Name = "nonexistent_user"
output := new(bytes.Buffer) output := new(bytes.Buffer)
err := RunGetAuthInfo(dummyAuthInfoOptions, output, conf) err := config.RunGetAuthInfo(dummyAuthInfoOptions, output, conf)
assert.Error(t, err) assert.Error(t, err)
}) })
t.Run("testSingleAuthInfo", func(t *testing.T) { t.Run("testSingleAuthInfo", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyAuthInfoOptions := DummyAuthInfoOptions() dummyAuthInfoOptions := testutil.DummyAuthInfoOptions()
output := new(bytes.Buffer) output := new(bytes.Buffer)
err := RunGetAuthInfo(dummyAuthInfoOptions, output, conf) err := config.RunGetAuthInfo(dummyAuthInfoOptions, output, conf)
expectedOutput := conf.AuthInfos["dummy_user"].String() + "\n" expectedOutput := conf.AuthInfos["dummy_user"].String() + "\n"
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expectedOutput, output.String()) assert.Equal(t, expectedOutput, output.String())
}) })
t.Run("testAllAuthInfo", func(t *testing.T) { t.Run("testAllAuthInfo", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
secondAuthInfo := DummyAuthInfo() secondAuthInfo := testutil.DummyAuthInfo()
secondUserName := "second_user" secondUserName := "second_user"
secondAuthInfo.authInfo.Username = secondUserName newKubeAuthInfo := testutil.DummyKubeAuthInfo()
newKubeAuthInfo.Username = secondUserName
secondAuthInfo.SetKubeAuthInfo(newKubeAuthInfo)
conf.AuthInfos[secondUserName] = secondAuthInfo conf.AuthInfos[secondUserName] = secondAuthInfo
dummyAuthInfoOptions := DummyAuthInfoOptions() dummyAuthInfoOptions := testutil.DummyAuthInfoOptions()
dummyAuthInfoOptions.Name = "" dummyAuthInfoOptions.Name = ""
output := new(bytes.Buffer) 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" expectedOutput := conf.AuthInfos["dummy_user"].String() + "\n" + conf.AuthInfos[secondUserName].String() + "\n"
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expectedOutput, output.String()) assert.Equal(t, expectedOutput, output.String())
}) })
t.Run("testNoAuthInfos", func(t *testing.T) { t.Run("testNoAuthInfos", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyAuthInfoOptions := DummyAuthInfoOptions() dummyAuthInfoOptions := testutil.DummyAuthInfoOptions()
dummyAuthInfoOptions.Name = "" dummyAuthInfoOptions.Name = ""
delete(conf.AuthInfos, "dummy_user") delete(conf.AuthInfos, "dummy_user")
output := new(bytes.Buffer) output := new(bytes.Buffer)
err := RunGetAuthInfo(dummyAuthInfoOptions, output, conf) err := config.RunGetAuthInfo(dummyAuthInfoOptions, output, conf)
expectedMessage := "No User credentials found in the configuration.\n" expectedMessage := "No User credentials found in the configuration.\n"
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expectedMessage, output.String()) assert.Equal(t, expectedMessage, output.String())
@ -76,34 +81,34 @@ func TestRunGetCluster(t *testing.T) {
const dummyClusterEphemeralName = "dummy_cluster_ephemeral" const dummyClusterEphemeralName = "dummy_cluster_ephemeral"
t.Run("testNonExistentCluster", func(t *testing.T) { t.Run("testNonExistentCluster", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyClusterOptions := DummyClusterOptions() dummyClusterOptions := testutil.DummyClusterOptions()
dummyClusterOptions.Name = "nonexistent_cluster" dummyClusterOptions.Name = "nonexistent_cluster"
output := new(bytes.Buffer) output := new(bytes.Buffer)
err := RunGetCluster(dummyClusterOptions, output, conf) err := config.RunGetCluster(dummyClusterOptions, output, conf)
assert.Error(t, err) assert.Error(t, err)
}) })
t.Run("testSingleCluster", func(t *testing.T) { t.Run("testSingleCluster", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyClusterOptions := DummyClusterOptions() dummyClusterOptions := testutil.DummyClusterOptions()
output := new(bytes.Buffer) output := new(bytes.Buffer)
err := RunGetCluster(dummyClusterOptions, output, conf) err := config.RunGetCluster(dummyClusterOptions, output, conf)
expectedCluster := DummyCluster() expectedCluster := testutil.DummyCluster()
expectedCluster.NameInKubeconf = dummyClusterEphemeralName expectedCluster.NameInKubeconf = dummyClusterEphemeralName
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expectedCluster.PrettyString(), output.String()) assert.Equal(t, expectedCluster.PrettyString(), output.String())
}) })
t.Run("testAllClusters", func(t *testing.T) { t.Run("testAllClusters", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyClusterOptions := DummyClusterOptions() dummyClusterOptions := testutil.DummyClusterOptions()
dummyClusterOptions.Name = "" dummyClusterOptions.Name = ""
output := new(bytes.Buffer) output := new(bytes.Buffer)
err := RunGetCluster(dummyClusterOptions, output, conf) err := config.RunGetCluster(dummyClusterOptions, output, conf)
expectedClusterTarget := DummyCluster() expectedClusterTarget := testutil.DummyCluster()
expectedClusterEphemeral := DummyCluster() expectedClusterEphemeral := testutil.DummyCluster()
expectedClusterEphemeral.NameInKubeconf = dummyClusterEphemeralName expectedClusterEphemeral.NameInKubeconf = dummyClusterEphemeralName
expectedOutput := expectedClusterEphemeral.PrettyString() + "\n" + expectedClusterTarget.PrettyString() + "\n" expectedOutput := expectedClusterEphemeral.PrettyString() + "\n" + expectedClusterTarget.PrettyString() + "\n"
@ -112,12 +117,12 @@ func TestRunGetCluster(t *testing.T) {
}) })
t.Run("testNoClusters", func(t *testing.T) { t.Run("testNoClusters", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyClusterOptions := DummyClusterOptions() dummyClusterOptions := testutil.DummyClusterOptions()
dummyClusterOptions.Name = "" dummyClusterOptions.Name = ""
delete(conf.Clusters, "dummy_cluster") delete(conf.Clusters, "dummy_cluster")
output := new(bytes.Buffer) output := new(bytes.Buffer)
err := RunGetCluster(dummyClusterOptions, output, conf) err := config.RunGetCluster(dummyClusterOptions, output, conf)
expectedMessage := "No clusters found in the configuration.\n" expectedMessage := "No clusters found in the configuration.\n"
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expectedMessage, output.String()) assert.Equal(t, expectedMessage, output.String())
@ -126,45 +131,45 @@ func TestRunGetCluster(t *testing.T) {
func TestRunGetContext(t *testing.T) { func TestRunGetContext(t *testing.T) {
t.Run("testNonExistentContext", func(t *testing.T) { t.Run("testNonExistentContext", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyContextOptions := DummyContextOptions() dummyContextOptions := testutil.DummyContextOptions()
dummyContextOptions.Name = "nonexistent_context" dummyContextOptions.Name = "nonexistent_context"
output := new(bytes.Buffer) output := new(bytes.Buffer)
err := RunGetContext(dummyContextOptions, output, conf) err := config.RunGetContext(dummyContextOptions, output, conf)
assert.Error(t, err) assert.Error(t, err)
}) })
t.Run("testSingleContext", func(t *testing.T) { t.Run("testSingleContext", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyContextOptions := DummyContextOptions() dummyContextOptions := testutil.DummyContextOptions()
output := new(bytes.Buffer) output := new(bytes.Buffer)
err := RunGetContext(dummyContextOptions, output, conf) err := config.RunGetContext(dummyContextOptions, output, conf)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, conf.Contexts["dummy_context"].PrettyString(), output.String()) assert.Equal(t, conf.Contexts["dummy_context"].PrettyString(), output.String())
}) })
t.Run("testAllContext", func(t *testing.T) { t.Run("testAllContext", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
newCtx := DummyContext() newCtx := testutil.DummyContext()
newCtx.NameInKubeconf = "second_context" newCtx.NameInKubeconf = "second_context"
conf.Contexts["second_context"] = newCtx conf.Contexts["second_context"] = newCtx
dummyContextOptions := DummyContextOptions() dummyContextOptions := testutil.DummyContextOptions()
dummyContextOptions.Name = "" dummyContextOptions.Name = ""
output := new(bytes.Buffer) 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() expectedOutput := conf.Contexts["dummy_context"].PrettyString() + conf.Contexts["second_context"].PrettyString()
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expectedOutput, output.String()) assert.Equal(t, expectedOutput, output.String())
}) })
t.Run("testNoContexts", func(t *testing.T) { t.Run("testNoContexts", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
delete(conf.Contexts, "dummy_context") delete(conf.Contexts, "dummy_context")
dummyContextOptions := DummyContextOptions() dummyContextOptions := testutil.DummyContextOptions()
dummyContextOptions.Name = "" dummyContextOptions.Name = ""
output := new(bytes.Buffer) output := new(bytes.Buffer)
err := RunGetContext(dummyContextOptions, output, conf) err := config.RunGetContext(dummyContextOptions, output, conf)
expectedOutput := "No Contexts found in the configuration.\n" expectedOutput := "No Contexts found in the configuration.\n"
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expectedOutput, output.String()) assert.Equal(t, expectedOutput, output.String())
@ -173,91 +178,93 @@ func TestRunGetContext(t *testing.T) {
func TestRunSetAuthInfo(t *testing.T) { func TestRunSetAuthInfo(t *testing.T) {
t.Run("testAddAuthInfo", func(t *testing.T) { t.Run("testAddAuthInfo", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyAuthInfoOptions := DummyAuthInfoOptions() dummyAuthInfoOptions := testutil.DummyAuthInfoOptions()
dummyAuthInfoOptions.Name = "second_user" dummyAuthInfoOptions.Name = "second_user"
dummyAuthInfoOptions.Token = "" dummyAuthInfoOptions.Token = ""
modified, err := RunSetAuthInfo(dummyAuthInfoOptions, conf, false) modified, err := config.RunSetAuthInfo(dummyAuthInfoOptions, conf, false)
assert.NoError(t, err) assert.NoError(t, err)
assert.False(t, modified) assert.False(t, modified)
assert.Contains(t, conf.AuthInfos, "second_user") assert.Contains(t, conf.AuthInfos, "second_user")
}) })
t.Run("testModifyAuthInfo", func(t *testing.T) { t.Run("testModifyAuthInfo", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyAuthInfoOptions := DummyAuthInfoOptions() dummyAuthInfoOptions := testutil.DummyAuthInfoOptions()
dummyAuthInfoOptions.Name = "dummy_user" dummyAuthInfoOptions.Name = "dummy_user"
dummyAuthInfoOptions.Password = "testpassword123" dummyAuthInfoOptions.Password = "testpassword123"
dummyAuthInfoOptions.Token = "" dummyAuthInfoOptions.Token = ""
modified, err := RunSetAuthInfo(dummyAuthInfoOptions, conf, false) modified, err := config.RunSetAuthInfo(dummyAuthInfoOptions, conf, false)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, modified) 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) { func TestRunSetCluster(t *testing.T) {
t.Run("testAddCluster", func(t *testing.T) { t.Run("testAddCluster", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyClusterOptions := DummyClusterOptions() dummyClusterOptions := testutil.DummyClusterOptions()
dummyClusterOptions.Name = "second_cluster" dummyClusterOptions.Name = "second_cluster"
modified, err := RunSetCluster(dummyClusterOptions, conf, false) modified, err := config.RunSetCluster(dummyClusterOptions, conf, false)
assert.NoError(t, err) assert.NoError(t, err)
assert.False(t, modified) assert.False(t, modified)
assert.Contains(t, conf.Clusters, "second_cluster") assert.Contains(t, conf.Clusters, "second_cluster")
}) })
t.Run("testModifyCluster", func(t *testing.T) { t.Run("testModifyCluster", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyClusterOptions := DummyClusterOptions() dummyClusterOptions := testutil.DummyClusterOptions()
dummyClusterOptions.Server = "http://123.45.67.890" 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.NoError(t, err)
assert.True(t, modified) assert.True(t, modified)
assert.Equal( assert.Equal(
t, "http://123.45.67.890", 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) { func TestRunSetContext(t *testing.T) {
t.Run("testAddContext", func(t *testing.T) { t.Run("testAddContext", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyContextOptions := DummyContextOptions() dummyContextOptions := testutil.DummyContextOptions()
dummyContextOptions.Name = "second_context" dummyContextOptions.Name = "second_context"
modified, err := RunSetContext(dummyContextOptions, conf, false) modified, err := config.RunSetContext(dummyContextOptions, conf, false)
assert.NoError(t, err) assert.NoError(t, err)
assert.False(t, modified) assert.False(t, modified)
assert.Contains(t, conf.Contexts, "second_context") assert.Contains(t, conf.Contexts, "second_context")
}) })
t.Run("testModifyContext", func(t *testing.T) { t.Run("testModifyContext", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
dummyContextOptions := DummyContextOptions() dummyContextOptions := testutil.DummyContextOptions()
dummyContextOptions.Namespace = "new_namespace" dummyContextOptions.Namespace = "new_namespace"
modified, err := RunSetContext(dummyContextOptions, conf, false) modified, err := config.RunSetContext(dummyContextOptions, conf, false)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, modified) 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) { func TestRunUseContext(t *testing.T) {
t.Run("testUseContext", func(t *testing.T) { t.Run("testUseContext", func(t *testing.T) {
conf := DummyConfig() conf := testutil.DummyConfig()
err := RunUseContext("dummy_context", conf) err := config.RunUseContext("dummy_context", conf)
assert.Nil(t, err) assert.Nil(t, err)
}) })
t.Run("testUseContextDoesNotExist", func(t *testing.T) { t.Run("testUseContextDoesNotExist", func(t *testing.T) {
conf := NewConfig() conf := config.NewConfig()
err := RunUseContext("foo", conf) err := config.RunUseContext("foo", conf)
assert.Error(t, err) assert.Error(t, err)
}) })
} }

View File

@ -416,6 +416,10 @@ func (c *Config) KubeConfig() *clientcmdapi.Config {
return c.kubeConfig return c.kubeConfig
} }
func (c *Config) SetKubeConfig(kubeConfig *clientcmdapi.Config) {
c.kubeConfig = kubeConfig
}
// Get A Cluster // Get A Cluster
func (c *Config) GetCluster(cName, cType string) (*Cluster, error) { func (c *Config) GetCluster(cName, cType string) (*Cluster, error) {
_, exists := c.Clusters[cName] _, exists := c.Clusters[cName]

View File

@ -39,47 +39,47 @@ func TestString(t *testing.T) {
}{ }{
{ {
name: "config", name: "config",
stringer: config.DummyConfig(), stringer: testutil.DummyConfig(),
}, },
{ {
name: "context", name: "context",
stringer: config.DummyContext(), stringer: testutil.DummyContext(),
}, },
{ {
name: "cluster", name: "cluster",
stringer: config.DummyCluster(), stringer: testutil.DummyCluster(),
}, },
{ {
name: "authinfo", name: "authinfo",
stringer: config.DummyAuthInfo(), stringer: testutil.DummyAuthInfo(),
}, },
{ {
name: "manifest", name: "manifest",
stringer: config.DummyManifest(), stringer: testutil.DummyManifest(),
}, },
{ {
name: "modules", name: "modules",
stringer: config.DummyModules(), stringer: testutil.DummyModules(),
}, },
{ {
name: "repository", name: "repository",
stringer: config.DummyRepository(), stringer: testutil.DummyRepository(),
}, },
{ {
name: "repo-auth", name: "repo-auth",
stringer: config.DummyRepoAuth(), stringer: testutil.DummyRepoAuth(),
}, },
{ {
name: "repo-checkout", name: "repo-checkout",
stringer: config.DummyRepoCheckout(), stringer: testutil.DummyRepoCheckout(),
}, },
{ {
name: "bootstrap", name: "bootstrap",
stringer: config.DummyBootstrap(), stringer: testutil.DummyBootstrap(),
}, },
{ {
name: "bootstrap", name: "bootstrap",
stringer: config.DummyBootstrap(), stringer: testutil.DummyBootstrap(),
}, },
{ {
name: "builder", name: "builder",
@ -116,7 +116,7 @@ func TestPrettyString(t *testing.T) {
data, err := fSys.ReadFile("/prettycluster-string.yaml") data, err := fSys.ReadFile("/prettycluster-string.yaml")
require.NoError(t, err) require.NoError(t, err)
cluster := config.DummyCluster() cluster := testutil.DummyCluster()
assert.EqualValues(t, cluster.PrettyString(), string(data)) assert.EqualValues(t, cluster.PrettyString(), string(data))
} }
@ -230,7 +230,7 @@ func TestEqual(t *testing.T) {
} }
func TestLoadConfig(t *testing.T) { func TestLoadConfig(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
assert.Len(t, conf.Clusters, 5) assert.Len(t, conf.Clusters, 5)
@ -241,7 +241,7 @@ func TestLoadConfig(t *testing.T) {
} }
func TestPersistConfig(t *testing.T) { func TestPersistConfig(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
err := conf.PersistConfig() err := conf.PersistConfig()
@ -370,7 +370,7 @@ func TestEnsureComplete(t *testing.T) {
} }
func TestPurge(t *testing.T) { func TestPurge(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
// Store it // Store it
@ -392,7 +392,7 @@ func TestPurge(t *testing.T) {
} }
func TestKClusterString(t *testing.T) { func TestKClusterString(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
kClusters := conf.KubeConfig().Clusters kClusters := conf.KubeConfig().Clusters
@ -402,7 +402,7 @@ func TestKClusterString(t *testing.T) {
assert.EqualValues(t, config.KClusterString(nil), "null\n") assert.EqualValues(t, config.KClusterString(nil), "null\n")
} }
func TestKContextString(t *testing.T) { func TestKContextString(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
kContexts := conf.KubeConfig().Contexts kContexts := conf.KubeConfig().Contexts
@ -412,7 +412,7 @@ func TestKContextString(t *testing.T) {
assert.EqualValues(t, config.KClusterString(nil), "null\n") assert.EqualValues(t, config.KClusterString(nil), "null\n")
} }
func TestKAuthInfoString(t *testing.T) { func TestKAuthInfoString(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
kAuthInfos := conf.KubeConfig().AuthInfos kAuthInfos := conf.KubeConfig().AuthInfos
@ -445,7 +445,7 @@ func TestValidClusterTypeFail(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
} }
func TestGetCluster(t *testing.T) { func TestGetCluster(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
cluster, err := conf.GetCluster("def", config.Ephemeral) cluster, err := conf.GetCluster("def", config.Ephemeral)
@ -465,10 +465,10 @@ func TestGetCluster(t *testing.T) {
} }
func TestAddCluster(t *testing.T) { func TestAddCluster(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
co := config.DummyClusterOptions() co := testutil.DummyClusterOptions()
cluster, err := conf.AddCluster(co) cluster, err := conf.AddCluster(co)
require.NoError(t, err) require.NoError(t, err)
@ -476,10 +476,10 @@ func TestAddCluster(t *testing.T) {
} }
func TestModifyCluster(t *testing.T) { func TestModifyCluster(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
co := config.DummyClusterOptions() co := testutil.DummyClusterOptions()
cluster, err := conf.AddCluster(co) cluster, err := conf.AddCluster(co)
require.NoError(t, err) require.NoError(t, err)
@ -498,7 +498,7 @@ func TestModifyCluster(t *testing.T) {
} }
func TestGetClusters(t *testing.T) { func TestGetClusters(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
clusters := conf.GetClusters() clusters := conf.GetClusters()
@ -506,7 +506,7 @@ func TestGetClusters(t *testing.T) {
} }
func TestGetContexts(t *testing.T) { func TestGetContexts(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
contexts := conf.GetContexts() contexts := conf.GetContexts()
@ -514,7 +514,7 @@ func TestGetContexts(t *testing.T) {
} }
func TestGetContext(t *testing.T) { func TestGetContext(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
context, err := conf.GetContext("def_ephemeral") context, err := conf.GetContext("def_ephemeral")
@ -530,19 +530,19 @@ func TestGetContext(t *testing.T) {
} }
func TestAddContext(t *testing.T) { func TestAddContext(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
co := config.DummyContextOptions() co := testutil.DummyContextOptions()
context := conf.AddContext(co) context := conf.AddContext(co)
assert.EqualValues(t, conf.Contexts[co.Name], context) assert.EqualValues(t, conf.Contexts[co.Name], context)
} }
func TestModifyContext(t *testing.T) { func TestModifyContext(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
co := config.DummyContextOptions() co := testutil.DummyContextOptions()
context := conf.AddContext(co) context := conf.AddContext(co)
co.Namespace += stringDelta co.Namespace += stringDelta
@ -560,7 +560,7 @@ func TestModifyContext(t *testing.T) {
// AuthInfo Related // AuthInfo Related
func TestGetAuthInfos(t *testing.T) { func TestGetAuthInfos(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
authinfos := conf.GetAuthInfos() authinfos := conf.GetAuthInfos()
@ -568,7 +568,7 @@ func TestGetAuthInfos(t *testing.T) {
} }
func TestGetAuthInfo(t *testing.T) { func TestGetAuthInfo(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
authinfo, err := conf.GetAuthInfo("def-user") authinfo, err := conf.GetAuthInfo("def-user")
@ -583,19 +583,19 @@ func TestGetAuthInfo(t *testing.T) {
} }
func TestAddAuthInfo(t *testing.T) { func TestAddAuthInfo(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
co := config.DummyAuthInfoOptions() co := testutil.DummyAuthInfoOptions()
authinfo := conf.AddAuthInfo(co) authinfo := conf.AddAuthInfo(co)
assert.EqualValues(t, conf.AuthInfos[co.Name], authinfo) assert.EqualValues(t, conf.AuthInfos[co.Name], authinfo)
} }
func TestModifyAuthInfo(t *testing.T) { func TestModifyAuthInfo(t *testing.T) {
conf, cleanup := config.InitConfig(t) conf, cleanup := testutil.InitConfig(t)
defer cleanup(t) defer cleanup(t)
co := config.DummyAuthInfoOptions() co := testutil.DummyAuthInfoOptions()
authinfo := conf.AddAuthInfo(co) authinfo := conf.AddAuthInfo(co)
co.Username += stringDelta co.Username += stringDelta

View File

@ -1,4 +1,4 @@
package config package config_test
import ( import (
"testing" "testing"
@ -7,6 +7,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"opendev.org/airship/airshipctl/pkg/config"
) )
const ( const (
@ -147,7 +149,7 @@ type TestCase struct {
} }
type TestRepos struct { type TestRepos struct {
TestData map[string]*Repository `json:"test-data"` TestData map[string]*config.Repository `json:"test-data"`
} }
func TestToCheckout(t *testing.T) { func TestToCheckout(t *testing.T) {

View File

@ -23,7 +23,7 @@ func getDummyPullSettings() *Settings {
mockPullSettings := &Settings{ mockPullSettings := &Settings{
AirshipCTLSettings: new(environment.AirshipCTLSettings), AirshipCTLSettings: new(environment.AirshipCTLSettings),
} }
mockConf := config.DummyConfig() mockConf := testutil.DummyConfig()
mockPullSettings.AirshipCTLSettings.SetConfig(mockConf) mockPullSettings.AirshipCTLSettings.SetConfig(mockConf)
return mockPullSettings return mockPullSettings
} }

View File

@ -10,11 +10,12 @@ import (
"opendev.org/airship/airshipctl/pkg/environment" "opendev.org/airship/airshipctl/pkg/environment"
"opendev.org/airship/airshipctl/pkg/remote/redfish" "opendev.org/airship/airshipctl/pkg/remote/redfish"
"opendev.org/airship/airshipctl/testutil"
) )
func initSettings(t *testing.T, rd *config.RemoteDirect, testdata string) *environment.AirshipCTLSettings { func initSettings(t *testing.T, rd *config.RemoteDirect, testdata string) *environment.AirshipCTLSettings {
settings := &environment.AirshipCTLSettings{} settings := &environment.AirshipCTLSettings{}
settings.SetConfig(config.DummyConfig()) settings.SetConfig(testutil.DummyConfig())
bi, err := settings.Config().CurrentContextBootstrapInfo() bi, err := settings.Config().CurrentContextBootstrapInfo()
require.NoError(t, err) require.NoError(t, err)
bi.RemoteDirect = rd bi.RemoteDirect = rd

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package config package testutil
import ( import (
"io/ioutil" "io/ioutil"
@ -25,39 +25,42 @@ import (
"github.com/stretchr/testify/require" "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 // DummyConfig used by tests, to initialize min set of data
func DummyConfig() *Config { func DummyConfig() *config.Config {
conf := &Config{ conf := &config.Config{
Kind: AirshipConfigKind, Kind: config.AirshipConfigKind,
APIVersion: AirshipConfigAPIVersion, APIVersion: config.AirshipConfigAPIVersion,
Clusters: map[string]*ClusterPurpose{ Clusters: map[string]*config.ClusterPurpose{
"dummy_cluster": DummyClusterPurpose(), "dummy_cluster": DummyClusterPurpose(),
}, },
AuthInfos: map[string]*AuthInfo{ AuthInfos: map[string]*config.AuthInfo{
"dummy_user": DummyAuthInfo(), "dummy_user": DummyAuthInfo(),
}, },
Contexts: map[string]*Context{ Contexts: map[string]*config.Context{
"dummy_context": DummyContext(), "dummy_context": DummyContext(),
}, },
Manifests: map[string]*Manifest{ Manifests: map[string]*config.Manifest{
"dummy_manifest": DummyManifest(), "dummy_manifest": DummyManifest(),
}, },
ModulesConfig: DummyModules(), ModulesConfig: DummyModules(),
CurrentContext: "dummy_context", CurrentContext: "dummy_context",
kubeConfig: kubeconfig.NewConfig(),
} }
conf.SetKubeConfig(kubeconfig.NewConfig())
dummyCluster := conf.Clusters["dummy_cluster"] dummyCluster := conf.Clusters["dummy_cluster"]
conf.KubeConfig().Clusters["dummy_cluster_target"] = dummyCluster.ClusterTypes[Target].KubeCluster() conf.KubeConfig().Clusters["dummy_cluster_target"] = dummyCluster.ClusterTypes[config.Target].KubeCluster()
conf.KubeConfig().Clusters["dummy_cluster_ephemeral"] = dummyCluster.ClusterTypes[Ephemeral].KubeCluster() conf.KubeConfig().Clusters["dummy_cluster_ephemeral"] = dummyCluster.ClusterTypes[config.Ephemeral].KubeCluster()
return conf return conf
} }
// DummyContext , utility function used for tests // DummyContext , utility function used for tests
func DummyContext() *Context { func DummyContext() *config.Context {
c := NewContext() c := config.NewContext()
c.NameInKubeconf = "dummy_cluster_ephemeral" c.NameInKubeconf = "dummy_cluster_ephemeral"
c.Manifest = "dummy_manifest" c.Manifest = "dummy_manifest"
context := kubeconfig.NewContext() context := kubeconfig.NewContext()
@ -70,8 +73,8 @@ func DummyContext() *Context {
} }
// DummyCluster, utility function used for tests // DummyCluster, utility function used for tests
func DummyCluster() *Cluster { func DummyCluster() *config.Cluster {
c := NewCluster() c := config.NewCluster()
cluster := kubeconfig.NewCluster() cluster := kubeconfig.NewCluster()
cluster.Server = "http://dummy.server" cluster.Server = "http://dummy.server"
@ -84,44 +87,48 @@ func DummyCluster() *Cluster {
} }
// DummyManifest , utility function used for tests // DummyManifest , utility function used for tests
func DummyManifest() *Manifest { func DummyManifest() *config.Manifest {
m := NewManifest() m := config.NewManifest()
// Repositories is the map of repository adddressable by a name // Repositories is the map of repository adddressable by a name
m.Repository = DummyRepository() m.Repository = DummyRepository()
m.TargetPath = "/var/tmp/" m.TargetPath = "/var/tmp/"
return m return m
} }
func DummyRepository() *Repository { // DummyRepository, utility function used for tests
return &Repository{ func DummyRepository() *config.Repository {
return &config.Repository{
URLString: "http://dummy.url.com", URLString: "http://dummy.url.com",
CheckoutOptions: &RepoCheckout{ CheckoutOptions: &config.RepoCheckout{
Tag: "v1.0.1", Tag: "v1.0.1",
ForceCheckout: false, ForceCheckout: false,
}, },
Auth: &RepoAuth{ Auth: &config.RepoAuth{
Type: "ssh-key", Type: "ssh-key",
KeyPath: "testdata/test-key.pem", KeyPath: "testdata/test-key.pem",
}, },
} }
} }
func DummyRepoAuth() *RepoAuth { // DummyRepoAuth , utility function used for tests
return &RepoAuth{ func DummyRepoAuth() *config.RepoAuth {
return &config.RepoAuth{
Type: "ssh-key", Type: "ssh-key",
KeyPath: "testdata/test-key.pem", KeyPath: "testdata/test-key.pem",
} }
} }
func DummyRepoCheckout() *RepoCheckout { // DummyRepoCheckout, utility function used for checks
return &RepoCheckout{ func DummyRepoCheckout() *config.RepoCheckout {
return &config.RepoCheckout{
Tag: "v1.0.1", Tag: "v1.0.1",
ForceCheckout: false, ForceCheckout: false,
} }
} }
func DummyAuthInfo() *AuthInfo { // DummyAuthInfo , utility function used for tests
a := NewAuthInfo() func DummyAuthInfo() *config.AuthInfo {
a := config.NewAuthInfo()
authinfo := kubeconfig.NewAuthInfo() authinfo := kubeconfig.NewAuthInfo()
authinfo.Username = "dummy_username" authinfo.Username = "dummy_username"
authinfo.Password = "dummy_password" authinfo.Password = "dummy_password"
@ -132,15 +139,27 @@ func DummyAuthInfo() *AuthInfo {
return a return a
} }
func DummyModules() *Modules { // DummyKubeAuthInfo , utility function used for tests
m := NewModules() 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() m.BootstrapInfo["dummy_bootstrap_config"] = DummyBootstrap()
return m return m
} }
// DummyClusterPurpose , utility function used for tests // DummyClusterPurpose , utility function used for tests
func DummyClusterPurpose() *ClusterPurpose { func DummyClusterPurpose() *config.ClusterPurpose {
cp := NewClusterPurpose() cp := config.NewClusterPurpose()
cp.ClusterTypes["ephemeral"] = DummyCluster() cp.ClusterTypes["ephemeral"] = DummyCluster()
cp.ClusterTypes["ephemeral"].NameInKubeconf = "dummy_cluster_ephemeral" cp.ClusterTypes["ephemeral"].NameInKubeconf = "dummy_cluster_ephemeral"
cp.ClusterTypes["target"] = DummyCluster() cp.ClusterTypes["target"] = DummyCluster()
@ -152,9 +171,9 @@ func DummyClusterPurpose() *ClusterPurpose {
// The returned config object will be associated with real files stored in a // The returned config object will be associated with real files stored in a
// directory in the user's temporary file storage // directory in the user's temporary file storage
// This directory can be cleaned up by calling the returned "cleanup" function // 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() t.Helper()
testDir, cleanup := testutil.TempDir(t, "airship-test") testDir, cleanup := TempDir(t, "airship-test")
configPath := filepath.Join(testDir, "config") configPath := filepath.Join(testDir, "config")
err := ioutil.WriteFile(configPath, []byte(testConfigYAML), 0666) 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) err = ioutil.WriteFile(kubeConfigPath, []byte(testKubeConfigYAML), 0666)
require.NoError(t, err) require.NoError(t, err)
conf = NewConfig() conf = config.NewConfig()
err = conf.LoadConfig(configPath, kubeConfigPath) err = conf.LoadConfig(configPath, kubeConfigPath)
require.NoError(t, err) require.NoError(t, err)
@ -172,10 +191,10 @@ func InitConfig(t *testing.T) (conf *Config, cleanup func(*testing.T)) {
return conf, cleanup return conf, cleanup
} }
func DummyClusterOptions() *ClusterOptions { func DummyClusterOptions() *config.ClusterOptions {
co := &ClusterOptions{} co := &config.ClusterOptions{}
co.Name = "dummy_cluster" co.Name = "dummy_cluster"
co.ClusterType = Ephemeral co.ClusterType = config.Ephemeral
co.Server = "http://1.1.1.1" co.Server = "http://1.1.1.1"
co.InsecureSkipTLSVerify = false co.InsecureSkipTLSVerify = false
co.CertificateAuthority = "" co.CertificateAuthority = ""
@ -184,8 +203,8 @@ func DummyClusterOptions() *ClusterOptions {
return co return co
} }
func DummyContextOptions() *ContextOptions { func DummyContextOptions() *config.ContextOptions {
co := &ContextOptions{} co := &config.ContextOptions{}
co.Name = "dummy_context" co.Name = "dummy_context"
co.Manifest = "dummy_manifest" co.Manifest = "dummy_manifest"
co.AuthInfo = "dummy_user" co.AuthInfo = "dummy_user"
@ -195,8 +214,8 @@ func DummyContextOptions() *ContextOptions {
return co return co
} }
func DummyAuthInfoOptions() *AuthInfoOptions { func DummyAuthInfoOptions() *config.AuthInfoOptions {
authinfo := &AuthInfoOptions{} authinfo := &config.AuthInfoOptions{}
authinfo.Username = "dummy_username" authinfo.Username = "dummy_username"
authinfo.Password = "dummy_password" authinfo.Password = "dummy_password"
authinfo.ClientCertificate = "dummy_certificate" authinfo.ClientCertificate = "dummy_certificate"
@ -205,14 +224,14 @@ func DummyAuthInfoOptions() *AuthInfoOptions {
return authinfo return authinfo
} }
func DummyBootstrap() *Bootstrap { func DummyBootstrap() *config.Bootstrap {
bs := &Bootstrap{} bs := &config.Bootstrap{}
cont := Container{ cont := config.Container{
Volume: "/dummy:dummy", Volume: "/dummy:dummy",
Image: "dummy_image:dummy_tag", Image: "dummy_image:dummy_tag",
ContainerRuntime: "docker", ContainerRuntime: "docker",
} }
builder := Builder{ builder := config.Builder{
UserDataFileName: "user-data", UserDataFileName: "user-data",
NetworkConfigFileName: "netconfig", NetworkConfigFileName: "netconfig",
OutputMetadataFileName: "output-metadata.yaml", OutputMetadataFileName: "output-metadata.yaml",