def test_link_logfiles_env_variable(self, mock_get_env, mock_symlink, mock_makedirs, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): c = DockerContainer("valid_rootfs_id") c.link_logfiles() mock_symlink.assert_called_with( '/tmp/something/docker/valid_rootfs_id/var/log/2', '/var/log/crawler_container_logs/random_prefix/var/log/2') assert mock_symlink.call_count == 6
def test_links_with_mounts(self, mock_rmtree, mock_symlink, mock_makedirs, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): inspect = { "Id": "valid_rootfs_id", "Created": "2016-07-06T16:38:05.479090842Z", "State": { "Status": "running", "Running": True, "Pid": 11186 }, "Image": "sha256:07c86167cdc4264926fa5d2894e34a339ad27", "Name": "/pensive_rosalind", "Mounts": [{'Destination':'/var', 'Source':'/mount/in/the/host'}], "Config": { "Cmd": [ "bash" ], "Image": "ubuntu:trusty" }, "NetworkSettings": { } } c = DockerContainer("valid_rootfs_id", inspect) c.link_logfiles() mock_symlink.assert_called_with( '/mount/in/the/host/log/2', '/var/log/crawler_container_logs/random_prefix/var/log/2') c.unlink_logfiles() assert mock_symlink.call_count == 4
def test_init_wrong_environment(self, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): with self.assertRaises(crawler_exceptions.ContainerInvalidEnvironment): c = DockerContainer("no_namespace") with self.assertRaises(crawler_exceptions.ContainerInvalidEnvironment): c = DockerContainer("throw_bad_environment_exception_id") with self.assertRaises(Exception): c = DockerContainer("throw_non_handled_exception_id") with self.assertRaises(crawler_exceptions.ContainerInvalidEnvironment): c = DockerContainer("throw_value_error_id")
def _test_non_implemented_methods(self): c = DockerContainer("some_id") with self.assertRaises(NotImplementedError): c.get_memory_cgroup_path() with self.assertRaises(NotImplementedError): c.get_cpu_cgroup_path() with self.assertRaises(NotImplementedError): c.link_logfiles() with self.assertRaises(NotImplementedError): c.unlink_logfiles()
def test_init_from_inspect_w_repotags2(self, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): inspect = { "Id": "good_id", "Created": "2016-07-06T16:38:05.479090842Z", "State": { "Status": "running", "Running": True, "Pid": 11186 }, "Image": "sha256:07c86167cdc4264926fa5d2894e34a339ad27", "Name": "/pensive_rosalind", "Mounts": [], "Config": { "Cmd": ["bash"], "Image": "ubuntu:trusty" }, "NetworkSettings": {}, 'RepoTag': 'registry.com:123/img:latest' } c = DockerContainer("good_id", inspect) mock_inspect.assert_not_called() assert not c.root_fs assert mocked_get_runtime_env.call_count == 1 assert c.docker_image_long_name == 'registry.com:123/img:latest' assert c.docker_image_short_name == 'img:latest' assert c.docker_image_tag == 'latest' assert c.docker_image_registry == 'registry.com:123' assert c.owner_namespace == ''
def test_init_from_inspect(self, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): inspect = { "Id": "good_id", "Created": "2016-07-06T16:38:05.479090842Z", "State": { "Status": "running", "Running": True, "Pid": 11186 }, "Image": "sha256:07c86167cdc4264926fa5d2894e34a339ad27", "Name": "/pensive_rosalind", "Mounts": [], "Config": { "Cmd": [ "bash" ], "Image": "ubuntu:trusty" }, "NetworkSettings": { } } c = DockerContainer("good_id", inspect) mock_inspect.assert_not_called() assert not c.root_fs assert mocked_get_runtime_env.call_count == 1
def test_features_crawler_crawl_outcontainer_cpu(self): c = DockerContainer(self.container['Id']) crawler = FeaturesCrawler(crawl_mode='OUTCONTAINER', container=c) for key, feature in crawler.crawl_cpu(): print key, feature cores = len(list(crawler.crawl_cpu())) assert cores > 0
def test_links_with_volumes(self, mock_rmtree, mock_symlink, mock_makedirs, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): inspect = { "Id": "valid_rootfs_id", "Created": "2016-07-06T16:38:05.479090842Z", "State": { "Status": "running", "Running": True, "Pid": 11186 }, "Image": "sha256:07c86167cdc4264926fa5d2894e34a339ad27", "Name": "/pensive_rosalind", # /var in the container is mapped to /mount/in/the/host # container was started with -v /var/in/the/host:/var "Volumes": { '/var': '/var/in/the/host' }, "Config": { "Cmd": ["bash"], "Image": "ubuntu:trusty" }, "NetworkSettings": {} } c = DockerContainer("valid_rootfs_id", inspect) c.link_logfiles() mock_symlink.assert_called_with( '/var/in/the/host/log/2', '/var/log/crawler_container_logs/random_prefix/var/log/2') c.unlink_logfiles() assert mock_symlink.call_count == 4
def test_link_and_unlink_docker_json_logfile(self, mock_json_logs, mock_rmtree, mock_symlink, mock_makedirs, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): c = DockerContainer("valid_rootfs_id") c.link_logfiles() mock_symlink.assert_called_with( '/var/lib/docker/abc/container/log.json', '/var/log/crawler_container_logs/random_prefix/docker.log') c.unlink_logfiles() assert mock_symlink.call_count == 5 assert mock_rmtree.call_count == 1
def _test_is_running(self, mock_exists): c = DockerContainer("good_id") assert c.is_running()
def test_init(self, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): c = DockerContainer("good_id") mock_inspect.assert_called() assert not c.root_fs assert mocked_get_runtime_env.call_count == 1
def test_link_logfiles_symlink_exception(self, mock_symlink, mock_makedirs, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): c = DockerContainer("valid_rootfs_id") c.link_logfiles()
def test_init_failed(self, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): with self.assertRaises(crawler_exceptions.ContainerNonExistent): DockerContainer("no_container_id") assert mocked_get_runtime_env.call_count == 0
def test_cpu_cgroup(self, mocked_ismount, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): c = DockerContainer("good_id") assert c.get_cpu_cgroup_path('abc') == '/cgroup/cpuacct/docker/good_id/abc'
def test_cpu_cgroup(self, mocked_ismount, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): c = DockerContainer("good_id") assert c.get_cpu_cgroup_path('abc') == ( "/cgroup/cpuacct/docker/good_id/" "abc") or ("cgroup/cpu,cpuacct/docker/good_id/abc")
def _test_to_str(self): c = DockerContainer("good_id") print(c)
def _test_eq_ne(self): c1 = DockerContainer("good_id") c2 = DockerContainer("ebcd") c3 = DockerContainer("ebcd") assert c1 != c2 assert c2 == c3
def test_is_docker(self, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): c = DockerContainer("good_id") assert c.is_docker_container() print(c)
def test_features_crawler_crawl_outcontainer_processes(self): c = DockerContainer(self.container['Id']) crawler = FeaturesCrawler(crawl_mode='OUTCONTAINER', container=c) assert len(list(crawler.crawl_processes())) == 2 # sleep + crawler
def test_features_crawler_crawl_outcontainer_mem(self): c = DockerContainer(self.container['Id']) crawler = FeaturesCrawler(crawl_mode='OUTCONTAINER', container=c) output = "%s" % list(crawler.crawl_memory()) assert 'memory_used' in output
def test_memory_cgroup(self, mocked_ismount, mock_get_rootfs, mock_inspect, mocked_get_runtime_env, mocked_dockerps): c = DockerContainer("good_id") assert c.get_memory_cgroup_path( 'abc') == '/cgroup/memory/docker/good_id/abc'