01d66c834d
* moving cli helper functions to the respective packages with _helper. * removed cmds.go within pkg/ dir and added config_helper.go * In the first commit(https://review.opendev.org/#/c/707216/) commit CLI helper functions were moved from the cmd/config package to the pkg/config package * Now we are trying to move the CLI helper functions within their respective packages instead of a command cmds.go. * Creating and implementing this convention will help us better distinguish library code and CLI helper code * Added MaxNArgs as 1 for get-credentials and updated test cases Relates-To: #43 Change-Id: Ibb8139102bd98c6c7596bd97377cf83381cbeb00
120 lines
3.7 KiB
Go
120 lines
3.7 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_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"opendev.org/airship/airshipctl/pkg/config"
|
|
"opendev.org/airship/airshipctl/testutil"
|
|
)
|
|
|
|
func TestRunSetAuthInfo(t *testing.T) {
|
|
t.Run("testAddAuthInfo", func(t *testing.T) {
|
|
conf := testutil.DummyConfig()
|
|
dummyAuthInfoOptions := testutil.DummyAuthInfoOptions()
|
|
dummyAuthInfoOptions.Name = "second_user"
|
|
dummyAuthInfoOptions.Token = ""
|
|
|
|
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 := testutil.DummyConfig()
|
|
dummyAuthInfoOptions := testutil.DummyAuthInfoOptions()
|
|
dummyAuthInfoOptions.Name = "dummy_user"
|
|
dummyAuthInfoOptions.Password = "testpassword123"
|
|
dummyAuthInfoOptions.Token = ""
|
|
|
|
modified, err := config.RunSetAuthInfo(dummyAuthInfoOptions, conf, false)
|
|
assert.NoError(t, err)
|
|
assert.True(t, modified)
|
|
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 := testutil.DummyConfig()
|
|
dummyClusterOptions := testutil.DummyClusterOptions()
|
|
dummyClusterOptions.Name = "second_cluster"
|
|
|
|
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 := testutil.DummyConfig()
|
|
dummyClusterOptions := testutil.DummyClusterOptions()
|
|
dummyClusterOptions.Server = "http://123.45.67.890"
|
|
|
|
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"].KubeCluster().Server)
|
|
})
|
|
}
|
|
|
|
func TestRunSetContext(t *testing.T) {
|
|
t.Run("testAddContext", func(t *testing.T) {
|
|
conf := testutil.DummyConfig()
|
|
dummyContextOptions := testutil.DummyContextOptions()
|
|
dummyContextOptions.Name = "second_context"
|
|
|
|
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 := testutil.DummyConfig()
|
|
dummyContextOptions := testutil.DummyContextOptions()
|
|
dummyContextOptions.Namespace = "new_namespace"
|
|
|
|
modified, err := config.RunSetContext(dummyContextOptions, conf, false)
|
|
assert.NoError(t, err)
|
|
assert.True(t, modified)
|
|
assert.Equal(t, "new_namespace", conf.Contexts["dummy_context"].KubeContext().Namespace)
|
|
})
|
|
}
|
|
|
|
func TestRunUseContext(t *testing.T) {
|
|
t.Run("testUseContext", func(t *testing.T) {
|
|
conf := testutil.DummyConfig()
|
|
err := config.RunUseContext("dummy_context", conf)
|
|
assert.Nil(t, err)
|
|
})
|
|
|
|
t.Run("testUseContextDoesNotExist", func(t *testing.T) {
|
|
conf := config.NewConfig()
|
|
err := config.RunUseContext("foo", conf)
|
|
assert.Error(t, err)
|
|
})
|
|
}
|