def test_enable_should_not_track_throttled_time_when_setting_the_cpu_quota_fails(
            self):
        with self._get_cgroup_configurator(enable=False) as configurator:
            if CGroupsTelemetry.get_track_throttled_time():
                raise Exception(
                    "Test setup should not start tracking Throttle Time")

            configurator.mocks.add_file(UnitFilePaths.cpu_quota,
                                        Exception("A TEST EXCEPTION"))

            configurator.enable()

            self.assertFalse(CGroupsTelemetry.get_track_throttled_time(),
                             "Throttle time should not be tracked")
    def test_enable_should_set_agent_cpu_quota_and_track_throttled_time(self):
        with self._get_cgroup_configurator(enable=False) as configurator:
            agent_drop_in_file_cpu_quota = configurator.mocks.get_mapped_path(
                UnitFilePaths.cpu_quota)
            if os.path.exists(agent_drop_in_file_cpu_quota):
                raise Exception(
                    "{0} should not have been created during test setup".
                    format(agent_drop_in_file_cpu_quota))

            configurator.enable()

            expected_quota = "CPUQuota={0}%".format(_AGENT_CPU_QUOTA)
            self.assertTrue(
                os.path.exists(agent_drop_in_file_cpu_quota),
                "{0} was not created".format(agent_drop_in_file_cpu_quota))
            self.assertTrue(
                fileutil.findre_in_file(agent_drop_in_file_cpu_quota,
                                        expected_quota),
                "CPUQuota was not set correctly. Expected: {0}. Got:\n{1}".
                format(expected_quota,
                       fileutil.read_file(agent_drop_in_file_cpu_quota)))
            self.assertTrue(CGroupsTelemetry.get_track_throttled_time(),
                            "Throttle time should be tracked")