171 lines
6.9 KiB
Python
171 lines
6.9 KiB
Python
# Copyright 2013 Cloudwatt
|
|
# Copyright 2010 Jacob Kaplan-Moss
|
|
# Copyright 2011 OpenStack Foundation
|
|
# Copyright 2012 IBM Corp.
|
|
# 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.
|
|
|
|
import fixtures
|
|
import mock
|
|
import six
|
|
|
|
import novaclient.shell
|
|
from novaclient.tests import utils
|
|
from novaclient.tests.v3 import fakes
|
|
|
|
|
|
class ShellFixture(fixtures.Fixture):
|
|
def setUp(self):
|
|
super(ShellFixture, self).setUp()
|
|
self.shell = novaclient.shell.OpenStackComputeShell()
|
|
|
|
def tearDown(self):
|
|
# For some method like test_image_meta_bad_action we are
|
|
# testing a SystemExit to be thrown and object self.shell has
|
|
# no time to get instantatiated which is OK in this case, so
|
|
# we make sure the method is there before launching it.
|
|
if hasattr(self.shell, 'cs'):
|
|
self.shell.cs.clear_callstack()
|
|
super(ShellFixture, self).tearDown()
|
|
|
|
|
|
class ShellTest(utils.TestCase):
|
|
FAKE_ENV = {
|
|
'NOVA_USERNAME': 'username',
|
|
'NOVA_PASSWORD': 'password',
|
|
'NOVA_PROJECT_ID': 'project_id',
|
|
'OS_COMPUTE_API_VERSION': '3',
|
|
'NOVA_URL': 'http://no.where',
|
|
}
|
|
|
|
def setUp(self):
|
|
"""Run before each test."""
|
|
super(ShellTest, self).setUp()
|
|
|
|
for var in self.FAKE_ENV:
|
|
self.useFixture(fixtures.EnvironmentVariable(var,
|
|
self.FAKE_ENV[var]))
|
|
self.shell = self.useFixture(ShellFixture()).shell
|
|
|
|
self.useFixture(fixtures.MonkeyPatch(
|
|
'novaclient.client.get_client_class',
|
|
lambda *_: fakes.FakeClient))
|
|
|
|
@mock.patch('sys.stdout', new_callable=six.StringIO)
|
|
def run_command(self, cmd, mock_stdout):
|
|
if isinstance(cmd, list):
|
|
self.shell.main(cmd)
|
|
else:
|
|
self.shell.main(cmd.split())
|
|
return mock_stdout.getvalue()
|
|
|
|
def assert_called(self, method, url, body=None, **kwargs):
|
|
return self.shell.cs.assert_called(method, url, body, **kwargs)
|
|
|
|
def assert_called_anytime(self, method, url, body=None):
|
|
return self.shell.cs.assert_called_anytime(method, url, body)
|
|
|
|
def test_list_deleted(self):
|
|
self.run_command('list --deleted')
|
|
self.assert_called('GET', '/servers/detail?deleted=True')
|
|
|
|
def test_aggregate_list(self):
|
|
self.run_command('aggregate-list')
|
|
self.assert_called('GET', '/os-aggregates')
|
|
|
|
def test_aggregate_create(self):
|
|
self.run_command('aggregate-create test_name nova1')
|
|
body = {"aggregate": {"name": "test_name",
|
|
"availability_zone": "nova1"}}
|
|
self.assert_called('POST', '/os-aggregates', body, pos=-2)
|
|
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
|
|
|
def test_aggregate_delete_by_id(self):
|
|
self.run_command('aggregate-delete 1')
|
|
self.assert_called('DELETE', '/os-aggregates/1')
|
|
|
|
def test_aggregate_delete_by_name(self):
|
|
self.run_command('aggregate-delete test')
|
|
self.assert_called('DELETE', '/os-aggregates/1')
|
|
|
|
def test_aggregate_update_by_id(self):
|
|
self.run_command('aggregate-update 1 new_name')
|
|
body = {"aggregate": {"name": "new_name"}}
|
|
self.assert_called('PUT', '/os-aggregates/1', body, pos=-2)
|
|
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
|
|
|
def test_aggregate_update_by_name(self):
|
|
self.run_command('aggregate-update test new_name')
|
|
body = {"aggregate": {"name": "new_name"}}
|
|
self.assert_called('PUT', '/os-aggregates/1', body, pos=-2)
|
|
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
|
|
|
def test_aggregate_update_with_availability_zone_by_id(self):
|
|
self.run_command('aggregate-update 1 foo new_zone')
|
|
body = {"aggregate": {"name": "foo", "availability_zone": "new_zone"}}
|
|
self.assert_called('PUT', '/os-aggregates/1', body, pos=-2)
|
|
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
|
|
|
def test_aggregate_update_with_availability_zone_by_name(self):
|
|
self.run_command('aggregate-update test foo new_zone')
|
|
body = {"aggregate": {"name": "foo", "availability_zone": "new_zone"}}
|
|
self.assert_called('PUT', '/os-aggregates/1', body, pos=-2)
|
|
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
|
|
|
def test_aggregate_set_metadata_by_id(self):
|
|
self.run_command('aggregate-set-metadata 1 foo=bar delete_key')
|
|
body = {"set_metadata": {"metadata": {"foo": "bar",
|
|
"delete_key": None}}}
|
|
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
|
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
|
|
|
def test_aggregate_set_metadata_by_name(self):
|
|
self.run_command('aggregate-set-metadata test foo=bar delete_key')
|
|
body = {"set_metadata": {"metadata": {"foo": "bar",
|
|
"delete_key": None}}}
|
|
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
|
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
|
|
|
def test_aggregate_add_host_by_id(self):
|
|
self.run_command('aggregate-add-host 1 host1')
|
|
body = {"add_host": {"host": "host1"}}
|
|
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
|
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
|
|
|
def test_aggregate_add_host_by_name(self):
|
|
self.run_command('aggregate-add-host test host1')
|
|
body = {"add_host": {"host": "host1"}}
|
|
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
|
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
|
|
|
def test_aggregate_remove_host_by_id(self):
|
|
self.run_command('aggregate-remove-host 1 host1')
|
|
body = {"remove_host": {"host": "host1"}}
|
|
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
|
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
|
|
|
def test_aggregate_remove_host_by_name(self):
|
|
self.run_command('aggregate-remove-host test host1')
|
|
body = {"remove_host": {"host": "host1"}}
|
|
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
|
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
|
|
|
def test_aggregate_details_by_id(self):
|
|
self.run_command('aggregate-details 1')
|
|
self.assert_called('GET', '/os-aggregates/1')
|
|
|
|
def test_aggregate_details_by_name(self):
|
|
self.run_command('aggregate-details test')
|
|
self.assert_called('GET', '/os-aggregates')
|