Browse Source

Update neutron-debug to use stevedore aliases

Updating neutron-debug as it is importing interface driver classes
directly and not using the load_driver_interface method.
Also adds unit tests for the neutron-debug tool.

Change-Id: Icf095b2182225e63e85d849d83c2cb36b8a39217
Closes-Bug: #1504536
tags/8.0.0.0b1
Martin Hickey 3 years ago
parent
commit
c4e0574da1
2 changed files with 142 additions and 1 deletions
  1. 2
    1
      neutron/debug/shell.py
  2. 140
    0
      neutron/tests/unit/debug/test_shell.py

+ 2
- 1
neutron/debug/shell.py View File

@@ -19,6 +19,7 @@ from oslo_config import cfg
19 19
 from oslo_utils import importutils
20 20
 
21 21
 from neutron.agent.common import config
22
+from neutron.agent.common import utils
22 23
 from neutron.agent.linux import interface
23 24
 from neutron.debug import debug_agent
24 25
 from neutronclient.common import exceptions as exc
@@ -75,7 +76,7 @@ class NeutronDebugShell(shell.NeutronShell):
75 76
         config.register_use_namespaces_opts_helper(cfg.CONF)
76 77
         cfg.CONF(['--config-file', self.options.config_file])
77 78
         config.setup_logging()
78
-        driver = importutils.import_object(cfg.CONF.interface_driver, cfg.CONF)
79
+        driver = utils.load_interface_driver(cfg.CONF)
79 80
         self.debug_agent = debug_agent.NeutronDebugAgent(cfg.CONF,
80 81
                                                          client,
81 82
                                                          driver)

+ 140
- 0
neutron/tests/unit/debug/test_shell.py View File

@@ -0,0 +1,140 @@
1
+# Copyright (c) 2015 IBM Corp.
2
+# All Rights Reserved.
3
+#
4
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5
+#    not use this file except in compliance with the License. You may obtain
6
+#    a copy of the License at
7
+#
8
+#         http://www.apache.org/licenses/LICENSE-2.0
9
+#
10
+#    Unless required by applicable law or agreed to in writing, software
11
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+#    License for the specific language governing permissions and limitations
14
+#    under the License.
15
+
16
+import argparse
17
+import logging
18
+import sys
19
+
20
+import fixtures
21
+import mock
22
+from neutronclient import shell as openstack_shell
23
+import six
24
+from testtools import matchers
25
+
26
+from neutron.debug import shell as debug_shell
27
+from neutron.tests import base
28
+
29
+
30
+class ShellTest(base.BaseTestCase):
31
+
32
+    def shell(self, argstr, check=False):
33
+        with mock.patch.dict('os.environ', clear=True):
34
+            with mock.patch('sys.stdout', new=six.moves.StringIO()) as \
35
+                    stdout_io:
36
+                with mock.patch('sys.stderr', new=six.moves.StringIO()) as \
37
+                        stderr_io:
38
+                    try:
39
+                        _shell = debug_shell.NeutronDebugShell(
40
+                                openstack_shell.NEUTRON_API_VERSION)
41
+                        _shell.run(argstr.split())
42
+                    except SystemExit:
43
+                        exc_type, exc_value, exc_traceback = sys.exc_info()
44
+                        self.assertEqual(0, exc_value.code)
45
+        return stdout_io.getvalue(), stderr_io.getvalue()
46
+
47
+    def test_run_unknown_command(self):
48
+        self.useFixture(fixtures.FakeLogger(level=logging.DEBUG))
49
+        stdout, stderr = self.shell('fake', check=True)
50
+        self.assertFalse(stdout)
51
+
52
+    def test_help(self):
53
+        help_text, stderr = self.shell('help')
54
+        self.assertFalse(stderr)
55
+
56
+    def test_bash_completion(self):
57
+        required = '.*os_user_domain_id.*'
58
+        bash_completion, stderr = self.shell('bash-completion')
59
+        self.assertThat(
60
+            bash_completion,
61
+            matchers.MatchesRegex(required))
62
+        self.assertFalse(stderr)
63
+
64
+    def test_help_on_subcommand(self):
65
+        stdout, stderr = self.shell('help probe-list')
66
+        self.assertFalse(stderr)
67
+
68
+    def test_help_command(self):
69
+        stdout, stderr = self.shell('help network-create')
70
+        self.assertFalse(stderr)
71
+
72
+    def test_bash_completion_command(self):
73
+        bash_completion, stderr = self.shell('neutron bash-completion')
74
+        self.assertFalse(stderr)
75
+
76
+    def test_unknown_auth_strategy(self):
77
+        self.useFixture(fixtures.FakeLogger(level=logging.DEBUG))
78
+        stdout, stderr = self.shell('--os-auth-strategy fake probe-list')
79
+        self.assertFalse(stdout)
80
+
81
+    def test_build_option_parser(self):
82
+        neutron_shell = debug_shell.NeutronDebugShell(
83
+                openstack_shell.NEUTRON_API_VERSION)
84
+        result = neutron_shell.build_option_parser('descr', '2.0')
85
+        self.assertTrue(isinstance(result, argparse.ArgumentParser))
86
+
87
+    def test_endpoint_option(self):
88
+        shell = debug_shell.NeutronDebugShell(
89
+                openstack_shell.NEUTRON_API_VERSION)
90
+        parser = shell.build_option_parser('descr', '2.0')
91
+
92
+        # Neither $OS_ENDPOINT_TYPE nor --os-endpoint-type
93
+        namespace = parser.parse_args([])
94
+        self.assertEqual('publicURL', namespace.os_endpoint_type)
95
+
96
+        # --endpoint-type but not $OS_ENDPOINT_TYPE
97
+        namespace = parser.parse_args(['--os-endpoint-type=admin'])
98
+        self.assertEqual('admin', namespace.os_endpoint_type)
99
+
100
+    def test_endpoint_environment_variable(self):
101
+        fixture = fixtures.EnvironmentVariable("OS_ENDPOINT_TYPE",
102
+                                               "public")
103
+        self.useFixture(fixture)
104
+
105
+        shell = debug_shell.NeutronDebugShell(
106
+                openstack_shell.NEUTRON_API_VERSION)
107
+        parser = shell.build_option_parser('descr', '2.0')
108
+
109
+        # $OS_ENDPOINT_TYPE but not --endpoint-type
110
+        namespace = parser.parse_args([])
111
+        self.assertEqual("public", namespace.os_endpoint_type)
112
+
113
+        # --endpoint-type and $OS_ENDPOINT_TYPE
114
+        namespace = parser.parse_args(['--endpoint-type=admin'])
115
+        self.assertEqual('admin', namespace.endpoint_type)
116
+
117
+    def test_timeout_option(self):
118
+        shell = debug_shell.NeutronDebugShell(
119
+                openstack_shell.NEUTRON_API_VERSION)
120
+        parser = shell.build_option_parser('descr', '2.0')
121
+
122
+        # Neither $OS_ENDPOINT_TYPE nor --endpoint-type
123
+        namespace = parser.parse_args([])
124
+        self.assertIsNone(namespace.http_timeout)
125
+
126
+        # --endpoint-type but not $OS_ENDPOINT_TYPE
127
+        namespace = parser.parse_args(['--http-timeout=50'])
128
+        self.assertEqual(50, namespace.http_timeout)
129
+
130
+    def test_timeout_environment_variable(self):
131
+        fixture = fixtures.EnvironmentVariable("OS_NETWORK_TIMEOUT",
132
+                                               "50")
133
+        self.useFixture(fixture)
134
+
135
+        shell = debug_shell.NeutronDebugShell(
136
+                openstack_shell.NEUTRON_API_VERSION)
137
+        parser = shell.build_option_parser('descr', '2.0')
138
+
139
+        namespace = parser.parse_args([])
140
+        self.assertEqual(50, namespace.http_timeout)

Loading…
Cancel
Save