blob: 515bfc3f3edfddd009a6c921bce2044eecf9ebe7 [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
22import unittest2
23from xmlrpclib import Server
24
25NOVA_DATA = {}
26GLANCE_DATA = {}
27SWIFT_DATA = {}
28RABBITMQ_DATA = {}
29CONFIG_DATA = {}
30KEYSTONE_DATA = {}
31
32class 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
46class 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
63class 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
80class FunctionalTest(unittest2.TestCase):
81 def setUp(self):
82 global GLANCE_DATA, NOVA_DATA, SWIFT_DATA, RABBITMQ_DATA, KEYSTONE_DATA, CONFIG_DATA
83 # Define config dict
84 self.config = CONFIG_DATA
85 # Define service specific dicts
86 self.glance = GLANCE_DATA
87 self.nova = NOVA_DATA
88 self.swift = SWIFT_DATA
89 self.rabbitmq = RABBITMQ_DATA
90 self.keystone = KEYSTONE_DATA
91
92 self._parse_defaults_file()
93
94 # Swift Setup
95 if 'swift' in self.config:
96 self.swift['auth_host'] = self.config['swift']['auth_host']
97 self.swift['auth_port'] = self.config['swift']['auth_port']
98 self.swift['auth_prefix'] = self.config['swift']['auth_prefix']
99 self.swift['auth_ssl'] = self.config['swift']['auth_ssl']
100 self.swift['account'] = self.config['swift']['account']
101 self.swift['username'] = self.config['swift']['username']
102 self.swift['password'] = self.config['swift']['password']
103 self.swift['ver'] = 'v1.0' # need to find a better way to get this.
104
105 # Glance Setup
106 self.glance['host'] = self.config['glance']['host']
107 self.glance['port'] = self.config['glance']['port']
108 if 'apiver' in self.config['glance']:
109 self.glance['apiver'] = self.config['glance']['apiver']
110
111 if 'nova' in self.config:
112 self.nova['host'] = self.config['nova']['host']
113 self.nova['port'] = self.config['nova']['port']
114 self.nova['ver'] = self.config['nova']['apiver']
115 self.nova['user'] = self.config['nova']['user']
116 self.nova['key'] = self.config['nova']['key']
117
118 if 'keystone' in self.config:
119 self.keystone['host'] = self.config['keystone']['host']
120 self.keystone['port'] = self.config['keystone']['port']
121 self.keystone['apiver'] = self.config['keystone']['apiver']
122 self.keystone['user'] = self.config['keystone']['user']
123 self.keystone['pass'] = self.config['keystone']['password']
124
125 def _md5sum_file(self, path):
126 md5sum = md5()
127 with open(path, 'rb') as file:
128 for chunk in iter(lambda: file.read(8192), ''):
129 md5sum.update(chunk)
130 return md5sum.hexdigest()
131
132 def _read_in_chunks(self, infile, chunk_size=1024 * 64):
133 file_data = open(infile, "rb")
134 while True:
135 # chunk = file_data.read(chunk_size).encode('base64')
136 chunk = file_data.read(chunk_size)
137 if chunk:
138 yield chunk
139 else:
140 return
141 file_data.close()
142
143 def _parse_defaults_file(self):
144 cfg = os.path.abspath(os.path.join(os.path.dirname(__file__),
145 "..", "etc", "config.ini"))
146 if os.path.exists(cfg):
147 self._build_config(cfg)
148 else:
149 raise Exception("Cannot read %s" % cfg)
150
151 def _build_config(self, config_file):
152 parser = ConfigParser.ConfigParser()
153 parser.read(config_file)
154
155 for section in parser.sections():
156 self.config[section] = {}
157 for value in parser.options(section):
158 self.config[section][value] = parser.get(section, value)
159 # print "%s = %s" % (value, parser.get(section, value))