def check_repair_error_test(self): if 'CLOUDINITD_TESTENV' in os.environ: # we cannot run this one in fake mode yet return (osf, outfile) = tempfile.mkstemp() os.close(osf) dir = os.path.expanduser("~/.cloudinitd/") conf_file = "%s/outputdep/top.conf" % (cloudinitd.nosetests.g_plans_dir) cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() cb.block_until_complete(poll_period=1.0) runname = cb.run_name svc = cb.get_service("onelvl1") secret = svc.get_attr_from_bag('iaas_secret') key = svc.get_attr_from_bag('iaas_key') iaas_url= svc.get_attr_from_bag('iaas_url') instance_id = svc.get_attr_from_bag('instance_id') con = iaas_get_con(svc._svc, key=key, secret=secret, iaasurl=iaas_url) instance = con.find_instance(instance_id) instance.terminate() print "start repair" rc = cloudinitd.cli.boot.main(["-O", outfile, "-v","-v","-v","repair", runname]) self._dump_output(outfile) n = "ERROR" line = self._find_str(outfile, n) self.assertNotEqual(line, None) print "start terminate" rc = cloudinitd.cli.boot.main(["terminate", "%s" % (runname)]) self.assertEqual(rc, 0)
def check_status_shutdown_error_test(self): (osf, outfile) = tempfile.mkstemp() os.close(osf) dir = os.path.expanduser("~/.cloudinitd/") conf_file = self.plan_basedir + "/terminate/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() cb.block_until_complete(poll_period=1.0) runname = cb.run_name svc = cb.get_service("sampleservice") p = svc.shutdown() rc = p.poll() while not rc: rc = p.poll() time.sleep(0.1) if 'CLOUDINITD_TESTENV' in os.environ: bkfab = os.environ['CLOUDINITD_FAB'] bkssh = os.environ['CLOUDINITD_SSH'] os.environ['CLOUDINITD_FAB'] = "/bin/false" os.environ['CLOUDINITD_SSH'] = "/bin/false" rc = cloudinitd.cli.boot.main(["-O", outfile, "-v","-v","-v","-v", "status", runname]) if 'CLOUDINITD_TESTENV' in os.environ: os.environ['CLOUDINITD_FAB'] = bkfab os.environ['CLOUDINITD_SSH'] = bkssh self._dump_output(outfile) n = "ERROR" line = self._find_str(outfile, n) self.assertNotEqual(line, None) rc = cloudinitd.cli.boot.main(["-O", outfile, "terminate", "%s" % (runname)]) self.assertEqual(rc, 0)
def test_validateiaas(self): self.plan_basedir = cloudinitd.nosetests.g_plans_dir dir = tempfile.mkdtemp() conf_file = self.plan_basedir + "/iaastypevalidate/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() cb.block_until_complete(poll_period=1.0) # check the log for a warning fname = os.path.expanduser("~/.cloudinitd/%s/badsvc.log" % (cb.run_name)) print fname self.assertTrue(os.path.exists(fname), "The path %s should exist" % (fname)) f = open(fname, "r") found = False for l in f.readlines(): print l ndx = l.find("WARN") if ndx >= 0: ndx = l.find("2.7") if ndx >= 0: found = True self.assertTrue(found, "a warning with the key 2.7 should be in the logfile %s" %(fname)) f.close() cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False) cb.shutdown() cb.block_until_complete(poll_period=1.0) fname = cb.get_db_file() os.remove(fname)
def reboot(options, args): """ Reboot an already booted plan. You must supply the run name of the booted plan. """ if len(args) < 2: print "The reboot command requires a run name. See --help" return 1 dbname = args[1] cb = CloudInitD(options.database, db_name=dbname, log_level=options.loglevel, level_callback=level_callback, service_callback=service_callback, logdir=options.logdir, terminate=True, boot=False, ready=False, continue_on_error=True) print_chars(1, "Rebooting %s\n" % (cb.run_name)) cb.shutdown() try: try: print_chars(1, "Terminating all services %s\n" % (cb.run_name)) options.logger.info("Terminating all services") cb.block_until_complete(poll_period=0.1) options.logger.info("Starting services back up") cb = CloudInitD(options.database, db_name=dbname, log_level=options.loglevel, level_callback=level_callback, service_callback=service_callback, logdir=options.logdir, terminate=False, boot=True, ready=True, continue_on_error=False) print_chars(1, "Booting all services %s\n" % (cb.run_name)) cb.start() cb.block_until_complete(poll_period=0.1) return 0 except CloudServiceException, svcex: print svcex except MultilevelException, mex: print mex
def _start_one(self, conf_file): dir = tempfile.mkdtemp() conf_file = self.plan_basedir + "/" + conf_file + "/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() cb.block_until_complete(poll_period=1.0) return (dir, cb)
def load(p, c, m, run_name, cloudinitd_dbdir, silent=False, terminate=False, wholerun=True): """Load any EPU related instances from a local cloudinit.d launch with the same run name. """ try: cb = CloudInitD(cloudinitd_dbdir, db_name=run_name, terminate=terminate, boot=False, ready=False) cb.start() cb.block_until_complete() except APIUsageException, e: raise IncompatibleEnvironment("Problem loading records from cloudinit.d: %s" % str(e))
def test_manyservices_one_vm_simple(self): self.plan_basedir = cloudinitd.nosetests.g_plans_dir dir = tempfile.mkdtemp() conf_file = self.plan_basedir + "/singlevmmanyservice/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() cb.block_until_complete(poll_period=1.0) cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False) cb.shutdown() cb.block_until_complete(poll_period=1.0)
def dep_not_found_test(self): self.plan_basedir = cloudinitd.nosetests.g_plans_dir dir = tempfile.mkdtemp() conf_file = self.plan_basedir + "/oneservice/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() ok = False try: cb.find_dep("notaservice", "whatever") except APIUsageException, ex: ok = True
def test_badlevel_bootpgm(self): ilist_1 = self._get_running_vms() count1 = len(ilist_1) self.plan_basedir = cloudinitd.nosetests.g_plans_dir dir = tempfile.mkdtemp() conf_file = self.plan_basedir + "/badlevel2/top.conf" pass_ex = True cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) try: cb.start() cb.block_until_complete(poll_period=1.0) except Exception, ex: pass_ex = True
def _start_one(self, conf_file): self.plan_basedir = cloudinitd.nosetests.g_plans_dir dir = tempfile.mkdtemp() conf_file = self.plan_basedir + "/" + conf_file + "/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() cb.block_until_complete(poll_period=1.0) cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False) cb.shutdown() cb.block_until_complete(poll_period=1.0) fname = cb.get_db_file() os.remove(fname)
def get_status_test(self): self.plan_basedir = cloudinitd.nosetests.g_plans_dir dir = tempfile.mkdtemp() conf_file = self.plan_basedir + "/oneservice/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() cb.block_until_complete(poll_period=1.0) svc = cb.get_service("sampleservice") status = svc.get_iaas_status() self.assertEqual("running", status, "status is %s" % (status)) cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False) cb.shutdown() cb.block_until_complete(poll_period=1.0) fname = cb.get_db_file() os.remove(fname)
def _status(options, args): global g_repair dbname = args[1] c_on_e = not g_repair options.name = dbname cb = CloudInitD(options.database, db_name=dbname, log_level=options.loglevel, level_callback=level_callback, service_callback=service_callback, logdir=options.logdir, terminate=False, boot=False, ready=True, continue_on_error=c_on_e) print_chars(1, "Checking status on %s\n" % (cb.run_name)) cb.start() try: try: cb.block_until_complete(poll_period=0.1) except CloudServiceException, svcex: print svcex return 1 except MultilevelException, mex: print mex return 1
def dep_keys_test(self): self.plan_basedir = cloudinitd.nosetests.g_plans_dir dir = tempfile.mkdtemp() conf_file = self.plan_basedir + "/oneservice/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() cb.block_until_complete(poll_period=1.0) svc = cb.get_service("sampleservice") attr_keys = svc.get_keys_from_bag() # until we figure out how to get __dict__ values from the sqlalchemy objects this will be in complete expectations = [ "hostname", "instance_id", # "name", # "level_id", # "image", # "iaas", # "allocation", # "keyname", # "localkey", # "username", # "scp_username", # "readypgm", # "hostname", # "bootconf", # "bootpgm", # "instance_id", # "iaas_url", # "iaas_key", # "iaas_secret", # "contextualized", # "securitygroups", "webmessage" ] for e in expectations: self.assertTrue(e in attr_keys, "The key %s should exist in %s" % (e, str(attr_keys))) cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False) cb.shutdown() cb.block_until_complete(poll_period=1.0) fname = cb.get_db_file() os.remove(fname)
def test_only_one_launched(self): if 'CLOUDINITD_CLEAN_ACCOUNT' not in os.environ: raise SkipTest() ilist_1 = self._get_running_vms() count1 = len(ilist_1) self.plan_basedir = cloudinitd.nosetests.g_plans_dir dir = tempfile.mkdtemp() conf_file = self.plan_basedir + "/singlevmmanyservice/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() cb.block_until_complete(poll_period=1.0) ilist_2 = self._get_running_vms() count2 = len(ilist_2) self.assertEqual(count1, count2 - 1, "there should be exactly 1 more VM in count 2: %d %d" % (count1, count2)) cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False) cb.shutdown() cb.block_until_complete(poll_period=1.0)
def test_env_set(self): if cloudinitd.nosetests.is_a_test(): raise SkipTest() dir = tempfile.mkdtemp() conf_file = self.plan_basedir + "/oneservice/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() cb.block_until_complete(poll_period=1.0) svc = cb.get_service("sampleservice") ssh_cmd = svc.get_ssh_command() + " ls -l %s" % (self.remote_dir) # form test directory command print ssh_cmd rc = os.system(ssh_cmd) self.assertEquals(rc, 0) cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False) cb.shutdown() cb.block_until_complete(poll_period=1.0)
def check_status_error_test(self): (osf, outfile) = tempfile.mkstemp() os.close(osf) dir = os.path.expanduser("~/.cloudinitd/") conf_file = self.plan_basedir + "/terminate/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.start() cb.block_until_complete(poll_period=1.0) runname = cb.run_name svc = cb.get_service("sampleservice") secret = svc.get_attr_from_bag('iaas_secret') key = svc.get_attr_from_bag('iaas_key') iaas_url = svc.get_attr_from_bag('iaas_url') instance_id = svc.get_attr_from_bag('instance_id') con = iaas_get_con(None, key=key, secret=secret, iaasurl=iaas_url) instance = con.find_instance(instance_id) instance.terminate() if 'CLOUDINITD_TESTENV' in os.environ: bkfab = os.environ['CLOUDINITD_FAB'] bkssh = os.environ['CLOUDINITD_SSH'] os.environ['CLOUDINITD_FAB'] = "/bin/false" os.environ['CLOUDINITD_SSH'] = "/bin/false" rc = cloudinitd.cli.boot.main(["-O", outfile, "-v","-v","-v","-v", "status", runname]) if 'CLOUDINITD_TESTENV' in os.environ: os.environ['CLOUDINITD_FAB'] = bkfab os.environ['CLOUDINITD_SSH'] = bkssh self._dump_output(outfile) n = "ERROR" line = self._find_str(outfile, n) self.assertNotEqual(line, None) rc = cloudinitd.cli.boot.main(["-O", outfile, "terminate", "%s" % (runname)]) if 'CLOUDINITD_TESTENV' in os.environ: # in fake mode we cannot detect that an instance was killed self.assertEqual(rc, 0) else: self.assertNotEqual(rc, 0)
def test_prelaunch(self): key = None secret = None url = None try: key = os.environ['CLOUDINITD_IAAS_ACCESS_KEY'] secret = os.environ['CLOUDINITD_IAAS_SECRET_KEY'] url = os.environ['CLOUDINITD_IAAS_URL'] except: pass # XXX this test may fail for nimbus con = cloudinitd.cb_iaas.iaas_get_con(None, key=key, secret=secret, iaasurl=url) i_list = con.get_all_instances() conf_file = "multilevelsimple" self.plan_basedir = cloudinitd.nosetests.g_plans_dir dir = tempfile.mkdtemp() conf_file = self.plan_basedir + "/" + conf_file + "/top.conf" cb = CloudInitD(dir, conf_file, terminate=False, boot=True, ready=True) cb.pre_start_iaas() post_boot_list = con.get_all_instances() self.assertNotEqual(len(i_list), len(post_boot_list), "The list of instances should have grown") self.assertTrue(len(i_list)+3 < len(post_boot_list), "The list of instances should have grown by more than the number of services in the first level") cb.start() post_start_list = con.get_all_instances() self.assertEqual(len(post_boot_list), len(post_start_list), "The list should not have grown") cb.block_until_complete(poll_period=1.0) cb = CloudInitD(dir, db_name=cb.run_name, terminate=True, boot=False, ready=False) cb.shutdown() cb.block_until_complete(poll_period=1.0) fname = cb.get_db_file() os.remove(fname)
def _status(self, dir, run_name): cb = CloudInitD(dir, db_name=run_name, terminate=False, boot=False, ready=True, continue_on_error=True) cb.start() cb.block_until_complete(poll_period=1.0)