Chetan Gaonker | c1a4c8a | 2017-04-13 00:24:44 +0000 | [diff] [blame] | 1 | import os |
| 2 | import shutil |
| 3 | import re |
| 4 | from novaclient import client as nova_client |
| 5 | from SSHTestAgent import SSHTestAgent |
| 6 | from CordTestUtils import * |
| 7 | from CordTestUtils import log_test as log |
| 8 | |
| 9 | log.setLevel('INFO') |
| 10 | |
| 11 | class OnboardingServiceUtils(object): |
| 12 | |
| 13 | @classmethod |
| 14 | def setUp(cls): |
| 15 | pass |
| 16 | |
| 17 | @classmethod |
| 18 | def tearDown(cls): |
| 19 | pass |
| 20 | |
| 21 | ''' |
| 22 | @method: get_nova_credentials_v2 |
| 23 | @Description: Get nova credentials |
| 24 | @params: |
| 25 | returns credential from env |
| 26 | ''' |
| 27 | @classmethod |
| 28 | def get_nova_credentials_v2(cls): |
| 29 | credential = {} |
| 30 | credential['username'] = os.environ['OS_USERNAME'] |
| 31 | credential['api_key'] = os.environ['OS_PASSWORD'] |
| 32 | credential['auth_url'] = os.environ['OS_AUTH_URL'] |
| 33 | credential['project_id'] = os.environ['OS_TENANT_NAME'] |
| 34 | return credential |
| 35 | |
| 36 | ''' |
| 37 | @method: get_compute_nodes |
| 38 | @Description: Get the list of compute nodes |
| 39 | @params: |
| 40 | returns node list |
| 41 | ''' |
| 42 | @classmethod |
| 43 | def get_compute_nodes(cls): |
| 44 | credentials = cls.get_nova_credentials_v2() |
| 45 | nvclient = nova_client.Client('2', **credentials) |
| 46 | return nvclient.hypervisors.list() |
| 47 | |
| 48 | ''' |
| 49 | @method: get_exampleservices |
| 50 | @Description: Get list of exampleservice's running in compute node |
| 51 | @params: status of exampleservice |
| 52 | returns exampleservice wrappers |
| 53 | ''' |
| 54 | @classmethod |
| 55 | def get_exampleservices(cls, active = True): |
| 56 | credentials = cls.get_nova_credentials_v2() |
| 57 | nvclient = nova_client.Client('2', **credentials) |
| 58 | exampleservices = nvclient.servers.list(search_opts = {'all_tenants': 1}) |
| 59 | if active is True: |
| 60 | exampleservices = filter(lambda exampleservice: exampleservice.status == 'ACTIVE', exampleservices) |
| 61 | exampleservice_wrappers = [] |
| 62 | for exampleservice in exampleservices: |
| 63 | exampleservice_wrappers.append(ExampleSeviceWrapper(exampleservice)) |
| 64 | return exampleservice_wrappers |
| 65 | |
| 66 | ''' |
| 67 | @method: health_check |
| 68 | @Description: Check if exampleservices are reachable |
| 69 | @params: |
| 70 | returns True |
| 71 | ''' |
| 72 | @classmethod |
| 73 | def health_check(cls): |
| 74 | '''Returns 0 if all active exampleservices are reachable through the compute node''' |
| 75 | exampleservices = cls.get_exampleservices() |
| 76 | exampleservice_status = [] |
| 77 | for exampleservice in exampleservices: |
| 78 | exampleservice_status.append(exampleservice.get_health()) |
| 79 | unreachable = filter(lambda st: st == False, exampleservice_status) |
| 80 | return len(unreachable) == 0 |
| 81 | |
Chetan Gaonker | 5225881 | 2017-05-03 00:40:46 +0000 | [diff] [blame] | 82 | def make_veth_pairs(self): |
| 83 | |
| 84 | def check_iface(iface): |
| 85 | return os.system('ip link show {}'.format(iface)) == 0 |
| 86 | |
| 87 | def make_veth(iface): |
| 88 | os.system('ip link add type veth') |
| 89 | os.system('ip link set {} up'.format(iface)) |
| 90 | peer = iface[:len('veth')] + str(int(iface[len('veth'):]) + 1) |
| 91 | os.system('ip link set {} up'.format(peer)) |
| 92 | assert has_iface(iface) |
| 93 | |
| 94 | for iface_number in (0, 2): |
| 95 | iface = 'veth{}'.format(iface_number) |
| 96 | if not check_iface(iface): |
| 97 | make_veth(iface) |
| 98 | yield asleep(2) |
| 99 | |
| 100 | def source_env(self): |
| 101 | a_dir = os.path.abspath(os.path.dirname(__file__)) |
| 102 | res = os.system('cd {}'.format(a_dir)) |
| 103 | assert res == 0 |
| 104 | |
| 105 | # set the env |
| 106 | command = ['bash', '-c', '. env.sh'] |
| 107 | proc = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 108 | stderr=subprocess.PIPE) |
| 109 | |
| 110 | if proc.wait() != 0: |
| 111 | err_msg = "Failed to source the environment'" |
| 112 | raise RuntimeError(err_msg) |
| 113 | |
| 114 | env = os.environ.copy() |
| 115 | return env |
| 116 | |
Chetan Gaonker | c685393 | 2017-04-24 22:16:37 +0000 | [diff] [blame] | 117 | class ExampleSeviceWrapper(object): |
Chetan Gaonker | c1a4c8a | 2017-04-13 00:24:44 +0000 | [diff] [blame] | 118 | |
| 119 | def __init__(self, exampleservice): |
| 120 | self.exampleservice = exampleservice |
| 121 | self.name = self.exampleservice.name |
| 122 | self.compute_node = self.get_compute_node() |
| 123 | self.ip = self.get_ip() |
| 124 | |
| 125 | ''' |
| 126 | @method: get_compute_node |
| 127 | @Description: |
| 128 | @params: |
| 129 | returns compute node name |
| 130 | ''' |
| 131 | def get_compute_node(self): |
| 132 | return self.exampleservice._info['OS-EXT-SRV-ATTR:hypervisor_hostname'] |
| 133 | |
| 134 | ''' |
| 135 | @method: get_ip |
| 136 | @Description: |
| 137 | @params: |
| 138 | returns ip of network |
| 139 | ''' |
| 140 | def get_ip(self): |
| 141 | if 'management' in self.exampleservice.networks: |
| 142 | ips = self.exampleservice.networks['management'] |
| 143 | if len(ips) > 0: |
| 144 | return ips[0] |
| 145 | return None |
| 146 | |
| 147 | ''' |
| 148 | @method: run_cmd_compute |
| 149 | @Description: |
| 150 | @params: |
| 151 | returns Status & output |
| 152 | ''' |
| 153 | def run_cmd_compute(self, cmd, timeout = 5): |
| 154 | ssh_agent = SSHTestAgent(self.compute_node) |
| 155 | st, output = ssh_agent.run_cmd(cmd, timeout = timeout) |
| 156 | if st == True and output: |
| 157 | output = output.strip() |
| 158 | else: |
| 159 | output = None |
| 160 | |
| 161 | return st, output |
| 162 | |
| 163 | ''' |
| 164 | @method: get_health |
| 165 | @Description: |
| 166 | @params: |
| 167 | returns Status |
| 168 | ''' |
| 169 | def get_health(self): |
| 170 | if self.ip is None: |
| 171 | return True |
| 172 | cmd = 'ping -c 1 {}'.format(self.ip) |
| 173 | log.info('Pinging ONBOARDED SERVICE %s at IP %s' %(self.name, self.ip)) |
| 174 | st, _ = self.run_cmd_compute(cmd) |
| 175 | log.info('ONBOARDED SERVICE %s at IP %s is %s' %(self.name, self.ip, 'reachable' if st == True else 'unreachable')) |
| 176 | return st |
| 177 | |
| 178 | ''' |
| 179 | @method: check_access |
| 180 | @Description: validates access |
| 181 | @params: |
| 182 | returns Status |
| 183 | ''' |
| 184 | def check_access(self): |
| 185 | if self.ip is None: |
| 186 | return True |
| 187 | ssh_agent = SSHTestAgent(self.compute_node) |
| 188 | st, _ = ssh_agent.run_cmd('ls', timeout=10) |
| 189 | if st == False: |
| 190 | log.error('Compute node at %s is not accessible' %(self.compute_node)) |
| 191 | return st |
| 192 | log.info('Checking if ONBOARDING SERVICE at %s is accessible from compute node %s' %(self.ip, self.compute_node)) |
| 193 | st, _ = ssh_agent.run_cmd('ssh {} ls'.format(self.ip), timeout=30) |
| 194 | if st == True: |
| 195 | log.info('OK') |
| 196 | return st |
| 197 | |
| 198 | ''' |
| 199 | @method: Validate services |
| 200 | @Description: This validates if expected service is running in example service VM |
| 201 | @params: |
| 202 | returns Status |
| 203 | ''' |
| 204 | def validate_service_in_vm(self): |
| 205 | if self.ip is None: |
| 206 | return True |
| 207 | ssh_agent = SSHTestAgent(self.compute_node) |
| 208 | st, _ = ssh_agent.run_cmd('ls', timeout=10) |
| 209 | if st == False: |
| 210 | log.error('Compute node at %s is not accessible' %(self.compute_node)) |
| 211 | return st |
| 212 | log.info('Checking if APACHE SERVICE at %s is running %s' %(self.ip, self.compute_node)) |
| 213 | st, _ = ssh_agent.run_cmd('ssh {} ls /var/run/apache2/apache2.pid'.format(self.ip), timeout=30) |
| 214 | if st == True: |
| 215 | log.info('OK') |
| 216 | return st |
| 217 | |
| 218 | |