def test_delete_report(self): status = DeployStatus() ping_report = {} ping_report['deployId'] = '123' ping_report['envId'] = '234' ping_report['envName'] = 'abc' ping_report['stageName'] = 'beta' ping_report['deployStage'] = DeployStage.SERVING_BUILD ping_report['status'] = AgentStatus.SUCCEEDED status.report = PingReport(jsonValue=ping_report) envs = {'abc': status} client = mock.Mock() estatus = mock.Mock() estatus.load_envs = mock.Mock(return_value=envs) deploy_goal = {} deploy_goal['deployId'] = '123' deploy_goal['envId'] = '234' deploy_goal['envName'] = 'abc' deploy_goal['stageName'] = 'beta' ping_response = {'deployGoal': deploy_goal, 'opCode': OpCode.DELETE} responses = [ PingResponse(jsonValue=ping_response), PingResponse(jsonValue=self.ping_noop_response) ] client.send_reports = mock.Mock(side_effect=responses) agent = DeployAgent(client=client, estatus=estatus, conf=self.config, executor=self.executor, helper=self.helper) agent.serve_build() calls = [mock.call(envs), mock.call({})] client.send_reports.assert_has_calls(calls) self.assertIsNone(agent._curr_report) self.assertEqual(agent._envs, {})
def test_deploy_stage_trnasition(self): report = self._new_report() deploy_status = DeployStatus() deploy_status.report = report env_status = {self.env_name: deploy_status} deployStages = [ "PRE_DOWNLOAD", "DOWNLOADING", "POST_DOWNLOAD", "STAGING", "PRE_RESTART", "RESTARTING", "POST_RESTART", "SERVING_BUILD", ] for i in range(0, len(deployStages)): response = self.client.send_reports(env_status) self.assertEqual(response.opCode, "DEPLOY") self.assertEqual(response.deployGoal.deployStage, deployStages[i]) report.deployStage = response.deployGoal.deployStage report.deployId = response.deployGoal.deployId # test ending case response = self.client.send_reports(env_status) self.assertEqual(response.deployGoal, None)
def test_unknow_status_cause_retry(self): report = self._new_report() deploy_status = DeployStatus() deploy_status.report = report env_status = {self.env_name : deploy_status} report.status = AgentStatus.UNKNOWN response = self.client.send_reports(env_status) report.deployStage = response.deployGoal.deployStage report.deployId = response.deployGoal.deployId response = self.client.send_reports(env_status) self.assertEqual(response.deployGoal.deployStage, 'PRE_DOWNLOAD')
def test_errorcode_stop_deployment(self): report = self._new_report() deploy_status = DeployStatus() deploy_status.report = report env_status = {self.env_name : deploy_status} # first try is allowed. report.errorCode = 123 response = self.client.send_reports(env_status) report.deployStage = response.deployGoal.deployStage report.deployId = response.deployGoal.deployId response = self.client.send_reports(env_status) self.assertEqual(response, None)
def _update_internal_deploy_goal(self, response): deploy_goal = response.deployGoal if not deploy_goal: log.info('No deploy goal to be updated.') return DeployReport(status_code=AgentStatus.SUCCEEDED) # use envName as status map key env_name = deploy_goal.envName if (self._envs is None) or (self._envs.get(env_name) is None): self._envs[env_name] = DeployStatus() # update deploy_status from response for the environment self._envs[env_name].update_by_response(response) # update script variables if deploy_goal.scriptVariables: log.info('Start to generate script variables for deploy: {}'. format(deploy_goal.deployId)) env_dir = self._config.get_agent_directory() working_dir = os.path.join(env_dir, "{}_SCRIPT_CONFIG".format(env_name)) with open(working_dir, "w+") as f: for key, value in deploy_goal.scriptVariables.items(): f.write("{}={}\n".format(key, value)) # load deploy goal to the config self._curr_report = self._envs[env_name] self._config.update_variables(self._curr_report) self._executor.update_configs(self._config) log.info('current deploy goal is: {}'.format(deploy_goal)) return DeployReport(status_code=AgentStatus.SUCCEEDED)
def test_deploy_stage_trnasition(self): report = self._new_report() deploy_status = DeployStatus() deploy_status.report = report env_status = {self.env_name : deploy_status} deployStages = ['PRE_DOWNLOAD', 'DOWNLOADING', 'POST_DOWNLOAD', 'STAGING', 'PRE_RESTART', 'RESTARTING', 'POST_RESTART', 'SERVING_BUILD'] for i in range(0, len(deployStages)): response = self.client.send_reports(env_status) self.assertEqual(response.opCode, "DEPLOY") self.assertEqual(response.deployGoal.deployStage, deployStages[i]) report.deployStage = response.deployGoal.deployStage report.deployId = response.deployGoal.deployId # test ending case response = self.client.send_reports(env_status) self.assertEqual(response.deployGoal, None)
def test_get_target(self): deploy_goal = {} deploy_goal['deployId'] = '123' deploy_goal['stageName'] = 'beta' deploy_goal['envName'] = 'pinboard' deploy_goal['deployStage'] = DeployStage.SERVING_BUILD ping_response = {'deployGoal': deploy_goal, 'opCode': OpCode.NOOP} response = PingResponse(jsonValue=ping_response) self.config.update_variables(DeployStatus(response)) self.assertEqual(os.environ['DEPLOY_ID'], '123') self.assertEqual(os.environ['ENV_NAME'], 'pinboard') self.assertEqual(os.environ['STAGE_NAME'], 'beta') self.assertEqual(self.config.get_target(), '/tmp/pinboard')
def test_report_health(self): status = DeployStatus() ping_report = {} ping_report['deployId'] = '123' ping_report['envId'] = '234' ping_report['envName'] = 'abc' ping_report['stageName'] = 'beta' ping_report['deployStage'] = DeployStage.SERVING_BUILD ping_report['status'] = AgentStatus.SUCCEEDED status.report = PingReport(jsonValue=ping_report) envs = {'234': status} client = mock.Mock() estatus = mock.Mock() estatus.load_envs = mock.Mock(return_value=envs) client.send_reports = \ mock.Mock(return_value=PingResponse(jsonValue=self.ping_noop_response)) agent = DeployAgent(client=client, estatus=estatus, conf=self.config, executor=self.executor, helper=self.helper) agent.serve_build() client.send_reports.assert_called_once_with(envs) self.assertEqual(agent._curr_report.report.envId, '234') self.assertEqual(agent._curr_report.report.deployStage, DeployStage.SERVING_BUILD) self.assertEqual(agent._curr_report.report.status, AgentStatus.SUCCEEDED)
def test_load_dump_file(self): fn = tempfile.mkstemp()[1] env_status = EnvStatus(fn) status1 = DeployStatus() ping_report = {} ping_report['deployId'] = 'deploy1' ping_report['envId'] = 'envId1' ping_report['envName'] = 'env1' ping_report['stageName'] = 'beta' ping_report['deployStage'] = DeployStage.POST_RESTART ping_report['status'] = AgentStatus.AGENT_FAILED ping_report['errorCode'] = 1 ping_report['errorMessage'] = 'Fail to open files' status1.report = PingReport(jsonValue=ping_report) status1.build_info = BuildInfo(commit='abc', build_url='http://google.com', build_id='234') status2 = DeployStatus() ping_report = {} ping_report['deployId'] = 'deploy2' ping_report['envId'] = 'envId2' ping_report['envName'] = 'env2' ping_report['stageName'] = 'prod' ping_report['deployStage'] = DeployStage.SERVING_BUILD ping_report['status'] = AgentStatus.SUCCEEDED status2.report = PingReport(jsonValue=ping_report) status2.build_info = BuildInfo(commit='bcd', build_url='http://pinterest.com', build_id='234') envs = {'env1': status1, 'env2': status2} env_status.dump_envs(envs) envs2 = env_status.load_envs() self.assertEqual(envs['env1'].report.status, envs2['env1'].report.status) self.assertEqual(envs['env1'].report.errorMessage, envs2['env1'].report.errorMessage) self.assertEqual(envs['env1'].build_info.build_commit, envs2['env1'].build_info.build_commit) self.assertEqual(envs['env2'].report.deployStage, envs2['env2'].report.deployStage) self.assertEqual(envs['env2'].build_info.build_url, envs2['env2'].build_info.build_url) os.remove(fn)
def load_envs(self): """ open up config file validate that the service selected exists """ envs = {} try: with self._lock, open(self._status_fn, 'r+') as config_fn: data = json.load(config_fn) log.debug('load status file: {}'.format(data)) envs = { key: DeployStatus(json_value=d) for key, d in data.items() } except IOError: log.info( "Could not find file {}. It happens when run deploy-agent the " "first time, or there is no deploy yet.".format( self._status_fn)) return {} except Exception: log.exception("Something went wrong in load_envs") finally: return envs
def test_load_dump_file(self): fn = tempfile.mkstemp()[1] env_status = EnvStatus(fn) status1 = DeployStatus() ping_report = {} ping_report['deployId'] = 'deploy1' ping_report['envId'] = 'envId1' ping_report['envName'] = 'env1' ping_report['stageName'] = 'beta' ping_report['deployStage'] = DeployStage.POST_RESTART ping_report['status'] = AgentStatus.AGENT_FAILED ping_report['errorCode'] = 1 ping_report['errorMessage'] = 'Fail to open files' status1.report = PingReport(jsonValue=ping_report) status1.build_info = BuildInfo(commit='abc', build_url='http://google.com', build_id='234') status2 = DeployStatus() ping_report = {} ping_report['deployId'] = 'deploy2' ping_report['envId'] = 'envId2' ping_report['envName'] = 'env2' ping_report['stageName'] = 'prod' ping_report['deployStage'] = DeployStage.SERVING_BUILD ping_report['status'] = AgentStatus.SUCCEEDED status2.report = PingReport(jsonValue=ping_report) status2.build_info = BuildInfo(commit='bcd', build_url='http://pinterest.com', build_id='234') envs = { 'env1': status1, 'env2': status2} env_status.dump_envs(envs) envs2 = env_status.load_envs() self.assertEqual(envs['env1'].report.status, envs2['env1'].report.status) self.assertEqual(envs['env1'].report.errorMessage, envs2['env1'].report.errorMessage) self.assertEqual(envs['env1'].build_info.build_commit, envs2['env1'].build_info.build_commit) self.assertEqual(envs['env2'].report.deployStage, envs2['env2'].report.deployStage) self.assertEqual(envs['env2'].build_info.build_url, envs2['env2'].build_info.build_url) os.remove(fn)