def test_webserver(): # let's setup the directory to serve first temp_dir_name = "shiny-%s" % random_str() temp_dir_path = os.path.join("/tmp", temp_dir_name) # helper class to create and inittialize the dir -- will be removed once we # leave the context manager with Directory(temp_dir_path, mode=0o0700): # let's put some file in it with open(os.path.join(temp_dir_path, "candle"), "w") as fd: fd.write("You no take candle!") container = run_container(temp_dir_path) try: # we need to wait for the webserver to start serving container.wait_for_port(port) # GET on / http_response = container.http_request(path="/", port=port) assert http_response.ok assert '<a href="candle">candle</a>' in http_response.content.decode( "utf-8") # now GETting the file assert 'You no take candle!' in container.http_request( path="/candle", port=port).content.decode("utf-8") finally: container.kill() container.delete()
def test_directory_selinux_type(): selinux_type = "container_file_t" p = os.path.join("/tmp/", random_str()) with Directory(p, selinux_type=selinux_type): output = subprocess.check_output(["ls", "-Z", "-1", "-d", p]) assert selinux_type in output.decode("utf-8") assert not os.path.isdir(p)
def test_directory_selinux_context(): selinux_context = "system_u:object_r:unlabeled_t:s0" p = os.path.join("/tmp/", random_str()) with Directory(p, selinux_context=selinux_context): output = subprocess.check_output(["ls", "-Z", "-1", "-d", p]) assert selinux_context in output.decode("utf-8") assert not os.path.isdir(p)
def test_directory_basic(): with Directory(os.path.join("/tmp/", random_str())) as d: with open(os.path.join(str(d), "file"), "w") as fd: fd.write("hi!") with open(os.path.join(str(d), "file")) as fd: assert fd.read() == "hi!" assert not os.path.isdir(str(d))
def test_directory_selinux_bad(): selinux_type = "voodoo_file_t" selinux_context = "janko_u:beer_r:spilled_all_over_the_table_t:s0" p = os.path.join("/tmp/", random_str()) with pytest.raises(ConuException): Directory(p, selinux_type=selinux_type, selinux_context=selinux_context) assert not os.path.isdir(p)
def test_directory_mode(): p = os.path.join("/tmp/", random_str()) d = Directory(p, mode=0o0700) try: d.initialize() m = os.stat(p).st_mode print(m) assert oct(m)[-4:] == "0700" finally: d.clean() assert not os.path.isdir(p)
def test_run_with_volumes_metadata_check(tmpdir, podman_backend): t = str(tmpdir) image = podman_backend.ImageClass(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG, pull_policy=PodmanImagePullPolicy.NEVER) container = image.run_via_binary(volumes=(Directory(t), "/mountpoint", "Z")) try: mount = container.inspect()["Mounts"][0] assert mount["source"] == t assert mount["destination"] == "/mountpoint" assert "Z" in mount["options"] finally: container.delete(force=True)
def test_run_with_volumes_metadata_check(): with DockerBackend() as backend: image = backend.ImageClass(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG, pull_policy=DockerImagePullPolicy.NEVER) container = image.run_via_binary(volumes=(Directory('/usr/bin'), "/mountpoint", "Z")) binds = container.get_metadata()["HostConfig"]["Binds"] assert "/usr/bin:/mountpoint:Z" in binds mount = container.get_metadata()["Mounts"][0] print(mount) assert mount["Source"] == "/usr/bin" assert mount["Destination"] == "/mountpoint" assert mount["Mode"] == "Z" container.delete(force=True)
def test_run_with_volumes_metadata_check(tmpdir, podman_backend): t = str(tmpdir) mountpoint_path = "/mountpoint" image = podman_backend.ImageClass(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG, pull_policy=PodmanImagePullPolicy.NEVER) container = image.run_via_binary(volumes=(Directory(t), mountpoint_path, "Z")) try: for mount in container.inspect()["Mounts"]: if mount["source"] == t and mount["destination"] == mountpoint_path: assert "Z" in mount["options"] break # break was not reached: the mountpoint was not found else: assert False, "No mountpoint matching criteria: %s:%s:%s" % ( t, mountpoint_path, "Z") finally: container.delete(force=True)
def test_user_ownership_int(): p = os.path.join("/tmp/", random_str()) with Directory(p, user_owner=99) as d: s = os.stat(d.path) assert s.st_uid == 99
def test_directory_acl(): p = os.path.join("/tmp/", random_str()) with Directory(p, facl_rules=["u:26:rwx"]) as d: x = subprocess.check_output(["getfacl", str(d)]).decode("utf-8") assert "user:26:rwx" in x.split("\n") assert not os.path.isdir(str(d))
def test_ownership(): p = os.path.join("/tmp/", random_str()) with Directory(p, user_owner="nobody", group_owner="nobody") as d: s = os.stat(d.path) assert s.st_gid == 99 assert s.st_uid == 99
def test_group_ownership_404(): p = os.path.join("/tmp/", random_str()) with pytest.raises(ConuException): Directory(p, group_owner="illuminati")
def test_group_ownership(): p = os.path.join("/tmp/", random_str()) with Directory(p, group_owner=99) as d: s = os.stat(d.path) assert s.st_gid == 99
def test_user_ownership_404(): p = os.path.join("/tmp/", random_str()) with pytest.raises(ConuException): Directory(p, user_owner="waldo")
assert graceful_get({"a": [{1: 2}, {"b": "c"}]}, "a", 1, "b") == "c" def test_http_client_get_url(): assert get_url(path="/", host="172.1.1.1", port=80) == "http://172.1.1.1:80/" assert get_url(path="/app", host="domain.example.org", port=443) == "http://domain.example.org:443/app" @pytest.mark.parametrize("input_parameter,result", [ ("/target/path", "/target/path"), (("/source/path", "/target/path"), "/source/path:/target/path"), (("/source/path", "/target/path", "mode"), "/source/path:/target/path:mode"), ((Directory("/source/path"), "/target/path"), "/source/path:/target/path"), ((Directory("/source/path"), "/target/path", "mode"), "/source/path:/target/path:mode"), ]) def test_volume_create_from_tuple(input_parameter, result): assert str(Volume.create_from_tuple(input_parameter)) == result @pytest.mark.parametrize("source,target,mode,result", [ (None, "/target/path", None, "/target/path"), ("/source/path", "/target/path", None, "/source/path:/target/path"), ("/source/path", "/target/path", "mode", "/source/path:/target/path:mode"), (Directory("/source/path"), "/target/path", None, "/source/path:/target/path"), (Directory("/source/path"), "/target/path", "mode", "/source/path:/target/path:mode"),