Ejemplo n.º 1
0
def test_get_env_def_for_app_env(mock_responses, client):
    mock_responses.add(
        responses.GET,
        "http://baseurl/appenvs?app=myapp&env=myenv",
        json={
            "app": "myapp",
            "env": "myenv",
            "env_def": {
                "env_id": "abcd1234",
                "packages": {
                    "pkg-a": "1.0",
                    "pkg-b": ">2.0,<3",
                },
                "channels": [],
            },
        },
        status=200,
    )

    app_env = client.get_env_def_for_app_env("myapp", "myenv")

    expected_app_env = ApplicationEnvironment(
        app=Application("myapp"),
        env="myenv",
        env_def=EnvironmentDefinition.from_dict(
            {"packages": {
                "pkg-a": "1.0",
                "pkg-b": ">2.0,<3"
            }}),
    )
    assert expected_app_env == app_env
Ejemplo n.º 2
0
def test_associate_application_environment(env_service: EnvironmentService):
    env_def = EnvironmentDefinition('{"packages": {"foo": "9.8.7"}}')
    env_service.save_env_def(env_def)

    app = Application(name="my-app")

    app_env = ApplicationEnvironment(app=app, env="prod", env_def=env_def)

    env_service.save_app_env(app_env)
Ejemplo n.º 3
0
def test_application_environment_roundtrip(env_service: EnvironmentService):
    env_def = EnvironmentDefinition('{"packages": {"foo": "9.8.7"}}')
    env_service.save_env_def(env_def)

    app = Application(name="my-app")

    app_env = ApplicationEnvironment(app=app, env="prod", env_def=env_def)

    env_service.save_app_env(app_env)

    app_env_returned = env_service.get_app_env("my-app", "prod")

    assert app_env_returned.env_def.packages == {"foo": "9.8.7"}
Ejemplo n.º 4
0
 def get_app_env(self, app_name: str,
                 env_name: str) -> ApplicationEnvironment:
     # we want to get the last one - so we sort by id desc and then we get the first
     query = (self.session.query(AppEnv).filter(
         AppEnv.app == app_name,
         AppEnv.env_name == env_name).order_by(AppEnv.id.desc()))
     if app_env_orm := query.first():
         env_def = self._env_def_from_orm_to_business_model(
             app_env_orm.env_def)
         app_env = ApplicationEnvironment(
             app=Application(app_env_orm.app),
             env=app_env_orm.env_name,
             env_def=env_def,
         )
         return app_env
Ejemplo n.º 5
0
def test_get_app_env(gateway, simple_env_def):
    gateway.save_env_def(simple_env_def)
    app_env = ApplicationEnvironment(env="test-env",
                                     app=Application(name="test-app"),
                                     env_def=simple_env_def)

    gateway.save_app_env(app_env)

    app_env_returned = gateway.get_app_env("test-app", "test-env")

    assert isinstance(app_env_returned, ApplicationEnvironment)
    assert app_env_returned.app.name == "test-app"
    assert app_env_returned.env == "test-env"
    assert app_env_returned.env_def.id == "0f89efe"
    assert app_env_returned.env_def.packages == {"pandas": ">=1.1,<1.2"}
Ejemplo n.º 6
0
def test_run(env_service: EnvironmentService):
    # First we create an environment definition
    env_def = EnvironmentDefinition('{"packages": {"foo": "9.8.7"}}')
    env_service.save_env_def(env_def)

    # Then we associate it to an (application, environment)
    app = Application(name="some_app")
    app_env = ApplicationEnvironment(app=app, env="uat", env_def=env_def)
    env_service.save_app_env(app_env)

    # Finally we ask the service to run something on that (app, env)
    env_service.run("some_app", "uat", ["hello", "world"])

    # We can then run the asserts against the deployment backend inside our service
    assert [("b262deb", env_def)] == env_service.deployment_backend.envs
    assert [("b262deb", ["hello", "world"])
            ] == env_service.deployment_backend.executed_commands
Ejemplo n.º 7
0
def test_save_app_env(gateway, simple_env_def):
    gateway.save_env_def(simple_env_def)

    assert gateway.session.query(AppEnv).all() == []

    app_env = ApplicationEnvironment(env="test-env",
                                     app=Application(name="test-app"),
                                     env_def=simple_env_def)

    gateway.save_app_env(app_env)

    # Assertions are made against ORM database objects
    (app_env_orm,
     ) = gateway.session.query(AppEnv).all()  # only one (unpacking)

    assert app_env_orm.app == "test-app"
    assert app_env_orm.env_name == "test-env"
    assert app_env_orm.env_def.id == simple_env_def.id
    assert app_env_orm.env_def.env_def == simple_env_def.env_def
Ejemplo n.º 8
0
def test_conda_deployment_backend_run_command(mocker, in_memory_store):
    backend = CondaDeploymentBackend()

    service = EnvironmentService(store=in_memory_store,
                                 deployment_backend=backend)

    env_def = EnvironmentDefinition(
        '{"packages": {"foo": "1.2.3", "bar": ">=4.5.1,<5.0"}}')
    service.save_env_def(env_def)

    app = Application(name="my-app")
    app_env = ApplicationEnvironment(app=app, env="prod", env_def=env_def)
    service.save_app_env(app_env)

    mock_subprocess_run = mocker.patch(
        "ee.backends.conda_deployment_backend.subprocess.run", autospec=True)
    mock_subprocess_Popen = mocker.patch(
        "ee.backends.conda_deployment_backend.subprocess.Popen", autospec=True)

    service.run("my-app", "prod", ["foo", "bar"])

    # This should trigger 3 calls to subprocess.run
    # 1) for checking if the environment already exists
    # 2) for creating the environment the first time
    # 3) for running the command
    assert mock_subprocess_run.call_args_list == [
        mocker.call(["conda", "list", "-n", "412b992"],
                    shell=SHELL,
                    capture_output=True),
        mocker.call(
            'conda create -n 412b992 -y "foo=1.2.3" "bar>=4.5.1,<5.0"'.split(),
            shell=SHELL,
            capture_output=True,
        ),
    ]

    mock_subprocess_Popen.assert_has_calls([
        mocker.call(
            "conda run --no-capture-output -n base "
            "conda run --no-capture-output -n 412b992 foo bar".split(),
            shell=SHELL,
        ),
    ])
Ejemplo n.º 9
0
    def get_env_def_for_app_env(self, app: str,
                                env: str) -> ApplicationEnvironment:
        """Send a request to fetch the env def for the pair (app, env)."""
        # TODO: should return only the EnvironmentDefinition instead?
        url = f"{self.base_url}/appenvs?app={app}&env={env}"
        resp = self.client.get(url, timeout=EE_REQUEST_TIMEOUT)
        resp.raise_for_status()

        data = resp.json()

        env_def_dict = {"packages": data["env_def"]["packages"]}
        if data["env_def"].get("channels"):
            env_def_dict["channels"] = data["env_def"]["channels"]

        app_env = ApplicationEnvironment(
            app=Application(data["app"]),
            env=data["env"],
            env_def=EnvironmentDefinition.from_dict(env_def_dict),
        )
        return app_env
Ejemplo n.º 10
0
async def configure_app_env(app_env_request: AppEnvRequest):
    """
    gotta send
        app name
        env name
        env id
    """
    # first we need to check if the given env_id is valid?
    env_def = store.get_env_def(app_env_request.env_id)
    if env_def is None:
        raise HTTPException(status_code=404, detail="env_id not found")

    app_env = ApplicationEnvironment(
        app=Application(name=app_env_request.app),
        env=app_env_request.env,
        env_def=env_def,
    )

    store.save_app_env(app_env)
    return AppEnvResponse(app=app_env.app.name,
                          env=app_env.env,
                          env_id=app_env.env_def.id)