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_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 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 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 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_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)