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 |
| 22 | import unittest2 |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 23 | |
| 24 | NOVA_DATA = {} |
| 25 | GLANCE_DATA = {} |
| 26 | SWIFT_DATA = {} |
| 27 | RABBITMQ_DATA = {} |
| 28 | CONFIG_DATA = {} |
| 29 | KEYSTONE_DATA = {} |
| 30 | |
Soren Hansen | 826d5df | 2011-08-29 11:30:40 +0200 | [diff] [blame] | 31 | |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 32 | class skip_test(object): |
| 33 | """Decorator that skips a test.""" |
| 34 | def __init__(self, msg): |
| 35 | self.message = msg |
| 36 | |
| 37 | def __call__(self, func): |
| 38 | def _skipper(*args, **kw): |
| 39 | """Wrapped skipper function.""" |
| 40 | raise nose.SkipTest(self.message) |
| 41 | _skipper.__name__ = func.__name__ |
| 42 | _skipper.__doc__ = func.__doc__ |
| 43 | return _skipper |
| 44 | |
| 45 | |
| 46 | class skip_if(object): |
| 47 | """Decorator that skips a test.""" |
| 48 | def __init__(self, condition, msg): |
| 49 | self.condition = condition |
| 50 | self.message = msg |
| 51 | |
| 52 | def __call__(self, func): |
| 53 | def _skipper(*args, **kw): |
| 54 | """Wrapped skipper function.""" |
| 55 | if self.condition: |
| 56 | raise nose.SkipTest(self.message) |
| 57 | func(*args, **kw) |
| 58 | _skipper.__name__ = func.__name__ |
| 59 | _skipper.__doc__ = func.__doc__ |
| 60 | return _skipper |
| 61 | |
| 62 | |
| 63 | class skip_unless(object): |
| 64 | """Decorator that skips a test.""" |
| 65 | def __init__(self, condition, msg): |
| 66 | self.condition = condition |
| 67 | self.message = msg |
| 68 | |
| 69 | def __call__(self, func): |
| 70 | def _skipper(*args, **kw): |
| 71 | """Wrapped skipper function.""" |
| 72 | if not self.condition: |
| 73 | raise nose.SkipTest(self.message) |
| 74 | func(*args, **kw) |
| 75 | _skipper.__name__ = func.__name__ |
| 76 | _skipper.__doc__ = func.__doc__ |
| 77 | return _skipper |
| 78 | |
| 79 | |
| 80 | class FunctionalTest(unittest2.TestCase): |
| 81 | def setUp(self): |
Soren Hansen | 826d5df | 2011-08-29 11:30:40 +0200 | [diff] [blame] | 82 | global GLANCE_DATA, NOVA_DATA, SWIFT_DATA, RABBITMQ_DATA |
| 83 | global KEYSTONE_DATA, CONFIG_DATA |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 84 | # Define config dict |
| 85 | self.config = CONFIG_DATA |
| 86 | # Define service specific dicts |
| 87 | self.glance = GLANCE_DATA |
| 88 | self.nova = NOVA_DATA |
| 89 | self.swift = SWIFT_DATA |
| 90 | self.rabbitmq = RABBITMQ_DATA |
| 91 | self.keystone = KEYSTONE_DATA |
| 92 | |
| 93 | self._parse_defaults_file() |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 94 | |
| 95 | # Swift Setup |
| 96 | if 'swift' in self.config: |
Aaron Lee | 35b8c92 | 2011-10-18 17:07:19 -0500 | [diff] [blame] | 97 | self.swift = self.config['swift'] |
Soren Hansen | 826d5df | 2011-08-29 11:30:40 +0200 | [diff] [blame] | 98 | 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] | 99 | |
| 100 | # Glance Setup |
Aaron Lee | 35b8c92 | 2011-10-18 17:07:19 -0500 | [diff] [blame] | 101 | self.glance = self.config['glance'] |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 102 | |
| 103 | if 'nova' in self.config: |
Aaron Lee | 35b8c92 | 2011-10-18 17:07:19 -0500 | [diff] [blame] | 104 | self.nova = self.config['nova'] |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 105 | self.nova['ver'] = self.config['nova']['apiver'] |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 106 | |
| 107 | if 'keystone' in self.config: |
Aaron Lee | 35b8c92 | 2011-10-18 17:07:19 -0500 | [diff] [blame] | 108 | self.keystone = self.config['keystone'] |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 109 | self.keystone['pass'] = self.config['keystone']['password'] |
| 110 | |
| 111 | def _md5sum_file(self, path): |
| 112 | md5sum = md5() |
| 113 | with open(path, 'rb') as file: |
| 114 | for chunk in iter(lambda: file.read(8192), ''): |
| 115 | md5sum.update(chunk) |
| 116 | return md5sum.hexdigest() |
| 117 | |
| 118 | def _read_in_chunks(self, infile, chunk_size=1024 * 64): |
| 119 | file_data = open(infile, "rb") |
| 120 | while True: |
| 121 | # chunk = file_data.read(chunk_size).encode('base64') |
| 122 | chunk = file_data.read(chunk_size) |
| 123 | if chunk: |
| 124 | yield chunk |
| 125 | else: |
| 126 | return |
| 127 | file_data.close() |
| 128 | |
| 129 | def _parse_defaults_file(self): |
| 130 | cfg = os.path.abspath(os.path.join(os.path.dirname(__file__), |
Soren Hansen | ec3f709 | 2011-09-08 13:03:42 +0200 | [diff] [blame] | 131 | os.path.pardir, os.path.pardir, |
| 132 | "etc", "config.ini")) |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 133 | if os.path.exists(cfg): |
| 134 | self._build_config(cfg) |
| 135 | else: |
| 136 | raise Exception("Cannot read %s" % cfg) |
| 137 | |
| 138 | def _build_config(self, config_file): |
| 139 | parser = ConfigParser.ConfigParser() |
| 140 | parser.read(config_file) |
| 141 | |
| 142 | for section in parser.sections(): |
| 143 | self.config[section] = {} |
| 144 | for value in parser.options(section): |
| 145 | self.config[section][value] = parser.get(section, value) |
| 146 | # print "%s = %s" % (value, parser.get(section, value)) |