def get_data_values(self): # Data values list_value = ListValue() list_value.add_all([IntegerValue(val) for val in range(1,15)]) data_values = [ VoidValue(), IntegerValue(3), DoubleValue(decimal.Decimal('7.2')), StringValue('blah'), SecretValue(), BooleanValue(True), BlobValue(b"a blob"), BlobValue(b"0\x82\x04\x00"), list_value, OptionalValue(), OptionalValue(IntegerValue(7)), StructValue('name'), self.get_struct_value(), self.get_error_value(), ] struct_value = StructValue('struct') for idx, data_val in enumerate(data_values): struct_value.set_field(str(idx), data_val) data_values.append(struct_value) return data_values
def test_publish(self): info = self.task_handle.get_info() progress = Progress() progress.total = 100 progress.completed = 42 progress.message = self.progress_msg desc2 = LocalizableMessage(id='msg2', default_message='task1', args=[]) info.description = desc2 info.cancelable = False info.status = 'RUNNING' info.start_time = datetime.utcnow() info.progress = progress self.task_handle.publish() result = self.task_manager.get_info(self.task_id) self.assertEqual(result.get_field('status'), StringValue('RUNNING')) progress_val = StructValue('com.vmware.cis.task.progress') progress_val.set_field('total', IntegerValue(100)) progress_val.set_field('completed', IntegerValue(42)) progress_val.set_field( 'message', TypeConverter.convert_to_vapi( self.progress_msg, LocalizableMessage.get_binding_type())) self.assertEqual(result.get_field('progress'), OptionalValue(progress_val)) self.assertEqual(result.get_field('cancelable'), BooleanValue(False))
def test_integer(self): input_val = '10' expected_val = IntegerValue(10) actual_val = DataValueConverter.convert_to_data_value(input_val) self.assertEqual(expected_val, actual_val) input_val = '1000000000000000000000000000000' long_val = 1000000000000000000000000000000 expected_val = IntegerValue(long_val) actual_val = DataValueConverter.convert_to_data_value(input_val) self.assertEqual(expected_val, actual_val)
def invoke(self, service_id, operation_id, input_value, ctx): # Allowed service_id: 'svc' if service_id != 'svc': return MethodResult(error=error_values[2]) # Allowed operation_ids: 'op1', 'op2' if operation_id == 'op1': return MethodResult(output=IntegerValue(10)) elif operation_id == 'op2': if ctx.security_context: return MethodResult(output=IntegerValue(20)) else: return MethodResult(error=error_values[3]) else: return MethodResult(error=error_values[2])
def test_op(self): ctx = ExecutionContext(ApplicationContext(), None) input_val = VoidValue() method_result = self.sec_ctx_filter.invoke('svc', 'op1', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def test_integer_value(self): input_val = IntegerValue(10) actual_val = DataValueConverter.convert_to_json(input_val) expected_val = '10' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def test_noauth_op_unknown_op(self): sec_ctx = None app_ctx = ApplicationContext() ctx = ExecutionContext(app_ctx, sec_ctx) input_val = VoidValue() method_result = self.authz_filter.invoke('com', 'op', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def test_dict(self): input_val = '{"int_field":10, "bool_field":true}' values = { 'bool_field': BooleanValue(True), 'int_field': IntegerValue(10) } expected_val = StructValue(values=values) actual_val = DataValueConverter.convert_to_data_value(input_val) self.assertEqual(expected_val, actual_val)
def test_invoke_error(self): ctx = ExecutionContext() input_ = StructValue(method_name) input_.set_field('message', IntegerValue(10)) input_.set_field('throw', BooleanValue(True)) actual_method_result = self.provider.invoke(interface_name, method_name, input_, ctx) self.assertFalse(actual_method_result.success())
def test_success_2(self): ctx = ExecutionContext() service_id = interface_id.get_name() operation_id = mock_definition input_value = StructValue(OPERATION_INPUT) input_value.set_field(param_name, IntegerValue(10)) method_result = self.local_provider.invoke(service_id, operation_id, input_value, ctx) self.assertTrue(method_result.success()) self.assertEqual(method_result.output, VoidValue())
def _visit_int(self, obj): # pylint: disable=R0201 """ Visit python int object :type obj: :class:`int` or :class:`long` for Python 2 and :class:`int` for Python 3 :param obj: Python integer object :rtype: :class:`vmware.vapi.data.value.IntegerValue` :return: Integer value """ return IntegerValue(obj)
def test_success(self): ctx = ExecutionContext() service_id = interface_id.get_name() operation_id = mock_definition method_def = self.introspection.get_method(service_id, operation_id) input_value = method_def.get_input_definition().new_value() input_value.set_field(param_name, IntegerValue(10)) method_result = self.local_provider.invoke(service_id, operation_id, input_value, ctx) self.assertTrue(method_result.success()) self.assertEqual(method_result.output, VoidValue())
def test_authn_op_with_sec_ctx(self): sec_ctx = SecurityContext({ SCHEME_ID: USER_PASSWORD_SCHEME_ID, USER_KEY: 'testuser', PASSWORD_KEY: 'password' }) ctx = ExecutionContext(None, sec_ctx) input_val = VoidValue() method_result = self.sec_ctx_filter.invoke('svc', 'op2', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(20)) self.assertEqual(method_result.error, None)
def test_invalid_user_pwd_scheme(self): sec_ctx = SecurityContext({ SCHEME_ID: OAUTH_SCHEME_ID, ACCESS_TOKEN: 'token' }) app_ctx = ApplicationContext() ctx = ExecutionContext(app_ctx, sec_ctx) input_val = VoidValue() method_result = self.authn_filter.invoke('com.pkg.svc', 'op', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def test_user_pwd_scheme(self): sec_ctx = SecurityContext({ SCHEME_ID: USER_PASSWORD_SCHEME_ID, USER_KEY: 'testuser', PASSWORD_KEY: 'password' }) app_ctx = ApplicationContext() ctx = ExecutionContext(app_ctx, sec_ctx) input_val = VoidValue() method_result = self.authn_filter.invoke('com.pkg.svc', 'op', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def test_op_with_sec_ctx_on_filter(self): sec_ctx = SecurityContext({ SCHEME_ID: USER_PASSWORD_SCHEME_ID, USER_KEY: 'testuser', PASSWORD_KEY: 'password' }) self.sec_ctx_filter.set_security_context(sec_ctx) ctx = ExecutionContext(ApplicationContext(), None) input_val = VoidValue() method_result = self.sec_ctx_filter.invoke('svc', 'op1', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def test_noauth_op_with_valid_user(self): sec_ctx = SecurityContext({ SCHEME_ID: USER_PASSWORD_SCHEME_ID, USER_KEY: 'testuser', PASSWORD_KEY: 'password', AUTHN_IDENTITY: UserIdentity('testuser') }) app_ctx = ApplicationContext() ctx = ExecutionContext(app_ctx, sec_ctx) input_val = VoidValue() method_result = self.authz_filter.invoke('com.pkg.svc', 'op1', input_val, ctx) self.assertEqual(method_result.output, IntegerValue(10)) self.assertEqual(method_result.error, None)
def test_invalid_input(self): ctx = ExecutionContext() expected_msg = message_factory.get_message('vapi.method.input.invalid') expected_error_value = make_error_value_from_msgs( invalid_argument_def, expected_msg) method_result = self.local_provider.invoke( service_id=raise_python_exception_id.get_interface_identifier( ).get_name(), operation_id=raise_python_exception_id.get_name(), input_value=IntegerValue(), ctx=ctx) self.assertEquals(None, method_result.output) self.assertEquals(expected_error_value, method_result.error)
def test_url_path_with_integer_path_variable(self): input_val = StructValue(name='operation-input', values={ 'string_val': StringValue('string1'), 'int_val_url': IntegerValue(100), }) rest_metadata = OperationRestMetadata( http_method='GET', url_template='/foo/{intValUrl}', path_variables={'int_val_url': 'intValUrl'}) url_path, actual_headers, actual_body, _ = RestSerializer.serialize_request( input_value=input_val, ctx=None, rest_metadata=rest_metadata, is_vapi_rest=True) expected_url_path = '/foo/100' expected_headers = {} expected_body = '{"string_val":"string1"}' self.assertEqual(expected_url_path, url_path) self.assertEqual(expected_headers, actual_headers) self.assertEqual(expected_body, actual_body)
def _test_method_failure(self, operation_id, error, message_id=None, *args): ctx = ExecutionContext() method_id = method_id_dict[operation_id] service_id = method_id.get_interface_identifier().get_name() if isinstance(error, ErrorValue): expected_error_value = error else: msg = message_factory.get_message(message_id, *args) expected_error_value = make_error_value_from_msgs(error, msg) provider = self.local_provider method_def = self.introspection.get_method(service_id, operation_id) input_value = method_def.get_input_definition().new_value() input_value.set_field(param_name, IntegerValue(0)) method_result = provider.invoke(service_id, operation_id, input_value, ctx) self.assertEquals(None, method_result.output) self.assertEquals(expected_error_value, method_result.error)
def get_struct_value(self): struct_value = StructValue('struct0') struct_value.set_field('int', IntegerValue(7)) struct_value.set_field('optional', OptionalValue(StringValue("123"))) struct_value.set_field('optional1', OptionalValue()) return struct_value
def get_error_value(self): error_value = ErrorValue('struct0') error_value.set_field('int', IntegerValue(7)) error_value.set_field('optional', OptionalValue(StringValue("123"))) error_value.set_field('optional1', OptionalValue()) return error_value
def invoke(self, ctx, method_id, input_value): return MethodResult(output=IntegerValue(10))
def invoke(self, service_id, operation_id, input_value, ctx): return MethodResult(output=IntegerValue(10))