Exemplo n.º 1
0
 def test_init_invalid_target_state(self):
     invalid_target_state = "target_state_test"
     with pytest.raises(ValueError) as ctx:
         EC2InstanceStateSensor(
             task_id="task_test",
             target_state=invalid_target_state,
             instance_id="i-123abc",
         )
     msg = f"Invalid target_state: {invalid_target_state}"
     assert str(ctx.value) == msg
Exemplo n.º 2
0
 def test_init_invalid_target_state(self):
     invalid_target_state = "target_state_test"
     with self.assertRaises(ValueError) as cm:
         EC2InstanceStateSensor(
             task_id="task_test",
             target_state=invalid_target_state,
             instance_id="i-123abc",
         )
     msg = f"Invalid target_state: {invalid_target_state}"
     self.assertEqual(str(cm.exception), msg)
Exemplo n.º 3
0
 def test_init(self):
     ec2_operator = EC2InstanceStateSensor(
         task_id="task_test",
         target_state="stopped",
         instance_id="i-123abc",
         aws_conn_id="aws_conn_test",
         region_name="region-test",
     )
     assert ec2_operator.task_id == "task_test"
     assert ec2_operator.target_state == "stopped"
     assert ec2_operator.instance_id == "i-123abc"
     assert ec2_operator.aws_conn_id == "aws_conn_test"
     assert ec2_operator.region_name == "region-test"
Exemplo n.º 4
0
 def test_init(self):
     ec2_operator = EC2InstanceStateSensor(
         task_id="task_test",
         target_state="stopped",
         instance_id="i-123abc",
         aws_conn_id="aws_conn_test",
         region_name="region-test",
     )
     self.assertEqual(ec2_operator.task_id, "task_test")
     self.assertEqual(ec2_operator.target_state, "stopped")
     self.assertEqual(ec2_operator.instance_id, "i-123abc")
     self.assertEqual(ec2_operator.aws_conn_id, "aws_conn_test")
     self.assertEqual(ec2_operator.region_name, "region-test")
Exemplo n.º 5
0
    def test_stopped(self):
        # create instance
        ec2_hook = EC2Hook()
        instances = ec2_hook.conn.create_instances(
            MaxCount=1,
            MinCount=1,
        )
        instance_id = instances[0].instance_id
        # start instance
        ec2_hook.get_instance(instance_id=instance_id).start()

        # stop sensor, waits until ec2 instance state became stopped
        stop_sensor = EC2InstanceStateSensor(
            task_id="stop_sensor",
            target_state="stopped",
            instance_id=instance_id,
        )
        # assert instance state is not stopped
        assert not stop_sensor.poke(None)
        # stop instance
        ec2_hook.get_instance(instance_id=instance_id).stop()
        # assert instance state is stopped
        assert stop_sensor.poke(None)
Exemplo n.º 6
0
    def test_running(self):
        # create instance
        ec2_hook = EC2Hook()
        instances = ec2_hook.conn.create_instances(
            MaxCount=1,
            MinCount=1,
        )
        instance_id = instances[0].instance_id
        # stop instance
        ec2_hook.get_instance(instance_id=instance_id).stop()

        # start sensor, waits until ec2 instance state became running
        start_sensor = EC2InstanceStateSensor(
            task_id="start_sensor",
            target_state="running",
            instance_id=instance_id,
        )
        # assert instance state is not running
        self.assertFalse(start_sensor.poke(None))
        # start instance
        ec2_hook.get_instance(instance_id=instance_id).start()
        # assert instance state is running
        self.assertTrue(start_sensor.poke(None))
Exemplo n.º 7
0
    def test_terminated(self):
        # create instance
        ec2_hook = EC2Hook()
        instances = ec2_hook.conn.create_instances(
            MaxCount=1,
            MinCount=1,
        )
        instance_id = instances[0].instance_id
        # start instance
        ec2_hook.get_instance(instance_id=instance_id).start()

        # stop sensor, waits until ec2 instance state became terminated
        stop_sensor = EC2InstanceStateSensor(
            task_id="stop_sensor",
            target_state="terminated",
            instance_id=instance_id,
        )
        # assert instance state is not terminated
        self.assertFalse(stop_sensor.poke(None))
        # stop instance
        ec2_hook.get_instance(instance_id=instance_id).terminate()
        # assert instance state is terminated
        self.assertTrue(stop_sensor.poke(None))