def test_start_clean(self):
        """ this tests that the instance start function
        starts an instance.
        """

        ctx = self.mock_ctx('test_start_clean')
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        reservation = ec2_client.run_instances(
            TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE)
        instance_id = reservation.instances[0].id
        ctx.instance.runtime_properties['aws_resource_id'] = instance_id
        ec2_client.stop_instances(instance_id)
        instance.start(ctx=ctx)
        reservations = ec2_client.get_all_reservations(instance_id)
        instance_object = reservations[0].instances[0]
        state = instance_object.update()
        self.assertEqual(state, 'running')
    def test_start_and_tag_name(self):
        """ this tests that the instance start function
        starts an instance.
        """

        ctx = self.mock_ctx('test_start_clean')
        ctx.node.properties['name'] = 'test_start_and_tag_name'
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        reservation = ec2_client.run_instances(
            TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE)
        instance_id = reservation.instances[0].id
        ctx.instance.runtime_properties['aws_resource_id'] = instance_id
        ec2_client.stop_instances(instance_id)
        instance.start(ctx=ctx)
        reservations = ec2_client.get_all_reservations(instance_id)
        instance_object = reservations[0].instances[0]
        self.assertEquals(instance_object.tags.get('Name'),
                          ctx.node.properties['name'])
    def test_start_and_tag_name(self):
        """ this tests that the instance start function
        starts an instance.
        """

        ctx = self.mock_ctx('test_start_clean')
        ctx.node.properties['name'] = 'test_start_and_tag_name'
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        reservation = ec2_client.run_instances(
            TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE)
        instance_id = reservation.instances[0].id
        ctx.instance.runtime_properties['aws_resource_id'] = instance_id
        ec2_client.stop_instances(instance_id)
        instance.start(ctx=ctx)
        reservations = ec2_client.get_all_reservations(instance_id)
        instance_object = reservations[0].instances[0]
        self.assertEquals(instance_object.tags.get('Name'),
                          ctx.node.properties['name'])
    def test_start_clean(self):
        """ this tests that the instance start function
        starts an instance.
        """

        ctx = self.mock_ctx('test_start_clean')
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        reservation = ec2_client.run_instances(
            TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE)
        instance_id = reservation.instances[0].id
        ctx.instance.runtime_properties['aws_resource_id'] = instance_id
        ec2_client.stop_instances(instance_id)
        instance.start(ctx=ctx)
        reservations = ec2_client.get_all_reservations(instance_id)
        instance_object = reservations[0].instances[0]
        state = instance_object.update()
        self.assertEqual(state, 'running')
        self.assertNotIn('Name', instance_object.tags)
    def test_windows_runtime_property_created(self, *_):
        """tests that the generated windows password is saved as
        a runtime property
        """

        ctx = self.mock_ctx('test_windows_runtime_property_created')
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        reservation = ec2_client.run_instances(
            TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE)
        instance_id = reservation.instances[0].id
        ctx.instance.runtime_properties['aws_resource_id'] = instance_id
        ctx.node.properties['use_password'] = True

        with mock.patch('ec2.instance._get_windows_password') \
                as mock_get_windows_password:
            mock_get_windows_password.return_value = 'pass'
            instance.start(ctx=ctx)
            self.assertEqual(ctx.instance.runtime_properties
                             [constants.ADMIN_PASSWORD_PROPERTY], 'pass')
    def test_windows_runtime_property_created(self, *_):
        """tests that the generated windows password is saved as
        a runtime property
        """

        ctx = self.mock_ctx('test_windows_runtime_property_created')
        current_ctx.set(ctx=ctx)

        ec2_client = connection.EC2ConnectionClient().client()
        reservation = ec2_client.run_instances(
            TEST_AMI_IMAGE_ID, instance_type=TEST_INSTANCE_TYPE)
        instance_id = reservation.instances[0].id
        ctx.instance.runtime_properties['aws_resource_id'] = instance_id
        ctx.node.properties['use_password'] = True

        with mock.patch('ec2.instance._get_windows_password') \
                as mock_get_windows_password:
            mock_get_windows_password.return_value = 'pass'
            instance.start(ctx=ctx)
            self.assertEqual(
                ctx.instance.runtime_properties[
                    constants.ADMIN_PASSWORD_PROPERTY], 'pass')