def usage_example(): # Create a new config object to ease reading the Platform.sh environment variables. # You can alternatively use os.environ yourself. config = Config() # Get the credentials to connect to the Redis service. credentials = config.credentials('redis') try: redis = Redis(credentials['host'], credentials['port']) key = "Deploy day" value = "Friday" # Set a value redis.set(key, value) # Read it back test = redis.get(key) return 'Found value <strong>{0}</strong> for key <strong>{1}</strong>.'.format( test.decode("utf-8"), key) except Exception as e: return e
def test_onenterprise_returns_false_on_standard(self): env = self.mockEnvironmentDeploy config = Config(env) self.assertFalse(config.on_enterprise())
def test_reading_existing_variable_works(self): env = self.mockEnvironmentDeploy config = Config(env) self.assertEqual('someval', config.variable('somevar'))
def usage_example(): # Create a new Config object to ease reading the Platform.sh environment variables. # You can alternatively use os.environ yourself. config = Config() # Get the credentials to connect to the Memcached service. credentials = config.credentials('memcached') try: # Try connecting to Memached server. memcached = pymemcache.Client( (credentials['host'], credentials['port'])) memcached.set('Memcached::OPT_BINARY_PROTOCOL', True) key = "Deploy_day" value = "Friday" # Set a value. memcached.set(key, value) # Read it back. test = memcached.get(key) return 'Found value <strong>{0}</strong> for key <strong>{1}</strong>.'.format( test.decode("utf-8"), key) except Exception as e: return e
def test_has_relationship_returns_true_for_existing_relationship(self): env = self.mockEnvironmentDeploy config = Config(env) self.assertTrue(config.has_relationship('database'))
def test_build_and_deploy_properties_mocked_in_local_exist(self): env = self.mockEnvironmentDeploy env.pop('PLATFORM_APPLICATION', None) env.pop('PLATFORM_ENVIRONMENT', None) env.pop('PLATFORM_BRANCH', None) config = Config(env) self.assertEqual('/app', config.appDir) self.assertEqual('app', config.applicationName) self.assertEqual('test-project', config.project) self.assertEqual('abc123', config.treeID) self.assertEqual('def789', config.projectEntropy) self.assertEqual('/app/web', config.documentRoot) self.assertEqual('1.2.3.4', config.smtpHost) self.assertEqual('8080', config.port) self.assertEqual('unix://tmp/blah.sock', config.socket) self.assertTrue(hasattr(config, 'appDir')) self.assertTrue(hasattr(config, 'applicationName')) self.assertTrue(hasattr(config, 'project')) self.assertTrue(hasattr(config, 'treeID')) self.assertTrue(hasattr(config, 'projectEntropy')) self.assertTrue(hasattr(config, 'documentRoot')) self.assertTrue(hasattr(config, 'smtpHost')) self.assertTrue(hasattr(config, 'port')) self.assertTrue(hasattr(config, 'socket'))
def usage_example(): # Create a new Config object to ease reading the Platform.sh environment variables. # You can alternatively use os.environ yourself. config = Config() # The 'database' relationship is generally the name of primary SQL database of an application. # It could be anything, though, as in the case here here where it's called "database". credentials = config.credentials('database') try: formatted = config.formatted_credentials('database', 'pymongo') server = '{0}://{1}:{2}@{3}'.format(credentials['scheme'], credentials['username'], credentials['password'], formatted) client = MongoClient(server) collection = client.main.starwars post = {"name": "Rey", "occupation": "Jedi"} post_id = collection.insert_one(post).inserted_id document = collection.find_one({"_id": post_id}) # Clean up after ourselves. collection.drop() return 'Found {0} ({1})<br />'.format(document['name'], document['occupation']) except Exception as e: return e
def test_build_and_deploy_properties_in_deploy_exists(self): env = self.mockEnvironmentDeploy config = Config(env) self.assertEqual('/app', config.appDir) self.assertEqual('app', config.applicationName) self.assertEqual('test-project', config.project) self.assertEqual('abc123', config.treeID) self.assertEqual('def789', config.projectEntropy) self.assertEqual('feature-x', config.branch) self.assertEqual('feature-x-hgi456', config.environment) self.assertEqual('/app/web', config.documentRoot) self.assertEqual('1.2.3.4', config.smtpHost) self.assertEqual('8080', config.port) self.assertEqual('unix://tmp/blah.sock', config.socket) self.assertTrue(hasattr(config, 'appDir')) self.assertTrue(hasattr(config, 'applicationName')) self.assertTrue(hasattr(config, 'project')) self.assertTrue(hasattr(config, 'treeID')) self.assertTrue(hasattr(config, 'projectEntropy')) self.assertTrue(hasattr(config, 'branch')) self.assertTrue(hasattr(config, 'environment')) self.assertTrue(hasattr(config, 'documentRoot')) self.assertTrue(hasattr(config, 'smtpHost')) self.assertTrue(hasattr(config, 'port')) self.assertTrue(hasattr(config, 'socket'))
def test_has_relationship_returns_false_for_missing_relationship(self): env = self.mockEnvironmentDeploy config = Config(env) self.assertFalse(config.has_relationship('missing'))
def test_pymongo_formatter(self): config = Config(self.mockEnvironmentDeploy) formatted = config.formatted_credentials('mongodb', 'pymongo') self.assertEqual('mongodb.internal:27017/main', formatted) # include formatted string
def test_reading_missing_variable_returns_default(self): env = self.mockEnvironmentDeploy config = Config(env) self.assertEqual('default-val', config.variable('missing', 'default-val'))
def test_onenterprise_returns_true_on_enterprise(self): env = self.mockEnvironmentDeploy env['PLATFORM_MODE'] = 'enterprise' config = Config(env) self.assertTrue(config.on_enterprise())
def test_formatted_credentials_calls_a_formatter(self): config = Config(self.mockEnvironmentDeploy) config.register_formatter('test', lambda credentials: 'called') formatted = config.formatted_credentials('database', 'test') self.assertEqual('called', formatted)
def test_credentials_missing_relationship_index_throws(self): env = self.mockEnvironmentDeploy config = Config(env) with self.assertRaises(KeyError): config.credentials('database', 3)
def test_credentials_missing_relationship_throws(self): env = self.mockEnvironmentDeploy config = Config(env) with self.assertRaises(KeyError): config.credentials('does-not-exist')
def test_onproduction_on_standard_prod_is_true(self): env = self.mockEnvironmentDeploy env['PLATFORM_BRANCH'] = 'master' config = Config(env) self.assertTrue(config.on_production())
def test_onproduction_on_dedicated_prod_is_true(self): env = self.mockEnvironmentDeploy env['PLATFORM_MODE'] = 'enterprise' env['PLATFORM_BRANCH'] = 'production' config = Config(env) self.assertTrue(config.on_production())
def test_onproduction_on_standard_stg_is_false(self): # The fixture has a non-master branch set by default. env = self.mockEnvironmentDeploy config = Config(env) self.assertFalse(config.on_production())
def test_onproduction_on_dedicated_stg_is_false(self): env = self.mockEnvironmentDeploy env['PLATFORM_MODE'] = 'enterprise' env['PLATFORM_BRANCH'] = 'staging' config = Config(env) self.assertFalse(config.on_production())
def test_variables_returns_on_platform(self): env = self.mockEnvironmentDeploy config = Config(env) variables = config.variables() self.assertEqual('someval', variables['somevar'])
def test_application_array_available(self): env = self.mockEnvironmentDeploy config = Config(env) app = config.application() self.assertEqual('python:3.7', app['type'])
def test_deploy_property_in_build_throws(self): env = self.mockEnvironmentBuild config = Config(env) self.assertFalse('branch' in dir(config)) with self.assertRaises(BuildTimeVariableAccessException): branch = config.branch
def test_missing_property_throws_in_deploy(self): env = self.mockEnvironmentDeploy config = Config(env) self.assertFalse('missing' in dir(config)) with self.assertRaises(AttributeError): missing = config.missing
def test_credentials_existing_relationship_returns(self): env = self.mockEnvironmentDeploy config = Config(env) creds = config.credentials('database') self.assertEqual('mysql', creds['scheme']) self.assertEqual('mysql:10.2', creds['type'])
def test_invalid_json_throws(self): with self.assertRaises(TypeError): config = Config({ 'PLATFORM_APPLICATION_NAME': 'app', 'PLATFORM_ENVIRONMENT': 'test-environment', 'PLATFORM_VARIABLES': base64.encodebytes('{some-invalid-json}') })
def getMasterKey(self): """ Retrieves the Meilisearch master key, either from the Platform.sh environment or locally. """ config = Config() if config.is_valid_platform(): return config.projectEntropy elif os.environ.get("MEILI_MASTER_KEY"): return os.environ["MEILI_MASTER_KEY"] else: return self.default["key"]
def getMasterKey(self): config = Config() if config.is_valid_platform() and not os.environ.get('TEMPLATE_DEMO'): if config.branch == "master": return config.projectEntropy else: return config.branch elif os.environ.get("MEILI_MASTER_KEY"): return os.environ["MEILI_MASTER_KEY"] else: return self.default["key"]
def test_credentials_work_in_local(self): env = self.mockEnvironmentDeploy env.pop('PLATFORM_APPLICATION', None) env.pop('PLATFORM_ENVIRONMENT', None) env.pop('PLATFORM_BRANCH', None) config = Config(env) creds = config.credentials('database') self.assertEqual('mysql', creds['scheme']) self.assertEqual('mysql:10.2', creds['type'])
def test_upstream_routes_for_app(self): config = Config(self.mockEnvironmentDeploy) routes = config.get_upstream_routes("app") self.assertEqual(len(routes), 2) self.assertTrue( "https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/" in routes) self.assertEqual( "https://www.{default}/", routes[ "https://www.master-7rqtwti-gcpjkefjk4wc2.us-2.platformsh.site/"] ["original_url"])
def test_build_property_in_build_exists(self): env = self.mockEnvironmentBuild config = Config(env) self.assertEqual('/app', config.appDir) self.assertEqual('app', config.applicationName) self.assertEqual('test-project', config.project) self.assertEqual('abc123', config.treeID) self.assertEqual('def789', config.projectEntropy) self.assertTrue(hasattr(config, 'appDir')) self.assertTrue(hasattr(config, 'applicationName')) self.assertTrue(hasattr(config, 'project')) self.assertTrue(hasattr(config, 'treeID')) self.assertTrue(hasattr(config, 'projectEntropy'))