started with mocking job execution

This commit is contained in:
Stephan 2018-12-21 16:30:17 +01:00
parent bf3c9f3b80
commit 3ea673b3d0
3 changed files with 1331 additions and 1233 deletions

View File

@ -247,6 +247,7 @@ class FakeJob(BaseModel):
self.document_parameters = document_parameters
def to_dict(self):
obj = {
'jobArn': self.job_arn,
'jobId': self.job_id,
@ -274,12 +275,46 @@ class FakeJob(BaseModel):
return regex_match and length_match
class FakeJobExecution(BaseModel):
def __init__(self, job_id, thing_arn, status='QUEUED', force_canceled=False, status_details_map={}):
self.job_id = job_id
self.status = status # IN_PROGRESS | CANCELED | COMPLETED
self.force_canceled = force_canceled
self.status_details_map = status_details_map
self.thing_arn = thing_arn
self.queued_at = time.mktime(datetime(2015, 1, 1).timetuple())
self.started_at = time.mktime(datetime(2015, 1, 1).timetuple())
self.last_updated_at = time.mktime(datetime(2015, 1, 1).timetuple())
self.execution_number = 123
self.version_number = 123
self.approximate_seconds_before_time_out = 123
def to_dict(self):
obj = {
'jobId': self.job_id,
'status': self.status,
'forceCancel': self.force_canceled,
'statusDetails': {'detailsMap': self.status_details_map},
'thing_arn': self.thing_arn,
'queuedAt': self.queued_at,
'startedAt': self.started_at,
'lastUpdatedAt': self.last_updated_at,
'executionNumber': self.execution_number,
'versionNumber': self.version_number,
'approximateSecondsBeforeTimedOut': self.approximate_seconds_before_time_out
}
return obj
class IoTBackend(BaseBackend):
def __init__(self, region_name=None):
super(IoTBackend, self).__init__()
self.region_name = region_name
self.things = OrderedDict()
self.jobs = OrderedDict()
self.job_executions = OrderedDict()
self.thing_types = OrderedDict()
self.thing_groups = OrderedDict()
self.certificates = OrderedDict()
@ -723,6 +758,11 @@ class IoTBackend(BaseBackend):
job = FakeJob(job_id, targets, document_source, document, description, presigned_url_config, target_selection,
job_executions_rollout_config, document_parameters, self.region_name)
self.jobs[job_id] = job
for thing_arn in targets:
thing_name = thing_arn.split(':')[-1]
job_execution = FakeJobExecution(job_id, thing_arn)
self.job_executions[(job_id, thing_name)] = job_execution
return job.job_arn, job_id, description
def describe_job(self, job_id):
@ -731,6 +771,15 @@ class IoTBackend(BaseBackend):
def get_job_document(self, job_id):
return self.jobs[job_id]
def describe_job_execution(self, job_id, thing_name, execution_number):
# TODO filter with execution number
return self.job_executions[(job_id, thing_name)]
def list_job_executions_for_job(self, job_id, status, max_results, next_token):
job_executions = [self.job_executions[je] for je in self.job_executions if je[0] == job_id]
# TODO: implement filters
return job_executions, next_token
available_regions = boto3.session.Session().get_available_regions("iot")
iot_backends = {region: IoTBackend(region) for region in available_regions}

View File

@ -160,6 +160,16 @@ class IoTResponse(BaseResponse):
# TODO: needs to be implemented to get document_source's content from S3
return json.dumps({'document': ''})
def list_job_executions_for_job(self):
job_executions, next_token = self.iot_backend.list_job_executions_for_job(job_id=self._get_param("jobId"),
status=self._get_param("status"),
max_results=self._get_param(
"maxResults"),
next_token=self._get_param(
"nextToken"))
return json.dumps(dict(executionSummaries=[_.to_dict() for _ in job_executions], nextToken=next_token))
def create_keys_and_certificate(self):
set_as_active = self._get_bool_param("setAsActive")
cert, key_pair = self.iot_backend.create_keys_and_certificate(

View File

@ -874,3 +874,42 @@ def test_get_job_document_with_document():
job_document = client.get_job_document(jobId=job_id)
job_document.should.have.key('document').which.should.equal("{\"field\": \"value\"}")
@mock_iot
def test_list_job_executions_for_job():
client = boto3.client('iot', region_name='eu-west-1')
name = "my-thing"
job_id = "TestJob"
# thing
thing = client.create_thing(thingName=name)
thing.should.have.key('thingName').which.should.equal(name)
thing.should.have.key('thingArn')
# job document
job_document = {
"field": "value"
}
job = client.create_job(
jobId=job_id,
targets=[thing["thingArn"]],
document=json.dumps(job_document),
description="Description",
presignedUrlConfig={
'roleArn': 'arn:aws:iam::1:role/service-role/iot_job_role',
'expiresInSec': 123
},
targetSelection="CONTINUOUS",
jobExecutionsRolloutConfig={
'maximumPerMinute': 10
}
)
job.should.have.key('jobId').which.should.equal(job_id)
job.should.have.key('jobArn')
job.should.have.key('description')
job_execution = client.list_job_executions_for_job(jobId=job_id)
job_execution.should.have.key('executionSummaries')
job_execution['executionSummaries'][0].should.have.key('jobId').which.should.equal(job_id)