def test_from_param_ctx_found(self): test_file = '/tmp/.test_kc/config' self.addCleanup(remove_file, test_file) cfg = Config.from_params(filepath=test_file) self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'self') cfg.set_cluster('other') cfg.set_credentials('other') cfg.set_context('other', cluster='other', user='******') cfg.set_current_context('other') self.assertEqual('other', cfg.current_context) cfg.write_file() cfg = Config.from_params(context_name='other', filepath=test_file) self.assertIsInstance(cfg.context, dict) self.assertEqual(cfg.context, { 'cluster': 'other', 'user': '******' }) self.assertIsInstance(cfg.cluster, dict) self.assertEqual(cfg.cluster, {}) self.assertIsInstance(cfg.user, dict) self.assertEqual(cfg.user, {}) cfg.write_file()
def test_merge_items(self): cfg = Config.from_params() # default item exists self.assertEqual(len(cfg.content['users']), 1) # updates default with new value cfg.set_credentials(None, token='abc123') self.assertEqual(len(cfg.content['users']), 1) # adds a new item to the list cfg.set_credentials('other', token='abc123') self.assertEqual(len(cfg.content['users']), 2) # duplicate; does nothing cfg.set_credentials(None, token='abc123') self.assertEqual(len(cfg.content['users']), 2) # updates default with new value cfg.set_credentials(None, token='abc456') self.assertEqual(len(cfg.content['users']), 2) # do not erase existing data cfg.set_credentials(None) self.assertEqual(len(cfg.content['users']), 2) auth = None for u in cfg.content['users']: if u['name'] == 'self': auth = u['user'] self.assertEqual(auth, {'token': 'abc456'})
def test_add_item(self): cfg = Config.from_params() orig = copy.deepcopy(cfg.content) cfg.set_cluster(None) cfg.set_credentials(None) cfg.set_context(None, cluster='self', user='******') cfg.set_current_context('self') self.assertEqual(orig, cfg.content) cfg.set_current_context('other') self.assertNotEqual('other', cfg.current_context) cfg.set_cluster('other') cfg.set_credentials('other') cfg.set_context('other', cluster='other', user='******') cfg.set_current_context('other') self.assertEqual('other', cfg.current_context) cfg = Config({'apiVersion': 'v1'}) self.assertIsNotNone(cfg) self.assertIsNone(cfg.current_context) self.assertNotIn('contexts', cfg.content) self.assertNotIn('clusters', cfg.content) self.assertNotIn('users', cfg.content) cfg.set_cluster(None) cfg.set_credentials(None) cfg.set_context(None, cluster='self', user='******') cfg.set_current_context('self') self.assertIn('contexts', cfg.content) self.assertIn('clusters', cfg.content) self.assertIn('users', cfg.content)
def test_from_params_auth_clientkey(self): cfg = Config.from_params(auth={'client-key': '/tmp/.minikube/apiserver.key'}) self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'self') self.assertIsInstance(cfg.contexts, dict) self.assertEqual(len(cfg.contexts), 1) self.assertIsInstance(cfg.clusters, dict) self.assertEqual(len(cfg.clusters), 1) self.assertIsInstance(cfg.users, dict) self.assertEqual(len(cfg.users), 1) self.assertIsInstance(cfg.context, dict) self.assertEqual(cfg.context, { 'cluster': 'self', 'user': '******' }) self.assertIsInstance(cfg.cluster, dict) self.assertEqual(cfg.cluster, {}) self.assertIsInstance(cfg.user, dict) self.assertEqual(cfg.user, { 'client-key': '/tmp/.minikube/apiserver.key' })
def test_from_params_all(self): cfg = Config.from_params( api='http://localhost', ca='/tmp/.minikube/ca.crt', auth={ 'client-certificate': '/tmp/.minikube/apiserver.crt', 'client-key': '/tmp/.minikube/apiserver.key'}, filepath=CONFIG_FILE) self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'self') self.assertIsInstance(cfg.contexts, dict) self.assertEqual(len(cfg.contexts), 1) self.assertIsInstance(cfg.clusters, dict) self.assertEqual(len(cfg.clusters), 1) self.assertIsInstance(cfg.users, dict) self.assertEqual(len(cfg.users), 1) self.assertIsInstance(cfg.context, dict) self.assertEqual(cfg.context, { 'cluster': 'self', 'user': '******' }) self.assertIsInstance(cfg.cluster, dict) self.assertEqual(cfg.cluster, { 'server': 'http://localhost', 'certificate-authority': '/tmp/.minikube/ca.crt' }) self.assertIsInstance(cfg.user, dict) self.assertEqual(cfg.user, { 'client-certificate': '/tmp/.minikube/apiserver.crt', 'client-key': '/tmp/.minikube/apiserver.key' })
def test_from_params_auth_clientcert(self): cfg = Config.from_params( auth={'client-certificate': '/tmp/.minikube/apiserver.crt'}, username='******' ) self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'self') self.assertIsInstance(cfg.contexts, dict) self.assertEqual(len(cfg.contexts), 1) self.assertIsInstance(cfg.clusters, dict) self.assertEqual(len(cfg.clusters), 1) self.assertIsInstance(cfg.users, dict) self.assertEqual(len(cfg.users), 1) self.assertIsInstance(cfg.context, dict) self.assertEqual(cfg.context, { 'cluster': 'self', 'user': '******' }) self.assertIsInstance(cfg.cluster, dict) self.assertEqual(cfg.cluster, {}) self.assertIsInstance(cfg.user, dict) self.assertEqual(cfg.user, { 'client-certificate': '/tmp/.minikube/apiserver.crt' })
def test_from_params_auth_token(self): cfg = Config.from_params(auth='qgfe3Z3nJPAtTS_OsNeThQEwzRvwSkHenxd4lS_x0RM') self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'self') self.assertIsInstance(cfg.contexts, dict) self.assertEqual(len(cfg.contexts), 1) self.assertIsInstance(cfg.clusters, dict) self.assertEqual(len(cfg.clusters), 1) self.assertIsInstance(cfg.users, dict) self.assertEqual(len(cfg.users), 1) self.assertIsInstance(cfg.context, dict) self.assertEqual(cfg.context, { 'cluster': 'self', 'user': '******' }) self.assertIsInstance(cfg.cluster, dict) self.assertEqual(cfg.cluster, {}) self.assertIsInstance(cfg.user, dict) self.assertEqual(cfg.user, { 'token': 'qgfe3Z3nJPAtTS_OsNeThQEwzRvwSkHenxd4lS_x0RM' })
def test_from_params_api_ca(self): cfg = Config.from_params(api='http://localhost', ca='/tmp/.minikube/ca.crt') self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'self') self.assertIsInstance(cfg.contexts, dict) self.assertEqual(len(cfg.contexts), 1) self.assertIsInstance(cfg.clusters, dict) self.assertEqual(len(cfg.clusters), 1) self.assertIsInstance(cfg.users, dict) self.assertEqual(len(cfg.users), 1) self.assertIsInstance(cfg.context, dict) self.assertEqual(cfg.context, { 'cluster': 'self', 'user': '******' }) self.assertIsInstance(cfg.cluster, dict) self.assertEqual(cfg.cluster, { 'server': 'http://localhost', 'certificate-authority': '/tmp/.minikube/ca.crt' }) self.assertIsInstance(cfg.user, dict) self.assertEqual(cfg.user, {})
def test_from_params_api_verify(self): cfg = Config.from_params(api='http://localhost', verify=False) self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'self') self.assertIsInstance(cfg.contexts, dict) self.assertEqual(len(cfg.contexts), 1) self.assertIsInstance(cfg.clusters, dict) self.assertEqual(len(cfg.clusters), 1) self.assertIsInstance(cfg.users, dict) self.assertEqual(len(cfg.users), 1) self.assertIsInstance(cfg.context, dict) self.assertEqual(cfg.context, { 'cluster': 'self', 'user': '******' }) self.assertIsInstance(cfg.cluster, dict) self.assertEqual(cfg.cluster, { 'server': 'http://localhost', 'insecure-skip-tls-verify': True }) self.assertIsInstance(cfg.user, dict) self.assertEqual(cfg.user, {})
def test_from_params_api_only(self): cfg = Config.from_params(api='http://localhost') self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'self') self.assertIsInstance(cfg.contexts, dict) self.assertEqual(len(cfg.contexts), 1) self.assertIsInstance(cfg.clusters, dict) self.assertEqual(len(cfg.clusters), 1) self.assertIsInstance(cfg.users, dict) self.assertEqual(len(cfg.users), 1) self.assertIsInstance(cfg.context, dict) self.assertEqual(cfg.context, { 'cluster': 'self', 'user': '******' }) self.assertIsInstance(cfg.cluster, dict) self.assertEqual(cfg.cluster, { 'server': 'http://localhost' }) self.assertIsInstance(cfg.user, dict) self.assertEqual(cfg.user, {})
def test_write_file(self): cfg = Config.from_params( api='http://localhost', ca='/tmp/.minikube/ca.crt', auth={ 'client-certificate': '/tmp/.minikube/apiserver.crt', 'client-key': '/tmp/.minikube/apiserver.key'}, filepath=CONFIG_FILE) self.assertIsNotNone(cfg) self.assertFalse(os.path.exists(CONFIG_FILE)) cfg.write_file() self.assertTrue(os.path.exists(CONFIG_FILE)) cfg.write_file() self.assertTrue(os.path.exists(CONFIG_FILE))
def test_from_param_ctx_not_found(self): cfg = Config.from_params(context_name='test') self.assertIsNotNone(cfg) self.assertEqual(cfg.current_context, 'test') self.assertIsInstance(cfg.contexts, dict) self.assertEqual(len(cfg.contexts), 1) self.assertIsInstance(cfg.clusters, dict) self.assertEqual(len(cfg.clusters), 1) self.assertIsInstance(cfg.users, dict) self.assertEqual(len(cfg.users), 1) self.assertIsInstance(cfg.context, dict) self.assertEqual(cfg.context, { 'cluster': 'self', 'user': '******' }) self.assertIsInstance(cfg.cluster, dict) self.assertEqual(cfg.cluster, {}) self.assertIsInstance(cfg.user, dict) self.assertEqual(cfg.user, {})