def test_has_cluster(self): self._content["clusters"] = [ OrderedDict([("cluster", None), ("name", "clustername")]), OrderedDict([("cluster", None), ("name", "anotherclustername")]) ] config = Kubeconfig(self._path, self._content) self.assertTrue(config.has_cluster("clustername")) self.assertFalse(config.has_cluster("othercluster"))
def test_has_cluster(self): self._content["clusters"] = [ OrderedDict([ ("cluster", None), ("name", "clustername") ]), OrderedDict([ ("cluster", None), ("name", "anotherclustername") ]) ] config = Kubeconfig(self._path, self._content) self.assertTrue(config.has_cluster("clustername")) self.assertFalse(config.has_cluster("othercluster"))
def test_load_noexist(self): no_exist_path = os.path.join(self._temp_directory, "this_does_not_exist") loaded_config = self._loader.load_kubeconfig(no_exist_path) self.assertEqual(loaded_config.content, _get_new_kubeconfig_content()) self._validator.validate_config.called_with( Kubeconfig(no_exist_path, _get_new_kubeconfig_content()))
def test_update_existing(self): initial = OrderedDict([("apiVersion", "v1"), ("clusters", [ OrderedDict([("cluster", OrderedDict([("server", "endpoint")])), ("name", "clustername")]) ]), ("contexts", []), ("current-context", None), ("kind", "Config"), ("preferences", OrderedDict()), ("users", [])]) cluster = OrderedDict([ ("cluster", OrderedDict([("certificate-authority-data", "data"), ("server", "endpoint")])), ("name", "clustername") ]) correct = OrderedDict([ ("apiVersion", "v1"), ("clusters", [ OrderedDict([("cluster", OrderedDict([("certificate-authority-data", "data"), ("server", "endpoint")])), ("name", "clustername")]) ]), ("contexts", []), ("current-context", None), ("kind", "Config"), ("preferences", OrderedDict()), ("users", []) ]) updated = self._appender.insert_entry(Kubeconfig(None, initial), "clusters", cluster) self.assertDictEqual(updated.content, correct)
def test_not_world_readable(self): tmpdir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, tmpdir) config_path = os.path.join(tmpdir, "config") config = Kubeconfig(config_path, None) KubeconfigWriter().write_kubeconfig(config) stat = os.stat(config_path) self.assertEqual(stat.st_mode & 0o777, 0o600)
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_truncates(self): tmpdir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, tmpdir) config_path = os.path.join(tmpdir, "config") with open(config_path, "w+") as f: f.write("#" * 100) KubeconfigWriter().write_kubeconfig(Kubeconfig(config_path, {})) empty_stat = os.stat(config_path) self.assertLessEqual(empty_stat.st_size, 4, "file should be '{}[newline]', 3/4 bytes long ")
def test_write_directory(self): content = OrderedDict([("current-context", "context"), ("apiVersion", "v1")]) containing_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, containing_dir) config = Kubeconfig(containing_dir, content) self.assertRaises(KubeconfigInaccessableError, self._writer.write_kubeconfig, 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))
def test_key_not_array(self): initial = OrderedDict([("apiVersion", "v1"), ("contexts", []), ("current-context", None), ("kind", "Config"), ("preferences", OrderedDict()), ("users", [])]) cluster = OrderedDict([ ("cluster", OrderedDict([("certificate-authority-data", "data"), ("server", "endpoint")])), ("name", "clustername") ]) self.assertRaises(KubeconfigError, self._appender.insert_entry, Kubeconfig(None, initial), "kind", cluster)
def test_basic_insert(self): initial = OrderedDict([ ("apiVersion", "v1"), ("clusters", [ OrderedDict([ ("cluster", OrderedDict([ ("certificate-authority-data", "data1"), ("server", "endpoint1") ])), ("name", "oldclustername") ]) ]), ("contexts", []), ("current-context", "simple"), ("kind", "Config"), ("preferences", OrderedDict()), ("users", []) ]) cluster = OrderedDict([ ("cluster", OrderedDict([ ("certificate-authority-data", "data2"), ("server", "endpoint2") ])), ("name", "clustername") ]) cluster_added_correct = OrderedDict([ ("apiVersion", "v1"), ("clusters", [ OrderedDict([ ("cluster", OrderedDict([ ("certificate-authority-data", "data1"), ("server", "endpoint1") ])), ("name", "oldclustername") ]), OrderedDict([ ("cluster", OrderedDict([ ("certificate-authority-data", "data2"), ("server", "endpoint2") ])), ("name", "clustername") ]) ]), ("contexts", []), ("current-context", "simple"), ("kind", "Config"), ("preferences", OrderedDict()), ("users", []) ]) cluster_added = self._appender.insert_entry(Kubeconfig(None, initial), "clusters", cluster) self.assertDictEqual(cluster_added.content, cluster_added_correct)
def test_write_order(self): content = OrderedDict([("current-context", "context"), ("apiVersion", "v1")]) file_to_write = tempfile.NamedTemporaryFile(mode='w').name self.addCleanup(os.remove, file_to_write) config = Kubeconfig(file_to_write, content) self._writer.write_kubeconfig(config) with open(file_to_write, 'r') as stream: self.assertMultiLineEqual( stream.read(), "current-context: context\n" "apiVersion: v1\n")
def test_load_simple(self): simple_path = self._clone_config("valid_simple") content = OrderedDict([ ("apiVersion", "v1"), ("clusters", [ OrderedDict([("cluster", OrderedDict([("server", "simple")])), ("name", "simple")]) ]), ("contexts", None), ("current-context", "simple"), ("kind", "Config"), ("preferences", OrderedDict()), ("users", None) ]) loaded_config = self._loader.load_kubeconfig(simple_path) self.assertEqual(loaded_config.content, content) self._validator.validate_config.called_with( Kubeconfig(simple_path, content))
def test_write_makedirs(self): content = OrderedDict([("current-context", "context"), ("apiVersion", "v1")]) containing_dir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, containing_dir) config_path = os.path.join(containing_dir, "dir1", "dir2", "dir3") config = Kubeconfig(config_path, content) self._writer.write_kubeconfig(config) with open(config_path, 'r') as stream: self.assertMultiLineEqual( stream.read(), "current-context: context\n" "apiVersion: v1\n")
def test_has_cluster_with_no_clusters(self): config = Kubeconfig(self._path, self._content) self.assertFalse(config.has_cluster("clustername"))
def test_no_content(self): config = Kubeconfig(self._path, None) self.assertEqual(config.content, _get_new_kubeconfig_content())
def test_load_empty(self): empty_path = self._clone_config("valid_empty_existing") loaded_config = self._loader.load_kubeconfig(empty_path) self.assertEqual(loaded_config.content, _get_new_kubeconfig_content()) self._validator.validate_config.called_with( Kubeconfig(empty_path, _get_new_kubeconfig_content()))
def test_has_cluster_with_no_clusters(self): config = Kubeconfig(self._path, self._content) self.assertFalse(config.has_cluster("clustername"))