def test_getOsCheckScript(self): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) ocs = bootstrap_obj.getOsCheckScript() self.assertEquals(ocs, "scriptDir/os_check_type.py")
def test_copyPasswordFile(self, write_mock, ssh_run_mock, ssh_init_mock, scp_run_mock, scp_init_mock, getPasswordFile_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root", password_file="PasswordFile") bootstrap_obj = Bootstrap("hostname", shared_state) getPasswordFile_mock.return_value = "PasswordFile" # Testing max retcode return expected1 = {"exitstatus": 42, "log": "log42", "errormsg": "errorMsg"} expected2 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} scp_init_mock.return_value = None scp_run_mock.return_value = expected1 ssh_init_mock.return_value = None ssh_run_mock.return_value = expected2 res = bootstrap_obj.copyPasswordFile() self.assertEquals(res, expected1["exitstatus"]) input_file = str(scp_init_mock.call_args[0][3]) remote_file = str(scp_init_mock.call_args[0][4]) self.assertEqual(input_file, "PasswordFile") self.assertEqual(remote_file, "PasswordFile") command = str(ssh_init_mock.call_args[0][3]) self.assertEqual(command, "chmod 600 PasswordFile") # Another order expected1 = {"exitstatus": 0, "log": "log0", "errormsg": "errorMsg"} expected2 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} scp_run_mock.return_value = expected1 ssh_run_mock.return_value = expected2
def __init__(self): Bootstrap.__init__(self) self._msfile = tempfile.NamedTemporaryFile() self._msfile.delete = False self._items=0 self._done=0 progs.register("multistrap");
def test_try_to_execute(self, write_mock): expected = 43 shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) # Normal case def act_normal_return_int(): return 43 ret = bootstrap_obj.try_to_execute(act_normal_return_int) self.assertEqual(ret["exitstatus"], expected) self.assertFalse(write_mock.called) write_mock.reset_mock() def act_normal_return(): return {"exitstatus": 43} ret = bootstrap_obj.try_to_execute(act_normal_return) self.assertEqual(ret["exitstatus"], expected) self.assertFalse(write_mock.called) write_mock.reset_mock() # Exception scenario def act(): raise IOError() ret = bootstrap_obj.try_to_execute(act) self.assertEqual(ret["exitstatus"], 177) self.assertTrue(write_mock.called)
def test_interruptBootstrap(self, write_mock, createDoneFile_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) bootstrap_obj.interruptBootstrap() self.assertTrue(createDoneFile_mock.called)
def test_getRepoFile(self, is_suse_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) is_suse_mock.return_value = False rf = bootstrap_obj.getRepoFile() self.assertEquals(rf, "/etc/yum.repos.d/ambari.repo")
def test_agent_setup_command_without_project_version(self): os.environ[AMBARI_PASSPHRASE_VAR_NAME] = "" version = None shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", version, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) runSetupCommand = bootstrap_obj.getRunSetupCommand("hostname") self.assertTrue(runSetupCommand.endswith(" 8440"))
def test_getOsCheckScriptRemoteLocation(self, getRemoteName_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) v = "/tmp/os_check_type1374259902.py" getRemoteName_mock.return_value = v ocs = bootstrap_obj.getOsCheckScriptRemoteLocation() self.assertEquals(ocs, v)
def test_getAmbariPort(self): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) self.assertEquals(bootstrap_obj.getAmbariPort(),"8440") shared_state.server_port = None bootstrap_obj = Bootstrap("hostname", shared_state) self.assertEquals(bootstrap_obj.getAmbariPort(),"null")
def test_is_suse(self, open_mock, isfile_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) isfile_mock.return_value = True f = open_mock.return_value f.read.return_value = " suse " self.assertTrue(bootstrap_obj.is_suse())
def test_checkSudoPackage(self, write_mock, run_mock, init_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440") bootstrap_obj = Bootstrap("hostname", shared_state) expected = 42 init_mock.return_value = None run_mock.return_value = expected res = bootstrap_obj.checkSudoPackage() self.assertEquals(res, expected) command = str(init_mock.call_args[0][3]) self.assertEqual(command, "rpm -qa | grep sudo")
def test_getRunSetupWithPasswordCommand(self, environ_mock): shared_state = SharedState("root", "123", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") environ_mock.__getitem__.return_value = "TEST_PASSPHRASE" bootstrap_obj = Bootstrap("hostname", shared_state) utime = 1234 bootstrap_obj.getUtime = MagicMock(return_value=utime) ret = bootstrap_obj.getRunSetupWithPasswordCommand("hostname") expected = "/var/lib/ambari-agent/tmp/ambari-sudo.sh -S python /var/lib/ambari-agent/tmp/setupAgent{0}.py hostname TEST_PASSPHRASE " \ "ambariServer root 8440 < /var/lib/ambari-agent/tmp/host_pass{0}".format(utime) self.assertEquals(ret, expected)
def test_getRunSetupWithPasswordCommand(self, environ_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") environ_mock.__getitem__.return_value = "TEST_PASSPHRASE" bootstrap_obj = Bootstrap("hostname", shared_state) utime = 1234 bootstrap_obj.getUtime = MagicMock(return_value=utime) ret = bootstrap_obj.getRunSetupWithPasswordCommand("hostname") expected = "sudo -S python /var/lib/ambari-agent/data/tmp/setupAgent{0}.py hostname TEST_PASSPHRASE " \ "ambariServer root 8440 < /var/lib/ambari-agent/data/tmp/host_pass{0}".format(utime) self.assertEquals(ret, expected)
def test_getRepoDir(self, is_suse_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440") bootstrap_obj = Bootstrap("hostname", shared_state) # Suse is_suse_mock.return_value = True res = bootstrap_obj.getRepoDir() self.assertEquals(res, "/etc/zypp/repos.d") # non-Suse is_suse_mock.return_value = False res = bootstrap_obj.getRepoDir() self.assertEquals(res, "/etc/yum.repos.d")
def test_createDoneFile(self, write_mock): tmp_dir = tempfile.gettempdir() shared_state = SharedState("root", "sshkey_file", "scriptDir", tmp_dir, "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) done_file = os.path.join(tmp_dir, "hostname.done") expected = 42 bootstrap_obj.createDoneFile(expected) with open(done_file) as df: res = df.read() self.assertEqual(res, str(expected)) os.unlink(done_file)
def test_checkSudoPackageUbuntu(self, write_mock, run_mock, init_mock, server_family_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "ubuntu12", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) expected = 42 init_mock.return_value = None run_mock.return_value = expected server_family_mock.return_value = ["ubuntu", "12"] res = bootstrap_obj.checkSudoPackage() self.assertEquals(res, expected) command = str(init_mock.call_args[0][3]) self.assertEqual(command, "dpkg --get-selections|grep -e '^sudo\s*install'")
def test_getRepoDir(self, is_suse_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) # Suse is_suse_mock.return_value = True res = bootstrap_obj.getRepoDir() self.assertEquals(res, "/etc/zypp/repos.d") # non-Suse is_suse_mock.return_value = False res = bootstrap_obj.getRepoDir() self.assertEquals(res, "/etc/yum.repos.d")
def test_copyNeededFiles(self, write_mock, ssh_run_mock, ssh_init_mock, scp_run_mock, scp_init_mock, getRemoteName_mock, getRepoFile_mock, getRepoDir, getMoveRepoFileCommand, is_redhat_family, is_ubuntu_family, is_suse_family): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") is_redhat_family.return_value = True is_ubuntu_family.return_value = False is_suse_family.return_value = False bootstrap_obj = Bootstrap("hostname", shared_state) getMoveRepoFileCommand.return_value = "MoveRepoFileCommand" getRepoDir.return_value = "RepoDir" getRemoteName_mock.return_value = "RemoteName" getRepoFile_mock.return_value = "RepoFile" expected1 = {"exitstatus": 42, "log": "log42", "errormsg": "errorMsg"} expected2 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} expected3 = {"exitstatus": 1, "log": "log1", "errormsg": "errorMsg"} expected4 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} scp_init_mock.return_value = None ssh_init_mock.return_value = None # Testing max retcode return scp_run_mock.side_effect = [expected1, expected3] ssh_run_mock.side_effect = [expected2, expected4] res = bootstrap_obj.copyNeededFiles() self.assertEquals(res, expected1["exitstatus"]) input_file = str(scp_init_mock.call_args[0][3]) remote_file = str(scp_init_mock.call_args[0][4]) self.assertEqual(input_file, "setupAgentFile") self.assertEqual(remote_file, "RemoteName") command = str(ssh_init_mock.call_args[0][3]) self.assertEqual(command, "sudo chmod 644 RepoFile") # Another order expected1 = {"exitstatus": 0, "log": "log0", "errormsg": "errorMsg"} expected2 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} expected3 = {"exitstatus": 1, "log": "log1", "errormsg": "errorMsg"} expected4 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} scp_run_mock.side_effect = [expected1, expected3] ssh_run_mock.side_effect = [expected2, expected4] res = bootstrap_obj.copyNeededFiles() self.assertEquals(res, expected2["exitstatus"]) # yet another order expected1 = {"exitstatus": 33, "log": "log33", "errormsg": "errorMsg"} expected2 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} expected3 = {"exitstatus": 42, "log": "log42", "errormsg": "errorMsg"} expected4 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} scp_run_mock.side_effect = [expected1, expected3] ssh_run_mock.side_effect = [expected2, expected4] res = bootstrap_obj.copyNeededFiles() self.assertEquals(res, expected3["exitstatus"])
def test_runSetupAgent(self, write_mock, run_mock, getRunSetupCommand_mock, init_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) getRunSetupCommand_mock.return_value = "RunSetupCommand" expected = 42 init_mock.return_value = None run_mock.return_value = expected res = bootstrap_obj.runSetupAgent() self.assertEquals(res, expected) command = str(init_mock.call_args[0][3]) self.assertEqual(command, "RunSetupCommand")
def test_changePasswordFileModeOnHost(self, getPasswordFile_mock, write_mock, run_mock, init_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) expected = 42 getPasswordFile_mock.return_value = "PasswordFile" init_mock.return_value = None run_mock.return_value = expected res = bootstrap_obj.changePasswordFileModeOnHost() self.assertEquals(res, expected) command = str(init_mock.call_args[0][3]) self.assertEqual(command, "chmod 600 PasswordFile")
def test_checkSudoPackage(self, write_mock, run_mock, init_mock, server_family_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) expected = 42 init_mock.return_value = None run_mock.return_value = expected server_family_mock.return_value = ["centos", "6"] res = bootstrap_obj.checkSudoPackage() self.assertEquals(res, expected) command = str(init_mock.call_args[0][3]) self.assertEqual(command, "rpm -qa | grep -e '^sudo\-'")
def main(self,argv): methodName ="main" self.rc = 0 self.bootstrap = Bootstrap() try: beginTime = Utilities.currentTimeMillis() cmdLineArgs = Utilities.getInputArgs(self.ArgsSignature,argv[1:]) trace, logFile = self.bootstrap._configureTraceAndLogging(cmdLineArgs) TR.info(methodName,"BOOT0101I BEGIN Bootstrap AWS ICPD Quickstart version 1.0.0.") if (trace): TR.info(methodName,"BOOT0102I Tracing with specification: '%s' to log file: '%s'" % (trace,logFile)) #endIf region = cmdLineArgs.get('region') self.bootstrap.region = region self.bootstrap.role = cmdLineArgs.get('role') self.bootstrap.fqdn = socket.getfqdn() self.bootStackId = cmdLineArgs.get('stackid') self.bootstrap.rootStackName = cmdLineArgs.get('stack-name') self.bootstrap._init(self.bootstrap.rootStackName,self.bootStackId) self.logExporter = LogExporter(region=self.bootstrap.region, bucket=self.bootstrap.ICPDeploymentLogsBucketName, keyPrefix='logs/%s' % self.bootstrap.rootStackName, role=self.bootstrap.role, fqdn=self.bootstrap.fqdn ) self.icpHome = "/opt/icp/%s" % self.bootstrap.ICPVersion installMapPath = os.path.join(self.home,"maps","icpd-install-artifact-map.yaml") self.installMap = self.bootstrap.loadInstallMap(mapPath=installMapPath, version=self.bootstrap.ICPDVersion, region=self.bootstrap.region) icpdS3Path = "{version}/{object}".format(version=self.installMap['version'],object=self.installMap['icpd-base-install-archive']) destPath = "/tmp/icp4d.tar" storageClassCmd = "kubectl get storageclass | nl | grep aws-efs | awk '{print $1}'" TR.info(methodName,"check_output Get StorageClass value from kubectl %s"%(storageClassCmd)) self.storageclassValue=check_output(['bash','-c', storageClassCmd]) TR.info(methodName,"check_output StorageclassValue returned : %s"%(self.storageclassValue)) self.bootstrap.getS3Object(self.bootstrap.ICPDArchiveBucketName, icpdS3Path, destPath) self.stackIds = self.bootstrap._getStackIds(self.bootStackId) self.bootstrap._getHosts(self.stackIds) self.installICPD() except ExitException: pass # ExitException is used as a "goto" end of program after emitting help info except Exception, e: TR.error(methodName,"ERROR: %s" % e, e) self.rc = 1
def test_createTargetDir(self, write_mock, run_mock, init_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) expected = 42 init_mock.return_value = None run_mock.return_value = expected res = bootstrap_obj.createTargetDir() self.assertEquals(res, expected) command = str(init_mock.call_args[0][3]) self.assertEqual( command, "sudo mkdir -p /var/lib/ambari-agent/data/tmp ; " "sudo chown -R root /var/lib/ambari-agent/data/tmp")
def test_checkSudoPackageDebian(self, write_mock, run_mock, init_mock, server_family_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "debian12", None, "8440") bootstrap_obj = Bootstrap("hostname", shared_state) expected = 42 init_mock.return_value = None run_mock.return_value = expected server_family_mock.return_value = ["debian", "12"] res = bootstrap_obj.checkSudoPackage() self.assertEquals(res, expected) command = str(init_mock.call_args[0][3]) self.assertEqual(command, "dpkg --get-selections|grep -e ^sudo")
def test_createTargetDir(self, write_mock, run_mock, init_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) expected = 42 init_mock.return_value = None run_mock.return_value = expected res = bootstrap_obj.createTargetDir() self.assertEquals(res, expected) command = str(init_mock.call_args[0][3]) self.assertEqual(command, "sudo mkdir -p /var/lib/ambari-agent/data/tmp ; " "sudo chown -R root /var/lib/ambari-agent/data/tmp")
def test_runOsCheckScript(self, write_mock, run_mock, init_mock, getOsCheckScriptRemoteLocation_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440") bootstrap_obj = Bootstrap("hostname", shared_state) getOsCheckScriptRemoteLocation_mock.return_value = "OsCheckScriptRemoteLocation" expected = 42 init_mock.return_value = None run_mock.return_value = expected res = bootstrap_obj.runOsCheckScript() self.assertEquals(res, expected) command = str(init_mock.call_args[0][3]) self.assertEqual(command, "chmod a+x OsCheckScriptRemoteLocation &&" " OsCheckScriptRemoteLocation centos6")
def test_checkSudoPackage(self, write_mock, run_mock, init_mock, is_redhat_family, is_ubuntu_family, is_suse_family): shared_state = SharedState("root", "123", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) expected = 42 init_mock.return_value = None run_mock.return_value = expected is_redhat_family.return_value = True is_ubuntu_family.return_value = False is_suse_family.return_value = False res = bootstrap_obj.checkSudoPackage() self.assertEquals(res, expected) command = str(init_mock.call_args[0][4]) self.assertEqual(command, "[ \"$EUID\" -eq 0 ] || rpm -qa | grep -e '^sudo\-'")
def test_getRepoFile(self, hasPassword_mock, getRemoteName_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) # Without password hasPassword_mock.return_value = False getRemoteName_mock.return_value = "RemoteName" rf = bootstrap_obj.getMoveRepoFileCommand("target") self.assertEquals(rf, "sudo mv RemoteName target/ambari.repo") # With password hasPassword_mock.return_value = True getRemoteName_mock.return_value = "RemoteName" rf = bootstrap_obj.getMoveRepoFileCommand("target") self.assertEquals(rf, "sudo -S mv RemoteName target/ambari.repo < RemoteName")
def handlerCpinfo(self): # 导入放在这里是防止模块初始化时的导入循环,也可以放到方法体中 from bootstrap import Bootstrap for index,tag in enumerate(self.classFile.cp_tag): # Class ,Fieldref ,Methodref ,InterfaceMethodref if tag not in [7,9,10,11]: continue # print tag,constant_type.get(tag),self.classFile.cp_info[index] # TODO 其他模块的getContent方法可以删掉 res = self.__searchContent(index) # print res # class eg:java/lang/System if tag == 7: # if not Base.methodArea.has_key(_class_path): # print '=======has Bootstrap========',_class_path # # TODO 处理class文件之后还需要加载class进方法区,bootstrap # print '+++++++++++++++++ClassInfo.handlerCpinfo+++++++++++++++%s' % res Bootstrap(res) # Fieldref eg:['java/lang/System', ['out', 'Ljava/io/PrintStream;']] # class name_and_type elif tag == 9: pass # Methodref eg: ['java/lang/Object', ['<init>', '()V']] # class name_and_type elif tag == 10: pass # InterfaceMethodref elif tag == 11: pass
def test_runOsCheckScript(self, write_mock, run_mock, init_mock, getOsCheckScriptRemoteLocation_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440") bootstrap_obj = Bootstrap("hostname", shared_state) getOsCheckScriptRemoteLocation_mock.return_value = "OsCheckScriptRemoteLocation" expected = 42 init_mock.return_value = None run_mock.return_value = expected res = bootstrap_obj.runOsCheckScript() self.assertEquals(res, expected) command = str(init_mock.call_args[0][3]) self.assertEqual( command, "chmod a+x OsCheckScriptRemoteLocation &&" " OsCheckScriptRemoteLocation centos6")
def test_getRepoFile(self, hasPassword_mock, getRemoteName_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440") bootstrap_obj = Bootstrap("hostname", shared_state) # Without password hasPassword_mock.return_value = False getRemoteName_mock.return_value = "RemoteName" rf = bootstrap_obj.getMoveRepoFileCommand("target") self.assertEquals(rf, "sudo mv RemoteName target/ambari.repo") # With password hasPassword_mock.return_value = True getRemoteName_mock.return_value = "RemoteName" rf = bootstrap_obj.getMoveRepoFileCommand("target") self.assertEquals( rf, "sudo -S mv RemoteName target/ambari.repo < RemoteName")
def test_checkSudoPackageUbuntu(self, write_mock, run_mock, init_mock, is_redhat_family, is_ubuntu_family, is_suse_family): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "ubuntu12", None, "8440", "root") is_redhat_family.return_value = False is_ubuntu_family.return_value = True is_suse_family.return_value = False bootstrap_obj = Bootstrap("hostname", shared_state) expected = 42 init_mock.return_value = None run_mock.return_value = expected res = bootstrap_obj.checkSudoPackage() self.assertEquals(res, expected) command = str(init_mock.call_args[0][3]) self.assertEqual(command, "dpkg --get-selections|grep -e '^sudo\s*install'")
def test_copyOsCheckScript(self, write_mock, run_mock, init_mock, getOsCheckScriptRemoteLocation_mock, getOsCheckScript_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) getOsCheckScript_mock.return_value = "OsCheckScript" getOsCheckScriptRemoteLocation_mock.return_value = "OsCheckScriptRemoteLocation" expected = 42 init_mock.return_value = None run_mock.return_value = expected res = bootstrap_obj.copyOsCheckScript() self.assertEquals(res, expected) input_file = str(init_mock.call_args[0][3]) remote_file = str(init_mock.call_args[0][4]) self.assertEqual(input_file, "OsCheckScript") self.assertEqual(remote_file, "OsCheckScriptRemoteLocation")
def test_copyOsCheckScript(self, write_mock, run_mock, init_mock, getOsCheckScriptRemoteLocation_mock, getOsCheckScript_mock): shared_state = SharedState("root", "123", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) getOsCheckScript_mock.return_value = "OsCheckScript" getOsCheckScriptRemoteLocation_mock.return_value = "OsCheckScriptRemoteLocation" expected = 42 init_mock.return_value = None run_mock.return_value = expected res = bootstrap_obj.copyOsCheckScript() self.assertEquals(res, expected) input_file = str(init_mock.call_args[0][4]) remote_file = str(init_mock.call_args[0][5]) self.assertEqual(input_file, "OsCheckScript") self.assertEqual(remote_file, "OsCheckScriptRemoteLocation")
def test_getRunSetupCommand(self, getRunSetupWithoutPasswordCommand_mock, getRunSetupWithPasswordCommand_mock, hasPassword_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) # With password hasPassword_mock.return_value = True getRunSetupWithPasswordCommand_mock.return_value = "RunSetupWithPasswordCommand" getRunSetupWithoutPasswordCommand_mock.return_value = "RunSetupWithoutPasswordCommand" res = bootstrap_obj.getRunSetupCommand("dummy-host") self.assertEqual(res, "RunSetupWithPasswordCommand") # Without password hasPassword_mock.return_value = False res = bootstrap_obj.getRunSetupCommand("dummy-host") self.assertEqual(res, "RunSetupWithoutPasswordCommand")
def test_getRepoFile(self, is_redhat_family, is_ubuntu_family, is_suse_family, hasPassword_mock, getRemoteName_mock): shared_state = SharedState("root", "123", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") is_redhat_family.return_value = True is_ubuntu_family.return_value = False is_suse_family.return_value = False bootstrap_obj = Bootstrap("hostname", shared_state) # Without password hasPassword_mock.return_value = False getRemoteName_mock.return_value = "RemoteName" rf = bootstrap_obj.getMoveRepoFileCommand("target") self.assertEquals(rf, "/var/lib/ambari-agent/tmp/ambari-sudo.sh mv RemoteName target/ambari.repo") # With password hasPassword_mock.return_value = True getRemoteName_mock.return_value = "RemoteName" rf = bootstrap_obj.getMoveRepoFileCommand("target") self.assertEquals(rf, "/var/lib/ambari-agent/tmp/ambari-sudo.sh -S mv RemoteName target/ambari.repo < RemoteName")
def test_createTargetDir(self, write_mock, run_mock, init_mock): shared_state = SharedState("root", "123", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) expected = 42 init_mock.return_value = None run_mock.return_value = expected res = bootstrap_obj.createTargetDir() self.assertEquals(res, expected) command = str(init_mock.call_args[0][4]) self.assertEqual( command, "SUDO=$([ \"$EUID\" -eq 0 ] && echo || echo sudo) ; $SUDO mkdir -p /var/lib/ambari-agent/tmp ; " "$SUDO chown -R root /var/lib/ambari-agent/tmp ; " "$SUDO chmod 755 /var/lib/ambari-agent ; " "$SUDO chmod 755 /var/lib/ambari-agent/data ; " "$SUDO chmod 1777 /var/lib/ambari-agent/tmp")
def test_copyNeededFiles(self, write_mock, ssh_run_mock, ssh_init_mock, scp_run_mock, scp_init_mock, getRemoteName_mock, getRepoFile_mock, getRepoDir, getMoveRepoFileCommand): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440") bootstrap_obj = Bootstrap("hostname", shared_state) getMoveRepoFileCommand.return_value = "MoveRepoFileCommand" getRepoDir.return_value = "RepoDir" getRemoteName_mock.return_value = "RemoteName" getRepoFile_mock.return_value = "RepoFile" expected1 = 42 expected2 = 17 expected3 = 1 scp_init_mock.return_value = None ssh_init_mock.return_value = None # Testing max retcode return scp_run_mock.side_effect = [expected1, expected3] ssh_run_mock.side_effect = [expected2] res = bootstrap_obj.copyNeededFiles() self.assertEquals(res, expected1) input_file = str(scp_init_mock.call_args[0][3]) remote_file = str(scp_init_mock.call_args[0][4]) self.assertEqual(input_file, "setupAgentFile") self.assertEqual(remote_file, "RemoteName") command = str(ssh_init_mock.call_args[0][3]) self.assertEqual(command, "MoveRepoFileCommand") # Another order expected1 = 0 expected2 = 17 expected3 = 1 scp_run_mock.side_effect = [expected1, expected3] ssh_run_mock.side_effect = [expected2] res = bootstrap_obj.copyNeededFiles() self.assertEquals(res, expected2) # yet another order expected1 = 33 expected2 = 17 expected3 = 42 scp_run_mock.side_effect = [expected1, expected3] ssh_run_mock.side_effect = [expected2] res = bootstrap_obj.copyNeededFiles() self.assertEquals(res, expected3)
def test_try_to_execute(self, write_mock): expected = 43 shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440") bootstrap_obj = Bootstrap("hostname", shared_state) # Normal case ret = bootstrap_obj.try_to_execute(lambda: expected) self.assertEqual(ret, expected) self.assertFalse(write_mock.called) write_mock.reset_mock() # Exception scenario def act(): raise IOError() ret = bootstrap_obj.try_to_execute(act) self.assertEqual(ret, 177) self.assertTrue(write_mock.called)
def test_bootstrap_resample(): """Tests the resample method of the Bootstrap class Tests comprise of simple resampling cases for which we can evalue the exact answer by hand. """ # All data is the same, variance should be zero. OLS = LeastSquares() DM = DesignMatrix('polynomial', 3) bootstrap = Bootstrap(OLS, DM) x = np.ones(10) * 2.25 y = x**3 #bootstrap.resample(x, y, 10) # This fails with an raise LinAlgError("Singular matrix") error on # TravisCI, but passes locally. Removing for now. #assert bootstrap.betaVariance == pytest.approx(np.zeros(4), abs=1e-15) # Ensure that larger noise in the data set gives larger computed # variance in the beta values from resampling. functions = { 0: lambda x: np.sin(x), 1: lambda x: np.cos(x), 2: lambda x: np.sin(2 * x), 3: lambda x: np.cos(2 * x), 4: lambda x: np.sin(3 * x), 5: lambda x: np.cos(3 * x), 6: lambda x: np.sin(4 * x), 7: lambda x: np.cos(4 * x), 8: lambda x: np.sin(5 * x), 9: lambda x: np.cos(5 * x), 10: lambda x: np.sin(x)**2, 11: lambda x: np.cos(x)**2, 12: lambda x: np.sin(2 * x)**2, 13: lambda x: np.cos(2 * x)**2, 14: lambda x: np.sin(3 * x)**2, 15: lambda x: np.cos(3 * x)**2, } DM = DesignMatrix(lambda j, x: functions[j](x), 9) OLS = LeastSquares() bootstrap = Bootstrap(OLS, DM) N = 100 x = np.linspace(0, 2 * np.pi, N) meanBetaVariance = np.zeros(6) ind = 0 for noiseScale in [0.0, 0.1, 1.0]: y = np.sin(1.5 * x) - 0.5 * np.cos(2 * x)**2 + np.random.normal( 0, noiseScale, N) bootstrap.resample(x, y, 100) meanBetaVariance[ind] = np.mean(bootstrap.betaVariance) if ind > 0: assert meanBetaVariance[ind - 1] < meanBetaVariance[ind] ind += 1
def test_copyPasswordFile(self, write_mock, ssh_run_mock, ssh_init_mock, scp_run_mock, scp_init_mock, getPasswordFile_mock): shared_state = SharedState("root", "123", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root", password_file="PasswordFile") bootstrap_obj = Bootstrap("hostname", shared_state) getPasswordFile_mock.return_value = "PasswordFile" # Testing max retcode return expected1 = {"exitstatus": 42, "log": "log42", "errormsg": "errorMsg"} expected2 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} scp_init_mock.return_value = None scp_run_mock.return_value = expected1 ssh_init_mock.return_value = None ssh_run_mock.return_value = expected2 res = bootstrap_obj.copyPasswordFile() self.assertEquals(res, expected1["exitstatus"]) input_file = str(scp_init_mock.call_args[0][4]) remote_file = str(scp_init_mock.call_args[0][4]) self.assertEqual(input_file, "PasswordFile") self.assertEqual(remote_file, "PasswordFile") command = str(ssh_init_mock.call_args[0][4]) self.assertEqual(command, "chmod 600 PasswordFile") # Another order expected1 = {"exitstatus": 0, "log": "log0", "errormsg": "errorMsg"} expected2 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} scp_run_mock.return_value = expected1 ssh_run_mock.return_value = expected2
def test_getAmbariPort(self): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) self.assertEquals(bootstrap_obj.getAmbariPort(), "8440") shared_state.server_port = None bootstrap_obj = Bootstrap("hostname", shared_state) self.assertEquals(bootstrap_obj.getAmbariPort(), "null")
def test_getRemoteName(self): shared_state = SharedState("root", "123", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") res = bootstrap_obj = Bootstrap("hostname", shared_state) utime1 = 1234 utime2 = 12345 bootstrap_obj.getUtime = MagicMock(return_value=utime1) remote1 = bootstrap_obj.getRemoteName("/tmp/setupAgent.sh") self.assertEquals(remote1, "/tmp/setupAgent{0}.sh".format(utime1)) bootstrap_obj.getUtime.return_value=utime2 remote1 = bootstrap_obj.getRemoteName("/tmp/setupAgent.sh") self.assertEquals(remote1, "/tmp/setupAgent{0}.sh".format(utime1)) remote2 = bootstrap_obj.getRemoteName("/tmp/host_pass") self.assertEquals(remote2, "/tmp/host_pass{0}".format(utime2))
def __init__(self): Bootstrap.__init__(self) self._tot_packs = 1 self._done_packs = 0 progs.register("debootstrap")
def test_run(self, error_mock, warn_mock, write_mock, createDoneFile_mock, hasPassword_mock, try_to_execute_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) # Testing workflow without password bootstrap_obj.copied_password_file = False hasPassword_mock.return_value = False try_to_execute_mock.return_value = {"exitstatus": 0, "log":"log0", "errormsg":"errormsg0"} bootstrap_obj.run() self.assertEqual(try_to_execute_mock.call_count, 7) # <- Adjust if changed self.assertTrue(createDoneFile_mock.called) self.assertEqual(bootstrap_obj.getStatus()["return_code"], 0) try_to_execute_mock.reset_mock() createDoneFile_mock.reset_mock() # Testing workflow with password bootstrap_obj.copied_password_file = True hasPassword_mock.return_value = True try_to_execute_mock.return_value = {"exitstatus": 0, "log":"log0", "errormsg":"errormsg0"} bootstrap_obj.run() self.assertEqual(try_to_execute_mock.call_count, 10) # <- Adjust if changed self.assertTrue(createDoneFile_mock.called) self.assertEqual(bootstrap_obj.getStatus()["return_code"], 0) error_mock.reset_mock() write_mock.reset_mock() try_to_execute_mock.reset_mock() createDoneFile_mock.reset_mock() # Testing workflow when some action failed before copying password bootstrap_obj.copied_password_file = False hasPassword_mock.return_value = False try_to_execute_mock.side_effect = [{"exitstatus": 0, "log":"log0", "errormsg":"errormsg0"}, {"exitstatus": 1, "log":"log1", "errormsg":"errormsg1"}] bootstrap_obj.run() self.assertEqual(try_to_execute_mock.call_count, 2) # <- Adjust if changed self.assertTrue("ERROR" in error_mock.call_args[0][0]) self.assertTrue("ERROR" in write_mock.call_args[0][0]) self.assertTrue(createDoneFile_mock.called) self.assertEqual(bootstrap_obj.getStatus()["return_code"], 1) try_to_execute_mock.reset_mock() createDoneFile_mock.reset_mock() # Testing workflow when some action failed after copying password bootstrap_obj.copied_password_file = True hasPassword_mock.return_value = True try_to_execute_mock.side_effect = [{"exitstatus": 0, "log":"log0", "errormsg":"errormsg0"}, {"exitstatus": 42, "log":"log42", "errormsg":"errormsg42"}, {"exitstatus": 0, "log":"log0", "errormsg":"errormsg0"}] bootstrap_obj.run() self.assertEqual(try_to_execute_mock.call_count, 3) # <- Adjust if changed self.assertTrue(createDoneFile_mock.called) self.assertEqual(bootstrap_obj.getStatus()["return_code"], 42) error_mock.reset_mock() write_mock.reset_mock() try_to_execute_mock.reset_mock() createDoneFile_mock.reset_mock() # Testing workflow when some action failed after copying password and # removing password failed too bootstrap_obj.copied_password_file = True hasPassword_mock.return_value = True try_to_execute_mock.side_effect = [{"exitstatus": 0, "log":"log0", "errormsg":"errormsg0"}, {"exitstatus": 17, "log":"log17", "errormsg":"errormsg17"}, {"exitstatus": 19, "log":"log19", "errormsg":"errormsg19"}] bootstrap_obj.run() self.assertEqual(try_to_execute_mock.call_count, 3) # <- Adjust if changed self.assertTrue("ERROR" in write_mock.call_args_list[0][0][0]) self.assertTrue("ERROR" in error_mock.call_args[0][0]) self.assertTrue("WARNING" in write_mock.call_args_list[1][0][0]) self.assertTrue("WARNING" in warn_mock.call_args[0][0]) self.assertTrue(createDoneFile_mock.called) self.assertEqual(bootstrap_obj.getStatus()["return_code"], 17)
def main(): # Set up logging logging.basicConfig( level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%Y-%d-%m %I:%M:%S %p') # Get parameters from the provided parameter file param_file_path = sys.argv[1] params = parameters.get_params(param_file_path) zkey = params['zkey'] mkey = params['mkey'] rkey = params['ra_key'] dkey = params['dec_key'] t0 = time.time() if params['bins']['bin_in_lookback_time'] == True: z_pref = 'lookt' else: z_pref = 'z' # Stack in Slices or ALL AT ONCE Choice made here if params['bins']['stack_all_z_at_once'] == True: n_slices = 1 else: n_slices = len(params['bins']['z_nodes']) - 1 #Save Parameter file in folder save_paramfile(params) for i in range(n_slices): if params['bins']['stack_all_z_at_once'] == True: j = None stacked_flux_density_key = 'all_'+z_pref else: j = i if params['bins']['bin_in_lookback_time'] == True: stacked_flux_density_key = '{:.2f}'.format(params['bins']['t_nodes'][j])+'-'+'{:.2f}'.format(params['bins']['t_nodes'][j+1]) else: stacked_flux_density_key = str(params['bins']['t_nodes'][j])+'-'+str(params['bins']['t_nodes'][j+1]) print stacked_flux_density_key # From parameter file read maps, psfs, cats, and divide them into bins sky_library = get_maps(params) cats = get_catalogs(params) if params['bootstrap'] == True: pcat = Bootstrap(cats.table) # Bootstrap Loop Starts here for iboot in np.arange(params['number_of_boots'])+params['boot0']: #stacked_flux_densities = {} if params['bootstrap'] == True: print 'Running ' +str(int(iboot))+' of '+ str(int(params['boot0'])) +'-'+ str(int(params['boot0']+params['number_of_boots']-1)) + ' bootstraps' pcat.perturb_catalog(perturb_z = params['perturb_z']) bootcat = Field_catalogs(pcat.pseudo_cat,zkey=zkey,mkey=mkey,rkey=rkey,dkey=dkey) binned_ra_dec = get_bin_radec(params, bootcat, single_slice = j) if params['save_bin_ids'] == False: bin_ids = None else: bin_ids = get_bin_ids(params, bootcat, single_slice = j) out_file_path = params['io']['output_folder']+'/bootstrapped_fluxes/'+params['io']['shortname'] out_file_suffix = '_'+stacked_flux_density_key+'_boot_'+str(int(iboot)) else: binned_ra_dec = get_bin_radec(params, cats, single_slice = j) if params['save_bin_ids'] == False: bin_ids = None else: bin_ids = get_bin_ids(params, cats, single_slice = j) out_file_path = params['io']['output_folder'] + '/simstack_fluxes/' + params['io']['shortname'] out_file_suffix = '_'+stacked_flux_density_key # Do simultaneous stacking if params['float_background'] == True: stacked_flux_densities = stack_libraries_in_layers_w_background(sky_library,binned_ra_dec) else: stacked_flux_densities = stack_libraries_in_layers(sky_library,binned_ra_dec) save_stacked_fluxes(stacked_flux_densities,params, out_file_path,out_file_suffix, IDs=bin_ids) #pdb.set_trace() # Summarize timing t1 = time.time() tpass = t1-t0 logging.info("Done!") logging.info("") logging.info("Total time : {:.4f} minutes\n".format(tpass/60.))
def main(): """ It is possible to use this system to either train a new neural network model through the bootstrap system and Levin tree search (LTS) algorithm, or to use a trained neural network with LTS. """ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' parser = argparse.ArgumentParser() parser.add_argument('-l', action='store', dest='loss_function', default='CrossEntropyLoss', help='Loss Function') parser.add_argument('-p', action='store', dest='problems_folder', help='Folder with problem instances') parser.add_argument('-m', action='store', dest='model_name', help='Name of the folder of the neural model') parser.add_argument( '-a', action='store', dest='search_algorithm', help= 'Name of the search algorithm (Levin, LevinStar, AStar, GBFS, PUCT)') parser.add_argument('-d', action='store', dest='problem_domain', help='Problem domain (Witness or SlidingTile)') parser.add_argument( '-b', action='store', dest='search_budget', default=1000, help= 'The initial budget (nodes expanded) allowed to the bootstrap procedure' ) parser.add_argument( '-g', action='store', dest='gradient_steps', default=10, help= 'Number of gradient steps to be performed in each iteration of the Bootstrap system' ) parser.add_argument('-cpuct', action='store', dest='cpuct', default='1.0', help='Constant C used with PUCT.') parser.add_argument('-time', action='store', dest='time_limit', default='43200', help='Time limit in seconds for search') parser.add_argument('-mix', action='store', dest='mix_epsilon', default='0.0', help='Mixture with a uniform policy') parser.add_argument('-w', action='store', dest='weight_astar', default='1.0', help='Weight to be used with WA*.') parser.add_argument('--default-heuristic', action='store_true', default=False, dest='use_heuristic', help='Use the default heuristic as input') parser.add_argument('--learned-heuristic', action='store_true', default=False, dest='use_learned_heuristic', help='Use/learn a heuristic') parser.add_argument('--blind-search', action='store_true', default=False, dest='blind_search', help='Perform blind search') parser.add_argument( '--learn', action='store_true', default=False, dest='learning_mode', help= 'Train as neural model out of the instances from the problem folder') parser.add_argument( '--fixed-time', action='store_true', default=False, dest='fixed_time', help= 'Run the planner for a fixed amount of time (specified by time_limit) for each problem instance' ) parser.add_argument( '-number-test-instances', action='store', dest='number_test_instances', default='0', help= 'Maximum number of test instances (value of zero will use all instances in the test file).' ) parameters = parser.parse_args() states = {} if parameters.problem_domain == 'SlidingTile': puzzle_files = [ f for f in listdir(parameters.problems_folder) if isfile(join(parameters.problems_folder, f)) ] j = 1 for filename in puzzle_files: with open(join(parameters.problems_folder, filename), 'r') as file: problems = file.readlines() for i in range(len(problems)): puzzle = SlidingTilePuzzle(problems[i]) states['puzzle_' + str(j)] = puzzle j += 1 elif parameters.problem_domain == 'Witness': puzzle_files = [ f for f in listdir(parameters.problems_folder) if isfile(join(parameters.problems_folder, f)) ] j = 1 for filename in puzzle_files: if '.' in filename: continue with open(join(parameters.problems_folder, filename), 'r') as file: puzzle = file.readlines() i = 0 while i < len(puzzle): k = i while k < len(puzzle) and puzzle[k] != '\n': k += 1 s = WitnessState() s.read_state_from_string(puzzle[i:k]) states['puzzle_' + str(j)] = s i = k + 1 j += 1 # s.read_state(join(parameters.problems_folder, filename)) # states[filename] = s elif parameters.problem_domain == 'Sokoban': problem = [] puzzle_files = [] if isfile(parameters.problems_folder): puzzle_files.append(parameters.problems_folder) else: puzzle_files = [ join(parameters.problems_folder, f) for f in listdir(parameters.problems_folder) if isfile(join(parameters.problems_folder, f)) ] problem_id = 0 for filename in puzzle_files: with open(filename, 'r') as file: all_problems = file.readlines() for line_in_problem in all_problems: if ';' in line_in_problem: if len(problem) > 0: puzzle = Sokoban(problem) states['puzzle_' + str(problem_id)] = puzzle problem = [] # problem_id = line_in_problem.split(' ')[1].split('\n')[0] problem_id += 1 elif '\n' != line_in_problem: problem.append(line_in_problem.split('\n')[0]) if len(problem) > 0: puzzle = Sokoban(problem) states['puzzle_' + str(problem_id)] = puzzle if int(parameters.number_test_instances) != 0: states_capped = {} counter = 0 for name, puzzle in states.items(): states_capped[name] = puzzle counter += 1 if counter == int(parameters.number_test_instances): break states = states_capped print('Loaded ', len(states), ' instances') # input_size = s.get_image_representation().shape KerasManager.register('KerasModel', KerasModel) ncpus = int(os.environ.get('SLURM_CPUS_PER_TASK', default=1)) k_expansions = 32 # print('Number of cpus available: ', ncpus) start = time.time() with KerasManager() as manager: nn_model = manager.KerasModel() bootstrap = None if parameters.learning_mode: bootstrap = Bootstrap(states, parameters.model_name, ncpus=ncpus, initial_budget=int(parameters.search_budget), gradient_steps=int( parameters.gradient_steps)) if parameters.search_algorithm == 'PUCT': bfs_planner = PUCT(parameters.use_heuristic, parameters.use_learned_heuristic, k_expansions, float(parameters.cpuct)) if parameters.use_learned_heuristic: nn_model.initialize(parameters.loss_function, parameters.search_algorithm, two_headed_model=True) else: nn_model.initialize(parameters.loss_function, parameters.search_algorithm, two_headed_model=False) if parameters.learning_mode: #bootstrap_learning_bfs(states, bfs_planner, nn_model, parameters.model_name, int(parameters.search_budget), ncpus) bootstrap.solve_uniform_online(bfs_planner, nn_model) elif parameters.blind_search: search(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit), int(parameters.search_budget)) elif parameters.fixed_time: nn_model.load_weights( join('trained_models_online', parameters.model_name, 'model_weights')) search_time_limit(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit)) else: nn_model.load_weights( join('trained_models_online', parameters.model_name, 'model_weights')) search(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit), int(parameters.search_budget)) if parameters.search_algorithm == 'Levin' or parameters.search_algorithm == 'LevinStar': if parameters.search_algorithm == 'Levin': bfs_planner = BFSLevin(parameters.use_heuristic, parameters.use_learned_heuristic, False, k_expansions, float(parameters.mix_epsilon)) else: bfs_planner = BFSLevin(parameters.use_heuristic, parameters.use_learned_heuristic, True, k_expansions, float(parameters.mix_epsilon)) if parameters.use_learned_heuristic: nn_model.initialize(parameters.loss_function, parameters.search_algorithm, two_headed_model=True) else: nn_model.initialize(parameters.loss_function, parameters.search_algorithm, two_headed_model=False) if parameters.learning_mode: # bootstrap_learning_bfs(states, bfs_planner, nn_model, parameters.model_name, int(parameters.search_budget), ncpus) bootstrap.solve_uniform_online(bfs_planner, nn_model) elif parameters.blind_search: search(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit), int(parameters.search_budget)) elif parameters.fixed_time: nn_model.load_weights( join('trained_models_online', parameters.model_name, 'model_weights')) search_time_limit(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit)) else: nn_model.load_weights( join('trained_models_online', parameters.model_name, 'model_weights')) search(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit), int(parameters.search_budget)) if parameters.search_algorithm == 'LevinMult': bfs_planner = BFSLevinMult(parameters.use_heuristic, parameters.use_learned_heuristic, k_expansions) if parameters.use_learned_heuristic: nn_model.initialize(parameters.loss_function, parameters.search_algorithm, two_headed_model=True) else: nn_model.initialize(parameters.loss_function, parameters.search_algorithm, two_headed_model=False) if parameters.learning_mode: # bootstrap_learning_bfs(states, bfs_planner, nn_model, parameters.model_name, int(parameters.search_budget), ncpus) bootstrap.solve_uniform_online(bfs_planner, nn_model) elif parameters.blind_search: search(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit), int(parameters.search_budget)) else: nn_model.load_weights( join('trained_models_online', parameters.model_name, 'model_weights')) search(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit), int(parameters.search_budget)) if parameters.search_algorithm == 'AStar': bfs_planner = AStar(parameters.use_heuristic, parameters.use_learned_heuristic, k_expansions, float(parameters.weight_astar)) if parameters.learning_mode and parameters.use_learned_heuristic: nn_model.initialize(parameters.loss_function, parameters.search_algorithm) # bootstrap_learning_bfs(states, bfs_planner, nn_model, parameters.model_name, int(parameters.search_budget), ncpus) bootstrap.solve_uniform_online(bfs_planner, nn_model) elif parameters.fixed_time and parameters.use_learned_heuristic: nn_model.initialize(parameters.loss_function, parameters.search_algorithm) nn_model.load_weights( join('trained_models_online', parameters.model_name, 'model_weights')) search_time_limit(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit)) elif parameters.use_learned_heuristic: nn_model.initialize(parameters.loss_function, parameters.search_algorithm) nn_model.load_weights( join('trained_models_online', parameters.model_name, 'model_weights')) search(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit), int(parameters.search_budget)) else: search(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit), int(parameters.search_budget)) if parameters.search_algorithm == 'GBFS': bfs_planner = GBFS(parameters.use_heuristic, parameters.use_learned_heuristic, k_expansions) if parameters.learning_mode: nn_model.initialize(parameters.loss_function, parameters.search_algorithm) # bootstrap_learning_bfs(states, bfs_planner, nn_model, parameters.model_name, int(parameters.search_budget), ncpus) bootstrap.solve_uniform_online(bfs_planner, nn_model) elif parameters.fixed_time and parameters.use_learned_heuristic: nn_model.initialize(parameters.loss_function, parameters.search_algorithm) nn_model.load_weights( join('trained_models_online', parameters.model_name, 'model_weights')) search_time_limit(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit)) elif parameters.use_learned_heuristic: nn_model.initialize(parameters.loss_function, parameters.search_algorithm) nn_model.load_weights( join('trained_models_online', parameters.model_name, 'model_weights')) search(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit), int(parameters.search_budget)) else: search(states, bfs_planner, nn_model, ncpus, int(parameters.time_limit), int(parameters.search_budget)) print('Total time: ', time.time() - start)
def test_copyNeededFiles(self, write_mock, ssh_run_mock, ssh_init_mock, scp_run_mock, scp_init_mock, getRemoteName_mock, getRepoFile_mock, getRepoDir, getMoveRepoFileCommand, is_redhat_family, is_ubuntu_family, is_suse_family, os_path_exists_mock): # # Ambari repo file exists # def os_path_exists_side_effect(*args, **kwargs): if args[0] == getRepoFile_mock(): return True else: return False os_path_exists_mock.side_effect = os_path_exists_side_effect os_path_exists_mock.return_value = None shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") is_redhat_family.return_value = True is_ubuntu_family.return_value = False is_suse_family.return_value = False bootstrap_obj = Bootstrap("hostname", shared_state) getMoveRepoFileCommand.return_value = "MoveRepoFileCommand" getRepoDir.return_value = "RepoDir" getRemoteName_mock.return_value = "RemoteName" getRepoFile_mock.return_value = "RepoFile" expected1 = {"exitstatus": 42, "log": "log42", "errormsg": "errorMsg"} expected2 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} expected3 = {"exitstatus": 1, "log": "log1", "errormsg": "errorMsg"} expected4 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} scp_init_mock.return_value = None ssh_init_mock.return_value = None # Testing max retcode return scp_run_mock.side_effect = [expected1, expected3] ssh_run_mock.side_effect = [expected2, expected4] res = bootstrap_obj.copyNeededFiles() self.assertEquals(res, expected1["exitstatus"]) input_file = str(scp_init_mock.call_args[0][3]) remote_file = str(scp_init_mock.call_args[0][4]) self.assertEqual(input_file, "setupAgentFile") self.assertEqual(remote_file, "RemoteName") command = str(ssh_init_mock.call_args[0][3]) self.assertEqual(command, "sudo chmod 644 RepoFile") # Another order expected1 = {"exitstatus": 0, "log": "log0", "errormsg": "errorMsg"} expected2 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} expected3 = {"exitstatus": 1, "log": "log1", "errormsg": "errorMsg"} expected4 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} scp_run_mock.side_effect = [expected1, expected3] ssh_run_mock.side_effect = [expected2, expected4] res = bootstrap_obj.copyNeededFiles() self.assertEquals(res, expected2["exitstatus"]) # yet another order expected1 = {"exitstatus": 33, "log": "log33", "errormsg": "errorMsg"} expected2 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} expected3 = {"exitstatus": 42, "log": "log42", "errormsg": "errorMsg"} expected4 = {"exitstatus": 17, "log": "log17", "errormsg": "errorMsg"} scp_run_mock.side_effect = [expected1, expected3] ssh_run_mock.side_effect = [expected2, expected4] res = bootstrap_obj.copyNeededFiles() self.assertEquals(res, expected3["exitstatus"]) # #Ambari repo file does not exist # os_path_exists_mock.side_effect = None os_path_exists_mock.return_value = False #Expectations: # SSH will not be called at all # SCP will be called once for copying the setup script file scp_run_mock.reset_mock() ssh_run_mock.reset_mock() expectedResult = { "exitstatus": 33, "log": "log33", "errormsg": "errorMsg" } scp_run_mock.side_effect = [expectedResult] res = bootstrap_obj.copyNeededFiles() self.assertFalse(ssh_run_mock.called) self.assertEquals(res, expectedResult["exitstatus"])
import sys from bootstrap import Bootstrap bootstrap = Bootstrap() bootstrap.load_params(sys.argv).exec()
def test_getSetupScript(self): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) self.assertEquals(bootstrap_obj.shared_state.script_dir, "scriptDir")
def test_run(self, error_mock, warn_mock, write_mock, createDoneFile_mock, hasPassword_mock, try_to_execute_mock): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) # Testing workflow without password bootstrap_obj.copied_password_file = False hasPassword_mock.return_value = False try_to_execute_mock.return_value = { "exitstatus": 0, "log": "log0", "errormsg": "errormsg0" } bootstrap_obj.run() self.assertEqual(try_to_execute_mock.call_count, 7) # <- Adjust if changed self.assertTrue(createDoneFile_mock.called) self.assertEqual(bootstrap_obj.getStatus()["return_code"], 0) try_to_execute_mock.reset_mock() createDoneFile_mock.reset_mock() # Testing workflow with password bootstrap_obj.copied_password_file = True hasPassword_mock.return_value = True try_to_execute_mock.return_value = { "exitstatus": 0, "log": "log0", "errormsg": "errormsg0" } bootstrap_obj.run() self.assertEqual(try_to_execute_mock.call_count, 10) # <- Adjust if changed self.assertTrue(createDoneFile_mock.called) self.assertEqual(bootstrap_obj.getStatus()["return_code"], 0) error_mock.reset_mock() write_mock.reset_mock() try_to_execute_mock.reset_mock() createDoneFile_mock.reset_mock() # Testing workflow when some action failed before copying password bootstrap_obj.copied_password_file = False hasPassword_mock.return_value = False try_to_execute_mock.side_effect = [{ "exitstatus": 0, "log": "log0", "errormsg": "errormsg0" }, { "exitstatus": 1, "log": "log1", "errormsg": "errormsg1" }] bootstrap_obj.run() self.assertEqual(try_to_execute_mock.call_count, 2) # <- Adjust if changed self.assertTrue("ERROR" in error_mock.call_args[0][0]) self.assertTrue("ERROR" in write_mock.call_args[0][0]) self.assertTrue(createDoneFile_mock.called) self.assertEqual(bootstrap_obj.getStatus()["return_code"], 1) try_to_execute_mock.reset_mock() createDoneFile_mock.reset_mock() # Testing workflow when some action failed after copying password bootstrap_obj.copied_password_file = True hasPassword_mock.return_value = True try_to_execute_mock.side_effect = [{ "exitstatus": 0, "log": "log0", "errormsg": "errormsg0" }, { "exitstatus": 42, "log": "log42", "errormsg": "errormsg42" }, { "exitstatus": 0, "log": "log0", "errormsg": "errormsg0" }] bootstrap_obj.run() self.assertEqual(try_to_execute_mock.call_count, 3) # <- Adjust if changed self.assertTrue(createDoneFile_mock.called) self.assertEqual(bootstrap_obj.getStatus()["return_code"], 42) error_mock.reset_mock() write_mock.reset_mock() try_to_execute_mock.reset_mock() createDoneFile_mock.reset_mock() # Testing workflow when some action failed after copying password and # removing password failed too bootstrap_obj.copied_password_file = True hasPassword_mock.return_value = True try_to_execute_mock.side_effect = [{ "exitstatus": 0, "log": "log0", "errormsg": "errormsg0" }, { "exitstatus": 17, "log": "log17", "errormsg": "errormsg17" }, { "exitstatus": 19, "log": "log19", "errormsg": "errormsg19" }] bootstrap_obj.run() self.assertEqual(try_to_execute_mock.call_count, 3) # <- Adjust if changed self.assertTrue("ERROR" in write_mock.call_args_list[0][0][0]) self.assertTrue("ERROR" in error_mock.call_args[0][0]) self.assertTrue("WARNING" in write_mock.call_args_list[1][0][0]) self.assertTrue("WARNING" in warn_mock.call_args[0][0]) self.assertTrue(createDoneFile_mock.called) self.assertEqual(bootstrap_obj.getStatus()["return_code"], 17)
def test_generateRandomFileName(self): shared_state = SharedState("root", "sshkey_file", "scriptDir", "bootdir", "setupAgentFile", "ambariServer", "centos6", None, "8440", "root") bootstrap_obj = Bootstrap("hostname", shared_state) self.assertTrue(bootstrap_obj.generateRandomFileName(None) == bootstrap_obj.getUtime())
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # This script is called after the plugin files have been copied to the target host file system from bootstrap import Bootstrap from modules import util import sys def isPythonVesrsionSupported(): currentPythonVersion = sys.version_info pluginSupportedPythonVersions = ['2.7.5','2.7.6','2.7.7','2.7.8','2.7.9','2.7.10','2.7.11'] isPythonVersionSupported = False currentPythonVersions = str(currentPythonVersion[0]) + "." +str(currentPythonVersion[1]) +"."+str(currentPythonVersion[2]) for version in pluginSupportedPythonVersions: if currentPythonVersions == version: isPythonVersionSupported = True break return isPythonVersionSupported if __name__ == "__main__": if (isPythonVesrsionSupported()): bootstrap = Bootstrap() bootstrap.setup() else: currentPythonVersion = sys.version_info util.sendEvent("Plugin vmware:", "Python version not supported: [" + str(currentPythonVersion[0]) + "." +str(currentPythonVersion[1]) +"."+str(currentPythonVersion[2])+"]", "error") sys.exit(-1)
#!/usr/bin/env python from bootstrap import Bootstrap #main app entry point if __name__ == "__main__": Bootstrap.run()