Browse Source

Update WinRM config options

A new config group is being added, allowing specifying WinRM
credentials to be used when accessing Hyper-V hosts. This will
also allow setting a transport type other than 'plaintext'.

At the same time, this change adds a new helper method that may
be used in order to run commands on the Hyper-V hosts.

Change-Id: I5852154d8275ebd36785bcf56899da3257f1a5a8
Alexandru Muresan 1 year ago
parent
commit
ef51813201

+ 34
- 5
oswin_tempest_plugin/clients/wsman.py View File

@@ -13,24 +13,39 @@
13 13
 #    License for the specific language governing permissions and limitations
14 14
 #    under the License.
15 15
 
16
+import os
17
+
16 18
 from oslo_log import log as logging
17 19
 from winrm import protocol
18 20
 
21
+from oswin_tempest_plugin import config
19 22
 from oswin_tempest_plugin import exceptions
20 23
 
21 24
 LOG = logging.getLogger(__name__)
25
+CONF = config.CONF
22 26
 
23 27
 protocol.Protocol.DEFAULT_TIMEOUT = "PT3600S"
24 28
 
25 29
 
26
-def run_wsman_cmd(host, username, password, cmd, fail_on_error=False):
30
+def run_wsman_cmd(host, cmd, username, password=None,
31
+                  cert_pem_path=None, cert_key_pem_path=None,
32
+                  transport_method='plaintext', fail_on_error=True):
27 33
     url = 'https://%s:5986/wsman' % host
34
+
35
+    if transport_method == 'ssl':
36
+        if not (os.path.exists(cert_pem_path) and
37
+                os.path.exists(cert_key_pem_path)):
38
+            raise exceptions.WSManException('Could not find certificate path '
39
+                                            'or certificate key path.')
40
+
28 41
     LOG.debug('Connecting to: %s', host)
29 42
     p = protocol.Protocol(endpoint=url,
30
-                          transport='plaintext',
43
+                          transport=transport_method,
31 44
                           server_cert_validation='ignore',
32 45
                           username=username,
33
-                          password=password)
46
+                          password=password,
47
+                          cert_pem=cert_pem_path,
48
+                          cert_key_pem=cert_key_pem_path)
34 49
 
35 50
     shell_id = p.open_shell()
36 51
     LOG.debug('Running command on host %(host)s: %(cmd)s',
@@ -54,7 +69,21 @@ def run_wsman_cmd(host, username, password, cmd, fail_on_error=False):
54 69
     return (std_out, std_err, return_code)
55 70
 
56 71
 
57
-def run_wsman_ps(host, username, password, cmd, fail_on_error=False):
72
+def run_wsman_ps(host, cmd, username, password, cert_pem_path=None,
73
+                 cert_key_pem_path=None, transport='plaintext',
74
+                 fail_on_error=True):
75
+
58 76
     cmd = ("powershell -NonInteractive -ExecutionPolicy RemoteSigned "
59 77
            "-Command \"%s\"" % cmd)
60
-    return run_wsman_cmd(host, username, password, cmd, fail_on_error)
78
+    return run_wsman_cmd(host, cmd, username, password, cert_pem_path,
79
+                         cert_key_pem_path, fail_on_error)
80
+
81
+
82
+def run_hv_host_wsman_ps(host, cmd, fail_on_error=True):
83
+    return run_wsman_ps(
84
+        host, cmd,
85
+        username=CONF.hyperv_host_auth.username,
86
+        password=CONF.hyperv_host_auth.password,
87
+        cert_pem_path=CONF.hyperv_host_auth.cert_pem_path,
88
+        cert_key_pem_path=CONF.hyperv_host_auth.cert_key_pem_path,
89
+        transport='plaintext', fail_on_error=fail_on_error)

+ 24
- 5
oswin_tempest_plugin/config.py View File

@@ -40,11 +40,6 @@ HyperVGroup = [
40 40
     cfg.BoolOpt('cluster_enabled',
41 41
                 default=False,
42 42
                 help="The compute nodes are joined into a Hyper-V Cluster."),
43
-    cfg.StrOpt('username',
44
-               help="The username of the Hyper-V hosts."),
45
-    cfg.StrOpt('password',
46
-               secret=True,
47
-               help='The password of the Hyper-V hosts.'),
48 43
     cfg.IntOpt('failover_timeout',
49 44
                default=120,
50 45
                help='The maximum amount of time to wait for a failover to '
@@ -54,9 +49,33 @@ HyperVGroup = [
54 49
                help='The amount of time to wait between failover checks.'),
55 50
 ]
56 51
 
52
+hyperv_host_auth_group = cfg.OptGroup(name='hyperv_host_auth',
53
+                                      title='Hyper-V host '
54
+                                            'authentication options')
55
+
56
+hyperv_host_auth_opts = [
57
+    cfg.StrOpt('username',
58
+               help="The username of the Hyper-V hosts."),
59
+    cfg.StrOpt('password',
60
+               secret=True,
61
+               help='The password of the Hyper-V hosts.'),
62
+    cfg.StrOpt('cert_pem_path',
63
+               default=None,
64
+               help='SSL certificate for WinRM remote PS connection.'),
65
+    cfg.StrOpt('cert_key_pem_path',
66
+               default=None,
67
+               help='SSL key paired with cert_pem_path for WinRM remote PS '
68
+                    'connection.'),
69
+    cfg.StrOpt('transport_method',
70
+               default='plaintext',
71
+               choices=('ssl', 'ntlm', 'plaintext'),
72
+               help='The method that should be used to establish a connection '
73
+                    'to a Hyper-V host.')
74
+]
57 75
 
58 76
 _opts = [
59 77
     (hyperv_group, HyperVGroup),
78
+    (hyperv_host_auth_group, hyperv_host_auth_opts),
60 79
 ]
61 80
 
62 81
 

+ 10
- 10
oswin_tempest_plugin/tests/scenario/test_cluster.py View File

@@ -63,12 +63,14 @@ class HyperVClusterTest(test_base.TestBase,
63 63
         super(HyperVClusterTest, cls).skip_checks()
64 64
 
65 65
         # check if the cluster Tests can be run.
66
-        conf_opts = ['cluster_enabled', 'username', 'password']
67
-        for conf_opt in conf_opts:
68
-            if not getattr(CONF.hyperv, conf_opt):
69
-                msg = ('The config option "hyperv.%s" has not been set. '
70
-                       'Skipping.' % conf_opt)
71
-                raise cls.skipException(msg)
66
+        if not CONF.hyperv.cluster_enabled:
67
+            msg = 'Hyper-V cluster tests are disabled.'
68
+            raise cls.skipException(msg)
69
+
70
+        if not CONF.hyperv_host_auth.username:
71
+            msg = ('No Hyper-V host username has been provided. '
72
+                   'Skipping cluster tests.')
73
+            raise cls.skipException(msg)
72 74
 
73 75
         if not CONF.compute.min_compute_nodes >= 2:
74 76
             msg = 'Expected at least 2 compute nodes.'
@@ -85,11 +87,9 @@ class HyperVClusterTest(test_base.TestBase,
85 87
         # succeed. On the 2nd failure, the VM will failover to another
86 88
         # node. Also, there needs to be a delay between commands, so the
87 89
         # original failover has time to finish.
88
-        wsman.run_wsman_ps(host_ip, CONF.hyperv.username,
89
-                           CONF.hyperv.password, cmd, True)
90
+        wsman.run_hv_host_wsman_ps(host_ip, cmd)
90 91
         time.sleep(CONF.hyperv.failover_sleep_interval)
91
-        wsman.run_wsman_ps(host_ip, CONF.hyperv.username,
92
-                           CONF.hyperv.password, cmd, True)
92
+        wsman.run_hv_host_wsman_ps(host_ip, cmd)
93 93
 
94 94
     def _wait_for_failover(self, server, original_host):
95 95
         """Waits for the given server to failover to another host.

Loading…
Cancel
Save