# Copyright 2013 NEC Corporation
# All Rights Reserved.
#
#    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 tempest.api.compute import base
from tempest.common import waiters
from tempest import test


class InstanceActionsTestJSON(base.BaseV2ComputeTest):

    @classmethod
    def setup_clients(cls):
        super(InstanceActionsTestJSON, cls).setup_clients()
        cls.client = cls.servers_client

    @classmethod
    def resource_setup(cls):
        super(InstanceActionsTestJSON, cls).resource_setup()
        server = cls.create_test_server(wait_until='ACTIVE')
        cls.request_id = server.response['x-compute-request-id']
        cls.server_id = server['id']

    @test.idempotent_id('77ca5cc5-9990-45e0-ab98-1de8fead201a')
    def test_list_instance_actions(self):
        # List actions of the provided server
        self.client.reboot_server(self.server_id, type='HARD')
        waiters.wait_for_server_status(self.client, self.server_id, 'ACTIVE')

        body = (self.client.list_instance_actions(self.server_id)
                ['instanceActions'])
        self.assertTrue(len(body) == 2, str(body))
        self.assertTrue(any([i for i in body if i['action'] == 'create']))
        self.assertTrue(any([i for i in body if i['action'] == 'reboot']))

    @test.idempotent_id('aacc71ca-1d70-4aa5-bbf6-0ff71470e43c')
    def test_get_instance_action(self):
        # Get the action details of the provided server
        body = self.client.show_instance_action(
            self.server_id, self.request_id)['instanceAction']
        self.assertEqual(self.server_id, body['instance_uuid'])
        self.assertEqual('create', body['action'])

@@ -1612,20 +1612,21 @@
 f.assert
-True
+Equal
 (len(bod
@@ -1627,19 +1627,17 @@
 en(body)
- ==
+,
  2, str(
@@ -1666,126 +1666,86 @@
 sert
-True(any(%5Bi for i in body if i%5B'action'%5D == 'create'%5D))%0A        self.assertTrue(any(%5Bi for i in body if i%5B'action'%5D ==
+Equal(sorted(%5Bi%5B'action'%5D for i in body%5D),%0A                         %5B'create',
  're
@@ -1751,17 +1751,16 @@
 eboot'%5D)
-)
 %0A%0A    @t
@@ -2085,24 +2085,24 @@
 nce_uuid'%5D)%0A
-
         self
@@ -2136,12 +2136,892 @@
 %5B'action'%5D)%0A
+%0A%0Aclass InstanceActionsV221TestJSON(base.BaseV2ComputeTest):%0A%0A    min_microversion = '2.21'%0A    max_microversion = 'latest'%0A%0A    @classmethod%0A    def setup_clients(cls):%0A        super(InstanceActionsV221TestJSON, cls).setup_clients()%0A        cls.client = cls.servers_client%0A%0A    @test.idempotent_id('0a0f85d4-10fa-41f6-bf80-a54fb4aa2ae1')%0A    def test_get_list_deleted_instance_actions(self):%0A%0A        # List actions of the deleted server%0A        server = self.create_test_server(wait_until='ACTIVE')%0A        self.client.delete_server(server%5B'id'%5D)%0A        waiters.wait_for_server_termination(self.client, server%5B'id'%5D)%0A        body = (self.client.list_instance_actions(server%5B'id'%5D)%0A                %5B'instanceActions'%5D)%0A        self.assertEqual(len(body), 2, str(body))%0A        self.assertEqual(sorted(%5Bi%5B'action'%5D for i in body%5D),%0A                         %5B'create', 'delete'%5D)%0A

Test case with list-instance-actions for deleted server