def load_kubeconfig(self, path): """ Loads the kubeconfig found at the given path. If no file is found at the given path, Generate a new kubeconfig to write back. If the kubeconfig is valid, loads the content from it. If the kubeconfig is invalid, throw the relevant exception. :param path: The path to load a kubeconfig from :type path: string :raises KubeconfigInaccessableError: if the kubeconfig can't be opened :raises KubeconfigCorruptedError: if the kubeconfig is invalid :return: The loaded kubeconfig :rtype: Kubeconfig """ try: with open(path, "r") as stream: loaded_content = ordered_yaml_load(stream) except IOError as e: if e.errno == errno.ENOENT: loaded_content = None else: raise KubeconfigInaccessableError( "Can't open kubeconfig for reading: {0}".format(e)) except yaml.YAMLError as e: raise KubeconfigCorruptedError( "YamlError while loading kubeconfig: {0}".format(e)) loaded_config = Kubeconfig(path, loaded_content) self._validator.validate_config(loaded_config) return loaded_config
def test_invalid(self): invalid_cases = glob.glob(get_testdata("invalid_*")) for case in invalid_cases: with open(case, 'r') as stream: content_dict = ordered_yaml_load(stream) config = Kubeconfig(None, content_dict) self.assertRaises(KubeconfigCorruptedError, self._validator.validate_config, config)
def test_valid(self): valid_cases = glob.glob(get_testdata( "valid_*" )) for case in valid_cases: with open(case, 'r') as stream: content_dict = ordered_yaml_load(stream) if content_dict is not None: config = Kubeconfig(None, content_dict) try: self._validator.validate_config(config) except KubeconfigError as e: self.fail("Valid file {0} raised {1}.".format(case, e))