def test_targeting(get_resource_status: MagicMock): """Should target the one matching resource specified in the command.""" get_resource_status.return_value = RESPONSE_SCENARIOS[-1] bundle = kuber.create_bundle() bundle.add("v1", "Namespace", "foo") bundle.add("v1", "Namespace", "bar") bundle.add("v1", "Namespace", "baz") bundle.cli(arguments=["status", "--target=namespace/foo"]) assert get_resource_status.call_count == 1
def test_custom_object_in_bundle(): """Should load a custom object successfully.""" bundle = kuber.create_bundle(kubernetes_version="latest").add_file( directory.joinpath("custom_object.yaml")) assert len(bundle.resources) == 1 co = typing.cast(custom_v1.CustomObject, bundle.resources.workflow[0]) assert co.api_version == "argoproj.io/v1alpha1" assert co.kind == "Workflow" assert co.to_yaml() is not None
def test_targeting_all(get_resource_status: MagicMock): """Should target all resources if no target is set on the command line.""" get_resource_status.return_value = RESPONSE_SCENARIOS[-1] bundle = kuber.create_bundle() bundle.add("v1", "Namespace", "foo") bundle.add("v1", "Namespace", "bar") bundle.add("v1", "Namespace", "baz") bundle.cli(arguments=["status"]) assert get_resource_status.call_count == 3
def test_assignments(): """Should manage internal settings as expected.""" settings = kuber.create_bundle().settings settings.add(a=1, b=2) settings.add(b=None) settings.c = "hello" settings["d"] = "goodbye" assert settings.a == 1 assert settings["a"] == 1 assert settings.grab("a", "b", "c", "d") == (1, None, "hello", "goodbye")
def test_custom_resource_definition(): """ Should not cause a recursion error while loading a CRD yaml definition. For more details on the source of this scenario see: https://github.com/sernst/kuber/issues/1 """ bundle = kuber.create_bundle(kubernetes_version="latest").add_file( os.path.join(MY_DIRECTORY, "crd.yaml") ) assert len(bundle.resources) == 1 crd: apiextensions_v1.CustomResourceDefinition = bundle.resources[0] assert ( crd.spec.versions[0].schema.open_apiv3_schema.not_ is None ), """
def test_settings(): """ Should properly assign a setting that is accessible through all possible avenues. """ s = kuber.create_bundle().settings s.add(a=42) assert s.fetch("a") == 42 assert s.grab("a")[0] == 42 assert s["a"] == 42 assert s.a == 42 assert s.to_dict() == {"a": 42} assert s.to_yaml().find("42") > 0 assert list(s.values()) == [42] assert list(s.keys()) == ["a"] assert list(s.items()) == [("a", 42)]
def test_file_error(): """Should error out if the file is not of the correct type.""" settings = kuber.create_bundle().settings with pytest.raises(IOError): settings.add_from_file(os.path.realpath(__file__))
def test_new(): """Should create new resource, add it to bundle and return it.""" bundle = kuber.create_bundle() resource = bundle.new("v1", "ConfigMap", "foo") assert resource == bundle.resources[-1] assert resource.metadata.name == "foo"
import kuber from kuber.v1_20 import apps_v1 def configure_deployment(): shared_labels = {"app": "foo"} d = apps_v1.Deployment() with d.metadata as md: md.name = "my-deployment" md.labels.update(shared_labels, component="application") with d.spec as s: s.selector.match_labels.update(shared_labels) s.template.metadata.labels.update(shared_labels) s.replicas = 2 d.append_container(name="app", image="my-app:1.0") return d if __name__ == "__main__": bundle = kuber.create_bundle(kubernetes_version="1.13") bundle.push(configure_deployment()) bundle.cli()
def test_version(): """Should initialize bundle as expected.""" bundle = kuber.create_bundle(kubernetes_version=latest.KUBERNETES_VERSION, bundle_name="foo") assert bundle.kubernetes_version.label == "latest" assert bundle.name == "foo"
def test_initialization(): """Should initialize bundle as expected.""" bundle = kuber.create_bundle(kubernetes_version="pre", bundle_name="foo") assert bundle.kubernetes_version.label == "pre" assert bundle.name == "foo" assert bundle.cli is not None