def testCacheNoCollection(self): collection = updaters.NoCollectionUpdater(self.cache) parameter_info = collection.ParameterInfo() self.assertEqual([('thing-1', ), ('thing-2', )], collection.Select(('*', ), parameter_info)) self.AssertSetEquals( [('tests.lib.core.cache.updaters:NoCollectionUpdater', 1, 1, 1, 12345678, 0)], self.GetTableList()) self.assertEqual( 'tests.lib.core.cache.updaters:NoCollectionUpdater', collection.GetTableForRow(('thing-1', ), parameter_info).name) self.Tick() self.cache.Close() self.cache = resource_cache.ResourceCache() collection = updaters.NoCollectionUpdater(self.cache) parameter_info = collection.ParameterInfo() self.assertEqual([('thing-0', ), ('thing-1', ), ('thing-n', )], collection.Select(('*', ), parameter_info)) self.AssertSetEquals( [('tests.lib.core.cache.updaters:NoCollectionUpdater', 1, 1, 1, 12345680, 0)], self.GetTableList())
def __call__(self, prefix='', parsed_args=None, **kwargs): """A completer function called by argparse in arg_complete mode.""" with progress_tracker.CompletionProgressTracker(): with resource_cache.ResourceCache() as cache: if len(parsed_args._GetCommand().ai.positional_completers) > 1: # pylint: disable=protected-access qualified_parameter_names = {'collection'} else: qualified_parameter_names = set() completer = None try: completer = self._completer_class( cache=cache, qualified_parameter_names=qualified_parameter_names) parameter_info = completer.ParameterInfo(parsed_args, self._argument) return completer.Complete(prefix, parameter_info) except BaseException as e: # pylint: disable=broad-except, e shall not pass # Fatal completer errors return two "completions", each an error # message that is displayed by the shell completers, and look more # like a pair of error messages than completions. This is much better # than the default that falls back to the file completer, typically # yielding the list of all files in the current directory. # # NOTICE: Each message must start with different characters, # otherwise they will be taken as valid completions. Also, the # messages are sorted in the display, so choose the first char wisely. if completer: completer_name = completer.collection else: completer_name = self._completer_class.__name__ return self._MakeCompletionErrorMessages([ u'{}ERROR: {} resource completer failed.'.format( prefix, completer_name), u'{}REASON: {}'.format(prefix, unicode(e)), ])
def __call__(self, prefix='', parsed_args=None, **kwargs): """A completer function suitable for argparse.""" if not isinstance(self._completer_class, type): # A function-type completer. try: return self._completer_class(prefix) except BaseException as e: # pylint: disable=broad-except, e shall not pass return self._HandleCompleterException(e, prefix=prefix) if not parsed_args: parsed_args = self._parsed_args with self._progress_tracker(): with resource_cache.ResourceCache() as cache: if parsed_args and len( parsed_args._GetCommand().ai.positional_completers) > 1: # pylint: disable=protected-access qualified_parameter_names = {'collection'} else: qualified_parameter_names = set() completer = None try: completer = self._completer_class( cache=cache, qualified_parameter_names=qualified_parameter_names) parameter_info = completer.ParameterInfo(parsed_args, self._argument) return completer.Complete(prefix, parameter_info) except BaseException as e: # pylint: disable=broad-except, e shall not pass return self._HandleCompleterException( e, prefix=prefix, completer=completer)
def testCacheDefaultNameWithAccount(self): account = '*****@*****.**' self.StartObjectPatch(properties.VALUES.core.account, 'Get', return_value=account) self.cache.Delete() self.cache = resource_cache.ResourceCache() self.assertTrue(os.path.sep + account + os.path.sep in self.cache.name)
def testCacheOneResourceTwoRequired(self): completer = TwoRequiredResourceCompleter(cache=self.cache) parameter_info = completer.ParameterInfo(self.parsed_args, self.argument) self.assertEqual(['aaa:pdq-a:xyz-a', 'aab:pdq-b:xyz-b'], completer.Complete('aa', parameter_info)) self.assertEqual(['abc:pdq-a:xyz-b'], completer.Complete('ab', parameter_info)) self.assertEqual([], completer.Complete('ac', parameter_info)) self.assertEqual(['aaa:pdq-a:xyz-a', 'aab:pdq-b:xyz-b'], completer.Complete('aa', parameter_info)) self.assertEqual(['abc:pdq-a:xyz-b'], completer.Complete('ab', parameter_info)) self.assertEqual([], completer.Complete('ac', parameter_info)) self.AssertSetEquals([('test.api.pdq-a.xyz-a', 3, 3, 1, 12345678, 0), ('test.api.pdq-a.xyz-b', 3, 3, 1, 12345678, 0), ('test.api.pdq-b.xyz-a', 3, 3, 1, 12345678, 0), ('test.api.pdq-b.xyz-b', 3, 3, 1, 12345678, 0), ('test.project.pdq-a', 1, 1, 1, 12345678, 0), ('test.project.pdq-b', 1, 1, 1, 12345678, 0), ('test.zone', 1, 1, 1, 12345678, 0)], self.GetTableList()) self.Tick() self.cache.Close() self.cache = resource_cache.ResourceCache() completer = TwoRequiredResourceCompleter(cache=self.cache) parameter_info = completer.ParameterInfo(self.parsed_args, self.argument) self.assertEqual([], completer.Complete('aa', parameter_info)) self.assertEqual(['abb:pdq-b:xyz-a', 'abc:pdq-c:xyz-b'], completer.Complete('ab', parameter_info)) self.assertEqual(['acc:pdq-b:xyz-b', 'ace:pdq-c:xyz-c'], completer.Complete('ac', parameter_info)) self.assertEqual([], completer.Complete('aa', parameter_info)) self.assertEqual(['abb:pdq-b:xyz-a', 'abc:pdq-c:xyz-b'], completer.Complete('ab', parameter_info)) self.assertEqual(['acc:pdq-b:xyz-b', 'ace:pdq-c:xyz-c'], completer.Complete('ac', parameter_info)) self.AssertSetEquals([('test.api.pdq-a.xyz-a', 3, 3, 1, 0, 0), ('test.api.pdq-a.xyz-b', 3, 3, 1, 0, 0), ('test.api.pdq-b.xyz-a', 3, 3, 1, 12345680, 0), ('test.api.pdq-b.xyz-b', 3, 3, 1, 12345680, 0), ('test.api.pdq-b.xyz-c', 3, 3, 1, 12345680, 0), ('test.api.pdq-c.xyz-a', 3, 3, 1, 12345680, 0), ('test.api.pdq-c.xyz-b', 3, 3, 1, 12345680, 0), ('test.api.pdq-c.xyz-c', 3, 3, 1, 12345680, 0), ('test.project.pdq-a', 1, 1, 1, 0, 0), ('test.project.pdq-b', 1, 1, 1, 12345680, 0), ('test.project.pdq-c', 1, 1, 1, 12345680, 0), ('test.zone', 1, 1, 1, 12345680, 0)], self.GetTableList())
def testCacheOneCollectionZeroRequiredShortTemplate(self): collection = updaters.ZeroRequiredCollectionUpdater(self.cache) parameter_info = collection.ParameterInfo() self.assertEqual([('aaa', 'pdq-a', 'xyz-a'), ('aab', 'pdq-b', 'xyz-b')], collection.Select(('aa*', ), parameter_info)) self.assertEqual([('abc', 'pdq-c', 'xyz-b')], collection.Select(('ab*', ), parameter_info)) self.assertEqual([], collection.Select(('ac*', ), parameter_info)) self.assertEqual([('aaa', 'pdq-a', 'xyz-a'), ('aab', 'pdq-b', 'xyz-b')], collection.Select(('aa*', ), parameter_info)) self.assertEqual([('abc', 'pdq-c', 'xyz-b')], collection.Select(('ab*', ), parameter_info)) self.assertEqual([], collection.Select(('ac*', ), parameter_info)) self.AssertSetEquals([('test.api', 3, 3, 1, 12345678, 0)], self.GetTableList()) self.assertEqual( 'test.api', collection.GetTableForRow(('abb', 'pdq-b', 'xyz-a'), parameter_info).name) self.assertEqual( 'test.api', collection.GetTableForRow(('abc', 'pdq-c', 'xyz-b'), parameter_info).name) self.Tick() self.cache.Close() self.cache = resource_cache.ResourceCache() collection = updaters.ZeroRequiredCollectionUpdater(self.cache) parameter_info = collection.ParameterInfo() self.assertEqual([], collection.Select(('aa*', ), parameter_info)) self.assertEqual([ ('abb', 'pdq-b', 'xyz-a'), ('abc', 'pdq-c', 'xyz-b'), ], collection.Select(('ab*', ), parameter_info)) self.assertEqual([('acc', 'pdq-d', 'xyz-b'), ('ace', 'pdq-e', 'xyz-c')], collection.Select(('ac*', ), parameter_info)) self.assertEqual([], collection.Select(('aa*', ), parameter_info)) self.assertEqual([ ('abb', 'pdq-b', 'xyz-a'), ('abc', 'pdq-c', 'xyz-b'), ], collection.Select(('ab*', ), parameter_info)) self.assertEqual([('acc', 'pdq-d', 'xyz-b'), ('ace', 'pdq-e', 'xyz-c')], collection.Select(('ac*', ), parameter_info)) self.AssertSetEquals([('test.api', 3, 3, 1, 12345680, 0)], self.GetTableList())
def SetUp(self): self.StartPropertyPatch(config.Paths, 'cache_dir', return_value=os.path.join( self.temp_path, 'cache')) self.StartObjectPatch(persistent_cache_base, 'Now', side_effect=self.Now) self.now = updaters.NOW_START_TIME self.cache = resource_cache.ResourceCache()
def __call__(self, prefix='', parsed_args=None, **kwargs): """A completer function suitable for argparse.""" if not isinstance(self._completer_class, type): # A function-type completer. return self._CompleteFromFunction(prefix=prefix) if not parsed_args: parsed_args = self._parsed_args with self._progress_tracker(): with resource_cache.ResourceCache() as cache: return self._CompleteFromCompleterClass( prefix=prefix, cache=cache, parsed_args=parsed_args)
def SetUp(self): self.StartPropertyPatch(config.Paths, 'cache_dir', return_value=os.path.join( self.temp_path, 'cache')) self.StartObjectPatch(persistent_cache_base, 'Now', side_effect=self.Now) self.now = updaters.NOW_START_TIME self.cache = resource_cache.ResourceCache() dest = 'instance' self.parsed_args = core_completer_test_base.MockNamespace( args={dest: None}) self.argument = self.parsed_args.GetPositionalArgument(dest)
def Run(self, args): # Delete the deprecated completion cache. resource_cache.DeleteDeprecatedCache() # Delete the completion cache. try: resource_cache.ResourceCache(create=False).Delete() except cache_exceptions.CacheNotFound: pass except cache_exceptions.Error as e: log.info('Unexpected resource cache error ignored: [%s].', e) # Re-compile python files. state = local_state.InstallationState.ForCurrent() state.CompilePythonFiles()
def SetUp(self): # mock the cache dir self.StartObjectPatch( config.Paths, 'cache_dir', return_value=os.path.join(self.temp_path, 'cache'), new_callable=mock.PropertyMock) # mock the cache timer self.StartObjectPatch(persistent_cache_base, 'Now', side_effect=self.Now) self.now = updaters.NOW_START_TIME # instantiate objects common to most tests self.cache = resource_cache.ResourceCache() # show all test failure more diff details self.maxDiff = None # pylint: disable=invalid-name
def Run(self, args): # Re-compile python files. state = local_state.InstallationState.ForCurrent() state.CompilePythonFiles() # Delete the deprecated completion cache. resource_cache.DeleteDeprecatedCache() # Delete the completion cache. try: resource_cache.ResourceCache(create=False).Delete() except cache_exceptions.CacheNotFound: pass except cache_exceptions.Error as e: log.info('Unexpected resource cache error ignored: [%s].', e) # Re-generate the static gcloud CLI tree. cli_tree.Dump(self._cli_power_users_only, path=cli_tree.CliTreePath())
def Update(self, uris): """Applies UpdateRows() to tables that contain the resources uris.""" try: with resource_cache.ResourceCache() as cache: completer = self._completer_class(cache=cache) tables = {} for uri in uris: row = completer.StringToRow(uri) table = completer.GetTableForRow(row) entry = tables.get(table.name) if not entry: entry = _TableRows(table) tables[table.name] = entry entry.rows.append(row) for table, rows in six.iteritems(tables): self.UpdateRows(table, rows) except Exception: # pylint: disable=broad-except pass
def _CreateAndDeleteCacheAcrossImplementations(self, create_implementation=None, delete_implementation=None): if create_implementation: encoding.SetEncodedValue(os.environ, 'CLOUDSDK_CACHE_IMPLEMENTATION', create_implementation) cache = resource_cache.ResourceCache() collection = updaters.NoCollectionUpdater(cache) parameter_info = collection.ParameterInfo() collection.Select(('*', ), parameter_info) cache.Close() if delete_implementation: encoding.SetEncodedValue(os.environ, 'CLOUDSDK_CACHE_IMPLEMENTATION', delete_implementation) resource_cache.Delete() with self.assertRaisesRegex(exceptions.CacheNotFound, r'resource.cache] not found'): resource_cache.Delete()
def testCacheNoCreateNotFound(self): self.DeleteCache() with self.assertRaisesRegex( exceptions.CacheNotFound, r'Persistent cache \[.*resource.cache\] not found.'): self.cache = resource_cache.ResourceCache(create=False)
def testCacheOneCollectionTwoRequiredCumulative(self): collection = updaters.TwoRequiredCollectionCumulativeUpdater( self.cache) parameter_info = collection.ParameterInfo() self.assertEqual([('aaa', 'pdq-a', 'xyz-a'), ('aab', 'pdq-b', 'xyz-b')], collection.Select(('aa*', None, None), parameter_info)) self.assertEqual([('abc', 'pdq-a', 'xyz-b')], collection.Select(('ab*', None, None), parameter_info)) self.assertEqual([], collection.Select(('ac*', None, None), parameter_info)) self.assertEqual([('aaa', 'pdq-a', 'xyz-a'), ('aab', 'pdq-b', 'xyz-b')], collection.Select(('aa*', None, None), parameter_info)) self.assertEqual([('abc', 'pdq-a', 'xyz-b')], collection.Select(('ab*', None, None), parameter_info)) self.assertEqual([], collection.Select(('ac*', None, None), parameter_info)) self.AssertSetEquals( [('test.project.zone.instance.aaa.pdq-a', 3, 3, 1, 12345678, 0), ('test.project.zone.instance.aab.pdq-b', 3, 3, 1, 12345678, 0), ('test.project.zone.instance.abc.pdq-a', 3, 3, 1, 12345678, 0), ('test.project', 1, 1, 1, 12345678, 0), ('test.project.zone.aaa', 2, 2, 1, 12345678, 0), ('test.project.zone.aab', 2, 2, 1, 12345678, 0), ('test.project.zone.abc', 2, 2, 1, 12345678, 0)], self.GetTableList()) self.assertEqual( 'test.project.zone.instance.aaa.pdq-a', collection.GetTableForRow(('aaa', 'pdq-a', 'xyz-a'), parameter_info, create=False).name) self.assertEqual( 'test.project.zone.instance.aaa.pdq-a', collection.GetTableForRow(('aaa', 'pdq-a', 'xyz-b'), parameter_info, create=False).name) self.assertEqual( 'test.project.zone.instance.aab.pdq-b', collection.GetTableForRow(('aab', 'pdq-b', 'xyz-a'), parameter_info, create=False).name) self.assertEqual( 'test.project.zone.instance.aab.pdq-b', collection.GetTableForRow(('aab', 'pdq-b', 'xyz-b'), parameter_info, create=False).name) self.Tick() self.cache.Close() self.cache = resource_cache.ResourceCache() collection = updaters.TwoRequiredCollectionCumulativeUpdater( self.cache) parameter_info = collection.ParameterInfo() self.assertEqual([], collection.Select(('aa*', None, None), parameter_info)) self.assertEqual([ ('abb', 'pdq-b', 'xyz-a'), ('abc', 'pdq-c', 'xyz-b'), ], collection.Select(('ab*', None, None), parameter_info)) self.assertEqual([('acc', 'pdq-b', 'xyz-b'), ('ace', 'pdq-c', 'xyz-c')], collection.Select(('ac*', None, None), parameter_info)) self.assertEqual([], collection.Select(('aa*', None, None), parameter_info)) self.assertEqual([ ('abb', 'pdq-b', 'xyz-a'), ('abc', 'pdq-c', 'xyz-b'), ], collection.Select(('ab*', None, None), parameter_info)) self.assertEqual([('acc', 'pdq-b', 'xyz-b'), ('ace', 'pdq-c', 'xyz-c')], collection.Select(('ac*', None, None), parameter_info)) self.AssertSetEquals( [('test.project.zone.instance.aaa.pdq-a', 3, 3, 1, 0, 0), ('test.project.zone.instance.aab.pdq-b', 3, 3, 1, 0, 0), ('test.project.zone.instance.abc.pdq-a', 3, 3, 1, 0, 0), ('test.project.zone.instance.abb.pdq-b', 3, 3, 1, 12345680, 0), ('test.project.zone.instance.abc.pdq-c', 3, 3, 1, 12345680, 0), ('test.project.zone.instance.acc.pdq-b', 3, 3, 1, 12345680, 0), ('test.project.zone.instance.ace.pdq-c', 3, 3, 1, 12345680, 0), ('test.project', 1, 1, 1, 12345680, 0), ('test.project.zone.aaa', 2, 2, 1, 0, 0), ('test.project.zone.aab', 2, 2, 1, 0, 0), ('test.project.zone.abc', 2, 2, 1, 12345680, 0), ('test.project.zone.abb', 2, 2, 1, 12345680, 0), ('test.project.zone.acc', 2, 2, 1, 12345680, 0), ('test.project.zone.ace', 2, 2, 1, 12345680, 0)], self.GetTableList())
def testCacheOneCollectionOneRequired(self): collection = updaters.OneRequiredCollectionUpdater(self.cache) parameter_info = collection.ParameterInfo() self.assertEqual([('aaa', 'pdq-a', 'xyz-a'), ('aab', 'pdq-b', 'xyz-b')], collection.Select(('aa*', None, None), parameter_info)) self.assertEqual([('abc', 'pdq-c', 'xyz-b')], collection.Select(('ab*', None, None), parameter_info)) self.assertEqual([], collection.Select(('ac*', None, None), parameter_info)) self.assertEqual([('aaa', 'pdq-a', 'xyz-a'), ('aab', 'pdq-b', 'xyz-b')], collection.Select(('aa*', None, None), parameter_info)) self.assertEqual([('abc', 'pdq-c', 'xyz-b')], collection.Select(('ab*', None, None), parameter_info)) self.assertEqual([], collection.Select(('ac*', None, None), parameter_info)) self.AssertSetEquals([('test.api.xyz-a', 3, 3, 1, 12345678, 0), ('test.api.xyz-b', 3, 3, 1, 12345678, 0), ('test.project', 1, 1, 1, 12345678, 0)], self.GetTableList()) self.assertEqual( 'test.api.xyz-a', collection.GetTableForRow(('aaa', 'pdq-a', 'xyz-a'), parameter_info, create=False).name) self.assertEqual( 'test.api.xyz-b', collection.GetTableForRow(('aab', 'pdq-b', 'xyz-b'), parameter_info, create=False).name) with self.assertRaisesRegex( exceptions.CacheTableNotFound, r'resource.cache] cache table \[test.api.xyz-c] not found'): collection.GetTableForRow(('ace', 'pdq-e', 'xyz-c'), parameter_info, create=False) self.assertEqual( 'test.api.xyz-c', collection.GetTableForRow(('ace', 'pdq-e', 'xyz-c'), parameter_info).name) self.Tick() self.cache.Close() self.cache = resource_cache.ResourceCache() collection = updaters.OneRequiredCollectionUpdater(self.cache) parameter_info = collection.ParameterInfo() self.assertEqual([], collection.Select(('aa*', None, None), parameter_info)) self.assertEqual([ ('abb', 'pdq-b', 'xyz-a'), ('abc', 'pdq-c', 'xyz-b'), ], collection.Select(('ab*', None, None), parameter_info)) self.assertEqual([('acc', 'pdq-d', 'xyz-b'), ('ace', 'pdq-e', 'xyz-c')], collection.Select(('ac*', None, None), parameter_info)) self.assertEqual([], collection.Select(('aa*', None, None), parameter_info)) self.assertEqual([ ('abb', 'pdq-b', 'xyz-a'), ('abc', 'pdq-c', 'xyz-b'), ], collection.Select(('ab*', None, None), parameter_info)) self.assertEqual([('acc', 'pdq-d', 'xyz-b'), ('ace', 'pdq-e', 'xyz-c')], collection.Select(('ac*', None, None), parameter_info)) self.AssertSetEquals([('test.api.xyz-a', 3, 3, 1, 12345680, 0), ('test.api.xyz-b', 3, 3, 1, 12345680, 0), ('test.api.xyz-c', 3, 3, 1, 12345680, 0), ('test.project', 1, 1, 1, 12345680, 0)], self.GetTableList())