Beispiel #1
0
 def test_constant_prompt(self):
     test_constant = app_constants.Constant('name', 'messsage', '',
                                            utils.StringParser(False))
     self.assertFalse(test_constant.valid)
     with mock.patch.object(utils, 'prompt', return_value='VALID!'):
         test_constant.prompt()
         self.assertTrue(test_constant.valid)
Beispiel #2
0
 def test_constant_constructor(self, default, parser, new_value, expected):
     test_constant = app_constants.Constant('name', 'message', default,
                                            parser)
     self.assertEqual('name: {}'.format(default), str(test_constant))
     self.assertEqual(
         "<Constant('name', 'message', {!r}, {!r})>".format(
             default, parser), repr(test_constant))
     self.assertEqual(
         app_constants.Constant('name', 'message', default, parser),
         test_constant)
     self.assertNotEqual(
         app_constants.Constant('other', 'message', default, parser),
         test_constant)
     test_constant.value = new_value
     self.assertTrue(test_constant.valid)
     self.assertEqual(test_constant.value, expected)
Beispiel #3
0
    def test_load_constants(self, mock_info_logging):
        test_client = _TestClientAPI()
        test_manager = gng_impl._Manager(
            self._valid_default_config,
            self._constants,
            None,
            False,
            storage_api=test_client,
        )
        test_manager._constants['test'].value = 'valid'
        test_manager._constants['other'].value = 'OVERRIDDEN'
        test_manager._constants['KeyError'] = app_constants.Constant(
            'KeyError', 'This constant is not stored and raises a KeyError',
            '')

        test_client.insert_blob(
            test_manager._config.constants_storage_path,
            {
                'test': '',
                'other': 'other valid'
            },
            bucket_name=test_manager._config.bucket,
        )

        test_manager.load_constants_from_storage()
        # Test the default value is used when the loaded value is invalid.
        self.assertEqual(test_manager._constants['test'].value, 'valid')
        # Test loading a valid value overrides the original.
        self.assertEqual(test_manager._constants['other'].value, 'other valid')
        # Test that the 'KeyError' constant calls logging.info.
        self.assertEqual(mock_info_logging.call_count, 1)
Beispiel #4
0
    def setUp(self):
        super(ManagerTest, self).setUp()
        # Save the real modules for clean up.
        self.real_open = builtins.open
        # Create a fake file system and stub out builtin modules.
        self.fs = fake_filesystem.FakeFilesystem()
        self.os = fake_filesystem.FakeOsModule(self.fs)
        self.open = fake_filesystem.FakeFileOpen(self.fs)
        self.stdout = StringIO()
        self.stubs = mox3_stubout.StubOutForTesting()
        self.stubs.SmartSet(builtins, 'open', self.open)
        self.stubs.SmartSet(common, 'os', self.os)
        self.stubs.SmartSet(sys, 'stdout', self.stdout)

        # Setup Testdata.
        self._testdata_path = '/testdata'
        self._valid_config_path = self._testdata_path + '/valid_config.yaml'
        self._blank_config_path = self._testdata_path + '/blank_config.yaml'

        self.fs.CreateFile(self._valid_config_path, contents=_VALID_CONFIG)
        self.fs.CreateFile(self._blank_config_path, contents=_BLANK_CONFIG)

        # Load the default config.
        self._valid_default_config = common.ProjectConfig.from_yaml(
            common.DEFAULT, self._valid_config_path)

        # Create test constants.
        self._constants = {
            'test':
            app_constants.Constant(
                'test',
                'message',
                '',
                parser=utils.StringParser(allow_empty_string=False),
            ),
            'other':
            app_constants.Constant('other', 'other message', 'value'),
        }

        # Mock out the authentication credentials.
        self.auth_patcher = mock.patch.object(auth, 'CloudCredentials')
        self.mock_creds = self.auth_patcher.start()
        self.mock_creds.return_value.get_credentials.return_value = (
            credentials.AnonymousCredentials())