Ejemplo n.º 1
0
  def setUp(self):
    super(TestMetricsIntegrationTests, self).setUp()

    # Save the original state of the collector.
    self.original_collector_instance = MetricsCollector.GetCollector()

    # Set dummy attributes for the collector.
    MetricsCollector.StartTestCollector('https://example.com', 'user-agent-007',
                                        {'a': 'b', 'c': 'd'})
    self.collector = MetricsCollector.GetCollector()
Ejemplo n.º 2
0
  def setUp(self):
    super(TestMetricsUnitTests, self).setUp()

    # Save the original state of the collector.
    self.original_collector_instance = MetricsCollector.GetCollector()

    # Set dummy attributes for the collector.
    MetricsCollector.StartTestCollector('https://example.com', 'user-agent-007',
                                        {'a': 'b', 'c': 'd'})
    self.collector = MetricsCollector.GetCollector()

    self.log_handler = MockLoggingHandler()
    # Use metrics logger to avoid impacting the root logger which may
    # interfere with other tests.
    logging.getLogger('metrics').setLevel(logging.DEBUG)
    logging.getLogger('metrics').addHandler(self.log_handler)
Ejemplo n.º 3
0
    def testDisabling(self):
        """Tests enabling/disabling of metrics collection."""
        self.assertEqual(self.collector, MetricsCollector.GetCollector())

        # Test when gsutil is part of the Cloud SDK and the user opted in there.
        with mock.patch.dict(os.environ,
                             values={
                                 'CLOUDSDK_WRAPPER': '1',
                                 'GA_CID': '555'
                             }):
            MetricsCollector._CheckAndSetDisabledCache()
            self.assertFalse(MetricsCollector._disabled_cache)
            self.assertEqual(self.collector, MetricsCollector.GetCollector())

        # Test when gsutil is part of the Cloud SDK and the user did not opt in
        # there.
        with mock.patch.dict(os.environ,
                             values={
                                 'CLOUDSDK_WRAPPER': '1',
                                 'GA_CID': ''
                             }):
            MetricsCollector._CheckAndSetDisabledCache()
            self.assertTrue(MetricsCollector._disabled_cache)
            self.assertEqual(None, MetricsCollector.GetCollector())

        # Test when gsutil is not part of the Cloud SDK and there is no UUID file.
        with mock.patch.dict(os.environ, values={'CLOUDSDK_WRAPPER': ''}):
            with mock.patch('os.path.exists', return_value=False):
                MetricsCollector._CheckAndSetDisabledCache()
                self.assertTrue(MetricsCollector._disabled_cache)
                self.assertEqual(None, MetricsCollector.GetCollector())

        # Test when gsutil is not part of the Cloud SDK and there is a UUID file.
        with mock.patch.dict(os.environ, values={'CLOUDSDK_WRAPPER': ''}):
            with mock.patch('os.path.exists', return_value=True):
                MetricsCollector._CheckAndSetDisabledCache()
                self.assertFalse(MetricsCollector._disabled_cache)
                self.assertEqual(self.collector,
                                 MetricsCollector.GetCollector())
Ejemplo n.º 4
0
    def testDisabling(self):
        """Tests enabling/disabling of metrics collection."""
        self.assertEqual(self.collector, MetricsCollector.GetCollector())

        # Test when gsutil is part of the Cloud SDK and the user opted in there.
        with mock.patch.dict(os.environ,
                             values={
                                 'CLOUDSDK_WRAPPER': '1',
                                 'GA_CID': '555'
                             }):
            MetricsCollector._CheckAndSetDisabledCache()
            self.assertFalse(MetricsCollector._disabled_cache)
            self.assertEqual(self.collector, MetricsCollector.GetCollector())

        # Test when gsutil is part of the Cloud SDK and the user did not opt in
        # there.
        with mock.patch.dict(os.environ,
                             values={
                                 'CLOUDSDK_WRAPPER': '1',
                                 'GA_CID': ''
                             }):
            MetricsCollector._CheckAndSetDisabledCache()
            self.assertTrue(MetricsCollector._disabled_cache)
            self.assertEqual(None, MetricsCollector.GetCollector())

        # Test when gsutil is not part of the Cloud SDK and there is no UUID file.
        with mock.patch.dict(os.environ, values={'CLOUDSDK_WRAPPER': ''}):
            with mock.patch('os.path.exists', return_value=False):
                MetricsCollector._CheckAndSetDisabledCache()
                self.assertTrue(MetricsCollector._disabled_cache)
                self.assertEqual(None, MetricsCollector.GetCollector())

        # Test when gsutil is not part of the Cloud SDK and there is a UUID file.
        with mock.patch.dict(os.environ, values={'CLOUDSDK_WRAPPER': ''}):
            with mock.patch('os.path.exists', return_value=True):
                # Mock the contents of the file.
                with mock.patch('__builtin__.open') as mock_open:
                    mock_open.return_value.__enter__ = lambda s: s

                    # Set the file.read() method to return the disabled text.
                    mock_open.return_value.read.return_value = metrics._DISABLED_TEXT
                    MetricsCollector._CheckAndSetDisabledCache()
                    self.assertTrue(MetricsCollector._disabled_cache)
                    self.assertEqual(None, MetricsCollector.GetCollector())

                    # Set the file.read() method to return a mock cid (analytics enabled).
                    mock_open.return_value.read.return_value = 'mock_cid'
                    MetricsCollector._CheckAndSetDisabledCache()
                    self.assertFalse(MetricsCollector._disabled_cache)
                    self.assertEqual(self.collector,
                                     MetricsCollector.GetCollector())

                    # Check that open/read was called twice.
                    self.assertEqual(2, len(mock_open.call_args_list))
                    self.assertEqual(
                        2, len(mock_open.return_value.read.call_args_list))