Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2010-2011 OpenStack LLC. |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
| 18 | import ConfigParser |
| 19 | from hashlib import md5 |
| 20 | import nose.plugins.skip |
| 21 | import os |
Justin Shepherd | 66c861a | 2011-08-18 11:02:53 -0500 | [diff] [blame] | 22 | from pprint import pprint |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 23 | import unittest2 |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 24 | |
| 25 | NOVA_DATA = {} |
| 26 | GLANCE_DATA = {} |
| 27 | SWIFT_DATA = {} |
| 28 | RABBITMQ_DATA = {} |
| 29 | CONFIG_DATA = {} |
| 30 | KEYSTONE_DATA = {} |
| 31 | |
Soren Hansen | 826d5df | 2011-08-29 11:30:40 +0200 | [diff] [blame] | 32 | |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 33 | class skip_test(object): |
| 34 | """Decorator that skips a test.""" |
| 35 | def __init__(self, msg): |
| 36 | self.message = msg |
| 37 | |
| 38 | def __call__(self, func): |
| 39 | def _skipper(*args, **kw): |
| 40 | """Wrapped skipper function.""" |
| 41 | raise nose.SkipTest(self.message) |
| 42 | _skipper.__name__ = func.__name__ |
| 43 | _skipper.__doc__ = func.__doc__ |
| 44 | return _skipper |
| 45 | |
| 46 | |
| 47 | class skip_if(object): |
| 48 | """Decorator that skips a test.""" |
| 49 | def __init__(self, condition, msg): |
| 50 | self.condition = condition |
| 51 | self.message = msg |
| 52 | |
| 53 | def __call__(self, func): |
| 54 | def _skipper(*args, **kw): |
| 55 | """Wrapped skipper function.""" |
| 56 | if self.condition: |
| 57 | raise nose.SkipTest(self.message) |
| 58 | func(*args, **kw) |
| 59 | _skipper.__name__ = func.__name__ |
| 60 | _skipper.__doc__ = func.__doc__ |
| 61 | return _skipper |
| 62 | |
| 63 | |
| 64 | class skip_unless(object): |
| 65 | """Decorator that skips a test.""" |
| 66 | def __init__(self, condition, msg): |
| 67 | self.condition = condition |
| 68 | self.message = msg |
| 69 | |
| 70 | def __call__(self, func): |
| 71 | def _skipper(*args, **kw): |
| 72 | """Wrapped skipper function.""" |
| 73 | if not self.condition: |
| 74 | raise nose.SkipTest(self.message) |
| 75 | func(*args, **kw) |
| 76 | _skipper.__name__ = func.__name__ |
| 77 | _skipper.__doc__ = func.__doc__ |
| 78 | return _skipper |
| 79 | |
| 80 | |
| 81 | class FunctionalTest(unittest2.TestCase): |
| 82 | def setUp(self): |
Soren Hansen | 826d5df | 2011-08-29 11:30:40 +0200 | [diff] [blame] | 83 | global GLANCE_DATA, NOVA_DATA, SWIFT_DATA, RABBITMQ_DATA |
| 84 | global KEYSTONE_DATA, CONFIG_DATA |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 85 | # Define config dict |
| 86 | self.config = CONFIG_DATA |
| 87 | # Define service specific dicts |
| 88 | self.glance = GLANCE_DATA |
| 89 | self.nova = NOVA_DATA |
| 90 | self.swift = SWIFT_DATA |
| 91 | self.rabbitmq = RABBITMQ_DATA |
| 92 | self.keystone = KEYSTONE_DATA |
| 93 | |
| 94 | self._parse_defaults_file() |
Justin Shepherd | 66c861a | 2011-08-18 11:02:53 -0500 | [diff] [blame] | 95 | pprint(self.config) |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 96 | |
| 97 | # Swift Setup |
| 98 | if 'swift' in self.config: |
| 99 | self.swift['auth_host'] = self.config['swift']['auth_host'] |
| 100 | self.swift['auth_port'] = self.config['swift']['auth_port'] |
| 101 | self.swift['auth_prefix'] = self.config['swift']['auth_prefix'] |
| 102 | self.swift['auth_ssl'] = self.config['swift']['auth_ssl'] |
| 103 | self.swift['account'] = self.config['swift']['account'] |
| 104 | self.swift['username'] = self.config['swift']['username'] |
| 105 | self.swift['password'] = self.config['swift']['password'] |
Soren Hansen | 826d5df | 2011-08-29 11:30:40 +0200 | [diff] [blame] | 106 | self.swift['ver'] = 'v1.0' # need to find a better way to get this |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 107 | |
| 108 | # Glance Setup |
| 109 | self.glance['host'] = self.config['glance']['host'] |
| 110 | self.glance['port'] = self.config['glance']['port'] |
| 111 | if 'apiver' in self.config['glance']: |
| 112 | self.glance['apiver'] = self.config['glance']['apiver'] |
| 113 | |
| 114 | if 'nova' in self.config: |
| 115 | self.nova['host'] = self.config['nova']['host'] |
| 116 | self.nova['port'] = self.config['nova']['port'] |
| 117 | self.nova['ver'] = self.config['nova']['apiver'] |
| 118 | self.nova['user'] = self.config['nova']['user'] |
| 119 | self.nova['key'] = self.config['nova']['key'] |
| 120 | |
| 121 | if 'keystone' in self.config: |
| 122 | self.keystone['host'] = self.config['keystone']['host'] |
| 123 | self.keystone['port'] = self.config['keystone']['port'] |
| 124 | self.keystone['apiver'] = self.config['keystone']['apiver'] |
| 125 | self.keystone['user'] = self.config['keystone']['user'] |
| 126 | self.keystone['pass'] = self.config['keystone']['password'] |
| 127 | |
| 128 | def _md5sum_file(self, path): |
| 129 | md5sum = md5() |
| 130 | with open(path, 'rb') as file: |
| 131 | for chunk in iter(lambda: file.read(8192), ''): |
| 132 | md5sum.update(chunk) |
| 133 | return md5sum.hexdigest() |
| 134 | |
| 135 | def _read_in_chunks(self, infile, chunk_size=1024 * 64): |
| 136 | file_data = open(infile, "rb") |
| 137 | while True: |
| 138 | # chunk = file_data.read(chunk_size).encode('base64') |
| 139 | chunk = file_data.read(chunk_size) |
| 140 | if chunk: |
| 141 | yield chunk |
| 142 | else: |
| 143 | return |
| 144 | file_data.close() |
| 145 | |
| 146 | def _parse_defaults_file(self): |
| 147 | cfg = os.path.abspath(os.path.join(os.path.dirname(__file__), |
Soren Hansen | ec3f709 | 2011-09-08 13:03:42 +0200 | [diff] [blame] | 148 | os.path.pardir, os.path.pardir, |
| 149 | "etc", "config.ini")) |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 150 | if os.path.exists(cfg): |
| 151 | self._build_config(cfg) |
| 152 | else: |
| 153 | raise Exception("Cannot read %s" % cfg) |
| 154 | |
| 155 | def _build_config(self, config_file): |
| 156 | parser = ConfigParser.ConfigParser() |
| 157 | parser.read(config_file) |
| 158 | |
| 159 | for section in parser.sections(): |
| 160 | self.config[section] = {} |
| 161 | for value in parser.options(section): |
| 162 | self.config[section][value] = parser.get(section, value) |
| 163 | # print "%s = %s" % (value, parser.get(section, value)) |