def test_advance_multiple_stages():
    model = ExampleModel()
    model._transitions = [
        ("frist_status", "second"),
        ("second", "third"),
        ("third", "fourth"),
        ("fourth", "fifth"),
    ]
    state_manager.set_transition(model_name="example::model",
                                 transition={
                                     "progression": "manual",
                                     "times": 1
                                 })

    model.status.should.equal("frist_status")
    model.status.should.equal("frist_status")
    model.advance()
    model.status.should.equal("second")
    model.status.should.equal("second")
    model.advance()
    model.status.should.equal("third")
    model.status.should.equal("third")
    model.advance()
    model.status.should.equal("fourth")
    model.status.should.equal("fourth")
    model.advance()
    model.status.should.equal("fifth")
    model.status.should.equal("fifth")
def test_override_status():
    model = ExampleModel()
    model.status = "creating"
    model._transitions = [("creating", "ready"), ("updating", "ready")]
    state_manager.set_transition(model_name="example::model",
                                 transition={
                                     "progression": "manual",
                                     "times": 1
                                 })

    model.status.should.equal("creating")
    model.advance()
    model.status.should.equal("ready")
    model.advance()
    # We're still ready
    model.status.should.equal("ready")

    # Override status manually
    model.status = "updating"

    model.status.should.equal("updating")
    model.advance()
    model.status.should.equal("ready")
    model.status.should.equal("ready")
    model.advance()
    model.status.should.equal("ready")
Пример #3
0
def test_cancel_pending_job():
    if settings.TEST_SERVER_MODE:
        raise SkipTest("Can't use state_manager in ServerMode directly")

    ec2_client, iam_client, _, _, batch_client = _get_clients()
    _, _, _, iam_arn = _setup(ec2_client, iam_client)

    # We need to be able to cancel a job that has not been started yet
    # Locally, our jobs start so fast that we can't cancel them in time
    # So artificially delay the status progression

    state_manager.set_transition("batch::job",
                                 transition={
                                     "progression": "time",
                                     "seconds": 2
                                 })

    commands = ["echo", "hello"]
    job_def_arn, queue_arn = prepare_job(batch_client, commands, iam_arn,
                                         "test")

    resp = batch_client.submit_job(
        jobName="test_job_name",
        jobQueue=queue_arn,
        jobDefinition=job_def_arn,
    )
    job_id = resp["jobId"]

    batch_client.cancel_job(jobId=job_id, reason="test_cancel")
    _wait_for_job_status(batch_client, job_id, "FAILED", seconds_to_wait=20)

    resp = batch_client.describe_jobs(jobs=[job_id])
    resp["jobs"][0]["jobName"].should.equal("test_job_name")
    resp["jobs"][0]["statusReason"].should.equal("test_cancel")
def test_advance_x_times():
    model = ExampleModel()
    state_manager.set_transition(model_name="example::model",
                                 transition={
                                     "progression": "manual",
                                     "times": 3
                                 })
    for _ in range(2):
        model.advance()
        model.status.should.equal("frist_status")

    # 3rd time is a charm
    model.advance()
    model.status.should.equal("second_status")

    # Status is still the same if we keep asking for it
    model.status.should.equal("second_status")

    # Advancing more does not make a difference - there's nothing to advance to
    model.advance()
    model.status.should.equal("second_status")
def test_realworld_delay():
    model = SlowModel()
    state_manager.set_transition(
        model_name="example::slowmodel",
        transition={
            "progression": "time",
            "seconds": 2
        },
    )

    model.status.should.equal("first")
    # The status will stick to 'first' for a long time
    # Advancing the model doesn't do anything, really
    for _ in range(10):
        model.advance()
        model.status.should.equal("first")

    import time

    time.sleep(2)
    # Status has only progressed after 2 seconds have passed
    model.status.should.equal("second")
Пример #6
0
    def set_transition(self, model_name, transition):
        from moto.moto_api import state_manager

        state_manager.set_transition(model_name, transition)