2016-07-07 02:14:18 +03:00
|
|
|
#!/usr/bin/env python3
|
2019-06-20 02:58:25 +03:00
|
|
|
# Copyright (c) Facebook, Inc. and its affiliates.
|
2016-07-07 02:14:18 +03:00
|
|
|
#
|
2019-06-20 02:58:25 +03:00
|
|
|
# This software may be used and distributed according to the terms of the
|
|
|
|
# GNU General Public License version 2.
|
2016-07-07 02:14:18 +03:00
|
|
|
|
|
|
|
import os
|
2016-08-05 22:49:21 +03:00
|
|
|
import stat
|
|
|
|
import tempfile
|
2018-10-10 22:47:33 +03:00
|
|
|
import textwrap
|
2018-05-10 07:33:49 +03:00
|
|
|
|
2017-04-14 21:31:47 +03:00
|
|
|
from eden.cli import util
|
2016-07-07 02:14:18 +03:00
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
from .lib import testcase
|
|
|
|
|
2016-07-07 02:14:18 +03:00
|
|
|
|
2016-07-08 21:25:45 +03:00
|
|
|
@testcase.eden_repo_test
|
2018-04-05 03:31:25 +03:00
|
|
|
class RCTest(testcase.EdenRepoTest):
|
2018-04-05 03:31:28 +03:00
|
|
|
def populate_repo(self) -> None:
|
2018-05-10 07:33:49 +03:00
|
|
|
self.repo.write_file("readme.txt", "test\n")
|
|
|
|
self.repo.commit("Initial commit.")
|
2016-07-07 02:14:18 +03:00
|
|
|
|
2018-04-05 03:31:28 +03:00
|
|
|
def test_eden_list(self) -> None:
|
2019-01-26 03:24:06 +03:00
|
|
|
mounts = self.eden.run_cmd("list")
|
|
|
|
self.assertEqual(f"{self.mount}\n", mounts)
|
2016-07-07 02:14:30 +03:00
|
|
|
|
2018-05-23 05:45:21 +03:00
|
|
|
self.eden.remove(self.mount)
|
2019-01-26 03:24:06 +03:00
|
|
|
mounts = self.eden.run_cmd("list")
|
|
|
|
self.assertEqual("", mounts, msg="There should be 0 mount paths after remove")
|
2016-07-07 02:14:30 +03:00
|
|
|
|
2016-07-08 21:25:39 +03:00
|
|
|
self.eden.clone(self.repo_name, self.mount)
|
2019-01-26 03:24:06 +03:00
|
|
|
mounts = self.eden.run_cmd("list")
|
|
|
|
self.assertEqual(f"{self.mount}\n", mounts)
|
2016-07-07 02:14:30 +03:00
|
|
|
|
2018-04-05 03:31:28 +03:00
|
|
|
def test_unmount_rmdir(self) -> None:
|
2018-05-10 07:33:49 +03:00
|
|
|
clients = os.path.join(self.eden_dir, "clients")
|
2016-07-07 02:14:30 +03:00
|
|
|
client_names = os.listdir(clients)
|
2018-05-10 07:33:49 +03:00
|
|
|
self.assertEqual(1, len(client_names), msg="There should only be 1 client")
|
2016-07-07 02:14:30 +03:00
|
|
|
test_client_dir = os.path.join(clients, client_names[0])
|
|
|
|
|
|
|
|
# Eden list command uses keys of directory map to get mount paths
|
2019-01-26 03:24:06 +03:00
|
|
|
mounts = self.eden.list_cmd_simple()
|
|
|
|
self.assertEqual({self.mount: "RUNNING"}, mounts)
|
2016-07-07 02:14:30 +03:00
|
|
|
|
2018-05-23 05:45:21 +03:00
|
|
|
self.eden.remove(self.mount)
|
2016-07-07 02:14:30 +03:00
|
|
|
self.assertFalse(os.path.isdir(test_client_dir))
|
|
|
|
|
2018-05-23 05:45:21 +03:00
|
|
|
# Check that _remove_path_from_directory_map in remove is successful
|
2019-01-26 03:24:06 +03:00
|
|
|
mounts = self.eden.list_cmd_simple()
|
2018-05-10 07:33:49 +03:00
|
|
|
self.assertEqual({}, mounts, msg="There should be 0 paths in the directory map")
|
2016-07-07 02:14:30 +03:00
|
|
|
|
2016-07-08 21:25:39 +03:00
|
|
|
self.eden.clone(self.repo_name, self.mount)
|
2018-05-10 07:33:49 +03:00
|
|
|
self.assertTrue(
|
|
|
|
os.path.isdir(test_client_dir),
|
|
|
|
msg="Client name should be restored verbatim because \
|
|
|
|
it should be a function of the mount point",
|
|
|
|
)
|
2019-01-26 03:24:06 +03:00
|
|
|
mounts = self.eden.list_cmd_simple()
|
2018-05-10 07:33:49 +03:00
|
|
|
self.assertEqual(
|
2019-01-26 03:24:06 +03:00
|
|
|
{self.mount: "RUNNING"},
|
2018-05-10 07:33:49 +03:00
|
|
|
mounts,
|
|
|
|
msg="The client directory should have been restored",
|
|
|
|
)
|
2016-08-05 22:49:21 +03:00
|
|
|
|
2018-04-05 03:31:28 +03:00
|
|
|
def test_override_system_config(self) -> None:
|
2018-05-10 07:33:49 +03:00
|
|
|
system_repo = self.create_repo("system_repo")
|
2016-08-05 22:49:21 +03:00
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
system_repo.write_file("hello.txt", "hola\n")
|
|
|
|
system_repo.commit("Initial commit.")
|
2016-08-05 22:49:21 +03:00
|
|
|
|
2018-04-24 23:17:19 +03:00
|
|
|
repo_info = util.get_repo(system_repo.path)
|
|
|
|
assert repo_info is not None
|
2016-08-05 22:49:21 +03:00
|
|
|
|
|
|
|
# Create temporary system config
|
2019-06-12 04:22:49 +03:00
|
|
|
system_config_dir = self.eden.etc_eden_dir / "config.d"
|
|
|
|
system_config_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
f, path = tempfile.mkstemp(dir=str(system_config_dir), suffix=".toml")
|
2016-08-05 22:49:21 +03:00
|
|
|
|
|
|
|
# Add system_repo to system config file
|
2018-10-10 22:47:33 +03:00
|
|
|
config = textwrap.dedent(
|
|
|
|
f"""\
|
|
|
|
["repository {self.repo_name}"]
|
|
|
|
path = "{repo_info.source}"
|
|
|
|
type = "{repo_info.type}"
|
|
|
|
"""
|
2018-05-19 09:05:39 +03:00
|
|
|
)
|
2018-05-10 07:33:49 +03:00
|
|
|
os.write(f, config.encode("utf-8"))
|
2016-08-05 22:49:21 +03:00
|
|
|
os.close(f)
|
|
|
|
|
|
|
|
# Clone repository
|
2018-05-10 07:33:49 +03:00
|
|
|
mount_path = os.path.join(self.mounts_dir, self.repo_name + "-1")
|
2016-08-05 22:49:21 +03:00
|
|
|
self.eden.clone(self.repo_name, mount_path)
|
|
|
|
|
|
|
|
# Verify that clone used repository data from user config
|
2018-05-10 07:33:49 +03:00
|
|
|
readme = os.path.join(mount_path, "hello.txt")
|
2016-08-05 22:49:21 +03:00
|
|
|
self.assertFalse(os.path.exists(readme))
|
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
hello = os.path.join(mount_path, "readme.txt")
|
2016-08-05 22:49:21 +03:00
|
|
|
st = os.lstat(hello)
|
|
|
|
self.assertTrue(stat.S_ISREG(st.st_mode))
|
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
with open(hello, "r") as hello_file:
|
|
|
|
self.assertEqual("test\n", hello_file.read())
|
2016-08-05 22:49:21 +03:00
|
|
|
|
|
|
|
# Add system_repo to system config file with new name
|
2018-05-10 07:33:49 +03:00
|
|
|
repo_name = "repo"
|
2016-08-05 22:49:21 +03:00
|
|
|
f = os.open(path, os.O_WRONLY)
|
2018-10-10 22:47:33 +03:00
|
|
|
config = textwrap.dedent(
|
|
|
|
f"""\
|
|
|
|
["repository {repo_name}"]
|
|
|
|
path = "{repo_info.source}"
|
|
|
|
type = "{repo_info.type}"
|
|
|
|
"""
|
2018-05-19 09:05:39 +03:00
|
|
|
)
|
2018-05-10 07:33:49 +03:00
|
|
|
os.write(f, config.encode("utf-8"))
|
2016-08-05 22:49:21 +03:00
|
|
|
os.close(f)
|
|
|
|
|
|
|
|
# Clone repository
|
2018-05-10 07:33:49 +03:00
|
|
|
mount_path = os.path.join(self.mounts_dir, repo_name + "-1")
|
2016-08-05 22:49:21 +03:00
|
|
|
self.eden.clone(repo_name, mount_path)
|
|
|
|
|
|
|
|
# Verify that clone used repository data from system config
|
2018-05-10 07:33:49 +03:00
|
|
|
readme = os.path.join(mount_path, "readme.txt")
|
2016-08-05 22:49:21 +03:00
|
|
|
self.assertFalse(os.path.exists(readme))
|
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
hello = os.path.join(mount_path, "hello.txt")
|
2016-08-05 22:49:21 +03:00
|
|
|
st = os.lstat(hello)
|
|
|
|
self.assertTrue(stat.S_ISREG(st.st_mode))
|
|
|
|
|
2018-05-10 07:33:49 +03:00
|
|
|
with open(hello, "r") as hello_file:
|
|
|
|
self.assertEqual("hola\n", hello_file.read())
|