|
| 1 | +# Copyright 2016 Huawei, Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +# |
| 15 | + |
| 16 | +import mock |
| 17 | + |
| 18 | +from openstackclient.compute.v2 import console |
| 19 | +from openstackclient.tests.compute.v2 import fakes as compute_fakes |
| 20 | + |
| 21 | + |
| 22 | +class TestConsole(compute_fakes.TestComputev2): |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + super(TestConsole, self).setUp() |
| 26 | + self.servers_mock = self.app.client_manager.compute.servers |
| 27 | + self.servers_mock.reset_mock() |
| 28 | + |
| 29 | + |
| 30 | +class TestConsoleUrlShow(TestConsole): |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + super(TestConsoleUrlShow, self).setUp() |
| 34 | + fake_console_data = {'remote_console': {'url': 'http://localhost', |
| 35 | + 'protocol': 'fake_protocol', |
| 36 | + 'type': 'fake_type'}} |
| 37 | + methods = { |
| 38 | + 'get_vnc_console': fake_console_data, |
| 39 | + 'get_spice_console': fake_console_data, |
| 40 | + 'get_serial_console': fake_console_data, |
| 41 | + 'get_rdp_console': fake_console_data, |
| 42 | + 'get_mks_console': fake_console_data, |
| 43 | + } |
| 44 | + self.fake_server = compute_fakes.FakeServer.create_one_server( |
| 45 | + methods=methods) |
| 46 | + self.servers_mock.get.return_value = self.fake_server |
| 47 | + |
| 48 | + self.columns = ( |
| 49 | + 'protocol', |
| 50 | + 'type', |
| 51 | + 'url', |
| 52 | + ) |
| 53 | + self.data = ( |
| 54 | + fake_console_data['remote_console']['protocol'], |
| 55 | + fake_console_data['remote_console']['type'], |
| 56 | + fake_console_data['remote_console']['url'] |
| 57 | + ) |
| 58 | + |
| 59 | + self.cmd = console.ShowConsoleURL(self.app, None) |
| 60 | + |
| 61 | + def test_console_url_show_by_default(self): |
| 62 | + arglist = [ |
| 63 | + 'foo_vm', |
| 64 | + ] |
| 65 | + verifylist = [ |
| 66 | + ('url_type', 'novnc'), |
| 67 | + ('server', 'foo_vm'), |
| 68 | + ] |
| 69 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 70 | + columns, data = self.cmd.take_action(parsed_args) |
| 71 | + self.fake_server.get_vnc_console.assert_called_once_with('novnc') |
| 72 | + self.assertEqual(self.columns, columns) |
| 73 | + self.assertEqual(self.data, data) |
| 74 | + |
| 75 | + def test_console_url_show_with_novnc(self): |
| 76 | + arglist = [ |
| 77 | + '--novnc', |
| 78 | + 'foo_vm', |
| 79 | + ] |
| 80 | + verifylist = [ |
| 81 | + ('url_type', 'novnc'), |
| 82 | + ('server', 'foo_vm'), |
| 83 | + ] |
| 84 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 85 | + columns, data = self.cmd.take_action(parsed_args) |
| 86 | + self.fake_server.get_vnc_console.assert_called_once_with('novnc') |
| 87 | + self.assertEqual(self.columns, columns) |
| 88 | + self.assertEqual(self.data, data) |
| 89 | + |
| 90 | + def test_console_url_show_with_xvpvnc(self): |
| 91 | + arglist = [ |
| 92 | + '--xvpvnc', |
| 93 | + 'foo_vm', |
| 94 | + ] |
| 95 | + verifylist = [ |
| 96 | + ('url_type', 'xvpvnc'), |
| 97 | + ('server', 'foo_vm'), |
| 98 | + ] |
| 99 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 100 | + columns, data = self.cmd.take_action(parsed_args) |
| 101 | + self.fake_server.get_vnc_console.assert_called_once_with('xvpvnc') |
| 102 | + self.assertEqual(self.columns, columns) |
| 103 | + self.assertEqual(self.data, data) |
| 104 | + |
| 105 | + def test_console_url_show_with_spice(self): |
| 106 | + arglist = [ |
| 107 | + '--spice', |
| 108 | + 'foo_vm', |
| 109 | + ] |
| 110 | + verifylist = [ |
| 111 | + ('url_type', 'spice-html5'), |
| 112 | + ('server', 'foo_vm'), |
| 113 | + ] |
| 114 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 115 | + columns, data = self.cmd.take_action(parsed_args) |
| 116 | + self.fake_server.get_spice_console.assert_called_once_with( |
| 117 | + 'spice-html5') |
| 118 | + self.assertEqual(self.columns, columns) |
| 119 | + self.assertEqual(self.data, data) |
| 120 | + |
| 121 | + def test_console_url_show_compatible(self): |
| 122 | + methods = { |
| 123 | + 'get_vnc_console': {'console': {'url': 'http://localhost', |
| 124 | + 'type': 'fake_type'}}, |
| 125 | + } |
| 126 | + old_fake_server = compute_fakes.FakeServer.create_one_server( |
| 127 | + methods=methods) |
| 128 | + old_columns = ( |
| 129 | + 'type', |
| 130 | + 'url', |
| 131 | + ) |
| 132 | + old_data = ( |
| 133 | + methods['get_vnc_console']['console']['type'], |
| 134 | + methods['get_vnc_console']['console']['url'] |
| 135 | + ) |
| 136 | + arglist = [ |
| 137 | + 'foo_vm', |
| 138 | + ] |
| 139 | + verifylist = [ |
| 140 | + ('url_type', 'novnc'), |
| 141 | + ('server', 'foo_vm'), |
| 142 | + ] |
| 143 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 144 | + with mock.patch.object(self.servers_mock, 'get', |
| 145 | + return_value=old_fake_server): |
| 146 | + columns, data = self.cmd.take_action(parsed_args) |
| 147 | + old_fake_server.get_vnc_console.assert_called_once_with('novnc') |
| 148 | + self.assertEqual(old_columns, columns) |
| 149 | + self.assertEqual(old_data, data) |
0 commit comments