def test_config_directory_is_replaced_with_file(self): os.mkdir(self.config_path) shutil.copy(fixture_path('configs', 'namesync-v2.conf'), os.path.join(self.config_path, 'namesync.conf')) self.namesync(fixture_path('example.com')) self.assertTrue(os.path.exists(self.config_path)) self.assertFalse(os.path.isdir(self.config_path))
def test_get_does_not_overwrite_an_existing_file(self): path = os.path.join(self.scratch_dir, 'example.com') shutil.copy(fixture_path('example.com.updated'), path) with self.assertRaises(SystemExit) as cm: self.namesync('--get', path) self.assertEqual(cm.exception.code, 1) self.assertTrue(filecmp.cmp(path, fixture_path('example.com.updated'))) # Only one API call made to retrieve the records self.assertEqual(len(self.provider.mock_calls), 1)
def test_v1_config_is_migrated(self): self.use_config('namesync-v1.conf') self.namesync(fixture_path('example.com')) self.assertEqual( json.loads(fixture_content(self.config_path)), json.loads(fixture_content('configs/namesync-v2.conf')) )
def test_env_config_dirs(monkeypatch, dirs, val): config_dirs = map(lambda d: fixture_path(__file__, d), dirs) monkeypatch.setenv('PROJECT_CONFIG_DIRS', ','.join(config_dirs)) config = Config.read() assert config['key'] == val
def test_lock_file(): config = Config.read() config['key'] = 2 config.lock() res = check_output([fixture_path(__file__, 'program.py')]) config.unlock() assert res.decode().strip() == '2'
class Config(CleanConfig): name = 'project' config_dirs = [ fixture_path(__file__), ] schema = Schema({ Required('key'): int, })
def test_updating_zone_should_output_changes_and_call_api(self): self.namesync('--zone', 'example.com', '--yes', fixture_path('example.com.updated')) self.outfile.seek(0) self.assertEqual(self.outfile.read(), '''\ ADD CNAME www example.com UPDATE A test 10.10.10.12 REMOVE A * 10.10.10.10 ''') self.assertEqual(len(self.provider.records.mock_calls), 1) self.assertEqual(len(self.provider.add.mock_calls), 1) self.assertEqual(len(self.provider.update.mock_calls), 1) self.assertEqual(len(self.provider.delete.mock_calls), 1)
def test_updating_zone_should_output_changes_and_call_api(self): self.namesync('--zone', 'example.com', '--yes', fixture_path('example.com.updated')) self.outfile.seek(0) self.assertEqual( self.outfile.read(), '''\ ADD CNAME www example.com UPDATE A test 10.10.10.12 REMOVE A * 10.10.10.10 ''') self.assertEqual(len(self.provider.records.mock_calls), 1) self.assertEqual(len(self.provider.add.mock_calls), 1) self.assertEqual(len(self.provider.update.mock_calls), 1) self.assertEqual(len(self.provider.delete.mock_calls), 1)
def test_updating_zone_is_interactive(self, mock_input): mock_input.side_effect = self.make_mock_input('y') self.namesync('--zone', 'example.com', fixture_path('example.com.updated')) self.outfile.seek(0) self.assertEqual(self.outfile.read(), '''\ The following changes will be made: ADD CNAME www example.com UPDATE A test 10.10.10.12 REMOVE A * 10.10.10.10 Do you want to continue? [y/N] y ADD CNAME www example.com UPDATE A test 10.10.10.12 REMOVE A * 10.10.10.10 ''') # The same API calls as test_updating_zone_should_output_changes_and_call_api() self.assertEqual(len(self.provider.mock_calls), 4)
def setUp(self): self.outfile = StringIO() self.scratch_dir = tempfile.mkdtemp() def remove_working_dir(): shutil.rmtree(self.scratch_dir) self.addCleanup(remove_working_dir) patcher = mock.patch('namesync.providers.dummy.DummyProvider') self.DummyProvider = patcher.start() self.addCleanup(patcher.stop) self.provider = self.DummyProvider.return_value with open(fixture_path('example.com')) as f: self.provider.records.return_value = flatfile_to_records(f) self.DummyProvider.needs_config.return_value = {}
def test_updating_zone_can_be_aborted(self, mock_input): mock_input.side_effect = self.make_mock_input('n') with self.assertRaises(SystemExit) as cm: self.namesync('--zone', 'example.com', fixture_path('example.com.updated')) self.outfile.seek(0) self.assertEqual(self.outfile.read(), '''\ The following changes will be made: ADD CNAME www example.com UPDATE A test 10.10.10.12 REMOVE A * 10.10.10.10 Do you want to continue? [y/N] n Abort. ''') # There are no API calls other than the initial call to retrieve the records self.assertEqual(len(self.provider.mock_calls), 1) self.assertEqual(cm.exception.code, 1)
def test_updating_zone_is_interactive(self, mock_input): mock_input.side_effect = self.make_mock_input('y') self.namesync('--zone', 'example.com', fixture_path('example.com.updated')) self.outfile.seek(0) self.assertEqual( self.outfile.read(), '''\ The following changes will be made: ADD CNAME www example.com UPDATE A test 10.10.10.12 REMOVE A * 10.10.10.10 Do you want to continue? [y/N] y ADD CNAME www example.com UPDATE A test 10.10.10.12 REMOVE A * 10.10.10.10 ''') # The same API calls as test_updating_zone_should_output_changes_and_call_api() self.assertEqual(len(self.provider.mock_calls), 4)
def test_updating_zone_can_be_aborted(self, mock_input): mock_input.side_effect = self.make_mock_input('n') with self.assertRaises(SystemExit) as cm: self.namesync('--zone', 'example.com', fixture_path('example.com.updated')) self.outfile.seek(0) self.assertEqual( self.outfile.read(), '''\ The following changes will be made: ADD CNAME www example.com UPDATE A test 10.10.10.12 REMOVE A * 10.10.10.10 Do you want to continue? [y/N] n Abort. ''') # There are no API calls other than the initial call to retrieve the records self.assertEqual(len(self.provider.mock_calls), 1) self.assertEqual(cm.exception.code, 1)
def test_nothing_should_happen_when_flatfile_and_api_are_in_sync(self): self.namesync(fixture_path('example.com')) self.outfile.seek(0) self.assertEqual(self.outfile.read(), 'All records up to date.\n') self.assertEqual(len(self.provider.records.mock_calls), 1)
def use_config(self, name): shutil.copy(fixture_path('configs', name), self.config_path)
def test_v1_config_is_migrated(self): self.use_config('namesync-v1.conf') self.namesync(fixture_path('example.com')) self.assertEqual( json.loads(fixture_content(self.config_path)), json.loads(fixture_content('configs/namesync-v2.conf')))