blob: b288ada99c9ce599db713587dc8e90bf21db5659 [file] [log] [blame]
Justin Shepherd0d9bbd12011-08-11 12:57:44 -05001# 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
18import ConfigParser
19from hashlib import md5
20import nose.plugins.skip
21import os
Justin Shepherd66c861a2011-08-18 11:02:53 -050022from pprint import pprint
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050023import unittest2
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050024
25NOVA_DATA = {}
26GLANCE_DATA = {}
27SWIFT_DATA = {}
28RABBITMQ_DATA = {}
29CONFIG_DATA = {}
30KEYSTONE_DATA = {}
31
Soren Hansen826d5df2011-08-29 11:30:40 +020032
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050033class 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
47class 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
64class 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
81class FunctionalTest(unittest2.TestCase):
82 def setUp(self):
Soren Hansen826d5df2011-08-29 11:30:40 +020083 global GLANCE_DATA, NOVA_DATA, SWIFT_DATA, RABBITMQ_DATA
84 global KEYSTONE_DATA, CONFIG_DATA
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050085 # 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 Shepherd66c861a2011-08-18 11:02:53 -050095 pprint(self.config)
Justin Shepherd0d9bbd12011-08-11 12:57:44 -050096
97 # Swift Setup
98 if 'swift' in self.config:
Aaron Lee35b8c922011-10-18 17:07:19 -050099 self.swift = self.config['swift']
Soren Hansen826d5df2011-08-29 11:30:40 +0200100 self.swift['ver'] = 'v1.0' # need to find a better way to get this
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500101
102 # Glance Setup
Aaron Lee35b8c922011-10-18 17:07:19 -0500103 self.glance = self.config['glance']
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500104
105 if 'nova' in self.config:
Aaron Lee35b8c922011-10-18 17:07:19 -0500106 self.nova = self.config['nova']
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500107 self.nova['ver'] = self.config['nova']['apiver']
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500108
109 if 'keystone' in self.config:
Aaron Lee35b8c922011-10-18 17:07:19 -0500110 self.keystone = self.config['keystone']
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500111 self.keystone['pass'] = self.config['keystone']['password']
112
113 def _md5sum_file(self, path):
114 md5sum = md5()
115 with open(path, 'rb') as file:
116 for chunk in iter(lambda: file.read(8192), ''):
117 md5sum.update(chunk)
118 return md5sum.hexdigest()
119
120 def _read_in_chunks(self, infile, chunk_size=1024 * 64):
121 file_data = open(infile, "rb")
122 while True:
123 # chunk = file_data.read(chunk_size).encode('base64')
124 chunk = file_data.read(chunk_size)
125 if chunk:
126 yield chunk
127 else:
128 return
129 file_data.close()
130
131 def _parse_defaults_file(self):
132 cfg = os.path.abspath(os.path.join(os.path.dirname(__file__),
Soren Hansenec3f7092011-09-08 13:03:42 +0200133 os.path.pardir, os.path.pardir,
134 "etc", "config.ini"))
Justin Shepherd0d9bbd12011-08-11 12:57:44 -0500135 if os.path.exists(cfg):
136 self._build_config(cfg)
137 else:
138 raise Exception("Cannot read %s" % cfg)
139
140 def _build_config(self, config_file):
141 parser = ConfigParser.ConfigParser()
142 parser.read(config_file)
143
144 for section in parser.sections():
145 self.config[section] = {}
146 for value in parser.options(section):
147 self.config[section][value] = parser.get(section, value)
148 # print "%s = %s" % (value, parser.get(section, value))