def test_two_different_api_calls_have_different_numbers(self): event = { 'event_type': 'API_CALL', 'id': 'my-id', 'request_id': 'some-id', 'payload': { 'service': 'ec2', 'operation': 'DescribeRegions', 'params': {} }, 'timestamp': 86400000, } self.formatter.display(event) collected_output = ensure_text_type(self.output.getvalue()) self.assertIn('[0] API call made', collected_output) other_event = { 'event_type': 'API_CALL', 'id': 'my-id', 'request_id': 'other-id', 'payload': { 'service': 'ec2', 'operation': 'DescribeRegions', 'params': {} }, 'timestamp': 86400000, } self.formatter.display(other_event) new_output = ensure_text_type(self.output.getvalue())[ len(collected_output):] self.assertIn('[1] API call made', new_output)
def test_show_nothing_when_no_history(self): out, err, rc = self.run_cmd('history list', expected_rc=255) error_message = ( 'No commands were found in your history. Make sure you have ' 'enabled history mode by adding "cli_history = enabled" ' 'to the config file.') self.assertEqual('', ensure_text_type(out)) self.assertEqual('\n%s\n' % error_message, ensure_text_type(err))
def test_show_nothing_when_no_history(self): out, err, rc = self.run_cmd('history list', expected_rc=255) error_message = ( 'No commands were found in your history. Make sure you have ' 'enabled history mode by adding "cli_history = enabled" ' 'to the config file.' ) self.assertEqual('', ensure_text_type(out)) self.assertEqual('\n%s\n' % error_message, ensure_text_type(err))
def get_completions_given_user_input(self, user_input): user_input = mock.Mock(text=ensure_text_type(user_input)) with cd(self.temporary_directory): completions = list(self.completer.get_completions( user_input, None)) return completions
def test_can_emit_single_row(self): self._format_records([{ 'id_a': 'foo', 'timestamp': self.timestamp, 'args': '["s3", "ls"]', 'rc': 0 }]) expected_output = 'foo %s s3 ls 0\n' % self.formatted_time actual_output = ensure_text_type(self.output_stream.getvalue()) self.assertEqual(expected_output, actual_output)
def _get_ongoing_dict_key(self, result): if not isinstance(result, BaseResult): raise ValueError( 'Any result using _get_ongoing_dict_key must subclass from ' 'BaseResult. Provided result is of type: %s' % type(result)) key_parts = [] for result_property in [result.transfer_type, result.src, result.dest]: if result_property is not None: key_parts.append(ensure_text_type(result_property)) return u':'.join(key_parts)
def test_unicode_yaml_scalar(self): self.parsed_response['Users'][1]['UserId'] = u'\u2713' stdout, _, _ = self.run_cmd( [ 'iam', 'list-users', '--output', 'yaml', '--query', 'Users[1].UserId' ], expected_rc=0 ) self.assertIn(u'\u2713', ensure_text_type(stdout))
def _get_ongoing_dict_key(self, result): if not isinstance(result, BaseResult): raise ValueError( 'Any result using _get_ongoing_dict_key must subclass from ' 'BaseResult. Provided result is of type: %s' % type(result) ) key_parts = [] for result_property in [result.transfer_type, result.src, result.dest]: if result_property is not None: key_parts.append(ensure_text_type(result_property)) return u':'.join(key_parts)
def test_display_unknown_type(self): event = { 'event_type': 'UNKNOWN', 'id': 'my-id', 'payload': 'foo', 'timestamp': 86400000, 'request_id': None } self.formatter.display(event) collected_output = ensure_text_type(self.output.getvalue()) self.assertEqual('', collected_output)
def test_can_emit_single_row(self): self._format_records([ { 'id_a': 'foo', 'timestamp': self.timestamp, 'args': '["s3", "ls"]', 'rc': 0 } ]) expected_output = 'foo %s s3 ls 0\n' % self.formatted_time actual_output = ensure_text_type(self.output_stream.getvalue()) self.assertEqual(expected_output, actual_output)
def test_can_truncate_args(self): # Truncate the argument if it won't fit in the space alotted to the # arguments field. self._format_records([ { 'id_a': 'foo', 'timestamp': self.timestamp, 'args': ('["s3", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]'), 'rc': 0 } ]) expected_output = 'foo %s s3 aaa... 0\n' % self.formatted_time actual_output = ensure_text_type(self.output_stream.getvalue()) self.assertEqual(expected_output, actual_output)
def test_can_emit_multiple_rows(self): self._format_records([{ 'id_a': 'foo', 'timestamp': self.timestamp, 'args': '["s3", "ls"]', 'rc': 0 }, { 'id_a': 'bar', 'timestamp': self.timestamp, 'args': '["s3", "cp"]', 'rc': 1 }]) expected_output = ('foo %s s3 ls 0\n' 'bar %s s3 cp 1\n') % ( self.formatted_time, self.formatted_time) actual_output = ensure_text_type(self.output_stream.getvalue()) self.assertEqual(expected_output, actual_output)
def test_can_emit_multiple_rows(self): self._format_records([ { 'id_a': 'foo', 'timestamp': self.timestamp, 'args': '["s3", "ls"]', 'rc': 0 }, { 'id_a': 'bar', 'timestamp': self.timestamp, 'args': '["s3", "cp"]', 'rc': 1 } ]) expected_output = ('foo %s s3 ls 0\n' 'bar %s s3 cp 1\n') % ( self.formatted_time, self.formatted_time) actual_output = ensure_text_type(self.output_stream.getvalue()) self.assertEqual(expected_output, actual_output)
def validate_arguments(self, args): """ Validates command line arguments using the retrieved information. """ if args.hostname: instances = self.opsworks.describe_instances( StackId=self._stack['StackId'])['Instances'] if any(args.hostname.lower() == instance['Hostname'] for instance in instances): raise ValueError( "Invalid hostname: '%s'. Hostnames must be unique within " "a stack." % args.hostname) if args.infrastructure_class == 'ec2' and args.local: # make sure the regions match region = json.loads(ensure_text_type( urlopen(IDENTITY_URL).read()))['region'] if region != self._stack['Region']: raise ValueError( "The stack's and the instance's region must match.")
def validate_arguments(self, args): """ Validates command line arguments using the retrieved information. """ if args.hostname: instances = self.opsworks.describe_instances( StackId=self._stack['StackId'] )['Instances'] if any(args.hostname.lower() == instance['Hostname'] for instance in instances): raise ValueError( "Invalid hostname: '%s'. Hostnames must be unique within " "a stack." % args.hostname) if args.infrastructure_class == 'ec2' and args.local: # make sure the regions match region = json.loads( ensure_text_type(urlopen(IDENTITY_URL).read()))['region'] if region != self._stack['Region']: raise ValueError( "The stack's and the instance's region must match.")
def test_unicode(self): value = u'baz' response = ensure_text_type(value) self.assertIsInstance(response, six.text_type) self.assertEqual(response, 'baz')
def test_non_ascii(self): value = b'\xe2\x9c\x93' response = ensure_text_type(value) self.assertIsInstance(response, six.text_type) self.assertEqual(response, u'\u2713')
def test_non_string_or_bytes_raises_error(self): value = 500 with self.assertRaises(ValueError): ensure_text_type(value)
def test_string(self): value = 'foo' response = ensure_text_type(value) self.assertIsInstance(response, six.text_type) self.assertEqual(response, 'foo')
def test_binary(self): value = b'bar' response = ensure_text_type(value) self.assertIsInstance(response, six.text_type) self.assertEqual(response, 'bar')
def test_yaml_prints_unicode_chars(self): self.parsed_response['Users'][1]['UserId'] = u'\u2713' stdout, _, _ = self.run_cmd( 'iam list-users --output yaml', expected_rc=0 ) self.assertIn(u'\u2713', ensure_text_type(stdout))
def assert_output(self, for_event, contains): self.formatter.display(for_event) collected_output = ensure_text_type(self.output.getvalue()) for line in contains: self.assertIn(line, collected_output)