[test] Added base class for cookbook test cases; some helpers
This commit is contained in:
parent
5c1bc85dd5
commit
79cf12a2c1
@ -130,3 +130,40 @@ def tearDown():
|
||||
if not ci.environment_cache_file:
|
||||
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
|
||||
|
||||
|
@ -99,6 +99,9 @@ class SSHClient(object):
|
||||
logging.info("Removing directory: %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):
|
||||
logging.info("Copying file: %s -> %s" % (frm, to))
|
||||
self.sftp_client.put(frm, to)
|
||||
|
Loading…
Reference in New Issue
Block a user