def __init__(self, docker=Docker(), runner=Runner(), file_wrapper=FileWrapper()): self.docker = docker self.runner = runner self.file_wrapper = file_wrapper
def __init__(self): self.sub_proc = SubProc() self.docker = Docker(self.sub_proc) self.runner = Runner(self.docker) self.file_wrapper = FileWrapper() self.bc_module = BcModule(self.docker, self.runner, self.file_wrapper) self.git = Git(self.sub_proc) self.ui = Ui() self.tools = [ help.Help(self.bc_module), update.Update(self.docker), version.Version(self.docker), ghrn.Ghrn(self.docker), docs.Docs(self.docker), ] self.config_reader = ConfigReader(self.file_wrapper, self.bc_module, self.tools)
def setUp(self): self.sub_proc_mock = MagicMock(SubProc) self.sut = Docker(sub_proc=self.sub_proc_mock)
class DockerTest(unittest.TestCase): def setUp(self): self.sub_proc_mock = MagicMock(SubProc) self.sut = Docker(sub_proc=self.sub_proc_mock) def test_that_container_exists_returns_true_when_result_is_0(self): # Fixture name = "theName" self.sub_proc_mock.call.return_value = 0 # Test actual = self.sut.container_exist(name) # Assert self.assertTrue(actual) self.sub_proc_mock.call.assert_called_once_with( ["docker", "inspect", "--format={{.Id}}", name], stdout=ANY, stderr=subprocess.STDOUT) def test_that_container_exists_returns_false_when_result_is_not_0(self): # Fixture name = "theName" self.sub_proc_mock.call.return_value = 1 # Test actual = self.sut.container_exist(name) # Assert self.assertFalse(actual) def test_that_stop_and_remove_container_makes_the_right_calls(self): # Fixture name = "theName" expected_calls = [ call(["docker", "stop", name], stdout=ANY), call(["docker", "rm", "-v", name], stdout=ANY) ] # Test self.sut.stop_and_remove_container(name) # Assert self.sub_proc_mock.check_call.assert_has_calls(expected_calls) def test_that_push_makes_the_right_calls(self): # Fixture name = "theName" # Test self.sut.push(name) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ["docker", "push", name]) def test_that_image_exist_returns_true_when_no_exception_is_raised(self): # Fixture name = "theName" # Test actual = self.sut.image_exists(name) # Assert self.assertTrue(actual) self.sub_proc_mock.check_output.assert_called_once_with( ["docker", "pull", name], stderr=subprocess.STDOUT) def test_that_image_exist_returns_false_when_image_is_not_found(self): # Fixture name = "theName" self.sub_proc_mock.check_output.side_effect = ToolbeltException( 'Error: image library/undefined not found') # Test actual = self.sut.image_exists(name) # Assert self.assertFalse(actual) def test_that_image_exist_returns_raises_exception(self): # Fixture name = "theName" self.sub_proc_mock.check_output.side_effect = ToolbeltException( 'Other message') # Test # Assert with self.assertRaises(ToolbeltException): self.sut.image_exists(name) def test_run_in_container(self): # Fixture image_name = "anImage" args = ['a1', 'a2'] volumes = [('v1', 'v2'), ('v3', 'v4')] volumes_from = ['vf1', 'vf2'] # Test self.sut.run_in_container(image_name, args, volumes, volumes_from) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'run', '--rm', '-v', '/var/run/docker.sock:/var/run/docker.sock', '-it', '-v', 'v1:v2', '-v', 'v3:v4', '--volumes-from="vf1"', '--volumes-from="vf2"', image_name, 'a1', 'a2']) def test_run_in_container_no_tty(self): # Fixture image_name = "anImage" args = [] with patch('sys.stdout') as mock: mock.isatty.return_value = False # Test self.sut.run_in_container(image_name, args) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'run', '--rm', '-v', '/var/run/docker.sock:/var/run/docker.sock', image_name]) def test_run_script_in_container(self): # Fixture image_name = "anImage" script = 'doit.sh' script_args = ['a1', 'a2'] volumes = [('v1', 'v2'), ('v3', 'v4')] volumes_from = ['vf1', 'vf2'] # Test self.sut.run_script_in_container(image_name, script, script_args, volumes, volumes_from) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'run', '--rm', '-v', '/var/run/docker.sock:/var/run/docker.sock', '-it', '-v', 'v1:v2', '-v', 'v3:v4', '--volumes-from="vf1"', '--volumes-from="vf2"', image_name, script, 'a1', 'a2']) def test_list_images(self): # Fixture out1 = "REPOSITORY TAG IMAGE ID CREATED SIZE\n" # noqa out2 = "repo/image1 49 1b69f8ee3b87 7 hours ago 675.2 MB\n" # noqa out3 = "repo/image2 17 bcf6d6f25607 12 hours ago 675.1 MB" # noqa self.sub_proc_mock.check_output.return_value = out1 + out2 + out3 expected = [ {"repo": 'repo/image1', "tag": '49', "id": '1b69f8ee3b87'}, {"repo": 'repo/image2', "tag": '17', "id": 'bcf6d6f25607'}, ] # Test actual = self.sut.list_images() # Assert self.sub_proc_mock.check_output.assert_called_once_with( ['docker', 'images']) self.assertEqual(expected, actual) def test_remove_images(self): # Fixture images = ['i1', 'i2'] # Test self.sut.remove_images(images) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'rmi', 'i1', 'i2']) def test_tag(self): # Fixture existing_image = "existing" new_image = "new" # Test self.sut.tag(existing_image, new_image) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'tag', existing_image, new_image]) def test_pull(self): # Fixture image = "someImage" # Test self.sut.pull(image) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'pull', image]) def test_inspect(self): # Fixture container = "my-container" expected = [{"Id": "123123", "Created": "2016-02-21T12:..."}] data = json.dumps(expected) self.sub_proc_mock.check_output.return_value = data # Test actual = self.sut.inspect(container) # Assert self.assertEqual(expected, actual) self.sub_proc_mock.check_output.assert_called_once_with( ["docker", "inspect", container])
def setUp(self): self.sub_proc_mock = MagicMock(SubProc) self.sut = Docker(sub_proc=self.sub_proc_mock)
class DockerTest(unittest.TestCase): def setUp(self): self.sub_proc_mock = MagicMock(SubProc) self.sut = Docker(sub_proc=self.sub_proc_mock) def test_that_container_exists_returns_true_when_result_is_0(self): # Fixture name = "theName" self.sub_proc_mock.call.return_value = 0 # Test actual = self.sut.container_exist(name) # Assert self.assertTrue(actual) self.sub_proc_mock.call.assert_called_once_with( ["docker", "inspect", "--format={{.Id}}", name], stdout=ANY, stderr=subprocess.STDOUT) def test_that_container_exists_returns_false_when_result_is_not_0(self): # Fixture name = "theName" self.sub_proc_mock.call.return_value = 1 # Test actual = self.sut.container_exist(name) # Assert self.assertFalse(actual) def test_that_stop_and_remove_container_makes_the_right_calls(self): # Fixture name = "theName" expected_calls = [ call(["docker", "stop", name], stdout=ANY), call(["docker", "rm", "-v", name], stdout=ANY) ] # Test self.sut.stop_and_remove_container(name) # Assert self.sub_proc_mock.check_call.assert_has_calls(expected_calls) def test_that_push_makes_the_right_calls(self): # Fixture name = "theName" # Test self.sut.push(name) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ["docker", "push", name]) def test_that_image_exist_returns_true_when_no_exception_is_raised(self): # Fixture name = "theName" # Test actual = self.sut.image_exists(name) # Assert self.assertTrue(actual) self.sub_proc_mock.check_output.assert_called_once_with( ["docker", "pull", name], stderr=subprocess.STDOUT) def test_that_image_exist_returns_false_when_image_is_not_found(self): # Fixture name = "theName" self.sub_proc_mock.check_output.side_effect = ToolbeltException( 'Error: image library/undefined not found') # Test actual = self.sut.image_exists(name) # Assert self.assertFalse(actual) def test_that_image_exist_returns_raises_exception(self): # Fixture name = "theName" self.sub_proc_mock.check_output.side_effect = ToolbeltException( 'Other message') # Test # Assert with self.assertRaises(ToolbeltException): self.sut.image_exists(name) def test_run_in_container(self): # Fixture uid = '123' image_name = "anImage" args = ['a1', 'a2'] volumes = [('v1', 'v2'), ('v3', 'v4')] volumes_from = ['vf1', 'vf2'] with patch('sys.stdout') as mock: mock.isatty.return_value = True # Test self.sut.run_in_container(uid, image_name, args, volumes, volumes_from) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'run', '--rm', '-u', uid, '-v', '/var/run/docker.sock:/var/run/docker.sock', '-it', '-v', 'v1:v2', '-v', 'v3:v4', '--volumes-from', 'vf1', '--volumes-from', 'vf2', image_name, 'a1', 'a2']) def test_run_in_container_no_tty(self): # Fixture uid = '123' image_name = "anImage" args = [] with patch('sys.stdout') as mock: mock.isatty.return_value = False # Test self.sut.run_in_container(uid, image_name, args) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'run', '--rm', '-u', uid, '-v', '/var/run/docker.sock:/var/run/docker.sock', image_name]) def test_run_script_in_container(self): # Fixture uid = '123' image_name = "anImage" script = 'doit.sh' script_args = ['a1', 'a2'] volumes = [('v1', 'v2'), ('v3', 'v4')] volumes_from = ['vf1', 'vf2'] with patch('sys.stdout') as mock: mock.isatty.return_value = True # Test self.sut.run_script_in_container(uid, image_name, script, script_args, volumes, volumes_from) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'run', '--rm', '-u', uid, '-v', '/var/run/docker.sock:/var/run/docker.sock', '-it', '-v', 'v1:v2', '-v', 'v3:v4', '--volumes-from', 'vf1', '--volumes-from', 'vf2', image_name, script, 'a1', 'a2']) def test_list_images(self): # Fixture out1 = "REPOSITORY TAG IMAGE ID CREATED SIZE\n" # noqa out2 = "repo/image1 49 1b69f8ee3b87 7 hours ago 675.2 MB\n" # noqa out3 = "repo/image2 17 bcf6d6f25607 12 hours ago 675.1 MB" # noqa self.sub_proc_mock.check_output.return_value = out1 + out2 + out3 expected = [ {"repo": 'repo/image1', "tag": '49', "id": '1b69f8ee3b87'}, {"repo": 'repo/image2', "tag": '17', "id": 'bcf6d6f25607'}, ] # Test actual = self.sut.list_images() # Assert self.sub_proc_mock.check_output.assert_called_once_with( ['docker', 'images']) self.assertEqual(expected, actual) def test_remove_images(self): # Fixture images = ['i1', 'i2'] # Test self.sut.remove_images(images) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'rmi', 'i1', 'i2']) def test_tag(self): # Fixture existing_image = "existing" new_image = "new" # Test self.sut.tag(existing_image, new_image) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'tag', existing_image, new_image]) def test_pull(self): # Fixture image = "someImage" # Test self.sut.pull(image) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'pull', image]) def test_inspect(self): # Fixture container = "my-container" expected = [{"Id": "123123", "Created": "2016-02-21T12:..."}] data = json.dumps(expected) self.sub_proc_mock.check_output.return_value = data # Test actual = self.sut.inspect(container) # Assert self.assertEqual(expected, actual) self.sub_proc_mock.check_output.assert_called_once_with( ["docker", "inspect", container]) def test_list_volumes(self): # Fixture self.sub_proc_mock.check_output.return_value = "123\n456\n789" expected = [ "123", "456", "789", ] # Test actual = self.sut.list_volumes() # Assert self.sub_proc_mock.check_output.assert_called_once_with( ['docker', 'volume', 'ls', '-q']) self.assertEqual(expected, actual) def test_list_volumes_with_filter(self): # Fixture self.sub_proc_mock.check_output.return_value = "123\n456\n789" expected = [ "123", "456", "789", ] # Test actual = self.sut.list_volumes(filter='a_filter') # Assert self.sub_proc_mock.check_output.assert_called_once_with( ['docker', 'volume', 'ls', '-q', '-f', 'a_filter']) self.assertEqual(expected, actual) def test_remove_volumes(self): # Fixture ids = [ "123", "456", "789", ] # Test self.sut.remove_volumes(ids) # Assert self.sub_proc_mock.check_call.assert_called_once_with( ['docker', 'volume', 'rm', '123', '456', '789'])
def __init__(self, docker=Docker()): self._docker = docker
def __init__(self, docker=Docker()): self._docker = docker self._IMAGE = "bitcraze/web-builder"