<filename>autoscale_cloudroast/test_repo/autoscale/system/group/test_group_servers.py
"""
Tests for `/groups/<groupId>/servers/` endpoint
"""
from cafe.drivers.unittest.decorators import tags

from test_repo.autoscale.fixtures import AutoscaleFixture


class ServersTests(AutoscaleFixture):
    """
    Group servers tests
    """

    def setUp(self):
        """
        Create scaling group with minEntities 1
        """
        self.groupid = self.autoscale_behaviors.create_scaling_group_min(
            gc_min_entities=1).entity.id

    def tearDown(self):
        """
        Delete the group
        """
        self.autoscale_client.delete_scaling_group(self.groupid, force='true')
        if hasattr(self, 'groupid2'):
            self.autoscale_client.delete_scaling_group(self.groupid2,
                                                       force='true')

    def assert_server_deleted(self, server_id):
        """
        Assert if given server is still in group
        """
        def check_deleted(time_elapsed):
            servers = self.get_servers_containing_given_name_on_tenant(
                group_id=self.groupid)
            if server_id in servers:
                self.fail('Server {} in group {} not deleted after {} seconds'
                          .format(server_id, self.groupid, time_elapsed))

        return self.autoscale_behaviors.retry(check_deleted, timeout=150,
                                              interval_time=15)

    @tags(speed='slow', convergence='yes')
    def test_delete_removes_and_replaces(self, replace=None):
        """
        `DELETE serverId` actually deletes the server and replaces with new
        server. This tests with optional replace argument as the same behavior
        can be tested with it.  By default, replace is not provided. This test
        also shows that server can be deleted with min servers
        """
        server_id = self.wait_for_expected_number_of_active_servers(
            self.groupid, 1)[0]
        resp = self.autoscale_client.delete_server(self.groupid, server_id,
                                                   replace)
        self.assertEqual(resp.status_code, 202,
                         'Delete server status is {}. Expected 202'.format(
                             resp.status_code))
        # Is server really deleted?
        self.assert_server_deleted(server_id)
        # New server replaced?
        self.wait_for_expected_group_state(self.groupid, 1)

    @tags(speed='slow', convergence='yes')
    def test_delete_removed_replaced_arg(self):
        """
        `DELETE serverId?replace=true` actually deletes the server and
        replaces with new server
        """
        self.test_delete_removes_and_replaces('true')

    @tags(speed='slow', convergence='yes')
    def test_delete_removed_not_replaced(self):
        """
        `DELETE serverId?replace=false` removes the sever and does not replace
        it
        """
        # Spin 1 more server
        policyid = self.autoscale_behaviors.create_policy_min(
            self.groupid, sp_change=1)['id']
        self.autoscale_client.execute_policy(self.groupid, policyid)
        # Delete 2nd server to check that any server can be deleted
        server_id = self.wait_for_expected_number_of_active_servers(
            self.groupid, 2)[1]
        resp = self.autoscale_client.delete_server(self.groupid, server_id,
                                                   replace='false')
        self.assertEqual(resp.status_code, 202,
                         'Delete server status is {}. Expected 202'.format(
                             resp.status_code))
        # Is server really deleted?
        self.assert_server_deleted(server_id)
        # New server not replaced?
        self.wait_for_expected_group_state(self.groupid, 1)

    @tags(convergence='yes')
    def test_delete_server_not_found(self):
        """
        `DELETE invalid_serverId` returns 404
        """
        # TODO: This may need to goto functional
        resp = self.autoscale_client.delete_server(self.groupid, 'junk')
        self.assertEqual(resp.status_code, 404,
                         'Delete server status is {}. Expected 404'.format(
                             resp.status_code))

    @tags(convergence='yes')
    def test_delete_server_not_found_in_diff_group(self):
        """
        `DELETE serverId` on server in same tenant but different group returns
        404
        """
        # Create another group and get server from there
        self.groupid2 = self.autoscale_behaviors.create_scaling_group_min(
            gc_min_entities=1).entity.id
        server_id = self.wait_for_expected_number_of_active_servers(
            self.groupid2, 1)[0]
        # Delete that server from first group
        resp = self.autoscale_client.delete_server(self.groupid, server_id)
        self.assertEqual(resp.status_code, 404,
                         'Delete server status is {}. Expected 404'.format(
                             resp.status_code))

    @tags(speed='slow', convergence='yes')
    def test_delete_below_min(self):
        """
        Calling `DELETE serverId` when number of servers are at minimum returns
        403
        """
        server_id = self.wait_for_expected_number_of_active_servers(
            self.groupid, 1)[0]
        resp = self.autoscale_client.delete_server(self.groupid, server_id,
                                                   replace='false')
        self.assertEqual(resp.status_code, 403,
                         'Delete server status is {}. Expected 403'.format(
                             resp.status_code))
        self.assertIn('CannotDeleteServerBelowMinError', resp.content)

    @tags(speed='slow', convergence='yes')
    def test_delete_server_invalid_replace_args(self):
        """
        `DELETE serverId?replace=bad` returns 400 with InvalidQueryArgument
        """
        server_id = self.wait_for_expected_number_of_active_servers(
            self.groupid, 1)[0]
        resp = self.autoscale_client.delete_server(self.groupid, server_id,
                                                   'bad')
        self.assertEqual(resp.status_code, 400,
                         'Delete server status is {}. Expected 400'.format(
                             resp.status_code))
        self.assertIn('InvalidQueryArgument', resp.content)

<filename>autoscale_cloudroast/test_repo/autoscale/system/group/test_group_servers.py
"""
Tests for `/groups/<groupId>/servers/` endpoint
"""
from cafe.drivers.unittest.decorators import tags

from test_repo.autoscale.fixtures import AutoscaleFixture


class ServersTests(AutoscaleFixture):
    """
    Group servers tests
    """

    def setUp(self):
        """
        Create scaling group with minEntities 1
        """
        self.groupid = self.autoscale_behaviors.create_scaling_group_min(
            gc_min_entities=1).entity.id

    def tearDown(self):
        """
        Delete the group
        """
        self.autoscale_client.delete_scaling_group(self.groupid, force='true')
        if hasattr(self, 'groupid2'):
            self.autoscale_client.delete_scaling_group(self.groupid2,
                                                       force='true')

    def assert_server_deleted(self, server_id):
        """
        Assert if given server is still in group
        """
        def check_deleted(time_elapsed):
            servers = self.get_servers_containing_given_name_on_tenant(
                group_id=self.groupid)
            if server_id in servers:
                self.fail('Server {} in group {} not deleted after {} seconds'
                          .format(server_id, self.groupid, time_elapsed))

        return self.autoscale_behaviors.retry(check_deleted, timeout=150,
                                              interval_time=15)

    @tags(speed='slow', convergence='yes')
    def test_delete_removes_and_replaces(self, replace=None):
        """
        `DELETE serverId` actually deletes the server and replaces with new
        server. This tests with optional replace argument as the same behavior
        can be tested with it.  By default, replace is not provided. This test
        also shows that server can be deleted with min servers
        """
        server_id = self.wait_for_expected_number_of_active_servers(
            self.groupid, 1)[0]
        resp = self.autoscale_client.delete_server(self.groupid, server_id,
                                                   replace)
        self.assertEqual(resp.status_code, 202,
                         'Delete server status is {}. Expected 202'.format(
                             resp.status_code))
        # Is server really deleted?
        self.assert_server_deleted(server_id)
        # New server replaced?
        self.wait_for_expected_group_state(self.groupid, 1)

    @tags(speed='slow', convergence='yes')
    def test_delete_removed_replaced_arg(self):
        """
        `DELETE serverId?replace=true` actually deletes the server and
        replaces with new server
        """
        self.test_delete_removes_and_replaces('true')

    @tags(speed='slow', convergence='yes')
    def test_delete_removed_not_replaced(self):
        """
        `DELETE serverId?replace=false` removes the sever and does not replace
        it
        """
        # Spin 1 more server
        policyid = self.autoscale_behaviors.create_policy_min(
            self.groupid, sp_change=1)['id']
        self.autoscale_client.execute_policy(self.groupid, policyid)
        # Delete 2nd server to check that any server can be deleted
        server_id = self.wait_for_expected_number_of_active_servers(
            self.groupid, 2)[1]
        resp = self.autoscale_client.delete_server(self.groupid, server_id,
                                                   replace='false')
        self.assertEqual(resp.status_code, 202,
                         'Delete server status is {}. Expected 202'.format(
                             resp.status_code))
        # Is server really deleted?
        self.assert_server_deleted(server_id)
        # New server not replaced?
        self.wait_for_expected_group_state(self.groupid, 1)

    @tags(convergence='yes')
    def test_delete_server_not_found(self):
        """
        `DELETE invalid_serverId` returns 404
        """
        # TODO: This may need to goto functional
        resp = self.autoscale_client.delete_server(self.groupid, 'junk')
        self.assertEqual(resp.status_code, 404,
                         'Delete server status is {}. Expected 404'.format(
                             resp.status_code))

    @tags(convergence='yes')
    def test_delete_server_not_found_in_diff_group(self):
        """
        `DELETE serverId` on server in same tenant but different group returns
        404
        """
        # Create another group and get server from there
        self.groupid2 = self.autoscale_behaviors.create_scaling_group_min(
            gc_min_entities=1).entity.id
        server_id = self.wait_for_expected_number_of_active_servers(
            self.groupid2, 1)[0]
        # Delete that server from first group
        resp = self.autoscale_client.delete_server(self.groupid, server_id)
        self.assertEqual(resp.status_code, 404,
                         'Delete server status is {}. Expected 404'.format(
                             resp.status_code))

    @tags(speed='slow', convergence='yes')
    def test_delete_below_min(self):
        """
        Calling `DELETE serverId` when number of servers are at minimum returns
        403
        """
        server_id = self.wait_for_expected_number_of_active_servers(
            self.groupid, 1)[0]
        resp = self.autoscale_client.delete_server(self.groupid, server_id,
                                                   replace='false')
        self.assertEqual(resp.status_code, 403,
                         'Delete server status is {}. Expected 403'.format(
                             resp.status_code))
        self.assertIn('CannotDeleteServerBelowMinError', resp.content)

    @tags(speed='slow', convergence='yes')
    def test_delete_server_invalid_replace_args(self):
        """
        `DELETE serverId?replace=bad` returns 400 with InvalidQueryArgument
        """
        server_id = self.wait_for_expected_number_of_active_servers(
            self.groupid, 1)[0]
        resp = self.autoscale_client.delete_server(self.groupid, server_id,
                                                   'bad')
        self.assertEqual(resp.status_code, 400,
                         'Delete server status is {}. Expected 400'.format(
                             resp.status_code))
        self.assertIn('InvalidQueryArgument', resp.content)

Tests for `/groups/<groupId>/servers/` endpoint Group servers tests Create scaling group with minEntities 1 Delete the group Assert if given server is still in group `DELETE serverId` actually deletes the server and replaces with new         server. This tests with optional replace argument as the same behavior         can be tested with it.  By default, replace is not provided. This test         also shows that server can be deleted with min servers # Is server really deleted? # New server replaced? `DELETE serverId?replace=true` actually deletes the server and         replaces with new server `DELETE serverId?replace=false` removes the sever and does not replace         it # Spin 1 more server # Delete 2nd server to check that any server can be deleted # Is server really deleted? # New server not replaced? `DELETE invalid_serverId` returns 404 # TODO: This may need to goto functional `DELETE serverId` on server in same tenant but different group returns         404 # Create another group and get server from there # Delete that server from first group Calling `DELETE serverId` when number of servers are at minimum returns         403 `DELETE serverId?replace=bad` returns 400 with InvalidQueryArgument