def test_display_secrets_in_namespace(
    mock_k8s_module: MagicMock, capsys: CaptureFixture
):
    mock_k8s_module.list_secrets_in_namespace.return_value = {
        "test-credentials": {"USERNAME": "******", "PASSWORD": "******"},
        "more-test-credentials": {"FOO": "bar"},
    }

    mock_k8s_module.namespace_exists.return_value = False
    display_secrets_in_namespace("the-namespace", "test-credentials")
    captured_one = capsys.readouterr()
    assert "could not be found on k8s cluster" in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    display_secrets_in_namespace("the-namespace", "test-credentials")
    captured_two = capsys.readouterr()
    assert "USERNAME=alex" in captured_two.out
    assert "PASSWORD=alex123" in captured_two.out

    mock_k8s_module.namespace_exists.return_value = True
    display_secrets_in_namespace("the-namespace", "test-credentialz")
    captured_three = capsys.readouterr()
    assert "cannot find secret=test-credentialz" in captured_three.out

    mock_k8s_module.namespace_exists.return_value = True
    display_secrets_in_namespace("the-namespace", None)
    captured_four = capsys.readouterr()
    assert "test-credentials" in captured_four.out
    assert "USERNAME=alex" in captured_four.out
    assert "PASSWORD=alex123" in captured_four.out
    assert "more-test-credentials" in captured_four.out
    assert "FOO=bar" in captured_four.out
Beispiel #2
0
def test_display_workflow_job_history(mock_k8s_module: MagicMock,
                                      capsys: CaptureFixture):
    mock_k8s_module.namespace_exists.return_value = False
    display_workflow_job_history("bodywork-dev", "bodywork-test-project")
    captured_one = capsys.readouterr()
    assert "namespace=bodywork-dev could not be found" in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_workflow_jobs.return_value = {
        "workflow-job-12345": {
            "start_time": datetime(2020, 10, 19, 1, 15),
            "completion_time": datetime(2020, 10, 19, 1, 30),
            "active": False,
            "succeeded": True,
            "failed": False,
        }
    }
    display_workflow_job_history("bodywork-dev", "bodywork-test-project")
    captured_two = capsys.readouterr()
    lines = captured_two.out.split("\n")
    header = lines[2]
    data = lines[3]
    assert ("JOB_NAME" in header) and ("workflow-job-12345" in data)
    assert ("START_TIME" in header) and (str(datetime(2020, 10, 19, 1, 15))
                                         in data)
    assert ("COMPLETION_TIME" in header) and (str(datetime(
        2020, 10, 19, 1, 30)) in data)
    assert (("ACTIVE" in header) and ("SUCCEEDED" in header)
            and ("FAILED" in header) and (f'0{" "*19}1{" "*19}0' in data))
Beispiel #3
0
def test_display_service_deployments_in_namespace(mock_k8s_module: MagicMock,
                                                  capsys: CaptureFixture):
    mock_k8s_module.namespace_exists.return_value = False
    display_service_deployments_in_namespace("bodywork-dev")
    captured_one = capsys.readouterr()
    assert "namespace=bodywork-dev could not be found" in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    service_url = "http://bodywork-test-project--serve.bodywork-dev.svc.cluster.local"
    mock_k8s_module.list_service_stage_deployments.return_value = {
        "bodywork-test-project--serve": {
            "service_url": service_url,
            "service_port": 5000,
            "service_exposed": "true",
            "available_replicas": 1,
            "unavailable_replicas": 0,
            "git_url": "project_repo_url",
            "git_branch": "project_repo_branch",
            "has_ingress": "true",
            "ingress_route": "/bodywork-dev/bodywork-test-project",
        }
    }
    display_service_deployments_in_namespace("bodywork-dev")
    captured_two = capsys.readouterr()
    assert re.findall(r"REPLICAS_AVAILABLE\s+1", captured_two.out)
    assert re.findall(r"REPLICAS_UNAVAILABLE\s+0", captured_two.out)
    assert re.findall(r"GIT_URL\s+project_repo_url", captured_two.out)
    assert re.findall(r"GIT_BRANCH\s+project_repo_branch", captured_two.out)
    assert re.findall(r"CLUSTER_SERVICE_PORT\s+5000", captured_two.out)
    assert re.findall(
        r"CLUSTER_SERVICE_URL\s+http://bodywork-test-project--serve.bodywork-dev.svc",
        captured_two.out,
    )
    assert re.findall(r"INGRESS_ROUTE\s+/bodywork-dev/bodywork-test-project",
                      captured_two.out)
def test_setup_namespace_on_k8s_cluster(mock_k8s_module: MagicMock,
                                        capsys: CaptureFixture):
    SA1 = BODYWORK_WORKFLOW_SERVICE_ACCOUNT
    SA2 = BODYWORK_JOBS_DEPLOYMENTS_SERVICE_ACCOUNT

    mock_k8s_module.namespace_exists.return_value = False
    mock_k8s_module.create_namespace.side_effect = None
    mock_k8s_module.service_account_exists.side_effect = [False, True]
    mock_k8s_module.setup_workflow_service_account.side_effect = None
    setup_namespace_with_service_accounts_and_roles("the-namespace")
    captured_one = capsys.readouterr()
    assert "creating namespace=the-namespace" in captured_one.out
    assert f"creating service-account={SA1}" in captured_one.out
    assert f"service-account={SA2} already exists in namespace" in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.create_namespace.side_effect = None
    mock_k8s_module.service_account_exists.side_effect = [True, True]
    mock_k8s_module.setup_workflow_service_account.side_effect = None
    setup_namespace_with_service_accounts_and_roles("the-namespace")
    captured_two = capsys.readouterr()
    assert "namespace=the-namespace already exists" in captured_two.out
    assert f"service-account={SA1} already exists in namespace" in captured_two.out
    assert f"service-account={SA2} already exists in namespace" in captured_two.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.create_namespace.side_effect = None
    mock_k8s_module.service_account_exists.side_effect = [False, False]
    mock_k8s_module.setup_workflow_service_account.side_effect = None
    mock_k8s_module.setup_job_and_deployment_service_accounts.side_effect = None
    setup_namespace_with_service_accounts_and_roles("the-namespace")
    captured_three = capsys.readouterr()
    assert "namespace=the-namespace already exists" in captured_three.out
    assert f"creating service-account={SA1}" in captured_three.out
    assert f"creating service-account={SA2}" in captured_three.out
def test_display_cronjobs_in_namespace(
    mock_k8s_module: MagicMock,
    capsys: CaptureFixture
):
    mock_k8s_module.namespace_exists.return_value = False
    display_cronjobs_in_namespace('bodywork-dev')
    captured_one = capsys.readouterr()
    assert 'namespace=bodywork-dev could not be found' in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_cronjobs.return_value = {
        'bodywork-test-project': {
            'schedule': '0 * * * *',
            'last_scheduled_time': datetime(2020, 9, 15),
            'git_url': 'project_repo_url',
            'git_branch': 'project_repo_branch'
        }
    }
    display_cronjobs_in_namespace('bodywork-dev')
    captured_two = capsys.readouterr()
    assert 'bodywork-test-project' in captured_two.out
    assert '0 * * * *' in captured_two.out
    assert '2020-09-15 00:00:00' in captured_two.out
    assert 'project_repo_url' in captured_two.out
    assert 'project_repo_branch' in captured_two.out
def test_inherited_subprograms(capsys: CaptureFixture) -> None:
    with raises(SystemExit):  # Command required.
        InheritedSubprogramsProgram.init()

    with raises(SystemExit):  # Argument in_file required.
        ParentProgram.init(ParentProgram.title())

    prog = InheritedSubprogramsProgram.init(
        ParentProgram.__name__, "--in_file", "in.file"
    )
    assert isinstance(prog, ParentProgram)
    assert prog.in_file == Path("in.file")
    assert prog.out_file is None
    prog.run()
    assert capsys.readouterr().out == ParentProgram.__name__ + "\n"

    with raises(SystemExit):  # Argument in_file missing.
        InheritedSubprogramsProgram.init(
            ChildProgram.__name__, "--test_file", "test.file"
        )

    prog = InheritedSubprogramsProgram.init(
        ChildProgram.__name__, "--in_file", "in.file", "--test_file", "test.file"
    )
    assert isinstance(prog, ChildProgram)
    assert prog.in_file == Path("in.file")
    assert prog.out_file is None
    assert prog.test_file == Path("test.file")
    prog.run()
    assert capsys.readouterr().out == ChildProgram.__name__ + "\n"
def test_display_service_deployments_in_namespace(mock_k8s_module: MagicMock,
                                                  capsys: CaptureFixture):
    mock_k8s_module.namespace_exists.return_value = False
    display_service_deployments_in_namespace('bodywork-dev')
    captured_one = capsys.readouterr()
    assert 'namespace=bodywork-dev could not be found' in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_service_stage_deployments.return_value = {
        'bodywork-test-project--serve': {
            'service_url': 'http://bodywork-test-project--serve:5000',
            'service_exposed': 'true',
            'available_replicas': 1,
            'unavailable_replicas': 0,
            'git_url': 'project_repo_url',
            'git_branch': 'project_repo_branch'
        }
    }
    display_service_deployments_in_namespace('bodywork-dev')
    captured_two = capsys.readouterr()
    assert 'http://bodywork-test-project--serve:5000' in captured_two.out
    assert 'true' in captured_two.out
    assert '1' in captured_two.out
    assert '0' in captured_two.out
    assert 'project_repo_url' in captured_two.out
    assert 'project_repo_branch' in captured_two.out
def test_display_workflow_job_history(mock_k8s_module: MagicMock,
                                      capsys: CaptureFixture):
    mock_k8s_module.namespace_exists.return_value = False
    display_workflow_job_history('bodywork-dev', 'bodywork-test-project')
    captured_one = capsys.readouterr()
    assert 'namespace=bodywork-dev could not be found' in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_workflow_jobs.return_value = {
        'workflow-job-12345': {
            'start_time': datetime(2020, 10, 19, 1, 15),
            'completion_time': datetime(2020, 10, 19, 1, 30),
            'active': False,
            'succeeded': True,
            'failed': False
        }
    }
    display_workflow_job_history('bodywork-dev', 'bodywork-test-project')
    captured_two = capsys.readouterr()
    lines = captured_two.out.split('\n')
    header = lines[2]
    data = lines[3]
    assert ('JOB_NAME' in header) and ('workflow-job-12345' in data)
    assert ('START_TIME' in header) and (str(datetime(2020, 10, 19, 1, 15))
                                         in data)
    assert ('COMPLETION_TIME' in header) and (str(datetime(
        2020, 10, 19, 1, 30)) in data)
    assert (('ACTIVE' in header) and ('SUCCEEDED' in header)
            and ('FAILED' in header) and (f'0{" "*19}1{" "*19}0' in data))
def test_display_workflow_cronjobs_in_namespace(mock_k8s_module: MagicMock,
                                                capsys: CaptureFixture):
    mock_k8s_module.namespace_exists.return_value = False
    display_cronjobs_in_namespace('bodywork-dev')
    captured_one = capsys.readouterr()
    assert 'namespace=bodywork-dev could not be found' in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_workflow_cronjobs.return_value = {
        'bodywork-test-project': {
            'schedule': '0 * * * *',
            'last_scheduled_time': datetime(2020, 9, 15),
            'retries': 2,
            'git_url': 'project_repo_url',
            'git_branch': 'project_repo_branch'
        }
    }
    display_cronjobs_in_namespace('bodywork-dev')
    captured_two = capsys.readouterr()
    assert re.findall(r'bodywork-test-project', captured_two.out)
    assert re.findall(r'SCHEDULE\s+0 * * * *', captured_two.out)
    assert re.findall(r'RETRIES\s+2', captured_two.out)
    assert re.findall(r'LAST_EXECUTED\s+2020-09-15 00:00:00', captured_two.out)
    assert re.findall(r'GIT_URL\s+project_repo_url', captured_two.out)
    assert re.findall(r'GIT_BRANCH\s+ project_repo_branch', captured_two.out)
def test_is_namespace_setup_for_bodywork(mock_k8s_module: MagicMock,
                                         capsys: CaptureFixture):
    SA1 = BODYWORK_WORKFLOW_SERVICE_ACCOUNT
    CRB = BODYWORK_WORKFLOW_CLUSTER_ROLE
    SA2 = BODYWORK_JOBS_DEPLOYMENTS_SERVICE_ACCOUNT

    mock_k8s_module.namespace_exists.return_value = False
    namespace_setup = is_namespace_available_for_bodywork("bodywork-dev")
    capture_one = capsys.readouterr()
    assert namespace_setup is False
    assert "namespace=bodywork-dev does not exist" in capture_one.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.service_account_exists.side_effect = [True, True]
    mock_k8s_module.cluster_role_binding_exists.return_value = True
    namespace_setup = is_namespace_available_for_bodywork("bodywork-dev")
    capture_two = capsys.readouterr()
    assert namespace_setup is True
    assert "namespace=bodywork-dev is setup for use by Bodywork" in capture_two.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.service_account_exists.side_effect = [False, False]
    mock_k8s_module.cluster_role_binding_exists.return_value = False
    mock_k8s_module.workflow_cluster_role_binding_name.return_value = (
        f"{CRB}--bodywork-dev")
    namespace_setup = is_namespace_available_for_bodywork("bodywork-dev")
    capture_three = capsys.readouterr()
    assert namespace_setup is False
    assert f"service-account={SA1} is missing from namespace" in capture_three.out
    assert f"cluster-role-binding={CRB}--bodywork-dev is missing" in capture_three.out
    assert f"service-account={SA2} is missing from namespace" in capture_three.out
def test_multiple_settings(tmp_path: Path, capsys: CaptureFixture) -> None:
    with raises(SystemExit):
        MultipleSettingsProgram.init("-h")

    foo_settings_file = tmp_path / "my-foo.toml"
    foo_settings_file.write_text("n = 3.5", encoding="UTF-8")

    bar_settings_file = tmp_path / "my-bar.toml"
    bar_settings_file.write_text("m = 4", encoding="UTF-8")

    capsys.readouterr()
    prog = MultipleSettingsProgram.init(
        "--foo", str(foo_settings_file), "-b", str(bar_settings_file)
    )
    assert isinstance(prog, MultipleSettingsProgram)
    assert isinstance(prog.foo, FooSettings)
    assert prog.foo.settings_file
    assert prog.foo.settings_file.resolve() == foo_settings_file.resolve()
    assert prog.foo.n == 3.5
    assert isinstance(prog.bar, BarSettings)
    assert prog.bar.settings_file
    assert prog.bar.settings_file.resolve() == bar_settings_file.resolve()
    assert prog.bar.m == 4
    prog.run()
    assert capsys.readouterr().out == "14.0\n"
Beispiel #12
0
def test_display_workflow_cronjobs_in_namespace(mock_k8s_module: MagicMock,
                                                capsys: CaptureFixture):
    mock_k8s_module.namespace_exists.return_value = False
    display_cronjobs_in_namespace("bodywork-dev")
    captured_one = capsys.readouterr()
    assert "namespace=bodywork-dev could not be found" in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_workflow_cronjobs.return_value = {
        "bodywork-test-project": {
            "schedule": "0 * * * *",
            "last_scheduled_time": datetime(2020, 9, 15),
            "retries": 2,
            "git_url": "project_repo_url",
            "git_branch": "project_repo_branch",
        }
    }
    display_cronjobs_in_namespace("bodywork-dev")
    captured_two = capsys.readouterr()
    assert re.findall(r"bodywork-test-project", captured_two.out)
    assert re.findall(r"SCHEDULE\s+0 * * * *", captured_two.out)
    assert re.findall(r"RETRIES\s+2", captured_two.out)
    assert re.findall(r"LAST_EXECUTED\s+2020-09-15 00:00:00", captured_two.out)
    assert re.findall(r"GIT_URL\s+project_repo_url", captured_two.out)
    assert re.findall(r"GIT_BRANCH\s+ project_repo_branch", captured_two.out)
def test_create_cronjob_in_namespace(mock_k8s_module: MagicMock,
                                     capsys: CaptureFixture):
    mock_k8s_module.namespace_exists.return_value = False
    create_cronjob_in_namespace('bodywork-dev', '0 * * * *',
                                'bodywork-test-project', 'project_repo_url',
                                'project_repo_branch')
    captured_one = capsys.readouterr()
    assert 'namespace=bodywork-dev could not be found' in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_cronjobs.return_value = {'bodywork-test-project': {}}
    create_cronjob_in_namespace('bodywork-dev', '0 * * * *',
                                'bodywork-test-project', 'project_repo_url',
                                'project_repo_branch')
    captured_two = capsys.readouterr()
    assert 'cronjob=bodywork-test-project already exists' in captured_two.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_cronjobs.return_value = {'foo': {}}
    mock_k8s_module.create_cronjob.side_effect = None
    create_cronjob_in_namespace('bodywork-dev', '0 * * *',
                                'bodywork-test-project', 'project_repo_url',
                                'project_repo_branch')
    captured_three = capsys.readouterr()
    assert 'schedule=0 * * * is not a valid cron schedule' in captured_three.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_cronjobs.return_value = {'foo': {}}
    mock_k8s_module.create_cronjob.side_effect = None
    create_cronjob_in_namespace('bodywork-dev', '0 * * * *',
                                'bodywork-test-project', 'project_repo_url',
                                'project_repo_branch')
    captured_four = capsys.readouterr()
    assert 'cronjob=bodywork-test-project created in namespace' in captured_four.out
Beispiel #14
0
def test_display_service_deployments_in_namespace(mock_k8s_module: MagicMock,
                                                  capsys: CaptureFixture):
    mock_k8s_module.namespace_exists.return_value = False
    display_service_deployments_in_namespace('bodywork-dev')
    captured_one = capsys.readouterr()
    assert 'namespace=bodywork-dev could not be found' in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    service_url = 'http://bodywork-test-project--serve.bodywork-dev.svc.cluster.local'
    mock_k8s_module.list_service_stage_deployments.return_value = {
        'bodywork-test-project--serve': {
            'service_url': service_url,
            'service_port': 5000,
            'service_exposed': 'true',
            'available_replicas': 1,
            'unavailable_replicas': 0,
            'git_url': 'project_repo_url',
            'git_branch': 'project_repo_branch',
            'has_ingress': 'true',
            'ingress_route': '/bodywork-dev/bodywork-test-project'
        }
    }
    display_service_deployments_in_namespace('bodywork-dev')
    captured_two = capsys.readouterr()
    assert re.findall(r'REPLICAS_AVAILABLE\s+1', captured_two.out)
    assert re.findall(r'REPLICAS_UNAVAILABLE\s+0', captured_two.out)
    assert re.findall(r'GIT_URL\s+project_repo_url', captured_two.out)
    assert re.findall(r'GIT_BRANCH\s+project_repo_branch', captured_two.out)
    assert re.findall(r'CLUSTER_SERVICE_PORT\s+5000', captured_two.out)
    assert re.findall(
        r'CLUSTER_SERVICE_URL\s+http://bodywork-test-project--serve.bodywork-dev.svc',
        captured_two.out)
    assert re.findall(r'INGRESS_ROUTE\s+/bodywork-dev/bodywork-test-project',
                      captured_two.out)
def test_verify_args(parser: CompatibleArgumentParser, capsys: CaptureFixture):
    # --with-license-file missing
    with pytest.raises(SystemExit) as ex:
        parser.parse_args(['--no-license-path'])
    capture = capsys.readouterr().err
    for arg in ('--no-license-path', '--with-license-file'):
        assert arg in capture

    with pytest.raises(SystemExit) as ex:
        parser.parse_args(['--with-notice-file'])
    capture = capsys.readouterr().err
    for arg in ('--with-notice-file', '--with-license-file'):
        assert arg in capture

    # --filter-strings missing
    with pytest.raises(SystemExit) as ex:
        parser.parse_args(['--filter-code-page=utf8'])
    capture = capsys.readouterr().err
    for arg in ('--filter-code-page', '--filter-strings'):
        assert arg in capture

    # invalid code-page
    with pytest.raises(SystemExit) as ex:
        parser.parse_args(['--filter-strings', '--filter-code-page=XX'])
    capture = capsys.readouterr().err
    for arg in ('invalid code', '--filter-code-page'):
        assert arg in capture
Beispiel #16
0
def test_display_secrets_in_namespace(
    mock_k8s_module: MagicMock,
    capsys: CaptureFixture
):
    mock_k8s_module.list_secrets_in_namespace.return_value = {
        'test-credentials': {'USERNAME': '******', 'PASSWORD': '******'},
        'more-test-credentials': {'FOO': 'bar'}
    }

    mock_k8s_module.namespace_exists.return_value = False
    display_secrets_in_namespace('the-namespace', 'test-credentials')
    captured_one = capsys.readouterr()
    assert 'could not be found on k8s cluster' in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    display_secrets_in_namespace('the-namespace', 'test-credentials')
    captured_two = capsys.readouterr()
    assert 'USERNAME=alex' in captured_two.out
    assert 'PASSWORD=alex123' in captured_two.out

    mock_k8s_module.namespace_exists.return_value = True
    display_secrets_in_namespace('the-namespace', 'test-credentialz')
    captured_three = capsys.readouterr()
    assert 'cannot find secret=test-credentialz' in captured_three.out

    mock_k8s_module.namespace_exists.return_value = True
    display_secrets_in_namespace('the-namespace', None)
    captured_four = capsys.readouterr()
    assert 'test-credentials' in captured_four.out
    assert 'USERNAME=alex' in captured_four.out
    assert 'PASSWORD=alex123' in captured_four.out
    assert 'more-test-credentials' in captured_four.out
    assert 'FOO=bar' in captured_four.out
Beispiel #17
0
    def test_increment_without_metadata_without_schema(
        self,
        capsys: CaptureFixture,
        archive_dir: LocalPath,
        archive_fixture: "TestArchive.ArchiveCacheAndHashPassthruChecker",
        schema_file: Optional[LocalPath],
        verbose: bool,
    ):
        # List of (expected frame filename, data filename) tuples
        targets: List[Tuple[str, str]] = [
            ("iris-part-1-of-6-combined.csv", "iris-part-1-of-6.csv"),
            ("iris-part-1-2.csv", "iris-part-2-of-6.csv"),
            ("iris-part-1-2-3.csv", "iris-part-3-of-6.csv"),
            ("iris-part-1-2-3-4.csv", "iris-part-4-of-6.csv"),
            ("iris-part-1-2-3-4-5.csv", "iris-part-5-of-6.csv"),
            ("iris_plus.csv", "iris-part-6-of-6.csv"),
        ]

        expected_hashfile = (
            LocalPath(archive_fixture.cache_file).dirpath(DEFAULT_HASH_FILE) if
            archive_fixture.hash_file is None else archive_fixture.hash_file)
        assert not os.path.exists(expected_hashfile)
        assert not os.path.exists(archive_fixture.cache_file)
        assert len(archive_dir.listdir()) == 0

        for expected_frame_filename, data_filename in targets:
            assert archive_fixture(
                archive_dir,
                [os.path.join(get_data_path(), data_filename)],
                cache_filepath=archive_fixture.cache_file,
                hash_filepath=archive_fixture.hash_file,
                verbose=verbose,
            )
            assert_captured_outerr(capsys.readouterr(), verbose, False)

            expected_frame = DataFrame(
                read_csv(
                    os.path.join(get_data_path(), expected_frame_filename),
                    dtype=str,
                    index_col="Index",
                ))
            del expected_frame["Species"]
            del expected_frame["PetalColor"]
            expected_frame.sort_index(inplace=True)
            actual_frame = DataFrame(
                read_csv(str(archive_fixture.cache_file),
                         dtype=str,
                         index_col="Index"))
            actual_frame.sort_index(inplace=True)
            assert_captured_outerr(capsys.readouterr(), False, False)

            assert_frame_equal(expected_frame, actual_frame)
            assert os.path.exists(expected_hashfile)
            assert syphon.check(
                archive_fixture.cache_file,
                hash_filepath=expected_hashfile,
                verbose=verbose,
            )
Beispiel #18
0
def test_inspect(capsys: CaptureFixture) -> None:
    df = pl.DataFrame({"a": [1]})
    df.lazy().inspect().collect()
    captured = capsys.readouterr()
    assert len(captured.out) > 0

    df.select(pl.col("a").cumsum().inspect().alias("bar"))
    res = capsys.readouterr()
    assert len(res.out) > 0
Beispiel #19
0
def test_prompt_list_returns_id_of_selected_option(prompt: mock.Mock, capsys: CaptureFixture) -> None:
    logger = Logger()
    options = [Option(id=1, label="Option 1"), Option(id=2, label="Option 2"), Option(id=3, label="Option 3")]

    prompt.return_value = 3
    selected_option = logger.prompt_list("Select an option", options)

    assert selected_option == 3

    capsys.readouterr()
Beispiel #20
0
def test_progress_creates_started_progress_instance(capsys: CaptureFixture) -> None:
    logger = Logger()
    progress = logger.progress()

    result = progress._started

    progress.stop()
    assert result

    capsys.readouterr()
Beispiel #21
0
def test_show_data_io_method_help(capsys: CaptureFixture):
    """Same help as when called directly."""
    plugin = MockDataIO("foo")
    help(plugin.load_dataset)
    original_help, _ = capsys.readouterr()

    show_data_io_method_help(format_name="mock", method_name="load_dataset")
    result, _ = capsys.readouterr()

    assert "This docstring is just for help testing of 'load_dataset'." in result
    assert result == original_help
def test_create_secrets_in_namespace(mock_k8s_module: MagicMock,
                                     capsys: CaptureFixture):
    mock_k8s_module.namespace_exists.return_value = False
    create_secret_in_namespace('bodywork-dev', 'test-credentials', {'A': 'b'})
    captured_one = capsys.readouterr()
    assert 'namespace=bodywork-dev could not be found on k8s cluster' in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.create_secret.side_effect = None
    create_secret_in_namespace('bodywork-dev', 'test-credentials', {'A': 'b'})
    captured_two = capsys.readouterr()
    assert 'test-credentials created in namespace=bodywork-dev' in captured_two.out
Beispiel #23
0
def test_create_workflow_cronjob_in_namespace(mock_k8s_module: MagicMock,
                                              capsys: CaptureFixture):
    mock_k8s_module.namespace_exists.return_value = False
    create_workflow_cronjob_in_namespace(
        "bodywork-dev",
        "0 * * * *",
        "bodywork-test-project",
        "project_repo_url",
        "project_repo_branch",
    )
    captured_one = capsys.readouterr()
    assert "namespace=bodywork-dev could not be found" in captured_one.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_workflow_cronjobs.return_value = {
        "bodywork-test-project": {}
    }
    create_workflow_cronjob_in_namespace(
        "bodywork-dev",
        "0 * * * *",
        "bodywork-test-project",
        "project_repo_url",
        "project_repo_branch",
    )
    captured_two = capsys.readouterr()
    assert "cronjob=bodywork-test-project already exists" in captured_two.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_workflow_cronjobs.return_value = {"foo": {}}
    mock_k8s_module.create_workflow_cronjob.side_effect = None
    create_workflow_cronjob_in_namespace(
        "bodywork-dev",
        "0 * * *",
        "bodywork-test-project",
        "project_repo_url",
        "project_repo_branch",
    )
    captured_three = capsys.readouterr()
    assert "schedule=0 * * * is not a valid cron schedule" in captured_three.out

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.list_workflow_cronjobs.return_value = {"foo": {}}
    mock_k8s_module.create_workflow_cronjob.side_effect = None
    create_workflow_cronjob_in_namespace(
        "bodywork-dev",
        "0 * * * *",
        "bodywork-test-project",
        "project_repo_url",
        "project_repo_branch",
    )
    captured_four = capsys.readouterr()
    assert "cronjob=bodywork-test-project created in namespace" in captured_four.out
Beispiel #24
0
def test_show_method_help(capsys: CaptureFixture,
                          plugin: MockPlugin | type[MockPlugin]):
    """Same help as when called directly.

    Note: help differs when called on class.method vs. instance.method
    """
    help(plugin.some_method)
    original_help, _ = capsys.readouterr()

    show_method_help(plugin, "some_method")
    result, _ = capsys.readouterr()

    assert "This docstring is just for help testing of 'some_method'." in result
    assert result == original_help
def test_pp(capsys: CaptureFixture) -> None:
    """Using the shortcut `pp` function."""
    colours = [["black", "cyan"], "white", ["green", "red", ["blue", "brown"]]]

    pprint.pp(colours)
    out, err = capsys.readouterr()
    assert out == "[['black', 'cyan'], 'white', ['green', 'red', ['blue', 'brown']]]\n"

    pprint.pp(colours, indent=4, width=30, depth=2)
    out, err = capsys.readouterr()
    assert (out == """\
[   ['black', 'cyan'],
    'white',
    ['green', 'red', [...]]]\n""")
Beispiel #26
0
    def test_run_activate_deactivate_project(self, capsys: CaptureFixture,
                                             project_loader):
        project_loader("project")

        features.register(CoreFeature())
        features.register(ShellFeature())
        load_registered_features()

        action = ActivateAction(self.build_shell_integration())
        action.execute()

        capture = capsys.readouterr()
        assert capture.out
        assert not capture.err

        filepath = re.match(self.filepath_regex, capture.out).group(1)
        with open(filepath, 'r', encoding='utf-8') as file:
            script = file.read()

        export_match = re.findall(self.export_regex, script, re.MULTILINE)
        env = dict(export_match)

        system_path = env.get('PATH', '')
        first = system_path.split(os.pathsep)[0]

        assert first == os.path.normpath(os.path.join(os.getcwd(), "./bin")) \
               or first == os.path.normpath(os.path.join(os.getcwd(), "./.bin"))

        os.environ.update(env)

        deactivate_action = DeactivateAction(self.build_shell_integration())
        deactivate_action.execute()

        capture = capsys.readouterr()
        assert capture.out
        assert not capture.err

        filepath = re.match(self.filepath_regex, capture.out).group(1)
        with open(filepath, 'r', encoding='utf-8') as file:
            script = file.read()

        export_match = re.findall(self.export_regex, script, re.MULTILINE)
        env = dict(export_match)

        system_path = env.get('PATH', '').split(os.pathsep)
        first = system_path[0]

        assert first != os.path.normpath(os.path.join(os.getcwd(), "./bin")) or \
               first != os.path.normpath(os.path.join(os.getcwd(), "./.bin"))
def test_create_secrets_in_namespace(
    mock_k8s_module: MagicMock, capsys: CaptureFixture
):
    mock_k8s_module.namespace_exists.return_value = False
    create_secret_in_namespace("bodywork-dev", "test-credentials", {"A": "b"})
    captured_one = capsys.readouterr()
    assert (
        "namespace=bodywork-dev could not be found on k8s cluster" in captured_one.out
    )

    mock_k8s_module.namespace_exists.return_value = True
    mock_k8s_module.create_secret.side_effect = None
    create_secret_in_namespace("bodywork-dev", "test-credentials", {"A": "b"})
    captured_two = capsys.readouterr()
    assert "test-credentials created in namespace=bodywork-dev" in captured_two.out
Beispiel #28
0
def verify_output(capsys: CaptureFixture, filename: str) -> None:
    """ Utility function to ensure output matches file. """
    captured, _ = capsys.readouterr()
    with capsys.disabled():
        with open(filename, encoding="utf-8") as output_file:
            expected = output_file.read()
    assert captured == expected
Beispiel #29
0
def test_session_scope_error(monkeypatch: MonkeyPatch, capsys: CaptureFixture,
                             default_domain: Domain):
    tracker_store = SQLTrackerStore(default_domain)
    tracker_store.sessionmaker = Mock()

    requested_schema = uuid.uuid4().hex

    # `ensure_schema_exists()` raises `ValueError`
    mocked_ensure_schema_exists = Mock(
        side_effect=ValueError(requested_schema))
    monkeypatch.setattr(
        rasa.core.tracker_store,
        "ensure_schema_exists",
        mocked_ensure_schema_exists,
    )

    # `SystemExit` is triggered by failing `ensure_schema_exists()`
    with pytest.raises(SystemExit):
        with tracker_store.session_scope() as _:
            pass

    # error message is printed
    assert (
        f"Requested PostgreSQL schema '{requested_schema}' was not found in the "
        f"database." in capsys.readouterr()[0])
def test_main_custom_template_dir(capsys: CaptureFixture) -> None:
    """Test main function with custom template directory."""

    input_filename = OPEN_API_DATA_PATH / 'api.yaml'
    custom_template_dir = DATA_PATH / 'templates'
    extra_template_data = OPEN_API_DATA_PATH / 'extra_data.json'

    with freeze_time(TIMESTAMP):
        main(
            [
                '--input',
                str(input_filename),
                '--custom-template-dir',
                str(custom_template_dir),
                '--extra-template-data',
                str(extra_template_data),
            ]
        )

    captured = capsys.readouterr()
    assert (
        captured.out
        == (EXPECTED_MAIN_PATH / 'main_custom_template_dir' / 'output.py').read_text()
    )
    assert not captured.err