def test_dump_accounts_existing_file(self): self.useFixture(fixtures.MockPatch('os.path.exists', return_value=True)) rename_mock = self.useFixture(fixtures.MockPatch('os.rename')).mock backup_file = '.'.join((self.opts.accounts, 'bak')) mocked_open = mock.mock_open() with mock.patch('{}.open'.format(account_generator.__name__), mocked_open, create=True): with mock.patch('yaml.safe_dump') as yaml_dump_mock: account_generator.setup_logging() account_generator.dump_accounts(self.resources, self.opts.identity_version, self.opts.accounts) rename_mock.assert_called_once_with(self.opts.accounts, backup_file) mocked_open.assert_called_once_with(self.opts.accounts, 'w') handle = mocked_open() # Ordered args in [0], keyword args in [1] accounts, f = yaml_dump_mock.call_args[0] self.assertEqual(handle, f) self.assertEqual(6, len(accounts)) if self.domain_is_in: self.assertIn('domain_name', accounts[0].keys()) else: self.assertNotIn('domain_name', accounts[0].keys()) self.assertEqual(1, len([x for x in accounts if x.get('types') == ['admin']])) self.assertEqual(3, len([x for x in accounts if 'roles' in x])) for account in accounts: self.assertIn('resources', account) self.assertIn('network', account.get('resources'))
def test_dump_accounts_existing_file(self): self.useFixture(fixtures.MockPatch('os.path.exists', return_value=True)) rename_mock = self.useFixture(fixtures.MockPatch('os.rename')).mock backup_file = '.'.join((self.opts.accounts, 'bak')) mocked_open = mock.mock_open() with mock.patch('{}.open'.format(account_generator.__name__), mocked_open, create=True): with mock.patch('yaml.safe_dump') as yaml_dump_mock: account_generator.setup_logging() account_generator.dump_accounts(self.resources, self.opts.identity_version, self.opts.accounts) rename_mock.assert_called_once_with(self.opts.accounts, backup_file) mocked_open.assert_called_once_with(self.opts.accounts, 'w') handle = mocked_open() # Ordered args in [0], keyword args in [1] accounts, f = yaml_dump_mock.call_args[0] self.assertEqual(handle, f) self.assertEqual(6, len(accounts)) if self.domain_is_in: self.assertIn('domain_name', accounts[0].keys()) else: self.assertNotIn('domain_name', accounts[0].keys()) self.assertEqual(1, len([x for x in accounts if x.get('types') == ['admin']])) self.assertEqual(3, len([x for x in accounts if 'roles' in x])) for account in accounts: self.assertIn('resources', account) self.assertIn('network', account.get('resources'))
def test_dump_accounts_existing_file(self): self.useFixture(fixtures.MockPatch("os.path.exists", return_value=True)) rename_mock = self.useFixture(fixtures.MockPatch("os.rename")).mock backup_file = ".".join((self.opts.accounts, "bak")) mocked_open = mock.mock_open() with mock.patch("{}.open".format(account_generator.__name__), mocked_open, create=True): with mock.patch("yaml.safe_dump") as yaml_dump_mock: account_generator.setup_logging() account_generator.dump_accounts(self.resources, self.opts.identity_version, self.opts.accounts) rename_mock.assert_called_once_with(self.opts.accounts, backup_file) mocked_open.assert_called_once_with(self.opts.accounts, "w") handle = mocked_open() # Ordered args in [0], keyword args in [1] accounts, f = yaml_dump_mock.call_args[0] self.assertEqual(handle, f) self.assertEqual(6, len(accounts)) if self.domain_is_in: self.assertIn("domain_name", accounts[0].keys()) else: self.assertNotIn("domain_name", accounts[0].keys()) self.assertEqual(1, len([x for x in accounts if x.get("types") == ["admin"]])) self.assertEqual(3, len([x for x in accounts if "roles" in x])) for account in accounts: self.assertIn("resources", account) self.assertIn("network", account.get("resources"))