class ESMetadataFactoryClass(factory.ObjectFactory): """Factory class for setting up an Elastic Search connection.""" _ELASTIC_SEARCH_TYPES = { CONNECTION_TYPE_PROD: factory.CachedFunctionCall(lambda: es_utils.ESMetadata( use_http=constants.ELASTIC_SEARCH_USE_HTTP, host=topology.topology.get(topology.ELASTIC_SEARCH_HOST_KEY), port=topology.topology.get(topology.ELASTIC_SEARCH_PORT_KEY), index=constants.ELASTIC_SEARCH_INDEX, udp_port=topology.topology.get(topology.ELASTIC_SEARCH_UDP_PORT_KEY ))), CONNECTION_TYPE_READONLY: factory.CachedFunctionCall(lambda: es_utils.ESMetadataRO( use_http=constants.ELASTIC_SEARCH_USE_HTTP, host=topology.topology.get(topology.ELASTIC_SEARCH_HOST_KEY), port=topology.topology.get(topology.ELASTIC_SEARCH_PORT_KEY), index=constants.ELASTIC_SEARCH_INDEX, udp_port=topology.topology.get(topology.ELASTIC_SEARCH_UDP_PORT_KEY ))) } def __init__(self): super(ESMetadataFactoryClass, self).__init__( 'elastic search connection', self._ELASTIC_SEARCH_TYPES, lambda from_setup, to_setup: from_setup == to_setup) def SetupProd(self): """Set up this factory to connect to the production Elastic Search.""" self.Setup(CONNECTION_TYPE_PROD) def SetupReadOnly(self): """Set up this factory to allow querying the production Elastic Search.""" self.Setup(CONNECTION_TYPE_READONLY)
class FactoryTest(cros_test_lib.TestCase): """Test that ObjectFactory behaves as expected.""" _OBJECT_NAME = 'Test Object Name' _OBJECT_TYPES = { 't0': _GET_OBJECT, 't1': factory.CachedFunctionCall(_GET_OBJECT), 't3': factory.CachedFunctionCall(_GET_OBJECT), 't4': None, } def _allowed_transitions(self, from_setup, to_setup): return from_setup == 't3' and to_setup == 't4' def setUp(self): self.of = factory.ObjectFactory(self._OBJECT_NAME, self._OBJECT_TYPES) self.of2 = factory.ObjectFactory(self._OBJECT_NAME, self._OBJECT_TYPES, self._allowed_transitions) def testGetInstance(self): self.of.Setup('t0') a = self.of.GetInstance() self.assertNotEqual(a, self.of.GetInstance()) def testGetCachedInstance(self): self.of.Setup('t1') a = self.of.GetInstance() self.assertEqual(a, self.of.GetInstance()) def testDuplicateSetupForbidden(self): self.of.Setup('t0') with self.assertRaises(factory.ObjectFactoryIllegalOperation): self.of.Setup('t0') def testNotSetup(self): with self.assertRaises(factory.ObjectFactoryIllegalOperation): self.of.GetInstance() def testUnknownSetupForbidden(self): with self.assertRaises(factory.ObjectFactoryIllegalOperation): self.of.Setup('unknown setup type') def testSetupWithInstanceForbidden(self): with self.assertRaises(factory.ObjectFactoryIllegalOperation): self.of.Setup('t0', None) def testSetupWithInstanceAllowed(self): self.of.Setup('t4', None) def testForbiddenTransition(self): self.of2.Setup('t0') with self.assertRaises(factory.ObjectFactoryIllegalOperation): self.of2.Setup('t1') def testAllowedTransition(self): self.of2.Setup('t3') a = self.of2.GetInstance() self.of2.Setup('t4', None) self.assertNotEqual(a, self.of2.GetInstance())
class StatsFactoryClass(factory.ObjectFactory): """Factory class for setting up a Statsd connection.""" _STATSD_TYPES = { CONNECTION_TYPE_PROD: factory.CachedFunctionCall(lambda: stats.Statsd( es=ESMetadataFactory.GetInstance(), host=topology.topology.get(topology.STATSD_HOST_KEY), port=topology.topology.get(topology.STATSD_PORT_KEY), prefix=constants.STATSD_PROD_PREFIX)), CONNECTION_TYPE_DEBUG: factory.CachedFunctionCall(lambda: stats.Statsd( es=ESMetadataFactory.GetInstance(), host=topology.topology.get(topology.STATSD_HOST_KEY), port=topology.topology.get(topology.STATSD_PORT_KEY), prefix=constants.STATSD_DEBUG_PREFIX)), CONNECTION_TYPE_MOCK: factory.CachedFunctionCall(lambda: stats_es_mock.Stats()) } def __init__(self): super(StatsFactoryClass, self).__init__( 'statsd connection', self._STATSD_TYPES, lambda from_setup, to_setup: from_setup == to_setup) def SetupProd(self): """Set up this factory to connect to the production Statsd.""" self.Setup(CONNECTION_TYPE_PROD) def SetupDebug(self): """Set up this factory to connect to the debug Statsd.""" self.Setup(CONNECTION_TYPE_DEBUG) def SetupMock(self): """Set up this factory to return a mock statsd object.""" self.Setup(CONNECTION_TYPE_MOCK)