# Copyright 2016 IBM Corp.
#
# 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.

import time

from nova import test
from nova.tests import fixtures as nova_fixtures
from nova.tests.functional.api import client


class TestServerGet(test.TestCase):
    REQUIRES_LOCKING = True

    def setUp(self):
        super(TestServerGet, self).setUp()
        self.useFixture(nova_fixtures.RealPolicyFixture())
        self.useFixture(nova_fixtures.NeutronFixture(self))
        self.useFixture(nova_fixtures.GlanceFixture(self))
        self.useFixture(nova_fixtures.CastAsCallFixture(self))

        # The non-admin API client is fine to stay at 2.1 since it just creates
        # and deletes the server.
        api_fixture = self.useFixture(nova_fixtures.OSAPIFixture(
            api_version='v2.1'))
        self.api = api_fixture.api
        self.admin_api = api_fixture.admin_api
        # The admin API client needs to be at microversion 2.16 to exhibit the
        # regression.
        self.admin_api.microversion = '2.16'

        self.start_service('conductor')
        self.start_service('scheduler')
        self.compute = self.start_service('compute')

        self.image_id = self.api.get_images()[0]['id']
        self.flavor_id = self.api.get_flavors()[0]['id']

    def test_list_deleted_instances(self):
        """Regression test for bug #1548980.

        Before fixing this bug, listing deleted instances returned a 404
        because lazy-loading services from a deleted instance failed. Now
        we should be able to list the deleted instance and the host_state
        attribute should be "".
        """
        server = dict(name='server1',
                      imageRef=self.image_id,
                      flavorRef=self.flavor_id)
        server = self.api.post_server({'server': server})
        self.api.delete_server(server['id'])
        # Wait 30 seconds for it to be gone.
        for x in range(30):
            try:
                self.api.get_server(server['id'])
                time.sleep(1)
            except client.OpenStackApiNotFoundException:
                break
        else:
            self.fail('Timed out waiting to delete server: %s' % server['id'])
        servers = self.admin_api.get_servers(search_opts={'deleted': 1})
        self.assertEqual(1, len(servers))
        self.assertEqual(server['id'], servers[0]['id'])
        # host_status is returned in the 2.16 microversion and since the server
        # is deleted it should be the empty string
        self.assertEqual(0, len(servers[0]['host_status']))
