def testMainInActiveMode(self,
                           _parseArgsMock,
                           purgeDeprecatedCompaniesMock,
                           promoteReadyMetricsToModelsMock,
                           timeSleepMock):

    configPatch = config_test_utils.ConfigAttributePatch(
      configName=metric_collectors.config.CONFIG_NAME,
      baseConfigDir=metric_collectors.config.CONFIG_DIR,
      values=[
        ("metric_maintenance_agent", "opmode",
         metric_collectors.config.OP_MODE_ACTIVE)
      ])

    class ExitFromMain(Exception):
      pass

    timeSleepMock.side_effect = [None, ExitFromMain]

    with configPatch:
      with self.assertRaises(ExitFromMain):
        agent.main()

    self.assertEqual(purgeDeprecatedCompaniesMock.call_count, 1)
    self.assertEqual(promoteReadyMetricsToModelsMock.call_count, 2)
    self.assertEqual(timeSleepMock.call_count, 2)
    def testMainInActiveMode(self, _parseArgsMock,
                             purgeDeprecatedCompaniesMock,
                             promoteReadyMetricsToModelsMock, timeSleepMock):

        configPatch = config_test_utils.ConfigAttributePatch(
            configName=metric_collectors.config.CONFIG_NAME,
            baseConfigDir=metric_collectors.config.CONFIG_DIR,
            values=[("metric_maintenance_agent", "opmode",
                     metric_collectors.config.OP_MODE_ACTIVE)])

        class ExitFromMain(Exception):
            pass

        timeSleepMock.side_effect = [None, ExitFromMain]

        with configPatch:
            with self.assertRaises(ExitFromMain):
                agent.main()

        self.assertEqual(purgeDeprecatedCompaniesMock.call_count, 1)
        self.assertEqual(promoteReadyMetricsToModelsMock.call_count, 2)
        self.assertEqual(timeSleepMock.call_count, 2)
    def testMainHandlesKeyboardInterruptReturns0(
            self, _parseArgsMock, purgeDeprecatedCompaniesMock,
            promoteReadyMetricsToModelsMock, timeSleepMock):

        configPatch = config_test_utils.ConfigAttributePatch(
            configName=metric_collectors.config.CONFIG_NAME,
            baseConfigDir=metric_collectors.config.CONFIG_DIR,
            values=[("metric_maintenance_agent", "opmode",
                     metric_collectors.config.OP_MODE_ACTIVE)])

        timeSleepMock.side_effect = KeyboardInterrupt

        with configPatch:
            returnCode = agent.main()

        self.assertEqual(returnCode, 0)
        self.assertEqual(purgeDeprecatedCompaniesMock.call_count, 1)
        self.assertEqual(promoteReadyMetricsToModelsMock.call_count, 1)
        self.assertEqual(timeSleepMock.call_count, 1)
    def testMainHandlesKeyboardInterruptReturns0(
            self, _parseArgsMock, purgeDeprecatedCompaniesMock,
            promoteReadyMetricsToModelsMock, timeSleepMock):

        configPatch = config_test_utils.ConfigAttributePatch(
            configName=metric_collectors.config.CONFIG_NAME,
            baseConfigDir=metric_collectors.config.CONFIG_DIR,
            values=[("metric_maintenance_agent", "opmode",
                     metric_collectors.config.OP_MODE_ACTIVE)])

        timeSleepMock.side_effect = KeyboardInterrupt

        with configPatch:
            returnCode = agent.main()

        self.assertEqual(returnCode, 0)
        self.assertEqual(purgeDeprecatedCompaniesMock.call_count, 1)
        self.assertEqual(promoteReadyMetricsToModelsMock.call_count, 1)
        self.assertEqual(timeSleepMock.call_count, 1)
    def testMainSkipsAndReturns0InHotStandbyMode(
            self, _parseArgsMock, purgeDeprecatedCompaniesMock,
            promoteReadyMetricsToModelsMock, timeSleepMock):

        configPatch = config_test_utils.ConfigAttributePatch(
            configName=metric_collectors.config.CONFIG_NAME,
            baseConfigDir=metric_collectors.config.CONFIG_DIR,
            values=[("metric_maintenance_agent", "opmode",
                     metric_collectors.config.OP_MODE_HOT_STANDBY)])

        timeSleepMock.side_effect = AssertionError(
            "Unexpected time.sleep() call")

        with configPatch:
            returnCode = agent.main()

        self.assertEqual(returnCode, 0)

        self.assertEqual(purgeDeprecatedCompaniesMock.call_count, 0)
        self.assertEqual(promoteReadyMetricsToModelsMock.call_count, 0)
    def testMainSkipsAndReturns0InHotStandbyMode(
            self, _parseArgsMock, purgeDeprecatedCompaniesMock,
            promoteReadyMetricsToModelsMock, timeSleepMock):

        configPatch = config_test_utils.ConfigAttributePatch(
            configName=metric_collectors.config.CONFIG_NAME,
            baseConfigDir=metric_collectors.config.CONFIG_DIR,
            values=[("metric_maintenance_agent", "opmode",
                     metric_collectors.config.OP_MODE_HOT_STANDBY)])

        timeSleepMock.side_effect = AssertionError(
            "Unexpected time.sleep() call")

        with configPatch:
            returnCode = agent.main()

        self.assertEqual(returnCode, 0)

        self.assertEqual(purgeDeprecatedCompaniesMock.call_count, 0)
        self.assertEqual(promoteReadyMetricsToModelsMock.call_count, 0)