Ejemplo n.º 1
0
    def testMergeEmptyString(self):
        """Test merging the empty or space only string."""
        message = protojson.decode_message(test_util.OptionalMessage, '')
        self.assertEquals(test_util.OptionalMessage(), message)

        message = protojson.decode_message(test_util.OptionalMessage, ' ')
        self.assertEquals(test_util.OptionalMessage(), message)
  def testListSummaries_filterReruns(self):
    test = self._createMockTest()
    test_run = self._createMockTestRunSummaries(test=test)[0]

    res = self.app.get('/_ah/api/mtt/v1/test_runs?prev_test_run_id=%s' %
                       test_run.key.id())
    self.assertEqual('200 OK', res.status)
    test_run_msg = protojson.decode_message(messages.TestRunSummaryList,
                                            res.body).test_runs
    self.assertEqual(test_run_msg, [])

    test_run_key = messages.ConvertToKey(ndb_models.TestRun, test_run.key.id())
    retry_run = self._createMockTestRunSummaries(
        test=test, prev_test_run_key=test_run_key)[0]
    retry_run_msg = messages.Convert(retry_run, messages.TestRunSummary)
    separate_retry = self._createMockTestRunSummaries(
        test=test, prev_test_run_key=test_run_key)[0]
    separate_retry_msg = messages.Convert(separate_retry,
                                          messages.TestRunSummary)

    res = self.app.get('/_ah/api/mtt/v1/test_runs?prev_test_run_id=%s' %
                       test_run.key.id())
    test_run_msg = protojson.decode_message(messages.TestRunSummaryList,
                                            res.body).test_runs
    self.assertEqual(test_run_msg[0].id, separate_retry_msg.id)
    self.assertEqual(test_run_msg[1].id, retry_run_msg.id)
  def testListSummaries(self):
    test = self._createMockTest()
    for i in range(1, 10):
      self._createMockTestRuns(test, device_specs=[str(i)], count=1)

    # fetch first page (5 results, run count 9 to 5, has more)
    res = self.app.get('/_ah/api/mtt/v1/test_runs?max_results=5')
    self.assertEqual('200 OK', res.status)
    first_page = protojson.decode_message(messages.TestRunSummaryList, res.body)
    self.assertLen(first_page.test_runs, 5)
    self.assertIsNone(first_page.prev_page_token)  # no previous runs
    self.assertIsNotNone(first_page.next_page_token)  # has following runs
    self.assertEqual(first_page.test_runs[0].device_specs, ['9'])
    self.assertEqual(first_page.test_runs[4].device_specs, ['5'])

    # fetch next page (4 results, run count 4 to 1, last page)
    res = self.app.get(
        '/_ah/api/mtt/v1/test_runs?max_results=5&page_token=%s' %
        first_page.next_page_token)
    self.assertEqual('200 OK', res.status)
    next_page = protojson.decode_message(messages.TestRunSummaryList, res.body)
    self.assertLen(next_page.test_runs, 4)
    self.assertIsNotNone(next_page.prev_page_token)  # has previous runs
    self.assertIsNone(next_page.next_page_token)  # no following runs
    self.assertEqual(next_page.test_runs[0].device_specs, ['4'])
    self.assertEqual(next_page.test_runs[3].device_specs, ['1'])

    # fetch previous page (same as first page)
    res = self.app.get(
        '/_ah/api/mtt/v1/test_runs?max_results=5&page_token=%s&backwards=true' %
        next_page.prev_page_token)
    self.assertEqual('200 OK', res.status)
    prev_page = protojson.decode_message(messages.TestRunSummaryList, res.body)
    self.assertEqual(first_page, prev_page)
Ejemplo n.º 4
0
  def testMergeEmptyString(self):
    """Test merging the empty or space only string."""
    message = protojson.decode_message(test_util.OptionalMessage, '')
    self.assertEquals(test_util.OptionalMessage(), message)

    message = protojson.decode_message(test_util.OptionalMessage, ' ')
    self.assertEquals(test_util.OptionalMessage(), message)
Ejemplo n.º 5
0
    def testList_filter(self, mock_get_remote_config_set_infos):
        imported_config = self._CreateImportedConfig()
        nonimported_config = self._CreateNonImportedConfig()
        updatable_config_old, updatable_config_new = self._CreateUpdatableConfig(
        )

        imported_message = self._CreateConfigSetInfoMessage(
            imported_config, ndb_models.ConfigSetStatus.IMPORTED)
        nonimported_message = self._CreateConfigSetInfoMessage(
            nonimported_config, ndb_models.ConfigSetStatus.NOT_IMPORTED)
        updatable_message_old = self._CreateConfigSetInfoMessage(
            updatable_config_old, ndb_models.ConfigSetStatus.UPDATABLE)
        updatable_message_new = self._CreateConfigSetInfoMessage(
            updatable_config_new, ndb_models.ConfigSetStatus.UPDATABLE)

        mock_get_remote_config_set_infos.return_value = [
            imported_message, nonimported_message, updatable_message_new
        ]

        args = 'include_remote=true&statuses=IMPORTED&statuses=UPDATABLE'
        res = self.app.get('/_ah/api/mtt/v1/config_sets?%s' % args)
        res_msg = protojson.decode_message(messages.ConfigSetInfoList,
                                           res.body)
        self.assertEqual(len(res_msg.config_set_infos), 2)
        self.assertEqual(imported_message, res_msg.config_set_infos[0])
        self.assertEqual(updatable_message_old, res_msg.config_set_infos[1])

        args = 'include_remote=true&statuses=NOT_IMPORTED'
        res = self.app.get('/_ah/api/mtt/v1/config_sets?%s' % args)
        res_msg = protojson.decode_message(messages.ConfigSetInfoList,
                                           res.body)
        self.assertEqual(len(res_msg.config_set_infos), 1)
        self.assertEqual(nonimported_message, res_msg.config_set_infos[0])
Ejemplo n.º 6
0
    def testUpdate(self, mock_schedule_test_plan_cron_job):
        """Tests test_plans.update API with invalid data."""
        test_plan = _CreateMockTestPlan('foo')
        test_plan_id = test_plan.key.id()
        data = {
            'name':
            'bar',
            'test_run_configs': [{
                'test_id': self.mock_test_id,
                'cluster': 'cluster',
                'run_target': 'run_target',
            }],
            'test_resource_pipes': [{
                'name': 'bar',
                'url': self.mock_test_resource_url,
            }],
            'before_device_action_ids': [
                self.mock_device_action_id,
            ]
        }

        res = self.app.put_json('/_ah/api/mtt/v1/test_plans/%s' % test_plan_id,
                                data)

        msg = protojson.decode_message(messages.TestPlan, res.body)
        self.assertEqual(test_plan_id, int(msg.id))
        test_plan = ndb_models.TestPlan.get_by_id(test_plan_id)
        self.assertIsNotNone(test_plan)
        self._VerifyTestPlan(data, test_plan)
        mock_schedule_test_plan_cron_job.assert_called_with(test_plan_id)
Ejemplo n.º 7
0
 def testNewNote_withExisting(self):
   """Tests adding a note to a cluster when one already exists."""
   user = '******'
   timestamp = datetime.datetime(2015, 10, 18, 20, 46)
   message = 'The Message'
   api_request = {'cluster_id': 'free',
                  'user': user,
                  'timestamp': timestamp.isoformat(),
                  'message': message
                 }
   api_response = self.testapp.post_json('/_ah/api/ClusterApi.NewNote',
                                         api_request)
   self.assertEqual('200 OK', api_response.status)
   # Query the same cluster again. Notes should be sorted.
   api_request = {'cluster_id': 'free', 'include_notes': True}
   api_response = self.testapp.post_json('/_ah/api/ClusterApi.GetCluster',
                                         api_request)
   cluster_info = protojson.decode_message(api_messages.ClusterInfo,
                                           api_response.body)
   self.assertEqual(2, len(cluster_info.notes))
   self.assertEqual(user, cluster_info.notes[0].user)
   self.assertEqual(timestamp, cluster_info.notes[0].timestamp)
   self.assertEqual(message, cluster_info.notes[0].message)
   self.assertEqual(self.note.user, cluster_info.notes[1].user)
   self.assertEqual(self.note.timestamp, cluster_info.notes[1].timestamp)
   self.assertEqual(self.note.message, cluster_info.notes[1].message)
Ejemplo n.º 8
0
 def testGetCluster_includeHosts(self):
   """Tests GetCluster returns hosts in order with their devices."""
   api_request = {'cluster_id': 'free', 'include_hosts': True}
   api_response = self.testapp.post_json(
       '/_ah/api/ClusterApi.GetCluster', api_request)
   cluster_info = protojson.decode_message(api_messages.ClusterInfo,
                                           api_response.body)
   self.assertEqual('200 OK', api_response.status)
   self.assertEqual('free', cluster_info.cluster_id)
   self.assertEqual(2, len(cluster_info.host_infos))
   self.assertEqual('host_0', cluster_info.host_infos[0].hostname)
   self.assertEqual(1, len(cluster_info.host_infos[0].device_infos))
   self.assertEqual('device_0',
                    cluster_info.host_infos[0].device_infos[0].device_serial)
   self.assertEqual('100',
                    cluster_info.host_infos[0].device_infos[0].battery_level)
   self.assertIsNone(cluster_info.host_infos[0].device_infos[0].timestamp)
   self.assertEqual('host_1', cluster_info.host_infos[1].hostname)
   self.assertEqual(1, len(cluster_info.host_infos[1].device_infos))
   self.assertEqual('device_1',
                    cluster_info.host_infos[1].device_infos[0].device_serial)
   self.assertEqual(self.TIMESTAMP,
                    cluster_info.host_infos[1].device_infos[0].timestamp)
   self.assertEqual(0, len(cluster_info.notes))
   self.assertItemsEqual(['shamu', 'flounder'],
                         [r.name for r in cluster_info.run_targets])
Ejemplo n.º 9
0
 def testBuildReport_noDates(self, mock_record_groups):
     # Test BuildReport with no given dates
     record_groups_0 = [
         report_api.RecordGroup(groupByField='field',
                                groupByValue='a',
                                count=1,
                                total=2)
     ]
     record_groups_1 = [
         report_api.RecordGroup(groupByField='field',
                                groupByValue='b',
                                count=3,
                                total=3)
     ]
     # Since no dates are provided, only record_groups_0 should be returned
     mock_record_groups.side_effect = [record_groups_0, record_groups_1]
     api_request = {
         'groupByKey': 'product',
         'states': ['Available', 'Allocated'],
     }
     api_response = self.testapp.post_json('/_ah/api/ReportApi.BuildReport',
                                           api_request)
     self.assertEqual('200 OK', api_response.status)
     report = protojson.decode_message(report_api.Report, api_response.body)
     daily_records = report.dailyRecords
     self.assertEqual(1, len(daily_records))
     self.assertEqual(record_groups_0, daily_records[0].recordGroups)
Ejemplo n.º 10
0
    def testTestListHarnessImages_withFilter_goldenImages(self):

        api_request = {
            'count': 4,
            'tag_prefix': 'golden_tradefed',
        }
        api_response = self.testapp.post_json(
            '/_ah/api/TestHarnessImageApi.ListTestHarnessImages', api_request)
        image_collection = protojson.decode_message(
            api_messages.TestHarnessImageMetadataCollection, api_response.body)
        self.assertEqual('200 OK', api_response.status)

        images = image_collection.images
        next_cursor = image_collection.next_cursor
        self.assertLen(images, 3)
        self.assertIsNone(next_cursor)

        self.assertTrue(
            _CheckTestHarnessImageEntityAndApiMessageEqual(
                self._entities[1], images[0]))
        self.assertTrue(
            _CheckTestHarnessImageEntityAndApiMessageEqual(
                self._entities[2], images[1]))
        self.assertTrue(
            _CheckTestHarnessImageEntityAndApiMessageEqual(
                self._entities[3], images[2]))
Ejemplo n.º 11
0
 def testListHostGroup(self):
     """Tests ListHostGroup."""
     datastore_test_util.CreateHost(
         cluster='free',
         hostname='host_0',
         timestamp=self.TIMESTAMP,
     )
     datastore_test_util.CreateHost(
         cluster='paid',
         hostname='host_1',
         timestamp=self.TIMESTAMP,
     )
     datastore_test_util.CreateHost(
         cluster='free',
         hostname='host_2',
         hidden=True,
     )
     api_request = {'type': 'HOST_GROUP'}
     api_response = self.testapp.post_json(
         '/_ah/api/FilterHintApi.ListFilterHints', api_request)
     host_group_collection = protojson.decode_message(
         api_messages.FilterHintCollection, api_response.body)
     self.assertEqual('200 OK', api_response.status)
     self.assertEqual(2, len(host_group_collection.filter_hints))
     host_groups = list(host_group_collection.filter_hints)
     self.assertEqual(host_groups[0].value, 'free')
     self.assertEqual(host_groups[1].value, 'paid')
Ejemplo n.º 12
0
 def testListDeviceStates(self):
     """Tests ListDeviceStates."""
     api_request = {'type': 'DEVICE_STATE'}
     api_response = self.testapp.post_json(
         '/_ah/api/FilterHintApi.ListFilterHints', api_request)
     device_state_collection = protojson.decode_message(
         api_messages.FilterHintCollection, api_response.body)
     self.assertEqual('200 OK', api_response.status)
     self.assertEqual(17, len(device_state_collection.filter_hints))
     states = list(device_state_collection.filter_hints)
     self.assertEqual([s.value for s in states], [
         common.DeviceState.ALLOCATED,
         common.DeviceState.AVAILABLE,
         common.DeviceState.CHECKING,
         common.DeviceState.FASTBOOT,
         common.DeviceState.GONE,
         common.DeviceState.IGNORED,
         common.DeviceState.UNAVAILABLE,
         common.DeviceState.UNKNOWN,
         common.DeviceState.INIT,
         common.DeviceState.DYING,
         common.DeviceState.MISSING,
         common.DeviceState.PREPPING,
         common.DeviceState.DIRTY,
         common.DeviceState.LAMEDUCK,
         common.DeviceState.IDLE,
         common.DeviceState.BUSY,
         common.DeviceState.OFFLINE,
     ])
Ejemplo n.º 13
0
 def testListTestHarnessVersion(self):
     """Tests ListTestHarnessVersion."""
     datastore_test_util.CreateHost(
         cluster='free',
         hostname='host_0',
         timestamp=self.TIMESTAMP,
     )
     datastore_test_util.CreateHost(
         cluster='paid',
         hostname='host_1',
         timestamp=self.TIMESTAMP,
         test_harness='MOBILE_HARNESS',
         test_harness_version='3.0.1',
     )
     datastore_test_util.CreateHost(
         cluster='free',
         hostname='host_2',
         lab_name='alab',
         assignee='auser',
         hidden=True,
     )
     api_request = {'type': 'TEST_HARNESS_VERSION'}
     api_response = self.testapp.post_json(
         '/_ah/api/FilterHintApi.ListFilterHints', api_request)
     test_harness_version_collection = protojson.decode_message(
         api_messages.FilterHintCollection, api_response.body)
     self.assertEqual('200 OK', api_response.status)
     self.assertEqual(2, len(test_harness_version_collection.filter_hints))
     test_harness_versions = list(
         test_harness_version_collection.filter_hints)
     self.assertEqual(test_harness_versions[0].value, '1234')
     self.assertEqual(test_harness_versions[1].value, '3.0.1')
Ejemplo n.º 14
0
    def protorpc_to_endpoints_error(self, status, body):
        """Convert a ProtoRPC error to the format expected by Google Endpoints.

    If the body does not contain an ProtoRPC message in state APPLICATION_ERROR
    the status and body will be returned unchanged.

    Args:
      status: HTTP status of the response from the backend
      body: JSON-encoded error in format expected by Endpoints frontend.

    Returns:
      Tuple of (http status, body)
    """
        try:
            rpc_error = protojson.decode_message(remote.RpcStatus, body)
        except (ValueError, messages.ValidationError):
            rpc_error = remote.RpcStatus()

        if rpc_error.state == remote.RpcStatus.State.APPLICATION_ERROR:

            error_class = _ERROR_NAME_MAP.get(rpc_error.error_name)
            if error_class:
                status, body = self.__write_error(error_class.http_status,
                                                  rpc_error.error_message)
        return status, body
Ejemplo n.º 15
0
  def testCqStats(self):
    project = 'chromium'
    cron_app = webtest.TestApp(
        webapp2.WSGIApplication([
          ('/check-cq', cron.CheckCQHandler),
        ])
    )
    cron_app.get('/check-cq')
    cq_data = self._make_api_call(
        'cq_stats_get',
        params={'project': project},
    ).json

    generated = {}
    for name, klass in (('single_run_data', models.CqStat),
                        ('queue_time_data', models.CqTimeInQueueForPatchStat),
                        ('total_time_data', models.CqTotalTimeForPatchStat)):
      generated[name] = [
        protojson.decode_message(klass.ProtoModel(), json.dumps(x))
        for x in cq_data[name]]

    expected = controller.get_cq_stats(project)
    for key in expected:
      expected[key] = [x.ToMessage() for x in expected[key]]

    self.assertEqual(generated, expected)
Ejemplo n.º 16
0
    def testTestListHarnessImages_allInOnePage(self):

        api_request = {'count': 4}
        api_response = self.testapp.post_json(
            '/_ah/api/TestHarnessImageApi.ListTestHarnessImages', api_request)
        image_collection = protojson.decode_message(
            api_messages.TestHarnessImageMetadataCollection, api_response.body)
        self.assertEqual('200 OK', api_response.status)

        images = image_collection.images
        next_cursor = image_collection.next_cursor
        self.assertLen(images, 4)
        self.assertIsNone(next_cursor)

        self.assertTrue(
            _CheckTestHarnessImageEntityAndApiMessageEqual(
                self._entities[0], images[0]))
        self.assertTrue(
            _CheckTestHarnessImageEntityAndApiMessageEqual(
                self._entities[1], images[1]))
        self.assertTrue(
            _CheckTestHarnessImageEntityAndApiMessageEqual(
                self._entities[2], images[2]))
        self.assertTrue(
            _CheckTestHarnessImageEntityAndApiMessageEqual(
                self._entities[3], images[3]))
Ejemplo n.º 17
0
    def testCreate(self, mock_schedule_test_plan_cron_job):
        """Tests test_plans.create API."""
        data = {
            'name':
            'foo',
            'cron_exp':
            '0 * * * *',
            'test_run_configs': [{
                'test_id': self.mock_test_id,
                'cluster': 'cluster',
                'run_target': 'run_target',
            }],
            'test_resource_pipes': [{
                'name': 'bar',
                'url': self.mock_test_resource_url,
            }],
            'before_device_action_ids': [
                self.mock_device_action_id,
            ]
        }

        res = self.app.post_json('/_ah/api/mtt/v1/test_plans', data)

        msg = protojson.decode_message(messages.TestPlan, res.body)
        test_plan_id = int(msg.id)
        test_plan = ndb_models.TestPlan.get_by_id(test_plan_id)
        self._VerifyTestPlan(data, test_plan)
        mock_schedule_test_plan_cron_job.assert_called_with(test_plan_id)
Ejemplo n.º 18
0
 def testNullValues(self):
   """Test that null values overwrite existing values."""
   self.assertEquals(MyMessage(),
                     protojson.decode_message(MyMessage,
                                              ('{"an_integer": null,'
                                               ' "a_nested": null'
                                               '}')))
Ejemplo n.º 19
0
 def testListHostUpdateStates(self):
     """Tests ListHostUpdateStates."""
     api_request = {'type': 'UPDATE_STATE'}
     api_response = self.testapp.post_json(
         '/_ah/api/FilterHintApi.ListFilterHints', api_request)
     host_state_collection = protojson.decode_message(
         api_messages.FilterHintCollection, api_response.body)
     self.assertEqual('200 OK', api_response.status)
     self.assertEqual(8, len(host_state_collection.filter_hints))
     states = list(host_state_collection.filter_hints)
     self.assertEqual(states[0].value,
                      api_messages.HostUpdateState.UNKNOWN.name)
     self.assertEqual(states[1].value,
                      api_messages.HostUpdateState.PENDING.name)
     self.assertEqual(states[2].value,
                      api_messages.HostUpdateState.SYNCING.name)
     self.assertEqual(states[3].value,
                      api_messages.HostUpdateState.SHUTTING_DOWN.name)
     self.assertEqual(states[4].value,
                      api_messages.HostUpdateState.RESTARTING.name)
     self.assertEqual(states[5].value,
                      api_messages.HostUpdateState.TIMED_OUT.name)
     self.assertEqual(states[6].value,
                      api_messages.HostUpdateState.ERRORED.name)
     self.assertEqual(states[7].value,
                      api_messages.HostUpdateState.SUCCEEDED.name)
  def testGetMetadata_returnsCompletedAttempts(self, mock_get_request):
    # request w/ RUNNING and COMPLETED attempts
    mock_request = mock.MagicMock()
    mock_get_request.return_value = mock_request
    running_attempt = self._createAttempt(CommandState.RUNNING)
    completed_attempt = self._createAttempt(CommandState.COMPLETED)
    mock_request.command_attempts = [running_attempt, completed_attempt]

    test = self._createMockTest()
    test_run = self._createMockTestRuns(test=test)[0]
    test_run.request_id = 'request'
    test_run.put()
    res = self.app.get(
        '/_ah/api/mtt/v1/test_runs/%s/metadata' % test_run.key.id())

    # metadata is sent back w/ only COMPLETED attempts
    metadata = protojson.decode_message(messages.TestRunMetadataList, res.body)
    expected = messages.TestRunMetadataList(
        test_runs=[
            messages.TestRunMetadata(
                test_run=messages.Convert(test_run, messages.TestRun),
                command_attempts=[completed_attempt])
        ],
        server_version=env.VERSION)
    self.assertEqual(expected, metadata)
Ejemplo n.º 21
0
 def testBuildReport(self, mock_record_groups):
     # Test BuildReport
     record_groups_0 = [
         report_api.RecordGroup(groupByField='field',
                                groupByValue='a',
                                count=1,
                                total=2)
     ]
     record_groups_1 = [
         report_api.RecordGroup(groupByField='field',
                                groupByValue='b',
                                count=3,
                                total=3)
     ]
     mock_record_groups.side_effect = [record_groups_0, record_groups_1]
     api_request = {
         'groupByKey': 'product_variant',
         'startDate': DATE_0.isoformat(),
         'endDate': DATE_1.isoformat(),
         'states': ['Available', 'Allocated'],
     }
     api_response = self.testapp.post_json('/_ah/api/ReportApi.BuildReport',
                                           api_request)
     self.assertEqual('200 OK', api_response.status)
     report = protojson.decode_message(report_api.Report, api_response.body)
     daily_records = report.dailyRecords
     self.assertEqual(2, len(daily_records))
     self.assertEqual(record_groups_0, daily_records[0].recordGroups)
     self.assertEqual(record_groups_1, daily_records[1].recordGroups)
Ejemplo n.º 22
0
    def testListPools(self):
        """Tests ListPools."""
        datastore_test_util.CreateHost(
            cluster='free',
            hostname='host_0',
            pools=['pool_1'],
            timestamp=self.TIMESTAMP,
        )
        datastore_test_util.CreateHost(
            cluster='paid',
            hostname='host_1',
            pools=['pool_2', 'pool_3'],
            timestamp=self.TIMESTAMP,
        )
        api_request = {'type': 'POOL'}
        api_response = self.testapp.post_json(
            '/_ah/api/FilterHintApi.ListFilterHints', api_request)
        pool_collection = protojson.decode_message(
            api_messages.FilterHintCollection, api_response.body)
        self.assertEqual('200 OK', api_response.status)

        pools = list(pool_collection.filter_hints)
        self.assertLen(pool_collection.filter_hints, 3)
        self.assertCountEqual(('pool_1', 'pool_2', 'pool_3'),
                              (pool.value for pool in pools))
Ejemplo n.º 23
0
 def testAuthAccountPrinciples_jumpHostNoAccess(self):
   datastore_test_util.CreateHostConfig(
       'mock.host.google.com', 'foo-lab', inventory_groups=['jump'])
   datastore_test_util.CreateHostGroupConfig(
       'bar-group',
       'foo-lab',
       account_principals={
           'bar-admin': {
               'principals': ['principalA', 'principalB']
           }
       })
   datastore_test_util.CreateHostGroupConfig('bar-group2', 'foo-lab')
   acl_service.CheckMembership = mock.MagicMock(return_value=False)
   api_request = {
       'hostname': 'mock.host.google.com',
       'host_account': 'android-test',
       'user_name': 'mock-user'
   }
   api_response = self.testapp.post_json('/_ah/api/AclApi.CheckSshAccessible',
                                         api_request)
   account_principals = protojson.decode_message(api_messages.AclCheckResult,
                                                 api_response.body)
   self.assertEqual('200 OK', api_response.status)
   self.assertFalse(account_principals.has_access)
   acl_service.CheckMembership.assert_has_calls([
       mock.call('mock-user', 'principalA'),
       mock.call('mock-user', 'principalB')
   ])
Ejemplo n.º 24
0
  def protorpc_to_endpoints_error(self, status, body):
    """Convert a ProtoRPC error to the format expected by Google Endpoints.

    If the body does not contain an ProtoRPC message in state APPLICATION_ERROR
    the status and body will be returned unchanged.

    Args:
      status: HTTP status of the response from the backend
      body: JSON-encoded error in format expected by Endpoints frontend.

    Returns:
      Tuple of (http status, body)
    """
    try:
      rpc_error = protojson.decode_message(remote.RpcStatus, body)
    except (ValueError, messages.ValidationError):
      rpc_error = remote.RpcStatus()

    if rpc_error.state == remote.RpcStatus.State.APPLICATION_ERROR:


      error_class = _ERROR_NAME_MAP.get(rpc_error.error_name)
      if error_class:
        status, body = self.__write_error(error_class.http_status,
                                          rpc_error.error_message)
    return status, body
Ejemplo n.º 25
0
 def testCreate_stripId(self):
     """Tests that new actions have new IDs."""
     data = {'id': '1', 'name': 'created', 'hook_class_name': 'simple'}
     response = self.app.post_json('/_ah/api/mtt/v1/test_run_actions', data)
     action = protojson.decode_message(messages.TestRunAction,
                                       response.body)
     self.assertNotEqual('1', action.id)
  def testGetMetadata_remoteAncestry(self, mock_open_file):
    # create hierarchy of test_runs
    test = self._createMockTest()
    child, parent = self._createMockTestRuns(test=test, count=2)  
    with tempfile.NamedTemporaryFile(suffix='.zip') as tmp_stream:
      # write parent metadata to a temporary zip file
      with zipfile.ZipFile(tmp_stream, 'w') as context_file:
        res = self.app.get('/_ah/api/mtt/v1/test_runs/%s/metadata' %
                           parent.key.id())
        context_file.writestr('mtt.json', res.body)
      tmp_stream.seek(0)
      mock_open_file.return_value = tmp_stream

      # mark child as a remote return
      child.prev_test_context = ndb_models.TestContextObj(test_resources=[
          ndb_models.TestResourceObj(name='mtt.json', url='url')
      ])
      child.put()

      res = self.app.get('/_ah/api/mtt/v1/test_runs/%s/metadata' %
                         child.key.id())

      # metadata is sent back w/ ordered ancestry information
      metadata = protojson.decode_message(messages.TestRunMetadataList,
                                          res.body)
      expected = messages.TestRunMetadataList(
          test_runs=[
              messages.TestRunMetadata(
                  test_run=messages.Convert(child, messages.TestRun)),
              messages.TestRunMetadata(
                  test_run=messages.Convert(parent, messages.TestRun))
          ],
          server_version=env.VERSION)
      self.assertEqual(expected, metadata)
Ejemplo n.º 27
0
    def testCqStats(self):
        project = 'chromium'
        cron_app = webtest.TestApp(
            webapp2.WSGIApplication([
                ('/check-cq', cron.CheckCQHandler),
            ]))
        cron_app.get('/check-cq')
        cq_data = self._make_api_call(
            'cq_stats_get',
            params={
                'project': project
            },
        ).json

        generated = {}
        for name, klass in (('single_run_data', models.CqStat),
                            ('queue_time_data',
                             models.CqTimeInQueueForPatchStat),
                            ('total_time_data',
                             models.CqTotalTimeForPatchStat)):
            generated[name] = [
                protojson.decode_message(klass.ProtoModel(), json.dumps(x))
                for x in cq_data[name]
            ]

        expected = controller.get_cq_stats(project)
        for key in expected:
            expected[key] = [x.ToMessage() for x in expected[key]]

        self.assertEqual(generated, expected)
  def testGetMetadata_ancestry(self, mock_get_request):
    mock_get_request.return_value = mock.MagicMock()
    test = self._createMockTest()

    # create hierarchy of test_runs
    child, parent, grandparent = self._createMockTestRuns(test=test, count=3)      child.prev_test_run_key = parent.key
    child.prev_test_context = ndb_models.TestContextObj()
    child.put()
    parent.prev_test_run_key = grandparent.key
    parent.prev_test_context = ndb_models.TestContextObj()
    parent.put()

    res = self.app.get('/_ah/api/mtt/v1/test_runs/%s/metadata' % child.key.id())

    # metadata is sent back w/ ordered ancestry information
    metadata = protojson.decode_message(messages.TestRunMetadataList, res.body)
    expected = messages.TestRunMetadataList(
        test_runs=[
            messages.TestRunMetadata(
                test_run=messages.Convert(child, messages.TestRun)),
            messages.TestRunMetadata(
                test_run=messages.Convert(parent, messages.TestRun)),
            messages.TestRunMetadata(
                test_run=messages.Convert(grandparent, messages.TestRun))
        ],
        server_version=env.VERSION)
    self.assertEqual(expected, metadata)
Ejemplo n.º 29
0
 def testGet(self):
     response = self.app.get('/_ah/api/mtt/v1/build_channel_providers/Mock')
     res = protojson.decode_message(messages.BuildChannelProvider,
                                    response.body)
     self.assertEqual(res.name, 'Mock')
     self.assertLen(res.option_defs, 1)
     self.assertEqual(res.option_defs[0].name, 'mock_option')
Ejemplo n.º 30
0
 def testNullValues(self):
     """Test that null values overwrite existing values."""
     self.assertEquals(
         MyMessage(),
         protojson.decode_message(MyMessage, ('{"an_integer": null,'
                                              ' "a_nested": null'
                                              '}')))
Ejemplo n.º 31
0
  def DoRequest(self,
                path,
                request,
                response_type,
                reg_path='/protorpc'):
    stdin = sys.stdin
    stdout = sys.stdout
    environ = os.environ
    try:
      sys.stdin = cStringIO.StringIO(protojson.encode_message(request))
      sys.stdout = cStringIO.StringIO()

      os.environ = webapp_test_util.GetDefaultEnvironment()
      os.environ['PATH_INFO'] = path
      os.environ['REQUEST_METHOD'] = 'POST'
      os.environ['CONTENT_TYPE'] = 'application/json'
      os.environ['wsgi.input'] = sys.stdin
      os.environ['wsgi.output'] = sys.stdout
      os.environ['CONTENT_LENGTH'] = len(sys.stdin.getvalue())

      service_handlers.run_services(
        [('/my_service', GetCalled.new_factory(self))], reg_path)

      header, body = sys.stdout.getvalue().split('\n\n', 1)

      return (header.split('\n')[0],
              protojson.decode_message(response_type, body))
    finally:
      sys.stdin = stdin
      sys.stdout = stdout
      os.environ = environ
Ejemplo n.º 32
0
def GetRequestInvocationStatus(
        request_id: int) -> api_messages.InvocationStatus:
    """Fetches the invocation status for a request."""
    res = _GetAPIClient().requests().invocationStatus().get(
        request_id=request_id).execute()
    return protojson.decode_message(  # pytype: disable=module-attr
        api_messages.InvocationStatus, json.dumps(res))
Ejemplo n.º 33
0
 def testProtojsonUnrecognizedNull(self):
   """Test that unrecognized fields that are None are skipped."""
   decoded = protojson.decode_message(
       MyMessage,
       '{"an_integer": 1, "unrecognized_null": null}')
   self.assertEquals(decoded.an_integer, 1)
   self.assertEquals(decoded.all_unrecognized_fields(), [])
Ejemplo n.º 34
0
  def testLeaseHostTasks_inconsistencyCommand(self, ensure_consistency):
    ensure_consistency.return_value = False
    self._AddCommand(REQUEST_ID, '2', 'command', 'cluster', 'run_target1')

    request = {
        'hostname': 'hostname',
        'cluster': 'cluster',
        'device_infos': [
            {
                'device_serial': 'd1',
                'hostname': 'hostname',
                'run_target': 'run_target1',
                'state': common.DeviceState.AVAILABLE,
            },
        ],
        'next_cluster_ids': ['cluster2', 'cluster3']
    }
    response = self.testapp.post_json(
        '/_ah/api/CommandTaskApi.LeaseHostTasks', request)
    self.assertEqual('200 OK', response.status)
    task_list = protojson.decode_message(command_task_api.CommandTaskList,
                                         response.body)

    self.assertEqual(0, len(task_list.tasks))
    ensure_consistency.assert_called_once_with(
        REQUEST_ID, '2', '%s-2-0' % REQUEST_ID)
Ejemplo n.º 35
0
  def testNumericEnumeration(self):
    """Test that numbers work for enum values."""
    message = protojson.decode_message(MyMessage, '{"an_enum": 2}')

    expected_message = MyMessage()
    expected_message.an_enum = MyMessage.Color.GREEN

    self.assertEquals(expected_message, message)
Ejemplo n.º 36
0
  def testAlphaEnumeration(self):
    """Test that alpha enum values work."""
    message = protojson.decode_message(MyMessage, '{"an_enum": "RED"}')

    expected_message = MyMessage()
    expected_message.an_enum = MyMessage.Color.RED

    self.assertEquals(expected_message, message)
 def testListTestCaseResults_nameFilter(self):
   """Tests that test case results can be filtered by name."""
   path = 'modules/module_3/test_cases?name=sT_2'
   response = self.app.get('/_ah/api/mtt/v1/test_results/' + path)
   result_list = protojson.decode_message(
       messages.TestCaseResultList, response.body)
   self.assertLen(result_list.results, 1)
   self.assertEqual('test_2', result_list.results[0].name)
Ejemplo n.º 38
0
  def testConvertIntegerToFloat(self):
    """Test that integers passed in to float fields are converted.

    This is necessary because JSON outputs integers for numbers with 0 decimals.
    """
    message = protojson.decode_message(MyMessage, '{"a_float": 10}')

    self.assertTrue(isinstance(message.a_float, float))
    self.assertEquals(10.0, message.a_float)
Ejemplo n.º 39
0
 def testProtojsonUnrecognizedFieldName(self):
   """Test that unrecognized fields are saved and can be accessed."""
   decoded = protojson.decode_message(MyMessage,
                                      ('{"an_integer": 1, "unknown_val": 2}'))
   self.assertEquals(decoded.an_integer, 1)
   self.assertEquals(1, len(decoded.all_unrecognized_fields()))
   self.assertEquals('unknown_val', decoded.all_unrecognized_fields()[0])
   self.assertEquals((2, messages.Variant.INT64),
                     decoded.get_unrecognized_field_info('unknown_val'))
Ejemplo n.º 40
0
  def testDecodeDateTime(self):
    for datetime_string, datetime_vals in (
        ('2012-09-30T15:31:50.262', (2012, 9, 30, 15, 31, 50, 262000)),
        ('2012-09-30T15:31:50', (2012, 9, 30, 15, 31, 50, 0))):
      message = protojson.decode_message(
          MyMessage, '{"a_datetime": "%s"}' % datetime_string)
      expected_message = MyMessage(
          a_datetime=datetime.datetime(*datetime_vals))

      self.assertEquals(expected_message, message)
Ejemplo n.º 41
0
  def testDecodeRepeatedDateTime(self):
    message = protojson.decode_message(
        MyMessage,
        '{"a_repeated_datetime": ["2012-09-30T15:31:50.262", '
        '"2010-01-21T09:52:00", "2000-01-01T01:00:59.999999"]}')
    expected_message = MyMessage(
        a_repeated_datetime=[
            datetime.datetime(2012, 9, 30, 15, 31, 50, 262000),
            datetime.datetime(2010, 1, 21, 9, 52),
            datetime.datetime(2000, 1, 1, 1, 0, 59, 999999)])

    self.assertEquals(expected_message, message)
Ejemplo n.º 42
0
def message_from_json(message_type, value):
  '''Returns an instance of a protorpc `Message` class.

    :param message_type: Protorpc `Message` class.
    :param value: JSON string to decode to a `Message`.
  '''
  if isinstance(value, str):
    value = value
  elif isinstance(value, dict):
    value = dumps(value)
  else:
    raise JsonValueError('Value must be a json str or dict.')
  return protojson.decode_message(message_type, value)
Ejemplo n.º 43
0
    def process(self, request, container, fallback=None):
        from protorpc import protojson, messages

        try:
            result = protojson.decode_message(container, request.body)
            self.errors = None

        except messages.ValidationError as e:
            result = container()
            self.errors = [e.message]

        self.container = result
        self.fallback = fallback
        return self
Ejemplo n.º 44
0
  def testConvertStringToNumbers(self):
    """Test that strings passed to integer fields are converted."""
    message = protojson.decode_message(MyMessage,
                                       """{"an_integer": "10",
                                           "a_float": "3.5",
                                           "a_repeated": ["1", "2"],
                                           "a_repeated_float": ["1.5", "2", 10]
                                           }""")

    self.assertEquals(MyMessage(an_integer=10,
                                a_float=3.5,
                                a_repeated=[1, 2],
                                a_repeated_float=[1.5, 2.0, 10.0]),
                      message)
Ejemplo n.º 45
0
def load_quota_config(quota_config_path=QUOTA_CONFIG_PATH):
  """Load quota configuration from from file.

  Args:
    quota_config_path: Quota to configuration file.  Contents of file must be
      a JSON encoded ProtoRPC message of the format defined by
      quota.services.QuotaConfig.

    Returns:
      quota.services.QuotaConfig instance with contents parsed from quota file.
  """
  with open(quota_config_path) as quota_config_file:
    encoded_config = quota_config_file.read()
  return protojson.decode_message(services.QuotaConfig, encoded_config)
    def process(self, request, container, fallback=None):
        from protorpc import protojson, messages

        try:
            self.partial_fields = json.loads(request.body).keys()
            result = protojson.decode_message(container, request.body)
            self.errors = None

        except (messages.ValidationError, ValueError) as e:
            result = container()
            self.errors = [e.message]
            self.partial_fields = None

        self.container = result
        self.fallback = fallback
        return self
Ejemplo n.º 47
0
 def _get_stats_to_download(self, locales):
     # 'stats' maps the service name to a mapping of languages to stats.
     stats = self.pod.read_yaml(Translator.TRANSLATOR_STATS_PATH)
     if self.KIND not in stats:
         self.pod.logger.info('Nothing found to download from {}'.format(self.KIND))
         return
     stats_to_download = stats[self.KIND]
     if locales:
         stats_to_download = dict([(lang, stat)
                                   for (lang, stat) in stats_to_download.iteritems()
                                   if lang in locales])
     for lang, stat in stats_to_download.iteritems():
         stat['lang'] = lang
         stat = json.dumps(stat)
         stat_message = protojson.decode_message(TranslatorStat, stat)
         stats_to_download[lang] = stat_message
     return stats_to_download
Ejemplo n.º 48
0
 def testProtojsonUnrecognizedFieldNumber(self):
   """Test that unrecognized fields are saved and can be accessed."""
   decoded = protojson.decode_message(
       MyMessage,
       '{"an_integer": 1, "1001": "unknown", "-123": "negative", '
       '"456_mixed": 2}')
   self.assertEquals(decoded.an_integer, 1)
   self.assertEquals(3, len(decoded.all_unrecognized_fields()))
   self.assertTrue(1001 in decoded.all_unrecognized_fields())
   self.assertEquals(('unknown', messages.Variant.STRING),
                     decoded.get_unrecognized_field_info(1001))
   self.assertTrue('-123' in decoded.all_unrecognized_fields())
   self.assertEquals(('negative', messages.Variant.STRING),
                     decoded.get_unrecognized_field_info('-123'))
   self.assertTrue('456_mixed' in decoded.all_unrecognized_fields())
   self.assertEquals((2, messages.Variant.INT64),
                     decoded.get_unrecognized_field_info('456_mixed'))
Ejemplo n.º 49
0
 def testUnrecognizedFieldVariants(self):
   """Test that unrecognized fields are mapped to the right variants."""
   for encoded, expected_variant in (
       ('{"an_integer": 1, "unknown_val": 2}', messages.Variant.INT64),
       ('{"an_integer": 1, "unknown_val": 2.0}', messages.Variant.DOUBLE),
       ('{"an_integer": 1, "unknown_val": "string value"}',
        messages.Variant.STRING),
       ('{"an_integer": 1, "unknown_val": [1, 2, 3]}', messages.Variant.INT64),
       ('{"an_integer": 1, "unknown_val": [1, 2.0, 3]}',
        messages.Variant.DOUBLE),
       ('{"an_integer": 1, "unknown_val": [1, "foo", 3]}',
        messages.Variant.STRING),
       ('{"an_integer": 1, "unknown_val": true}', messages.Variant.BOOL)):
     decoded = protojson.decode_message(MyMessage, encoded)
     self.assertEquals(decoded.an_integer, 1)
     self.assertEquals(1, len(decoded.all_unrecognized_fields()))
     self.assertEquals('unknown_val', decoded.all_unrecognized_fields()[0])
     _, decoded_variant = decoded.get_unrecognized_field_info('unknown_val')
     self.assertEquals(expected_variant, decoded_variant)
Ejemplo n.º 50
0
 def from_string(cls, content):
   return protojson.decode_message(messages.IndexMessage, content)
Ejemplo n.º 51
0
def config_from_json(preprocessor_class, content):
    config_class = preprocessor_class.Config
    return protojson.decode_message(config_class, content)
Ejemplo n.º 52
0
def decode_json_to_message(message_type, value):
  return decode_message(message_type, dumps(value))
Ejemplo n.º 53
0
 def parse_config(cls, config):
     """Parse the config into the protobuf."""
     return protojson.decode_message(
         cls.Config, json.dumps(config))
Ejemplo n.º 54
0
def config_from_json(deployment_class, content):
  config_class = deployment_class.Config
  return protojson.decode_message(config_class, content)
Ejemplo n.º 55
0
 def DecodeProto(data):
   return protojson.decode_message(request_type, data)
Ejemplo n.º 56
0
def config_from_json(config_class, config):
    config = json.dumps(config)
    return protojson.decode_message(config_class, config)
Ejemplo n.º 57
0
 def testDecodeRepeatedCustom(self):
   message = protojson.decode_message(
       MyMessage, '{"a_repeated_custom": [1, 2, 3]}')
   self.assertEquals(MyMessage(a_repeated_custom=[1, 2, 3]), message)
Ejemplo n.º 58
0
def message_from_json_str(message_type, value):
  return decode_message(message_type, value)
Ejemplo n.º 59
0
 def testDecodeCustom(self):
   message = protojson.decode_message(MyMessage, '{"a_custom": 1}')
   self.assertEquals(MyMessage(a_custom=1), message)