|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from vagrant import Vagrant |
| 19 | +from unittest import TestCase |
| 20 | +from paramiko.config import SSHConfig |
| 21 | +from paramiko.client import SSHClient, AutoAddPolicy |
| 22 | +from fabric.api import env |
| 23 | +from envassert import file, detect |
| 24 | + |
| 25 | +from StringIO import StringIO |
| 26 | + |
| 27 | +from nose.plugins.attrib import attr |
| 28 | + |
| 29 | +import os.path |
| 30 | + |
| 31 | + |
| 32 | +_defaultVagrantDir = os.path.abspath(os.path.join( |
| 33 | + os.path.basename(__file__), '..', '..', '..', 'tools', 'vagrant', 'systemvm')) |
| 34 | + |
| 35 | + |
| 36 | +class SystemVM(object): |
| 37 | + def __init__(self, |
| 38 | + host='default', |
| 39 | + vagrantDir=None, |
| 40 | + controlVagrant=True): |
| 41 | + global _defaultVagrantDir |
| 42 | + self.host = host |
| 43 | + self._controlVagrant = controlVagrant |
| 44 | + if vagrantDir is None: |
| 45 | + vagrantDir = _defaultVagrantDir |
| 46 | + self._vagrant = Vagrant(root=vagrantDir) |
| 47 | + self._startedVagrant = False |
| 48 | + self._sshClient = None |
| 49 | + self._sshConfigStr = None |
| 50 | + self._sshConfig = None |
| 51 | + self._sshHostConfig = None |
| 52 | + |
| 53 | + def maybeUp(self): |
| 54 | + if not self._controlVagrant: |
| 55 | + return |
| 56 | + state = self._vagrant.status(vm_name=self.host)[0].state |
| 57 | + if state == Vagrant.NOT_CREATED: |
| 58 | + self._vagrant.up(vm_name=self.host) |
| 59 | + self._startedVagrant = True |
| 60 | + elif state in [Vagrant.POWEROFF, Vagrant.SAVED, Vagrant.ABORTED]: |
| 61 | + raise Exception( |
| 62 | + "SystemVM testing does not support resume(), do not use vagrant suspend/halt") |
| 63 | + elif state == Vagrant.RUNNING: |
| 64 | + self._startedVagrant = False |
| 65 | + else: |
| 66 | + raise Exception("Unrecognized vagrant state %s" % state) |
| 67 | + |
| 68 | + def maybeDestroy(self): |
| 69 | + if not self._controlVagrant or not self._startedVagrant: |
| 70 | + return |
| 71 | + self._vagrant.destroy(vm_name=self.host) |
| 72 | + if self._sshClient is not None: |
| 73 | + self._sshClient.close() |
| 74 | + |
| 75 | + def loadSshConfig(self): |
| 76 | + if self._sshConfig is None: |
| 77 | + self._sshConfigStr = self._vagrant.ssh_config(vm_name=self.host) |
| 78 | + configObj = StringIO(self._sshConfigStr) |
| 79 | + self._sshConfig = SSHConfig() |
| 80 | + # noinspection PyTypeChecker |
| 81 | + self._sshConfig.parse(configObj) |
| 82 | + self._sshHostConfig = self._sshConfig.lookup(self.host) |
| 83 | + |
| 84 | + @property |
| 85 | + def sshConfig(self): |
| 86 | + if self._sshConfig is None: |
| 87 | + self.loadSshConfig() |
| 88 | + return self._sshConfig |
| 89 | + |
| 90 | + @property |
| 91 | + def sshConfigStr(self): |
| 92 | + if self._sshConfigStr is None: |
| 93 | + self.loadSshConfig() |
| 94 | + return self._sshConfigStr |
| 95 | + |
| 96 | + @property |
| 97 | + def sshClient(self): |
| 98 | + if self._sshClient is None: |
| 99 | + self.loadSshConfig() |
| 100 | + self._sshClient = SSHClient() |
| 101 | + self._sshClient.set_missing_host_key_policy(AutoAddPolicy()) |
| 102 | + self._sshClient.connect(self.hostname, self.sshPort, self.sshUser, |
| 103 | + key_filename=self.sshKey, timeout=10) |
| 104 | + return self._sshClient |
| 105 | + |
| 106 | + @property |
| 107 | + def hostname(self): |
| 108 | + return self._sshHostConfig.get('hostname', self.host) |
| 109 | + |
| 110 | + @property |
| 111 | + def sshPort(self): |
| 112 | + return int(self._sshHostConfig.get('port', 22)) |
| 113 | + |
| 114 | + @property |
| 115 | + def sshUser(self): |
| 116 | + return self._sshHostConfig.get('user', 'root') |
| 117 | + |
| 118 | + @property |
| 119 | + def sshKey(self): |
| 120 | + return self._sshHostConfig.get('identityfile', '~/.ssh/id_rsa') |
| 121 | + |
| 122 | + |
| 123 | +class SystemVMTestCase(TestCase): |
| 124 | + @classmethod |
| 125 | + def setUpClass(cls): |
| 126 | + cls.systemvm = SystemVM() |
| 127 | + cls.systemvm.maybeUp() |
| 128 | + |
| 129 | + @classmethod |
| 130 | + def tearDownClass(cls): |
| 131 | + # noinspection PyUnresolvedReferences |
| 132 | + cls.systemvm.maybeDestroy() |
| 133 | + |
| 134 | + def setUp(self): |
| 135 | + self.sshClient = self.systemvm.sshClient |
| 136 | + # self._env_host_string_orig = env.host_string |
| 137 | + env.host_string = "%s:%s" % (self.systemvm.hostname, self.systemvm.sshPort) |
| 138 | + env.user = self.systemvm.sshUser |
| 139 | + env.port = self.systemvm.sshPort |
| 140 | + env.key_filename = self.systemvm.sshKey |
| 141 | + env.use_ssh_config = True |
| 142 | + env.abort_on_prompts = True |
| 143 | + env.command_timeout = 10 |
| 144 | + env.timeout = 5 |
| 145 | + env.platform_family = detect.detect() |
| 146 | + |
| 147 | + # this could break down when executing multiple test cases in parallel in the same python process |
| 148 | + # def tearDown(self): |
| 149 | + # env.host_string = self._env_host_string_orig |
0 commit comments