def test_parser(mocker, octane_app, path, is_file, command, archivators, password, password_required): restore_mock = mocker.patch('octane.commands.restore.restore_data') mocker.patch("os.path.isfile", return_value=is_file) params = [command] if path: params += ["--from", path] if password and password_required: params += ["--admin-password", password] try: octane_app.run(params) except AssertionError: # parse error, app returns 2 assert not restore_mock.called assert not password or path is None except ValueError: # Invalid path to backup file assert not restore_mock.called assert not is_file else: context = None if password and password_required: context = backup_restore.NailgunCredentialsContext( user="******", password=password) restore_mock.assert_called_once_with(path, archivators, context) assert path is not None assert is_file
def test_post_restore_nailgun(mocker, mock_open, dump, calls, data_for_update): mock_links = mocker.patch.object( postgres.NailgunArchivator, "_create_links_on_remote_logs") data = yaml.dump(dump) mock_subprocess_call = mocker.patch("octane.util.subprocess.call") run_in_container_mock = mocker.patch( "octane.util.docker.run_in_container", return_value=(data, None)) run_sql_mock = mocker.patch.object( postgres.NailgunArchivator, "_run_sql_in_container", return_value=[data_for_update] ) json_mock = mocker.patch("json.dumps") token = "123" def mock_init(self, *args, **kwargs): self.auth_token = token mocker.patch.object(keystoneclient, "__init__", mock_init) post_data = mocker.patch("requests.post") mocker.patch("os.environ", new_callable=mock.PropertyMock(return_value={})) postgres.NailgunArchivator( None, backup_restore.NailgunCredentialsContext( user="******", password="******") )._post_restore_action() headers = { "X-Auth-Token": token, "Content-Type": "application/json" } post_url = 'http://127.0.0.1:8000/api/v1/releases/' post_call = mock.call(post_url, json_mock.return_value, headers=headers) for call in post_data.call_args_list: assert post_call == call json_mock.assert_has_calls([mock.call(d) for d in calls], any_order=True) assert json_mock.call_count == 3 mock_subprocess_call.assert_called_once_with([ "fuel", "release", "--sync-deployment-tasks", "--dir", "/etc/puppet/"], env={'KEYSTONE_PASS': '******', 'KEYSTONE_USER': '******'} ) run_in_container_mock.assert_called_once_with( "nailgun", ["cat", magic_consts.OPENSTACK_FIXTURES], stdout=subprocess.PIPE ) json_mock.assert_called_with({"deployed_before": {"value": True}}) mock_links.assert_called_once_with() run_sql_mock.assert_has_calls([ mock.call("select id, generated from attributes;"), ])
def test_post_restore_nailgun(mocker, mock_open, dump, calls, data_for_update): data = yaml.dump(dump) mock_subprocess_call = mocker.patch("octane.util.subprocess.call") run_in_container_mock = mocker.patch("octane.util.docker.run_in_container", side_effect=[ (data, None), (data_for_update, None), ("updated", None), ]) json_mock = mocker.patch("json.dumps") token = "123" def mock_init(self, *args, **kwargs): self.auth_token = token mocker.patch.object(keystoneclient, "__init__", mock_init) post_data = mocker.patch("requests.post") postgres.NailgunArchivator( None, backup_restore.NailgunCredentialsContext( user="******", password="******"))._post_restore_action() headers = {"X-Auth-Token": token, "Content-Type": "application/json"} post_url = 'http://127.0.0.1:8000/api/v1/releases/' post_call = mock.call(post_url, json_mock.return_value, headers=headers) for call in post_data.call_args_list: assert post_call == call json_mock.assert_has_calls([mock.call(d) for d in calls], any_order=True) assert json_mock.call_count == 3 mock_subprocess_call.assert_called_once_with([ "fuel", "release", "--sync-deployment-tasks", "--dir", "/etc/puppet/", "--user", "admin", "--password", "password", ]) run_in_container_mock.assert_called_with("postgres", [ "sudo", "-u", "postgres", "psql", "nailgun", "--tuples-only", "-c", "update attributes as a set generated = b.generated " "from (values (1, '{0}')) " "as b(id, generated) where a.id = b.id;".format(json_mock.return_value) ], stdout=subprocess.PIPE) json_mock.assert_called_with({"deployed_before": {"value": True}})
def test_path_restore(mocker, cls, path, members): subprocess_mock = mocker.patch("octane.util.subprocess.call") members = [TestMember(n, f, e) for n, f, e in members] archive = TestArchive(members, cls) mocker.patch("os.environ", new_callable=mock.PropertyMock(return_value={})) cls( archive, backup_restore.NailgunCredentialsContext('user', 'password') ).restore() for member in members: member.assert_extract(path) if cls is ssh.SshArchivator: subprocess_mock.assert_called_once_with( ["fuel-bootstrap", "build", "--activate"], env={'KEYSTONE_PASS': '******', 'KEYSTONE_USER': '******'}) else: assert not subprocess_mock.called
def test_post_restore_puppet_apply_host(mocker, mock_open, exc_on_apply): class TestException(Exception): pass mkstemp_mock = mocker.patch("tempfile.mkstemp", return_value=(1, "/etc/fuel/.astute.yaml.bac")) mock_copy = mocker.patch("shutil.copy") mock_move = mocker.patch("shutil.move") yaml_load = mocker.patch( "yaml.load", return_value={"FUEL_ACCESS": { "password": "******" }}) yaml_dump = mocker.patch("yaml.safe_dump") context = backup_restore.NailgunCredentialsContext(user="******", password="******") archivator = puppet.PuppetApplyHost(None, context) if exc_on_apply: mock_apply = mocker.patch("octane.util.puppet.apply_host", side_effect=TestException("test exception")) pytest.raises(TestException, archivator.restore) else: mock_apply = mocker.patch("octane.util.puppet.apply_host") archivator.restore() assert mock_apply.called assert mock_open.call_args_list == [ mock.call("/etc/fuel/astute.yaml"), mock.call("/etc/fuel/astute.yaml", "w"), ] yaml_load.assert_called_once_with(mock_open.return_value) yaml_dump.asswer_called_once_with( {'FUEL_ACCESS': { 'password': '******' }}, mock_open.return_value, default_flow_style=False) mock_copy.assert_called_once_with("/etc/fuel/astute.yaml", "/etc/fuel/.astute.yaml.bac") mock_move.assert_called_once_with("/etc/fuel/.astute.yaml.bac", "/etc/fuel/astute.yaml") mkstemp_mock.assert_called_once_with(dir="/etc/fuel", prefix=".astute.yaml.octane")
def get_context(self, parsed_args): return backup_restore.NailgunCredentialsContext( password=parsed_args.admin_password, user="******")
def test_create_links_on_remote_logs( mocker, mock_open, nodes, is_dir, exception): domain_name = "test_domain" mocker.patch("yaml.load", return_value={"DNS_DOMAIN": domain_name}) domain_names = [] fuel_client_values = [] is_link_exists = [] moved_nodes = [] for idx, node_link_exits in enumerate(nodes): node, link_exists = node_link_exits node_domain_name = "{0}.{1}".format(node, domain_name) domain_names.append(node_domain_name) ip_addr = "10.21.10.{0}".format(idx + 1) fuel_client_mock = mocker.Mock() fuel_client_mock.data = { "meta": { "system": { "fqdn": node_domain_name } }, "ip": ip_addr, } fuel_client_values.append(fuel_client_mock) is_link_exists.append(link_exists) if not link_exists: moved_nodes.append((node_domain_name, ip_addr)) is_link_mock = mocker.patch("os.path.islink", side_effect=is_link_exists) mocker.patch("os.path.isdir", return_value=is_dir) mocker.patch("fuelclient.objects.node.Node.get_all", return_value=fuel_client_values) run_in_container_mock = mocker.patch( "octane.util.docker.run_in_container") rename_mock = mocker.patch("os.rename") symlink_mock = mocker.patch("os.symlink") mkdir_mock = mocker.patch("os.mkdir") context = backup_restore.NailgunCredentialsContext( user="******", password="******") archivator = backup_restore.postgres.NailgunArchivator(None, context) if not exception: class TestException(Exception): pass is_link_mock.side_effect = TestException("test exc") with pytest.raises(TestException): archivator._create_links_on_remote_logs() assert not mkdir_mock.called assert not rename_mock.called else: archivator._create_links_on_remote_logs() path = "/var/log/docker-logs/remote/" path_pairs = [(os.path.join(path, d), os.path.join(path, i)) for d, i in moved_nodes] sym_calls = [mock.call(d, os.path.join(path, i)) for d, i in moved_nodes] if is_dir: assert [mock.call(i, d) for d, i in path_pairs] == \ rename_mock.call_args_list assert not mkdir_mock.called else: assert [mock.call(d) for d, _ in path_pairs] == \ mkdir_mock.call_args_list assert not rename_mock.called assert sym_calls == symlink_mock.call_args_list assert [mock.call("rsyslog", ["service", "rsyslog", "stop"]), mock.call("rsyslog", ["service", "rsyslog", "start"])] == \ run_in_container_mock.call_args_list