def test_it_raises_if_unknown_context_is_set(self, dummy_doc): assert len(dummy_doc["contexts"]) == 1 unknown_context_name = "xyz" assert dummy_doc["contexts"][0]["name"] != unknown_context_name cfg = config.KubeConfig(dummy_doc) with pytest.raises(errors.PyKubeError, match="unknown context"): cfg.current_context = unknown_context_name
async def test_it_raises_if_the_config_cant_be_converted_to_yaml( self, dummy_doc, tmp_path: Path ): cfg = config.KubeConfig(dummy_doc) cfg.cluster["w"] = object() # can't be serialized natively with pytest.raises(config.SerializationError, match="failed to persist"): await cfg.persist(tmp_path / "P")
def test_it_has_no_current_context_when_current_context_is_not_set_in_source_doc( self, dummy_doc ): del dummy_doc["current-context"] cfg = config.KubeConfig(dummy_doc) with pytest.raises(errors.PyKubeError, match="current context"): assert cfg.current_context
async def test_it_writes_to_the_path_attributes_without_path_arg( self, dummy_doc, tmp_path: Path ): cfg = config.KubeConfig(dummy_doc) cfg.path = tmp_path / "A" await cfg.persist() assert yaml.safe_load(cfg.path.read_text()) == cfg.as_dict()
def test_it_has_the_current_context_provided_as_init_argument(self, dummy_doc): dummy_doc["contexts"] = [ {"name": "context-A", "context": {"cluster": "A"}}, {"name": "context-B", "context": {"cluster": "B"}}, ] dummy_doc["current-context"] = "context-A" cfg = config.KubeConfig(dummy_doc, current_context="context-B") assert cfg.current_context == "context-B"
def test_it_sets_localhost_as_default_server(self, dummy_doc): dummy_doc["clusters"] = [ {"name": "A", "cluster": {"x": "y"}}, {"name": "B", "cluster": {"server": "S"}}, ] cfg = config.KubeConfig(dummy_doc) assert cfg.clusters["A"]["server"] == "http://localhost" assert cfg.clusters["B"]["server"] == "S"
def test_it_doesnt_change_when_source_doc_changes(self, dummy_doc): old_server = dummy_doc["clusters"][0]["cluster"]["server"] new_server = "http://changed" assert old_server != new_server cfg = config.KubeConfig(dummy_doc) dummy_doc["clusters"][0]["cluster"]["server"] = new_server assert cfg.cluster["server"] == old_server
def test_source_doc_doesnt_change_when_instance_changes(self, dummy_doc): old_server = dummy_doc["clusters"][0]["cluster"]["server"] new_server = "http://changed" assert old_server != new_server old_doc = deepcopy(dummy_doc) cfg = config.KubeConfig(dummy_doc) cfg.cluster["server"] = new_server assert dummy_doc == old_doc
def test_it_returns_a_dict_of_all_contexts_by_name(self, dummy_doc): dummy_doc["contexts"] = [ {"name": "A", "context": {"x": "y"}}, {"name": "B", "context": {"y": "z"}}, ] cfg = config.KubeConfig(dummy_doc) assert cfg.contexts.keys() == {"A", "B"} assert cfg.contexts["A"]["x"] == "y" assert cfg.contexts["B"]["y"] == "z"
def test_it_returns_the_currently_selected_cluster( self, selected, expected, dummy_doc ): dummy_doc["users"] = [ {"name": "user-A", "user": {"a": "b"}}, {"name": "user-B", "user": {"b": "c"}}, ] dummy_doc["contexts"] = [ {"name": "context-X", "context": {"user": "******"}}, {"name": "context-Y", "context": {"user": "******"}}, ] dummy_doc["current-context"] = selected cfg = config.KubeConfig(dummy_doc) assert mapping_issubset(expected, cfg.user)
async def test_it_writes_to_the_path_arg( self, with_attr: bool, dummy_doc, tmp_path: Path ): cfg = config.KubeConfig(dummy_doc) if with_attr: cfg.path = tmp_path / "A" else: cfg.path = None path = tmp_path / "P" await cfg.persist(path) assert yaml.safe_load(path.read_text()) == cfg.as_dict() if with_attr: assert not cfg.path.exists()
def test_it_makes_bof_the_client_certificate_and_key_if_present( self, field, dummy_doc, tmp_path: Path ): path = tmp_path / "file" path.write_text("in-file") dummy_doc["users"] = [ {"name": "A", "user": {"x": "y"}}, {"name": "B", "user": {field: str(path)}}, {"name": "C", "user": {f"{field}-data": base64.b64encode(b"inline")}}, ] cfg = config.KubeConfig(dummy_doc) assert field not in cfg.users["A"], "field not created if absent" for user_name in "BC": assert isinstance(cfg.users[user_name][field], config.BytesOrFile) assert f"{field}-data" not in cfg.users[user_name]
def test_it_makes_bof_the_certificate_authority_if_present( self, dummy_doc, tmp_path: Path ): ca_path = tmp_path / "ca" ca_path.write_text("in-file") ca = "certificate-authority" dummy_doc["clusters"] = [ {"name": "A", "cluster": {"x": "y"}}, {"name": "B", "cluster": {ca: str(ca_path)}}, {"name": "C", "cluster": {f"{ca}-data": base64.b64encode(b"inline")}}, ] cfg = config.KubeConfig(dummy_doc) assert ca not in cfg.clusters["A"], "field not created if absent" for cluster_name in "BC": assert isinstance(cfg.clusters[cluster_name][ca], config.BytesOrFile) assert f"{ca}-data" not in cfg.clusters[cluster_name]
def test_it_has_a_path_when_path_kwarg_is_provided(self, dummy_doc): path = Path("abc") cfg = config.KubeConfig(dummy_doc, path=path) assert cfg.path == path
async def test_it_raises_without_path_as_arg_or_attribute(self, dummy_doc): cfg = config.KubeConfig(dummy_doc) assert cfg.path is None with pytest.raises(config.SerializationError, match="no path associated"): await cfg.persist()
def test_it_is_the_input_plus_default_fields(self, doc, expected): assert mapping_issubset(doc, expected) assert config.KubeConfig(doc).as_dict() == expected
def test_it_raises_when_there_is_no_current_context(self, dummy_doc): dummy_doc["contexts"] = [{"name": "X", "context": {"namespace": "N"}}] dummy_doc.pop("current-context", None) cfg = config.KubeConfig(dummy_doc) with pytest.raises(errors.PyKubeError, match="current context not set"): _ = cfg.namespace
def test_it_is_the_current_contexts_namespace(self, dummy_doc): dummy_doc["contexts"] = [{"name": "X", "context": {"namespace": "N"}}] dummy_doc["current-context"] = "X" cfg = config.KubeConfig(dummy_doc) assert cfg.namespace == "N"
def test_it_is_default_when_not_specified(self, dummy_doc): dummy_doc["contexts"] = [{"name": "X", "context": {}}] dummy_doc["current-context"] = "X" cfg = config.KubeConfig(dummy_doc) assert cfg.namespace == "default"
def test_it_raises_when_there_is_no_current_context(self, dummy_doc): dummy_doc.pop("current-context", None) cfg = config.KubeConfig(dummy_doc) with pytest.raises(errors.PyKubeError, match="current context not set"): _ = cfg.user
def test_it_has_a_none_path_when_path_kwarg_is_not_provided(self, dummy_doc): cfg = config.KubeConfig(dummy_doc) assert cfg.path is None
def test_it_raises_when_there_is_no_current_context(self, dummy_doc): del dummy_doc["current-context"] cfg = config.KubeConfig(dummy_doc) with pytest.raises(errors.PyKubeError, match="current context not set"): _ = cfg.cluster
def test_it_has_a_current_context_when_current_context_is_set_in_source_doc( self, dummy_doc ): assert dummy_doc["current-context"] cfg = config.KubeConfig(dummy_doc) assert cfg.current_context == dummy_doc["current-context"]
def test_it_raises_if_empty_context_is_set(self, dummy_doc): cfg = config.KubeConfig(dummy_doc) with pytest.raises(errors.PyKubeError, match="invalid context"): cfg.current_context = "" with pytest.raises(errors.PyKubeError, match="invalid context"): cfg.current_context = None
def test_it_records_new_context(self, dummy_doc): dummy_doc["contexts"].append({"name": "xyz", "context": {}}) cfg = config.KubeConfig(dummy_doc) assert cfg.current_context != "xyz" cfg.current_context = "xyz" assert cfg.current_context == "xyz"
async def test_it_raises_if_the_path_is_invalid( self, dummy_doc, tmp_path: Path ): cfg = config.KubeConfig(dummy_doc) with pytest.raises(config.SerializationError): await cfg.persist(tmp_path) # a directory
def test_it_only_reads_the_underlying_dict_the_first_time(self, dummy_doc): cfg = config.KubeConfig(dummy_doc) first_id = id(cfg.users) second_id = id(cfg.users) assert first_id == second_id