Exemplo n.º 1
0
    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, {})
Exemplo n.º 2
0
    def test_report_with_deploy_goal(self):
        if os.path.exists('/tmp/env_status'):
            os.remove('/tmp/env_status')

        build = {}
        build['id'] = '123'
        build['url'] = 'https://test'
        client = mock.Mock()
        deploy_goal = {}
        deploy_goal['deployId'] = '123'
        deploy_goal['envName'] = '456'
        deploy_goal['envId'] = '789'
        deploy_goal['stageName'] = 'beta'
        deploy_goal['deployStage'] = DeployStage.PRE_DOWNLOAD
        deploy_goal['scriptVariables'] = build
        ping_response = {'deployGoal': deploy_goal, 'opCode': OpCode.DEPLOY}

        responses = [
            PingResponse(jsonValue=ping_response),
            PingResponse(jsonValue=self.ping_noop_response)
        ]
        client.send_reports = mock.Mock(side_effect=responses)
        agent = DeployAgent(client=client, estatus=self.estatus, conf=self.config,
                            executor=self.executor, helper=self.helper)
        agent.process_deploy = mock.Mock(return_value=(DeployReport(AgentStatus.SUCCEEDED)))
        agent.serve_build()
        self.assertEqual(agent._curr_report.report.envId, '789')
        self.assertEqual(agent._curr_report.report.deployStage, DeployStage.PRE_DOWNLOAD)
        self.assertEqual(len(agent._envs), 1)
Exemplo n.º 3
0
    def _create_response(self, report):
        # check if this is the first step
        if report is None or report.deployId is None or report.deployId != self._deploy_id:
            # first report from agent, start first deploy stage.
            return self._new_response_value(DeployStage.PRE_DOWNLOAD)
        if report.errorCode != 0:
            # terminate the deployment.
            return None
        numeric_deploy_stage = DeployStage._NAMES_TO_VALUES[report.deployStage]
        if report.status == AgentStatus.SUCCEEDED:
            # check if this is the last deploy stage.
            if numeric_deploy_stage == DeployStage.SERVING_BUILD:
                return PingResponse({'opCode': OperationCode.NOOP})

            # move to next deploy stage
            next_deploy_stage = _DEPLOY_STAGE_TRANSITIONS.get(
                numeric_deploy_stage)
            return self._new_response_value(next_deploy_stage)

        if report.status == AgentStatus.UNKNOWN:
            # retry current stage
            return self._new_response_value(numeric_deploy_stage)

        # terminate deployment
        return None
Exemplo n.º 4
0
    def ping(self, ping_request):
        # python object -> json
        response = self._ping_internal(ping_request.to_json())

        # json -> python object
        ping_response = PingResponse(jsonValue=response)
        return ping_response
Exemplo n.º 5
0
    def test_init_report(self):
        if os.path.exists('/tmp/env_status'):
            os.remove('/tmp/env_status')

        client = mock.Mock()
        client.send_reports = \
            mock.Mock(return_value=(PingResponse(jsonValue=self.ping_noop_response)))
        d = DeployAgent(client=client, estatus=self.estatus,
                        conf=self.config, executor=self.executor, helper=self.helper)
        d.serve_build()
        client.send_reports.assert_called_once_with({})
Exemplo n.º 6
0
 def _new_response_value(self, numeric_deploy_stage):
     value= {'opCode': OperationCode.DEPLOY,
             'deployGoal': {'deployId': self._deploy_id,
                            'envId': self._env_id,
                            'envName': self._env_name,
                            'stageName': self._stage,
                            'deployStage': numeric_deploy_stage}}
     if numeric_deploy_stage == DeployStage.DOWNLOADING:
         value['deployGoal']['build'] = self._build
     if numeric_deploy_stage == DeployStage.PRE_DOWNLOAD:
         value['deployGoal']['scriptVariables'] = self._script_variables
     return PingResponse(value)
Exemplo n.º 7
0
    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')
Exemplo n.º 8
0
    def test_plan_change(self):
        old_response = None
        new_response = None
        self.assertFalse(DeployAgent.plan_changed(old_response, new_response))
        new_response = PingResponse()
        self.assertTrue(DeployAgent.plan_changed(old_response, new_response))
        old_response = PingResponse()
        old_response.opCode = OpCode.DEPLOY
        new_response.opCode = OpCode.NOOP
        self.assertTrue(DeployAgent.plan_changed(old_response, new_response))
        new_response.opCode = OpCode.DEPLOY
        self.assertFalse(DeployAgent.plan_changed(old_response, new_response))

        deploy_goal = {}
        deploy_goal['deployId'] = '123'
        deploy_goal2 = {}
        deploy_goal2['deployId'] = '234'
        old_response.deployGoal = DeployGoal(jsonValue=deploy_goal)
        new_response.deployGoal = DeployGoal(jsonValue=deploy_goal2)
        self.assertTrue(DeployAgent.plan_changed(old_response, new_response))
        new_response.deployGoal.deployId = '123'
        new_response.deployGoal.deployStage = DeployStage.PRE_RESTART
        old_response.deployGoal.deployStage = DeployStage.PRE_RESTART
        self.assertFalse(DeployAgent.plan_changed(old_response, new_response))
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    def test_agent_with_switch_command(self):
        ping_response_list = [
            PingResponse(jsonValue=self.ping_response1),
            PingResponse(jsonValue=self.ping_response2),
            PingResponse(jsonValue=self.ping_response3),
            PingResponse(jsonValue=self.ping_response4),
            PingResponse(jsonValue=self.ping_response5),
            PingResponse(jsonValue=self.ping_response6),
            PingResponse(jsonValue=self.ping_noop_response)]

        client = mock.Mock()
        client.send_reports = mock.Mock(side_effect=ping_response_list)

        d = DeployAgent(client=client, estatus=self.estatus, conf=self.config,
                        executor=self.executor, helper=self.helper)
        d.serve_build()

        calls = [mock.call(['deploy-downloader', '-f', '/etc/deployagent.conf', '-v',
                            '123', '-u', 'https://test', '-e', 'abc']),
                 mock.call(['deploy-stager', '-f', '/etc/deployagent.conf', '-v',
                            '123', '-t', '/tmp/tests', '-e', 'abc'])]
        self.executor.run_cmd.assert_has_calls(calls)
        self.assertEqual(len(d._envs), 0)
Exemplo n.º 11
0
    def test_set_and_get_deploy_status(self):
        envvar = {}
        envvar['id'] = 'bar'
        envvar['url'] = 'https://abc-123.tar.gz'

        build = {}
        build['id'] = '123'
        build['name'] = 'bar'
        build['commitShort'] = '234'
        build['artifactUrl'] = 'https://abc-123.tar.gz'

        ping_response1 = self.ping_response1
        ping_response1['deployGoal']['scriptVariables'] = envvar

        ping_response2 = self.ping_response2
        ping_response2['deployGoal']['build'] = build

        deploy_goal5 = {}
        deploy_goal5['deployId'] = '123'
        deploy_goal5['envName'] = 'abc'
        deploy_goal5['envId'] = 'def'
        deploy_goal5['stageName'] = 'beta'
        deploy_goal5['deployStage'] = DeployStage.RESTARTING

        deploy_goal6 = {}
        deploy_goal6['deployId'] = '123'
        deploy_goal6['envName'] = 'abc'
        deploy_goal6['envId'] = 'def'
        deploy_goal6['stageName'] = 'beta'
        deploy_goal6['deployStage'] = DeployStage.POST_RESTART

        deploy_goal7 = {}
        deploy_goal7['deployId'] = '123'
        deploy_goal7['envName'] = 'abc'
        deploy_goal7['envId'] = 'def'
        deploy_goal7['stageName'] = 'beta'
        deploy_goal7['deployStage'] = DeployStage.SERVING_BUILD

        ping_response5 = {'deployGoal': deploy_goal5, 'opCode': OpCode.DEPLOY}
        ping_response6 = {'deployGoal': deploy_goal6, 'opCode': OpCode.DEPLOY}
        ping_response7 = {'deployGoal': deploy_goal7, 'opCode': OpCode.DEPLOY}

        ping_response_list = [
            PingResponse(jsonValue=ping_response1),
            PingResponse(jsonValue=ping_response2),
            PingResponse(jsonValue=self.ping_response3),
            PingResponse(jsonValue=self.ping_response4),
            PingResponse(jsonValue=ping_response5),
            PingResponse(jsonValue=ping_response6),
            PingResponse(jsonValue=ping_response7),
            PingResponse(jsonValue=self.ping_noop_response)
        ]

        client = mock.Mock()
        client.send_reports = mock.Mock(side_effect=ping_response_list)
        d = DeployAgent(client=client, estatus=self.estatus, conf=self.config,
                        executor=self.executor, helper=self.helper)
        d.serve_build()
        calls = [mock.call(stage)
                 for stage in ['PRE_DOWNLOAD', 'PRE_RESTART', 'RESTARTING', 'POST_RESTART']]
        self.executor.execute_command.assert_has_calls(calls)
        self.assertEqual(len(d._envs), 1)
        self.assertEqual(d._curr_report.report.envId, 'def')
        self.assertEqual(d._curr_report.report.envName, 'abc')
        self.assertEqual(d._curr_report.report.deployId, '123')
        self.assertEqual(d._curr_report.report.stageName, 'beta')
        self.assertEqual(d._curr_report.report.deployStage, DeployStage.SERVING_BUILD)
Exemplo n.º 12
0
    def test_agent_with_switch_goal(self):
        build = {}
        build['id'] = '123'
        build['name'] = 'bar'
        build['commitShort'] = '345'
        build['commit'] = 'abcd'
        build['artifactUrl'] = 'https://test'

        build2 = {}
        build2['id'] = '123'
        build2['name'] = 'fool'
        build2['commit'] = 'abcd'
        build2['commitShort'] = '345'
        build2['artifactUrl'] = 'https://test2'

        envvar = {}
        envvar['id'] = 'abc'
        envvar['url'] = 'https://test'

        envvar2 = {}
        envvar2['id'] = 'bcd'
        envvar2['url'] = 'https://test2'

        ping_response1 = self.ping_response1
        ping_response1['deployGoal']['scriptVariables'] = envvar

        ping_response2 = self.ping_response2
        ping_response2['deployGoal']['build'] = build

        ping_response3 = self.ping_response3
        ping_response4 = self.ping_response4

        deploy_goal5 = {}
        deploy_goal5['deployId'] = '234'
        deploy_goal5['envName'] = 'bcd'
        deploy_goal5['envId'] = 'efg'
        deploy_goal5['stageName'] = 'prod'
        deploy_goal5['deployStage'] = DeployStage.PRE_DOWNLOAD
        deploy_goal5['scriptVariables'] = envvar2

        deploy_goal6 = {}
        deploy_goal6['deployId'] = '234'
        deploy_goal6['envName'] = 'bcd'
        deploy_goal6['envId'] = 'efg'
        deploy_goal6['stageName'] = 'prod'
        deploy_goal6['deployStage'] = DeployStage.DOWNLOADING
        deploy_goal6['build'] = build2

        deploy_goal7 = {}
        deploy_goal7['deployId'] = '234'
        deploy_goal7['envName'] = 'bcd'
        deploy_goal7['envId'] = 'efg'
        deploy_goal7['stageName'] = 'prod'
        deploy_goal7['deployStage'] = DeployStage.STAGING

        deploy_goal8 = {}
        deploy_goal8['deployId'] = '234'
        deploy_goal8['envName'] = 'bcd'
        deploy_goal8['envId'] = 'efg'
        deploy_goal8['stageName'] = 'prod'
        deploy_goal8['deployStage'] = DeployStage.RESTARTING

        deploy_goal9 = {}
        deploy_goal9['deployId'] = '234'
        deploy_goal9['envName'] = 'bcd'
        deploy_goal9['envId'] = 'efg'
        deploy_goal9['stageName'] = 'prod'
        deploy_goal9['deployStage'] = DeployStage.POST_RESTART

        deploy_goal10 = {}
        deploy_goal10['deployId'] = '234'
        deploy_goal10['envName'] = 'bcd'
        deploy_goal10['envId'] = 'efg'
        deploy_goal10['stageName'] = 'prod'
        deploy_goal10['deployStage'] = DeployStage.SERVING_BUILD

        ping_response5 = {'deployGoal': deploy_goal5, 'opCode': OpCode.DEPLOY}
        ping_response6 = {'deployGoal': deploy_goal6, 'opCode': OpCode.DEPLOY}
        ping_response7 = {'deployGoal': deploy_goal7, 'opCode': OpCode.DEPLOY}
        ping_response8 = {'deployGoal': deploy_goal8, 'opCode': OpCode.DEPLOY}
        ping_response9 = {'deployGoal': deploy_goal9, 'opCode': OpCode.DEPLOY}
        ping_response10 = {'deployGoal': deploy_goal10, 'opCode': OpCode.DEPLOY}

        ping_response_list = [
            PingResponse(jsonValue=ping_response1),
            PingResponse(jsonValue=ping_response2),
            PingResponse(jsonValue=ping_response3),
            PingResponse(jsonValue=ping_response4),
            PingResponse(jsonValue=ping_response5),
            PingResponse(jsonValue=ping_response6),
            PingResponse(jsonValue=ping_response7),
            PingResponse(jsonValue=ping_response8),
            PingResponse(jsonValue=ping_response9),
            PingResponse(jsonValue=ping_response10),
            PingResponse(jsonValue=self.ping_noop_response)
        ]

        client = mock.Mock()
        client.send_reports = mock.Mock(side_effect=ping_response_list)
        d = DeployAgent(client=client, estatus=self.estatus, conf=self.config,
                        executor=self.executor, helper=self.helper)
        d.serve_build()
        calls = [mock.call(['deploy-downloader', '-f', '/etc/deployagent.conf', '-v',
                            '123', '-u', 'https://test', '-e', 'abc']),
                 mock.call(['deploy-stager', '-f', '/etc/deployagent.conf', '-v',
                            '123', '-t', '/tmp/tests', '-e', 'abc']),
                 mock.call(['deploy-downloader', '-f', '/etc/deployagent.conf', '-v',
                            '123', '-u', 'https://test2', '-e', 'bcd']),
                 mock.call(['deploy-stager', '-f', '/etc/deployagent.conf', '-v',
                            '123', '-t', '/tmp/tests', '-e', 'bcd'])]
        self.executor.run_cmd.assert_has_calls(calls)
        self.assertEqual(len(d._envs), 2)
        self.assertEqual(d._envs['abc'].report.deployStage, DeployStage.PRE_RESTART)
        self.assertEqual(d._envs['abc'].report.deployId, '123')
        self.assertEqual(d._envs['abc'].report.envId, 'def')
        self.assertEqual(d._envs['abc'].report.status, AgentStatus.SUCCEEDED)
        self.assertEqual(d._envs['abc'].build_info.build_commit, 'abcd')
        self.assertEqual(d._envs['abc'].build_info.build_url, 'https://test')

        self.assertEqual(d._envs['bcd'].report.deployStage, DeployStage.SERVING_BUILD)
        self.assertEqual(d._envs['bcd'].report.deployId, '234')
        self.assertEqual(d._envs['bcd'].report.envId, 'efg')
        self.assertEqual(d._envs['bcd'].report.status, AgentStatus.SUCCEEDED)
        self.assertEqual(d._envs['bcd'].build_info.build_commit, 'abcd')
        self.assertEqual(d._envs['bcd'].build_info.build_url, 'https://test2')