def isolated_filesystem(self): """ A context manager that creates a temporary folder and changes the current working directory to it for isolated filesystem tests. """ # If user config is found, back it up for duration of each test. config_path = os.path.expanduser('~/.pynsotrc') backup_path = config_path + '.orig' backed_up = False if os.path.exists(config_path): log.debug('Config found, backing up...') os.rename(config_path, backup_path) backed_up = True cwd = os.getcwd() t = tempfile.mkdtemp() os.chdir(t) rcfile = dotfile.Dotfile('.pynsotrc') rcfile.write(self.client_config) try: yield t finally: os.chdir(cwd) if backed_up: log.debug('Restoring original config.') os.rename(backup_path, config_path) # Restore original try: shutil.rmtree(t) except (OSError, IOError): pass
def test_read_failure(self): """Test that that a missing file raises an error.""" self.filepath = tempfile.mktemp() # Doesn't create the temp file config = dotfile.Dotfile(self.filepath) self.assertRaises( IOError, # This means it's trying to read from stdin config.read, )
def test_validate_fields(self): """Test that fields check out.""" config = dotfile.Dotfile(self.filepath) self.config_data.pop('default_site', None) # We're going to test every field. fields = sorted(self.config_data) my_config = {} # Iterate through the sorted list of fields to make sure that each one # raises an error as expected. err = 'Missing required field: ' for field in fields: with self.assertRaisesRegexp(dotfile.DotfileError, err + field): config.read() config.validate_fields(my_config) my_config[field] = self.config_data[field] config.write(my_config)
def test_networks_bulk_add(config): """Test ``nsot networks add -s 1 -b /tmp/networks``""" headers = {'Content-Type': 'application/json'} auth_url = config['url'] + '/authenticate/' networks_url = config['url'] + '/sites/1/networks/' BULK_ADD = ( 'cidr:attributes\n' '10.0.0.0/8:owner=jathan\n' '10.0.0.0/24:owner=jathan\n' ) BULK_FAIL = ( 'cidr:attributes\n' '10.0.0.0/24:owner=jathan,bacon=delicious\n' '10.0.0.0/24:owner=jathan\n' ) BULK_ERROR = { 'status': 'error', 'error': { 'code': 400, 'message': 'Attribute name (bacon) does not exist.' } } runner = CliRunner(config) with runner.isolated_requests() as mock: mock.post(auth_url, json=AUTH_RESPONSE, headers=headers) mock.post(networks_url, json=NETWORKS_RESPONSE, headers=headers) # Write the bulk file. with open('bulk_file', 'w') as fh: fh.writelines(BULK_ADD) # Test *with* provided site_id result = runner.invoke( app, shlex.split('networks add -s 1 -b bulk_file') ) assert result.exit_code == 0 expected_output = ( "[SUCCESS] Added network!\n" "[SUCCESS] Added network!\n" ) assert result.output == expected_output # Test an invalid add mock.post(auth_url, json=AUTH_RESPONSE, headers=headers) mock.post(networks_url, json=BULK_FAIL, headers=headers, status_code=400) with open('bulk_fail', 'w') as fh: fh.writelines(BULK_FAIL) result = runner.invoke(app, shlex.split('networks add -s 1 -b bulk_fail')) assert result.exit_code != 0 # Test *without* provided site_id, but with default_site in a new # dotfile. rcfile = dotfile.Dotfile('.pynsotrc') config['default_site'] = '1' rcfile.write(config) mock.post(auth_url, json=AUTH_RESPONSE, headers=headers) mock.post(networks_url, json=NETWORKS_RESPONSE, headers=headers) result = runner.invoke(app, shlex.split('networks add -b bulk_file')) assert result.exit_code == 0 assert result.output == expected_output
def test_validate_fields_auth_header(self): """Test that auth_header fields check out.""" config = dotfile.Dotfile(self.filepath) self._validate_test_fields('auth_header', config)
def test_validate_perms_success(self): """Test that file permissions are ok.""" self.filepath = tempfile.mktemp() # Doesn't create a temp file config = dotfile.Dotfile(self.filepath) config.write({}) config.validate_perms()
def test_write(self): """Test that file can be written.""" config = dotfile.Dotfile(self.filepath) config.write(self.config_data['auth_token'])
def test_read_success(self): """Test that file can be read.""" config = dotfile.Dotfile(self.filepath) config.write(self.config_data['auth_token']) config.read()