def test_if_codebox_run_increments_instance_counter(self): self.assertIsNone(redis.get(self.codebox_limit_key)) CodeBoxTask.delay(**self.run_kwargs) set_current_instance(self.instance) self.assertEqual(redis.get(self.codebox_limit_key), b'1')
def get_codebox_spec(spec_key): serialized_spec = redis.get(spec_key) if serialized_spec is not None: try: return json.loads(serialized_spec) except ValueError: return None
def test_running_endpoint_with_admin(self, uwsgi_mock): response = self.client.post(self.script_run_url, HTTP_X_API_KEY=self.admin.key) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(uwsgi_mock.add_var.called) meta = json.loads(redis.get(METADATA_TEMPLATE.format(instance_pk=self.instance.pk, trace_type='socket_endpoint', trace_pk=1))) self.assertEqual(meta['admin'], {'id': self.admin.id, 'email': self.admin.email})
def test_running_endpoint_with_user(self, uwsgi_mock): user = G(User, username='******') response = self.client.post(self.script_run_url, HTTP_X_USER_KEY=user.key) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(uwsgi_mock.add_var.called) meta = json.loads(redis.get(METADATA_TEMPLATE.format(instance_pk=self.instance.pk, trace_type='socket_endpoint', trace_pk=1))) self.assertEqual(meta['user'], {'id': user.id, 'username': user.username, 'user_key': user.key})
def test_running_endpoint_with_environment(self, uwsgi_mock): self.socket.refresh_from_db() self.socket.environment = G(SocketEnvironment, status=SocketEnvironment.STATUSES.OK) self.socket.save() response = self.client.post(self.script_run_url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(uwsgi_mock.add_var.called) payload = json.loads(redis.get(PAYLOAD_TEMPLATE.format(instance_pk=self.instance.pk, trace_type='socket_endpoint', trace_pk=1))) self.assertIn('environment_url', payload)
def test_running_script_endpoint(self, uwsgi_mock): # Call POST which should get meta that are specified just for POST response = self.client.post(self.script_run_url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(uwsgi_mock.add_var.called) meta = json.loads(redis.get(METADATA_TEMPLATE.format(instance_pk=self.instance.pk, trace_type='socket_endpoint', trace_pk=1))) self.assertEqual(meta['metadata'], {'parameters': {'magic_param': 'arg2'}}) # And now call PATCH - which should be run with full meta as it doesn't have it's own self.client.patch(self.script_run_url) meta = json.loads(redis.get(METADATA_TEMPLATE.format(instance_pk=self.instance.pk, trace_type='socket_endpoint', trace_pk=2))) self.assertEqual(meta['metadata'], self.socket_endpoint_script.metadata) trace_url = reverse('v2:socket-endpoint-trace-list', args=(self.instance.name, self.socket_endpoint_script.name)) response = self.client.get(trace_url, HTTP_X_API_KEY=self.apikey) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data['objects']), 2)
def run(self, incentive_pk, instance_pk, payload_key, meta_key, trace_pk, expire_at=None, result_key=None, template_name=None, script_pk=None): payload = redis.get(payload_key) meta = redis.get(meta_key) self.process(instance_pk=instance_pk, incentive_pk=incentive_pk, script_pk=script_pk, additional_args=payload, result_key=result_key, template_name=template_name, trace_pk=trace_pk, expire_at=expire_at, meta=meta) redis.delete(payload_key)
def test_if_exceeding_limit_blocks_codebox_execution(self): self.assertIsNone(redis.get(self.codebox_limit_key)) for _ in range(settings.BILLING_CONCURRENT_CODEBOXES['builder'] + 3): CodeBoxTask.delay(**self.run_kwargs) set_current_instance(self.instance) self.assertEqual(len(CodeBoxRunTask.delay.mock_calls), settings.BILLING_CONCURRENT_CODEBOXES['builder']) trace_list = CodeBoxTrace.list(codebox=self.codebox) self.assertEqual( len([ trace for trace in trace_list if trace.status == CodeBoxTrace.STATUS_CHOICES.BLOCKED ]), 3)
def test_if_executed_codebox_decrements_instance_counter(self): self.assertEqual(redis.incr(self.codebox_limit_key), 1) CodeBoxTask.delay(**self.run_kwargs) self.assertEqual(redis.get(self.codebox_limit_key), b'0')