def test_config_unchanged_api_key(self, mock_gen, mock_restart):
     '''The forwarder is not restarted with unchanged `api_key`'''
     mock_gen.return_value = 'a fake diamond config\n'
     self.fs.CreateFile('/hg-agent.cfg',
                        contents=textwrap.dedent('''
                            api_key: "00000000-0000-0000-0000-000000000000"
                        '''))
     periodic.config_once(ConfigArgs('/hg-agent.cfg', '/diamond.cfg'))
     periodic.config_once(ConfigArgs('/hg-agent.cfg', '/diamond.cfg'))
     mock_restart.assert_called_once_with(mock.ANY, 'diamond')
    def test_config_write_except(self, mock_gen, mock_restart, mock_logging):
        mock_gen.return_value = 'a fake diamond config\n'
        mock_restart.side_effect = Exception('test')

        self.fs.CreateFile('/hg-agent.cfg',
                           contents=textwrap.dedent('''
                               api_key: "00000000-0000-0000-0000-000000000000"
                           '''))

        periodic.config_once(ConfigArgs('/hg-agent.cfg', '/diamond.cfg'))
        mock_logging.exception.assert_called()
    def test_config_invalid(self, mock_gen, mock_logging):
        self.fs.CreateFile('/hg-agent.cfg',
                           contents=textwrap.dedent('''
                               api_key: "00000000-0000-0000-0000-000000000000"
                               invalid_key: "test"
                           '''))
        periodic.config_once(ConfigArgs('/hg-agent.cfg', '/diamond.cfg'))

        # If validation fails, we never reach gen.
        mock_logging.error.assert_called()
        mock_gen.assert_not_called()
    def test_config_diamond_unchanged(self, mock_gen, mock_restart):

        old_diamond = 'a fake diamond config\n'
        mock_gen.return_value = old_diamond
        self.fs.CreateFile('/diamond.cfg', contents=old_diamond)

        self.fs.CreateFile('/hg-agent.cfg',
                           contents=textwrap.dedent('''
                               api_key: "00000000-0000-0000-0000-000000000000"
                           '''))

        periodic.config_once(ConfigArgs('/hg-agent.cfg', '/diamond.cfg'))
        mock_restart.assert_not_called()
 def test_config_new_endpoint_url(self, mock_gen, mock_restart):
     '''The forwarder is restarted with an entirely new `endpoint_url`'''
     mock_gen.return_value = 'a fake diamond config\n'
     self.fs.CreateFile('/hg-agent.cfg',
                        contents=textwrap.dedent('''
                            api_key: "00000000-0000-0000-0000-000000000000"
                        '''))
     periodic.config_once(ConfigArgs('/hg-agent.cfg', '/diamond.cfg'))
     config = self.fs.get_object('/hg-agent.cfg')
     config.set_contents(textwrap.dedent('''
                            api_key: "00000000-0000-0000-0000-000000000000"
                            endpoint_url: "https://my-endpoint"
                         '''))
     periodic.config_once(ConfigArgs('/hg-agent.cfg', '/diamond.cfg'))
     mock_restart.assert_any_call(mock.ANY, 'forwarder')
 def test_config_changed_api_key(self, mock_gen, mock_restart):
     '''The forwarder/receiver are restarted with changed `api_key`'''
     mock_gen.return_value = 'a fake diamond config\n'
     self.fs.CreateFile('/hg-agent.cfg',
                        contents=textwrap.dedent('''
                            api_key: "00000000-0000-0000-0000-000000000000"
                        '''))
     periodic.config_once(ConfigArgs('/hg-agent.cfg', '/diamond.cfg'))
     config = self.fs.get_object('/hg-agent.cfg')
     config.set_contents(textwrap.dedent('''
                            api_key: "10000000-0000-0000-0000-000000000001"
                         '''))
     periodic.config_once(ConfigArgs('/hg-agent.cfg', '/diamond.cfg'))
     mock_restart.assert_any_call(mock.ANY, 'forwarder')
     mock_restart.assert_any_call(mock.ANY, 'receiver')
    def test_config_new_diamond(self, mock_gen, mock_restart):

        # As jinja2 uses the filesystem, we need to mock this out here (but
        # note its functionality is tested in TestDiamondConfigGen above).
        mock_gen.return_value = 'a fake diamond config\n'

        self.fs.CreateFile('/hg-agent.cfg',
                           contents=textwrap.dedent('''
                               api_key: "00000000-0000-0000-0000-000000000000"
                           '''))

        self.assertFalse(os.path.exists('/diamond.cfg'))
        periodic.config_once(ConfigArgs('/hg-agent.cfg', '/diamond.cfg'))
        self.assertTrue(os.path.exists('/diamond.cfg'))
        mock_restart.assert_called_once_with(mock.ANY, 'diamond')
 def test_config_load_error(self, mock_validate, mock_logging):
     periodic.config_once(ConfigArgs('/hg-agent.cfg', '/diamond.cfg'))
     mock_logging.error.assert_called()
     # If load fails, we never reach validate
     mock_validate.assert_not_called()