<commit_before># Copyright 2015 Isotoma Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from touchdown.tests.aws import StubberTestCase
from touchdown.tests.fixtures.aws import LaunchConfigurationFixture
from touchdown.tests.stubs.aws import AutoScalingGroupStubber


class TestCreateAutoScalingGroupuration(StubberTestCase):

    def test_create_auto_scaling_group(self):
        goal = self.create_goal('apply')

        launch_config = self.fixtures.enter_context(LaunchConfigurationFixture(goal, self.aws))

        auto_scaling_group = self.fixtures.enter_context(AutoScalingGroupStubber(
            goal.get_service(
                self.aws.add_auto_scaling_group(
                    name='my-asg',
                    min_size=0,
                    max_size=0,
                    launch_configuration=launch_config,
                ),
                'apply',
            )
        ))

        auto_scaling_group.add_describe_auto_scaling_groups_empty_response()
        auto_scaling_group.add_create_auto_scaling_group()
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()
        goal.execute()

    def test_create_auto_scaling_group_wait_for_healthy(self):
        goal = self.create_goal('apply')

        launch_config = self.fixtures.enter_context(LaunchConfigurationFixture(goal, self.aws))

        auto_scaling_group = self.fixtures.enter_context(AutoScalingGroupStubber(
            goal.get_service(
                self.aws.add_auto_scaling_group(
                    name='my-asg',
                    min_size=1,
                    max_size=1,
                    launch_configuration=launch_config,
                ),
                'apply',
            )
        ))

        auto_scaling_group.add_describe_auto_scaling_groups_empty_response()
        auto_scaling_group.add_create_auto_scaling_group(
            min_size=1,
            max_size=1,
        )
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()

        # Now wait for an ec2 instance to be in a healthy state
        # First of all lets mock one starting...
        auto_scaling_group.add_describe_auto_scaling_groups_one_response(
            instances=[{'LifecycleState': 'Pending'}],
        )

        # And now its ready...
        auto_scaling_group.add_describe_auto_scaling_groups_one_response(
            instances=[{'LifecycleState': 'InService'}],
        )

        goal.execute()

    def test_create_auto_scaling_group_idempotent(self):
        goal = self.create_goal('apply')

        launch_config = self.fixtures.enter_context(LaunchConfigurationFixture(goal, self.aws))

        auto_scaling_group = self.fixtures.enter_context(AutoScalingGroupStubber(
            goal.get_service(
                self.aws.add_auto_scaling_group(
                    name='my-asg',
                    min_size=0,
                    max_size=0,
                    launch_configuration=launch_config,
                ),
                'apply',
            )
        ))

        auto_scaling_group.add_describe_auto_scaling_groups_one_response()

        self.assertEqual(len(list(goal.plan())), 0)
        self.assertEqual(len(goal.get_changes(auto_scaling_group.resource)), 0)


class TestDestroyAutoScalingGroupuration(StubberTestCase):

    def test_destroy_auto_scaling_group(self):
        goal = self.create_goal('destroy')

        auto_scaling_group = self.fixtures.enter_context(AutoScalingGroupStubber(
            goal.get_service(
                self.aws.add_auto_scaling_group(
                    name='my-asg',
                    min_size=0,
                    max_size=0,
                ),
                'destroy',
            )
        ))

        auto_scaling_group.add_describe_auto_scaling_groups_one_response()
<commit_msg>Tests: ASG Teardown planning should know there are instances to remove<commit_after># Copyright 2015 Isotoma Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from touchdown.tests.aws import StubberTestCase
from touchdown.tests.fixtures.aws import LaunchConfigurationFixture
from touchdown.tests.stubs.aws import AutoScalingGroupStubber


class TestCreateAutoScalingGroupuration(StubberTestCase):

    def test_create_auto_scaling_group(self):
        goal = self.create_goal('apply')

        launch_config = self.fixtures.enter_context(LaunchConfigurationFixture(goal, self.aws))

        auto_scaling_group = self.fixtures.enter_context(AutoScalingGroupStubber(
            goal.get_service(
                self.aws.add_auto_scaling_group(
                    name='my-asg',
                    min_size=0,
                    max_size=0,
                    launch_configuration=launch_config,
                ),
                'apply',
            )
        ))

        auto_scaling_group.add_describe_auto_scaling_groups_empty_response()
        auto_scaling_group.add_create_auto_scaling_group()
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()
        goal.execute()

    def test_create_auto_scaling_group_wait_for_healthy(self):
        goal = self.create_goal('apply')

        launch_config = self.fixtures.enter_context(LaunchConfigurationFixture(goal, self.aws))

        auto_scaling_group = self.fixtures.enter_context(AutoScalingGroupStubber(
            goal.get_service(
                self.aws.add_auto_scaling_group(
                    name='my-asg',
                    min_size=1,
                    max_size=1,
                    launch_configuration=launch_config,
                ),
                'apply',
            )
        ))

        auto_scaling_group.add_describe_auto_scaling_groups_empty_response()
        auto_scaling_group.add_create_auto_scaling_group(
            min_size=1,
            max_size=1,
        )
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()
        auto_scaling_group.add_describe_auto_scaling_groups_one_response()

        # Now wait for an ec2 instance to be in a healthy state
        # First of all lets mock one starting...
        auto_scaling_group.add_describe_auto_scaling_groups_one_response(
            instances=[{'LifecycleState': 'Pending'}],
        )

        # And now its ready...
        auto_scaling_group.add_describe_auto_scaling_groups_one_response(
            instances=[{'LifecycleState': 'InService'}],
        )

        goal.execute()

    def test_create_auto_scaling_group_idempotent(self):
        goal = self.create_goal('apply')

        launch_config = self.fixtures.enter_context(LaunchConfigurationFixture(goal, self.aws))

        auto_scaling_group = self.fixtures.enter_context(AutoScalingGroupStubber(
            goal.get_service(
                self.aws.add_auto_scaling_group(
                    name='my-asg',
                    min_size=0,
                    max_size=0,
                    launch_configuration=launch_config,
                ),
                'apply',
            )
        ))

        auto_scaling_group.add_describe_auto_scaling_groups_one_response()

        self.assertEqual(len(list(goal.plan())), 0)
        self.assertEqual(len(goal.get_changes(auto_scaling_group.resource)), 0)


class TestDestroyAutoScalingGroupuration(StubberTestCase):

    def test_destroy_auto_scaling_group(self):
        goal = self.create_goal('destroy')

        auto_scaling_group = self.fixtures.enter_context(AutoScalingGroupStubber(
            goal.get_service(
                self.aws.add_auto_scaling_group(
                    name='my-asg',
                    min_size=0,
                    max_size=0,
                ),
                'destroy',
            )
        ))

        # Test stopping an ASG that has instances running
        auto_scaling_group.add_describe_auto_scaling_groups_one_response(
            instances=[{'LifecycleState': 'InService'}],
        )

{'fraction_non_alphanumeric': 0.07269824374667376, 'fraction_numerical': 0.004470463012240553, 'mean_word_length': 2.4838709677419355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3949201', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2520, 'n_words': 436}