[test] Added base class for cookbook test cases; some helpers

This commit is contained in:
Maxim Kulkin 2012-07-18 17:42:44 +04:00
parent 5c1bc85dd5
commit 79cf12a2c1
2 changed files with 40 additions and 0 deletions

View File

@ -130,3 +130,40 @@ def tearDown():
if not ci.environment_cache_file: if not ci.environment_cache_file:
ci.destroy_environment() ci.destroy_environment()
class CookbokTestCase(unittest.TestCase):
def setUp(self):
self.ip = ci.environment.node['cookbooks'].ip_address
self.cookbooks_dir = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
"..", "..", "cooks", "cookbooks"
)
)
self.remote = SSHClient()
self.remote.connect_ssh(str(self.ip), "root", "r00tme")
self.remote.mkdir("/opt/os-cookbooks/")
with self.remote.open('/tmp/solo.rb', 'w') as solo_rb:
solo_rb.write("""
file_cache_path "/tmp/chef"
cookbook_path "/opt/os-cookbooks"
""")
def upload_cookbooks(self, cookbooks):
if not isinstance(cookbooks, list):
cookbooks = [cookbooks]
for cookbook in cookbooks:
self.remote.scp_d(os.path.join(self.cookbooks_dir, cookbook), "/opt/os-cookbooks/")
def chef_solo(self, attributes={}):
with self.remote.open('/tmp/solo.json', 'w') as f:
f.write(json.dumps(attributes))
result = self.remote.exec_cmd("chef-solo -l debug -c /tmp/solo.rb -j /tmp/solo.json")
if result['exit_status'] != 0:
raise ChefRunError(result['exit_status'], result['stdout'], result['stderr'])
return result

View File

@ -99,6 +99,9 @@ class SSHClient(object):
logging.info("Removing directory: %s" % path) logging.info("Removing directory: %s" % path)
return self.exec_cmd("rm -rf %s" % path) return self.exec_cmd("rm -rf %s" % path)
def open(self, path, mode='r'):
return self.sftp_client.open(path, mode)
def scp(self, frm, to): def scp(self, frm, to):
logging.info("Copying file: %s -> %s" % (frm, to)) logging.info("Copying file: %s -> %s" % (frm, to))
self.sftp_client.put(frm, to) self.sftp_client.put(frm, to)