def test_kube_config__init(self): """ Verify creating a new instance of KubeConfig works as it should. """ file_path = get_fixture_file_path('test/kubeconfig.good') kc = kubeconfig.KubeConfig(file_path) kc.file_path = os.path.realpath(file_path)
def __init__(self, conf): """ Creates an instance of the Client. :param conf: Configuration dict :type conf: dict :returns: A Client instance :rtype: Client """ self._endpoints = [] for ep in conf['endpoint']: if ep.endswith('/'): ep = ep[:-1] self._endpoints.append(ep) # Take the first endpoint by default self.endpoint = conf['endpoint'][0] self._con = MultiServerSession(self._endpoints) self._con.headers['Content-Type'] = 'application/json' # Favor a kubeconfig over user/pass if conf.get('kubeconfig', None): from commctl import kubeconfig try: kube_user = kubeconfig.KubeConfig( conf['kubeconfig']).current_user for header in kube_user.auth_headers: self._con.headers[header[0]] = header[1] except kubeconfig.KubeConfigNoUserError as error: raise InvalidConfiguration('{0}: {1}'.format( type(error), error)) else: self._con.auth = (conf['username'], conf['password'])
def test_kube_config_current_user_with_token(self): """ Verify KubeConfig.current_user returns the proper user data with token. """ kc = kubeconfig.KubeConfig( get_fixture_file_path('test/kubeconfig.good')) self.assertEquals('token', kc.current_user.type) self.assertEquals('a', kc.current_user.token) self.assertEquals('a', kc.current_user.name)
def test_kube_config_current_user_with_password(self): """ Verify KubeConfig.current_user returns the proper user data with password. """ kc = kubeconfig.KubeConfig( get_fixture_file_path('test/kubeconfig.password')) self.assertEquals('password', kc.current_user.type) self.assertEquals('a', kc.current_user.username) self.assertEquals('a', kc.current_user.password) self.assertEquals('a', kc.current_user.name)
def test_kube_config_current_context(self): """ Verify KubeConfig.current_context returns the proper context. """ expected = { 'context': { 'cluster': 'commissaire', 'user': '******' }, 'name': 'commissaire', } kc = kubeconfig.KubeConfig( get_fixture_file_path('test/kubeconfig.good')) self.assertEquals(expected, kc.current_context)