def test_not_prod_stage(self, local_server_factory, example_dir): config1 = Config(workspace='1', environment_variables_file=Path(example_dir) / "config" / "vars_dev.json") config2 = ProdConfig(environment_variables_file=Path(example_dir) / "config" / "vars_prod.json") local_server = local_server_factory( WithEnvMS(configs=[config1, config2]), workspace='1') response = local_server.make_call(requests.get, '/') assert response.status_code == 200 assert response.text == 'test dev environment variable'
def test_prod_stage(self, local_server_factory, example_dir): def auth(*args): return True config1 = Config(environment_variables_file=Path(example_dir) / "config" / "vars_dev.json") config2 = ProdConfig(environment_variables_file=Path(example_dir) / "config" / "vars_prod.json", auth=auth) local_server = local_server_factory( WithEnvMS(configs=[config1, config2]), workspace="v1") response = local_server.make_call(requests.get, '/', headers={'authorization': 'token'}) assert response.status_code == 200 assert response.text == 'test secret environment variable'
def test_run_dev_stage(self, example_dir): config = Config(environment_variables_file=Path("config") / "vars_dev.json") app = WithEnvMS(configs=config) CwsRunner(app) port = ThreadedLocalServer.unused_tcp_port() server = threading.Thread(target=run_server_example, args=(example_dir, app, port), daemon=True) server.start() counter = 1 time.sleep(counter) while not server.is_alive() and counter < 3: time.sleep(counter) counter += 1 response = requests.get(f'http://localhost:{port}/', headers={'Authorization': "token"}) assert response.text == "test dev environment variable"
def __init__(self, **kwargs): config = Config(cors=CORSConfig(allow_origin='*')) super().__init__(configs=config, **kwargs)
def __init__(self, **kwargs): config = Config(cors=CORSConfig( allow_origin=['www.test.fr', 'www.test.com'])) super().__init__(configs=config, **kwargs)
def test_env(self, local_server_factory, example_dir): config = Config(environment_variables_file=Path(example_dir) / "config" / "vars_dev.json") local_server = local_server_factory(TechMS(configs=[config])) response = local_server.make_call(requests.get, '/env', timeout=500) assert response.status_code == 200 assert response.text == "Simple microservice for test dev environment variable.\n"
from coworks import TechMicroService, entry from coworks.config import Config from coworks.cws.runner import CwsRunner class SimpleMicroService(TechMicroService): @entry def get(self): return "Simple microservice ready.\n" app = SimpleMicroService(configs=Config()) CwsRunner(app)
def __init__(self): super().__init__(name='test', configs=Config())
from coworks.cws.runner import CwsRunner class SimpleMicroService(TechMicroService): def __init__(self, **kwargs): super().__init__(**kwargs) self.value = 0 def auth(self, auth_request): return auth_request.token == "token" @entry def get(self): return f"Stored value {self.value}.\n" @entry def post(self, value=None): if value is not None: self.value = value return f"Value stored ({value}).\n" CONFIG = Config(workspace="dev") app = SimpleMicroService(name="sample-first-microservice", configs=[CONFIG]) CwsRunner(app) XRayContextManager(app, xray_recorder) if __name__ == '__main__': app.execute('run')
@entry def get_init(self): return f"Initial value is {self.init_value}.\n" @entry def get_env(self): return f"Simple microservice for {os.getenv('test')}.\n" # usefull for test info (don't remove) tech_app = TechMS() TestCmd(tech_app, name='test') CwsTemplateWriter(tech_app) app = TechMS(configs=Config(environment_variables_file="config/vars_dev.json")) TestCmd(tech_app, name='test') class RunnerMock(CwsRunner): @classmethod def multi_execute(cls, project_dir, workspace, execution_list): for command, options in execution_list: port = options['port'] assert type(port) is int project1 = TechMS() RunnerMock(project1) project2 = TechMS()