# Copyright 2016 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.utils import data_utils
from tempest import config
from tempest.lib import exceptions as lib_exc
from tempest import test

CONF = config.CONF


class VolumesAdminNegativeTest(base.BaseV2ComputeAdminTest):

    @classmethod
    def skip_checks(cls):
        super(VolumesAdminNegativeTest, cls).skip_checks()
        if not CONF.service_available.cinder:
            skip_msg = ("%s skipped as Cinder is not available" % cls.__name__)
            raise cls.skipException(skip_msg)

    @classmethod
    def setup_clients(cls):
        super(VolumesAdminNegativeTest, cls).setup_clients()
        cls.servers_admin_client = cls.os_adm.servers_client

    @classmethod
    def resource_setup(cls):
        super(VolumesAdminNegativeTest, cls).resource_setup()
        cls.server = cls.create_test_server(wait_until='ACTIVE')

    @test.idempotent_id('309b5ecd-0585-4a7e-a36f-d2b2bf55259d')
    def test_update_attached_volume_with_nonexistent_volume_in_uri(self):
        volume = self.create_volume()
        nonexistent_volume = data_utils.rand_uuid()
        self.assertRaises(lib_exc.NotFound,
                          self.servers_admin_client.update_attached_volume,
                          self.server['id'], nonexistent_volume,
                          volumeId=volume['id'])

    @test.idempotent_id('7dcac15a-b107-46d3-a5f6-cb863f4e454a')
    def test_update_attached_volume_with_nonexistent_volume_in_body(self):
        volume = self.create_volume()
        self.attach_volume(self.server, volume)

        nonexistent_volume = data_utils.rand_uuid()
        self.assertRaises(lib_exc.BadRequest,
                          self.servers_admin_client.update_attached_volume,
                          self.server['id'], volume['id'],
                          volumeId=nonexistent_volume)

Add negative tests about update-volume API
Add negative tests about update-volume API

As the following part of API-WG guidline[1],

 If a request contains a reference to a nonexistent resource in the
 body (not URI), the code should be 400 Bad Request. Do not use 404
 NotFound because :rfc:`7231#section-6.5.4` (section 6.5.4) mentions
 the origin server did not find a current representation for the
 target resource for 404 and representation for the target resource
 means a URI

Nova should return a NotFound(404) on this first test case, but it
should return a BadRequest response(400) in this second case, because
the second volume id is specified in a request body.

[1]: https://github.com/openstack/api-wg/blob/master/guidelines/http.rst#failure-code-clarifications

Depends-On: Ib781b116f5af713d64b5880858cc4f81c3da3977
Related-Bug: #1629110
Change-Id: I409c4fd53e272f6b15fb2a34068e3d003317290e
