Example #1
0
def test_keymanager_keys_file():

    key_manager = _make_sample_cgap_key_manager()

    original_file = key_manager.keys_file

    assert isinstance(original_file, str)

    assert key_manager.keys_file == original_file

    with override_environ(CGAP_KEYS_FILE=None):
        assert os.environ.get('CGAP_KEYS_FILE') is None
        assert key_manager.keys_file == original_file

    assert key_manager.keys_file == original_file

    with override_environ(CGAP_KEYS_FILE=""):
        assert os.environ.get('CGAP_KEYS_FILE') == ""
        assert key_manager.keys_file == original_file

    assert key_manager.keys_file == original_file

    alternate_file = 'some-other-file'

    with override_environ(CGAP_KEYS_FILE=alternate_file):
        assert os.environ.get('CGAP_KEYS_FILE') == alternate_file

        key_manager = CGAPKeyManager()
        assert key_manager.keys_file == alternate_file

    key_manager = _make_sample_cgap_key_manager()

    assert key_manager.keys_file == original_file
Example #2
0
def test_import_fails_without_initialization():

    # Loading this library fails if proper env variables were not set up.
    # TODO: I'm not sure I think that's a good idea. Functions should fail, imports should not. -kmp 14-Aug-2020

    with override_environ(FF_ACCESS_KEY=None):
        with pytest.raises(Exception) as exec_info:
            from dcicutils import jh_utils  # NOQA
        assert 'ERROR USING JUPYTERHUB_UTILS!' in str(exec_info.value)

    with override_environ(FF_ACCESS_SECRET=None):
        with pytest.raises(Exception) as exec_info:
            from dcicutils import jh_utils  # NOQA
        assert 'ERROR USING JUPYTERHUB_UTILS!' in str(exec_info.value)
Example #3
0
def test_fourfront_keymanager_creation():

    sample_ff_key_manager_1 = FourfrontKeyManager()

    assert sample_ff_key_manager_1.keys_file == FourfrontKeyManager._default_keys_file(
    )

    sample_ff_key_manager_2 = _make_sample_fourfront_key_manager()

    assert sample_ff_key_manager_2.keys_file == SAMPLE_FOURFRONT_KEYS_FILE

    with override_environ(FF_DEFAULT_ENV=SAMPLE_FOURFRONT_LOCAL_PSEUDOENV):

        sample_ff_key_manager_3 = FourfrontKeyManager()

        assert sample_ff_key_manager_3.keys_file == FourfrontKeyManager._default_keys_file(
        )
        # Make sure class default is different than test value. More of a test-integrity test than an absolute need.
        assert sample_ff_key_manager_3.keys_file != SAMPLE_FOURFRONT_KEYS_FILE

    other_keys_file = "other-ff-keys.json"

    class MyFourfrontKeyManager(FourfrontKeyManager):
        KEYS_FILE = other_keys_file

    sample_ff_key_manager_4 = MyFourfrontKeyManager(
    )  # Tests that no error is raised

    assert sample_ff_key_manager_4.keys_file == other_keys_file
Example #4
0
def test_s3utils_s3_put_secret():

    util = s3Utils(env='fourfront-mastertest')
    standard_algorithm = "AES256"
    environmental_key = 'environmental-key'
    with override_environ(S3_ENCRYPT_KEY=environmental_key):
        with mock.patch.object(util.s3, "put_object") as mock_put_object:
            def mocked_put_object(**kwargs):
                return kwargs
            mock_put_object.side_effect = mocked_put_object
            item = {'a': 1, 'b': 2}
            some_key = 'some-key'
            some_secret = 'some-secret'
            assert util.s3_put_secret(item, keyname=some_key) == {
                "Body": item,
                "Bucket": util.sys_bucket,
                "Key": some_key,
                "SSECustomerKey": environmental_key,
                "SSECustomerAlgorithm": standard_algorithm,
            }
            some_bucket = 'some-bucket'
            assert util.s3_put_secret(item, keyname=some_key, bucket=some_bucket) == {
                "Body": item,
                "Bucket": some_bucket,
                "Key": some_key,
                "SSECustomerKey": environmental_key,
                "SSECustomerAlgorithm": standard_algorithm,
            }
            assert util.s3_put_secret(item, keyname=some_key, secret=some_secret) == {
                "Body": item,
                "Bucket": util.sys_bucket,
                "Key": some_key,
                "SSECustomerKey": some_secret,
                "SSECustomerAlgorithm": standard_algorithm,
            }
Example #5
0
    def test_it(args_in,
                expect_exit_code,
                expect_called,
                expect_call_args=None):

        with override_environ(CGAP_KEYS_FILE=keyfile):
            with mock.patch.object(
                    submit_genelist_module,
                    "submit_any_ingestion") as mock_submit_any_ingestion:
                try:
                    # Outside of the call, we will always see the default filename for cgap keys
                    # but inside the call, because of a decorator, the default might be different.
                    # See additional test below.
                    assert KeyManager.keydicts_filename(
                    ) == KeyManager.DEFAULT_KEYDICTS_FILENAME

                    def mocked_submit_genelist(*args, **kwargs):
                        ignored(args, kwargs)
                        # We don't need to test this function's actions because we test its call args below.
                        # However, we do need to run this one test from the same dynamic context,
                        # so this is close enough.
                        assert KeyManager.keydicts_filename() == (
                            keyfile or KeyManager.DEFAULT_KEYDICTS_FILENAME)

                    mock_submit_any_ingestion.side_effect = mocked_submit_genelist
                    submit_genelist_main(args_in)
                    mock_submit_any_ingestion.assert_called_with(
                        **expect_call_args)
                except SystemExit as e:
                    assert e.code == expect_exit_code
                assert mock_submit_any_ingestion.call_count == (
                    1 if expect_called else 0)
Example #6
0
def test_cgap_keymanager_creation():

    sample_cgap_key_manager_1 = CGAPKeyManager()

    assert sample_cgap_key_manager_1.keys_file == CGAPKeyManager._default_keys_file(
    )

    sample_cgap_key_manager_2 = _make_sample_cgap_key_manager()

    assert sample_cgap_key_manager_2.keys_file == SAMPLE_CGAP_KEYS_FILE

    with override_environ(CGAP_KEYS_FILE=SAMPLE_CGAP_KEYS_FILE):

        sample_cgap_key_manager_3 = CGAPKeyManager()

        assert sample_cgap_key_manager_3.keys_file == SAMPLE_CGAP_KEYS_FILE
        # Make sure class default is different than test value. More of a test-integrity test than an absolute need.
        assert sample_cgap_key_manager_3.keys_file != CGAPKeyManager._default_keys_file(
        )

    other_keys_file = "other-cgap-keys.json"

    class MyCGAPKeyManager(CGAPKeyManager):
        KEYS_FILE = other_keys_file

    sample_cgap_key_manager_4 = MyCGAPKeyManager(
    )  # Tests that no error is raised

    assert sample_cgap_key_manager_4.keys_file == other_keys_file
Example #7
0
def default_env_for_testing(default_env):
    with override_environ(
            **{base_module.DEFAULT_ENV_VAR: default_env}):  # step 1 of 2
        with mock.patch.object(
                base_module,
                "DEFAULT_ENV",  # step 2 of 2
                base_module._compute_default_env()
        ):  # noqa - need private function for testing
            yield
Example #8
0
def build_production_ini_from_global_application_configuration():
    """ This function makes a request to secrets manager for the identity passed to the container.
        See documentation on API in dcicutils.
    """
    identity = assume_identity()

    # build production.ini
    with override_environ(**identity):

        CGAPDockerIniFileManager.build_ini_file_from_template(
            '/home/nginx/cgap-portal/deploy/ini_files/cgap_any_alpha.ini',
            '/home/nginx/cgap-portal/production.ini')
Example #9
0
def test_keymanager():

    original_file = KeyManager.keydicts_filename()

    assert isinstance(original_file, str)

    with KeyManager.alternate_keydicts_filename(None):
        assert KeyManager.keydicts_filename() == original_file

    assert KeyManager.keydicts_filename() == original_file

    with override_environ(CGAP_KEYS_FILE=None):
        assert os.environ.get('CGAP_KEYS_FILE') is None
        with KeyManager.alternate_keydicts_filename_from_environ():
            assert KeyManager.keydicts_filename() == original_file
        assert KeyManager.keydicts_filename() == original_file

    assert KeyManager.keydicts_filename() == original_file

    with override_environ(CGAP_KEYS_FILE=""):
        assert os.environ.get('CGAP_KEYS_FILE') == ""
        with KeyManager.alternate_keydicts_filename_from_environ():
            assert KeyManager.keydicts_filename() == original_file

    assert KeyManager.keydicts_filename() == original_file

    alternate_file = 'some-other-file'

    with KeyManager.alternate_keydicts_filename(alternate_file):
        assert KeyManager.keydicts_filename() == alternate_file

    assert KeyManager.keydicts_filename() == original_file

    with override_environ(CGAP_KEYS_FILE=alternate_file):
        assert os.environ.get('CGAP_KEYS_FILE') == alternate_file
        with KeyManager.alternate_keydicts_filename_from_environ():
            assert KeyManager.keydicts_filename() == alternate_file
        assert KeyManager.keydicts_filename() == original_file

    assert KeyManager.keydicts_filename() == original_file
Example #10
0
def test_post_ingestion_submission_wrong_project(anontestapp, institution,
                                                 bgm_user, bgm_project,
                                                 bgm_access_key, non_bgm_user,
                                                 non_bgm_project,
                                                 non_bgm_access_key):
    with override_environ(REMOTE_USER=None):
        creation_post_data = {
            "ingestion_type": "metadata_bundle",
            "institution": institution['name'],
            "project": non_bgm_project['name'],
            "processing_status": {
                "state": "submitted"
            }
        }
        keypair = (bgm_access_key['access_key_id'],
                   bgm_access_key['secret_access_key'])
        creation_post_headers = {
            'Content-type': 'application/json',
            'Accept': 'application/json',
            'Authorization': basic_auth(*keypair),
        }

        def dbg(label, x, *props):
            if not props:
                print("%s: %s" % (label, json.dumps(x, indent=2, default=str)))
            else:
                for prop in props:
                    print(
                        "%s[%r]: %s" %
                        (label, prop,
                         json.dumps(
                             x.get(prop, "<MISSING>"), indent=2, default=str)))

        dbg("bgm_user", bgm_user, '@id', 'user_institution', 'project_roles')
        dbg("non_bgm_user", non_bgm_user, '@id', 'user_institution',
            'project_roles')
        dbg("bgm_project", bgm_project, '@id', 'uuid', 'name')
        dbg("non_bgm_project", non_bgm_project, '@id', 'uuid', 'name')
        dbg("bgm_access_key", bgm_access_key, 'access_key_id',
            'secret_access_key')
        dbg("non_bgm_access_key", non_bgm_access_key, 'access_key_id',
            'secret_access_key')
        dbg("post data", creation_post_data)
        dbg("post headers", creation_post_headers)

        print("posting /IngestionSubmission, expecting 403, with keypair",
              keypair)
        response = anontestapp.post_json("/IngestionSubmission",
                                         creation_post_data,
                                         headers=creation_post_headers,
                                         status=403)
        ignorable(response)
Example #11
0
def test_s3_utils_environment_variable_use():

    with pytest.raises(SynonymousEnvironmentVariablesMismatched):

        with override_environ(GLOBAL_BUCKET_ENV='should-be-unused',
                              GLOBAL_ENV_BUCKET='inconsistently-unused'):

            # If we do the simple-minded version of this, the environment variable doesn't matter
            s3Utils(sys_bucket='foo')

            with pytest.raises(SynonymousEnvironmentVariablesMismatched):
                # If we don't initialize the sys_bucket, we have to go through the smart protocols
                # and expect environment variables to be in order.
                s3Utils()
Example #12
0
def test_env_manager_global_env_bucket_name():

    # These tests expect to be run in an environment that does not have these buckets bound globally.
    assert os.environ.get('GLOBAL_ENV_BUCKET') is None
    assert os.environ.get('GLOBAL_BUCKET_ENV') is None

    with EnvManager.global_env_bucket_named(name='foo'):

        assert os.environ.get('GLOBAL_ENV_BUCKET') == 'foo'
        assert os.environ.get('GLOBAL_BUCKET_ENV') == 'foo'
        assert EnvManager.global_env_bucket_name() == 'foo'

        with override_environ(GLOBAL_BUCKET_ENV='bar'):

            assert os.environ.get('GLOBAL_ENV_BUCKET') == 'foo'
            assert os.environ.get('GLOBAL_BUCKET_ENV') == 'bar'
            with pytest.raises(SynonymousEnvironmentVariablesMismatched):
                EnvManager.global_env_bucket_name()

            with override_environ(GLOBAL_ENV_BUCKET='bar'):

                assert os.environ.get('GLOBAL_ENV_BUCKET') == 'bar'
                assert os.environ.get('GLOBAL_BUCKET_ENV') == 'bar'
                assert EnvManager.global_env_bucket_name() == 'bar'

        with override_environ(GLOBAL_ENV_BUCKET='bar'):

            assert os.environ.get('GLOBAL_ENV_BUCKET') == 'bar'
            assert os.environ.get('GLOBAL_BUCKET_ENV') == 'foo'
            with pytest.raises(SynonymousEnvironmentVariablesMismatched):
                EnvManager.global_env_bucket_name()

            with override_environ(GLOBAL_BUCKET_ENV='bar'):

                assert os.environ.get('GLOBAL_ENV_BUCKET') == 'bar'
                assert os.environ.get('GLOBAL_BUCKET_ENV') == 'bar'
                assert EnvManager.global_env_bucket_name() == 'bar'
Example #13
0
    def test_it(args_in,
                expect_exit_code,
                expect_called,
                expect_call_args=None):

        with override_environ(CGAP_KEYS_FILE=keyfile):
            with mock.patch.object(
                    upload_item_data_module,
                    "upload_item_data") as mock_upload_item_data:
                with mock.patch.object(
                        submission_module,
                        "get_health_page") as mock_get_health_page:

                    mock_get_health_page.return_value = {
                        HealthPageKey.S3_ENCRYPT_KEY_ID:
                        mocked_s3_encrypt_key_id
                    }

                    with system_exit_expected(exit_code=expect_exit_code):
                        # Outside of the call, we will always see the default filename for cgap keys
                        # but inside the call, because of a decorator, the default might be different.
                        # See additional test below.
                        assert KeyManager.keydicts_filename(
                        ) == KeyManager.DEFAULT_KEYDICTS_FILENAME

                        def mocked_upload_item_data(*args, **kwargs):
                            ignored(args, kwargs)
                            # We don't need to test this function's actions because we test its call args below.
                            # However, we do need to run this one test from the same dynamic context,
                            # so this is close enough.
                            if KeyManager.keydicts_filename() == (
                                    keyfile
                                    or KeyManager.DEFAULT_KEYDICTS_FILENAME):
                                exit(0)
                            else:
                                # This case is not expected to be reached but must be here for testing to catch
                                exit(1)  # pragma: no cover

                        mock_upload_item_data.side_effect = mocked_upload_item_data
                        upload_item_data_main(args_in)
                        raise AssertionError(
                            "upload_item_data_main should not exit normally."
                        )  # pragma: no cover
                    assert mock_upload_item_data.call_count == (
                        1 if expect_called else 0)
def build_ini_file(environment, use_prod):
    """ Wrapper method for main functionality that can be invoked directly by others.

        :param environment: environment to simulate
        :param use_prod: an extra value that must be true to simulate staging/production
        :returns: True if successful, False otherwise
    """
    if is_stg_or_prd_env(environment) and not use_prod:
        raise Exception(
            'Tried to run on production env %s without prod identifier!' %
            environment)
    beanstalk_env = get_beanstalk_environment_variables(environment)
    template_file_name = ProductionIniFileManager.environment_template_filename(
        environment)
    with override_environ(**beanstalk_env):
        ProductionIniFileManager.build_ini_file_from_template(
            template_file_name, 'production.ini')
    return True
    def test_it(args_in,
                expect_exit_code,
                expect_called,
                expect_call_args=None):
        output = []
        with override_environ(CGAP_KEYS_FILE=keyfile):
            with mock.patch.object(show_upload_info_module,
                                   "print") as mock_print:
                mock_print.side_effect = lambda *args: output.append(" ".join(
                    args))
                with mock.patch.object(
                        show_upload_info_module,
                        "show_upload_info") as mock_show_upload_info:
                    try:
                        # Outside of the call, we will always see the default filename for cgap keys
                        # but inside the call, because of a decorator, the default might be different.
                        # See additional test below.
                        assert KeyManager.keydicts_filename(
                        ) == KeyManager.DEFAULT_KEYDICTS_FILENAME

                        def mocked_show_upload_info(*args, **kwargs):
                            ignored(args, kwargs)
                            # We don't need to test this function's actions because we test its call args below.
                            # However, we do need to run this one test from the same dynamic context,
                            # so this is close enough.
                            assert KeyManager.keydicts_filename() == (
                                keyfile
                                or KeyManager.DEFAULT_KEYDICTS_FILENAME)

                        mock_show_upload_info.side_effect = mocked_show_upload_info
                        show_upload_info_main(args_in)
                        raise AssertionError(
                            "show_upload_info_main should not exit normally."
                        )  # pragma: no cover
                    except SystemExit as e:
                        assert e.code == expect_exit_code
                    assert mock_show_upload_info.call_count == (
                        1 if expect_called else 0)
                    assert output == []
Example #16
0
def test_s3_utils_legacy_behavior():
    # From https://hms-dbmi.atlassian.net/browse/C4-674

    outfile_bucket = 'my-outfile-bucket'
    sys_bucket = 'my-system-bucket'
    raw_file_bucket = 'my-raw_file-bucket'

    def test_it():

        # As long as sys_bucket= is given in the s3Utils() call, it will just fill the slots
        # with given values and won't try to do anything smart.

        s = s3Utils(outfile_bucket, sys_bucket, raw_file_bucket)
        assert s.outfile_bucket == outfile_bucket
        assert s.sys_bucket == sys_bucket
        assert s.raw_file_bucket == raw_file_bucket
        assert s.blob_bucket is None
        assert s.metadata_bucket is None
        assert s.tibanna_cwls_bucket is None
        assert s.tibanna_output_bucket is None

        s = s3Utils(sys_bucket=sys_bucket)
        assert s.outfile_bucket is None
        assert s.sys_bucket == sys_bucket
        assert s.raw_file_bucket is None
        assert s.blob_bucket is None
        assert s.metadata_bucket is None
        assert s.tibanna_cwls_bucket is None
        assert s.tibanna_output_bucket is None

    test_it()

    # Test that certain legacy behavior is unperturbed by GLOBAL_ENV_BUCKET (or its older name, GLOBAL_BUCKET_ENV)
    with override_environ(GLOBAL_BUCKET_ENV='should-be-unused',
                          GLOBAL_ENV_BUCKET='should-be-unused'):
        test_it()
Example #17
0
def test_transitional_equivalence():
    """
    We used to use separate files for each environment. This tests that the new any.ini technology,
    with a few new environment variables, will produce the same thing.

    This proves that if we set at least "ENCODED_ES_SERVER" and "ENCODED_BS_ENV" environment variables,
    or invoke generate_ini_file adding the "--es_server" nad "--bs_env" arguments, we should get a proper
    production.ini.
    """

    # TODO: Once this mechanism is in place, the files cgap.ini, cgapdev.ini, cgaptest.ini, and cgapwolf.ini
    #       can either be removed (and these transitional tests removed) or transitioned to be test data.

    def tester(ref_ini,
               bs_env,
               data_set,
               es_server,
               es_namespace=None,
               line_checker=None):

        print("tester entered.")
        print(" ref_ini=", ref_ini)
        print(" bs_env=", bs_env)
        print(" data_set=", data_set)
        print(" es_server=", es_server)
        print(" es_namespace=", es_namespace)
        print(" line_checker=", line_checker)

        assert ref_ini[:-4] == bs_env[
            10:]  # "xxx.ini" needs to match "fourfront-xxx"

        es_namespace = es_namespace or bs_env

        # Test of build_ini_from_template with just 2 keyword arguments explicitly supplied (bs_env, es_server),
        # and others defaulted.

        old_output = StringIO()
        new_output = StringIO()

        build_ini_stream_from_template(os.path.join(TEMPLATE_DIR, ref_ini),
                                       old_output,
                                       bs_env=bs_env,
                                       es_server=es_server)
        build_ini_stream_from_template(
            os.path.join(TEMPLATE_DIR, "any.ini"),
            new_output,
            # data_env and es_namespace are something we should be able to default
            bs_env=bs_env,
            es_server=es_server)

        old_content = old_output.getvalue()
        new_content = new_output.getvalue()
        assert old_content == new_content

        # Test of build_ini_from_template with all 4 keyword arguments explicitly supplied (bs_env, data_set,
        # es_server, es_namespace), none defaulted.

        old_output = StringIO()
        new_output = StringIO()

        build_ini_stream_from_template(os.path.join(TEMPLATE_DIR, ref_ini),
                                       old_output,
                                       bs_env=bs_env,
                                       data_set=data_set,
                                       es_server=es_server,
                                       es_namespace=es_namespace)
        build_ini_stream_from_template(os.path.join(TEMPLATE_DIR, "any.ini"),
                                       new_output,
                                       bs_env=bs_env,
                                       data_set=data_set,
                                       es_server=es_server,
                                       es_namespace=es_namespace)

        old_content = old_output.getvalue()
        new_content = new_output.getvalue()
        assert old_content == new_content

        problems = []

        if line_checker:

            for raw_line in io.StringIO(new_content):
                line = raw_line.rstrip()
                problem = line_checker.check(line)
                if problem:
                    problems.append(problem)

            line_checker.check_finally()

            assert problems == [], "Problems found:\n%s" % "\n".join(problems)

        print("tester succeeded.")

    with mock.patch.object(ProductionIniFileManager,
                           "get_app_version",
                           return_value=MOCKED_PROJECT_VERSION):
        with mock.patch("toml.load",
                        return_value={
                            "tool": {
                                "poetry": {
                                    "version": MOCKED_LOCAL_GIT_VERSION
                                }
                            }
                        }):

            class Checker:
                def __init__(self,
                             expect_indexer: typing.Optional[str] = "true"):
                    self.indexer = None
                    self.expect_indexer = expect_indexer

                def check_any(self, line):
                    if line.startswith('indexer ='):
                        print("saw indexer line:", repr(line))
                        self.indexer = line.split('=')[1].strip()

                def check(self, line):
                    self.check_any(line)

                def check_finally(self):
                    assert self.indexer == self.expect_indexer, (
                        "Expected 'indexer = %s' but value seen was %r." %
                        (self.expect_indexer, self.indexer))

            class ProdChecker(Checker):
                def check(self, line):
                    if 'bucket =' in line:
                        fragment = 'fourfront-cgap'
                        if fragment not in line:
                            return "'%s' missing in '%s'" % (fragment, line)
                    self.check_any(line)

            with override_environ(
                    ENCODED_INDEXER=None
            ):  # Make sure any global settings are masked.

                bs_env = "fourfront-cgap"
                data_set = data_set_for_env(bs_env)
                tester(
                    ref_ini="cgap.ini",
                    bs_env=bs_env,
                    data_set=data_set,
                    es_server=
                    "search-fourfront-cgap-ewf7r7u2nq3xkgyozdhns4bkni.us-east-1.es.amazonaws.com:80",
                    line_checker=ProdChecker())

                bs_env = "fourfront-cgapdev"
                data_set = data_set_for_env(bs_env)
                tester(
                    ref_ini="cgapdev.ini",
                    bs_env=bs_env,
                    data_set=data_set,
                    es_server=
                    "search-fourfront-cgapdev-gnv2sgdngkjbcemdadmaoxcsae.us-east-1.es.amazonaws.com:80",
                    line_checker=Checker())

                bs_env = "fourfront-cgaptest"
                data_set = data_set_for_env(bs_env)
                tester(
                    ref_ini="cgaptest.ini",
                    bs_env=bs_env,
                    data_set=data_set,
                    es_server=
                    "search-fourfront-cgaptest-dxiczz2zv7f3nshshvevcvmpmy.us-east-1.es.amazonaws.com:80",
                    line_checker=Checker())

                bs_env = "fourfront-cgapwolf"
                data_set = data_set_for_env(bs_env)
                tester(
                    ref_ini="cgapwolf.ini",
                    bs_env=bs_env,
                    data_set=data_set,
                    es_server=
                    "search-fourfront-cgapwolf-r5kkbokabymtguuwjzspt2kiqa.us-east-1.es.amazonaws.com:80",
                    line_checker=Checker())

                with override_environ(ENCODED_INDEXER=""):

                    tester(
                        ref_ini="cgap.ini",
                        bs_env="fourfront-cgap",
                        data_set="prod",
                        es_server=
                        "search-fourfront-cgap-ewf7r7u2nq3xkgyozdhns4bkni.us-east-1.es.amazonaws.com:80",
                        line_checker=ProdChecker())

                with override_environ(ENCODED_INDEXER="TRUE"):

                    tester(
                        ref_ini="cgap.ini",
                        bs_env="fourfront-cgap",
                        data_set="prod",
                        es_server=
                        "search-fourfront-cgap-ewf7r7u2nq3xkgyozdhns4bkni.us-east-1.es.amazonaws.com:80",
                        line_checker=ProdChecker())

                with override_environ(ENCODED_INDEXER="FALSE"):

                    tester(
                        ref_ini="cgap.ini",
                        bs_env="fourfront-cgap",
                        data_set="prod",
                        es_server=
                        "search-fourfront-cgap-ewf7r7u2nq3xkgyozdhns4bkni.us-east-1.es.amazonaws.com:80",
                        line_checker=ProdChecker(expect_indexer=None))
Example #18
0
def test_build_ini_file_from_template():
    # NOTE: This implicitly also tests build_ini_file_from_stream.

    some_template_file_name = "mydir/whatever"
    some_ini_file_name = "mydir/production.ini"
    env_vars = dict(RDS_DB_NAME='snow_white',
                    RDS_USERNAME='******',
                    RDS_PASSWORD='******',
                    RDS_HOSTNAME='unittest',
                    RDS_PORT="6543")

    with override_environ(**env_vars):

        for env_var in env_vars:
            assert env_var in os.environ and os.environ[env_var] == env_vars[
                env_var], ("os.environ[%r] did not get added correctly" %
                           env_var)

        class MockFileStream:
            FILE_SYSTEM = {}

            @classmethod
            def reset(cls):
                cls.FILE_SYSTEM = {}

            def __init__(self, filename, mode):
                assert 'w' in mode
                self.filename = filename
                self.output_string_stream = StringIO()

            def __enter__(self):
                return self.output_string_stream

            def __exit__(self, type, value, traceback):
                self.FILE_SYSTEM[
                    self.filename] = self.output_string_stream.getvalue(
                    ).strip().split('\n')

        def mocked_open(filename, mode='r', encoding=None):
            assert encoding in (None, 'utf-8')
            # In this test there are two opens, one for read and one for write, so we discriminate on that basis.
            print("Enter mock_open", filename, mode)
            if mode == 'r':
                if filename == EB_MANIFEST_FILENAME:
                    print("reading mocked EB MANIFEST:", EB_MANIFEST_FILENAME)
                    return StringIO(
                        '{"Some": "Stuff", "VersionLabel": "%s", "Other": "Stuff"}\n'
                        % MOCKED_BUNDLE_VERSION)
                elif filename == some_template_file_name:
                    print("reading mocked TEMPLATE FILE", some_ini_file_name)
                    return StringIO(
                        '[Foo]\n'
                        'DATABASE = "${RDS_DB_NAME}"\n'
                        'SOME_URL = "http://${RDS_USERNAME}@$RDS_HOSTNAME:$RDS_PORT/"\n'
                        'OOPS = "$NOT_AN_ENV_VAR"\n'
                        'HMMM = "${NOT_AN_ENV_VAR_EITHER}"\n'
                        'SHHH = "$RDS_PASSWORD"\n'
                        'VERSION = "${APP_VERSION}"\n'
                        'PROJECT_VERSION = "${PROJECT_VERSION}"\n')
                elif filename == PYPROJECT_FILE_NAME:
                    print("reading mocked TOML FILE", PYPROJECT_FILE_NAME)
                    return StringIO('[something]\n'
                                    'version = "5.6.7"\n'
                                    '[tool.poetry]\n'
                                    'author = "somebody"\n'
                                    'version = "%s"\n' %
                                    MOCKED_PROJECT_VERSION)
                else:
                    raise AssertionError("mocked_open(%r, %r) unsupported." %
                                         (filename, mode))
            else:
                assert mode == 'w'
                assert filename == some_ini_file_name
                return MockFileStream(filename, mode)

        with mock.patch("subprocess.check_output") as mock_check_output:
            mock_check_output.side_effect = make_mocked_check_output_for_get_version(
            )
            with mock.patch("os.path.exists") as mock_exists:

                def mocked_exists(filename):
                    return filename in [
                        EB_MANIFEST_FILENAME, some_template_file_name
                    ]

                mock_exists.side_effect = mocked_exists
                with mock.patch("io.open", side_effect=mocked_open):
                    build_ini_file_from_template(some_template_file_name,
                                                 some_ini_file_name)

        assert MockFileStream.FILE_SYSTEM[some_ini_file_name] == [
            '[Foo]',
            'DATABASE = "snow_white"',
            'SOME_URL = "http://user@unittest:6543/"',
            'OOPS = "$NOT_AN_ENV_VAR"',
            'HMMM = "${NOT_AN_ENV_VAR_EITHER}"',
            'SHHH = "my-secret"',
            'VERSION = "%s"' % MOCKED_BUNDLE_VERSION,
            'PROJECT_VERSION = "%s"' % MOCKED_PROJECT_VERSION,
        ]

        MockFileStream.reset()

        with mock.patch("subprocess.check_output") as mock_check_output:
            mock_check_output.side_effect = make_mocked_check_output_for_get_version(
            )
            with mock.patch("os.path.exists") as mock_exists:

                def mocked_exists(filename):
                    # Important to this test: This will return False for EB_MANIFEST_FILENAME,
                    # causing the strategy of using the version there to fall through,
                    # so we expect to try using the git version instead.
                    return filename in [some_template_file_name]

                mock_exists.side_effect = mocked_exists
                with mock.patch("io.open", side_effect=mocked_open):
                    build_ini_file_from_template(some_template_file_name,
                                                 some_ini_file_name)

        assert MockFileStream.FILE_SYSTEM[some_ini_file_name] == [
            '[Foo]',
            'DATABASE = "snow_white"',
            'SOME_URL = "http://user@unittest:6543/"',
            'OOPS = "$NOT_AN_ENV_VAR"',
            'HMMM = "${NOT_AN_ENV_VAR_EITHER}"',
            'SHHH = "my-secret"',
            'VERSION = "%s"' % MOCKED_LOCAL_GIT_VERSION,
            'PROJECT_VERSION = "%s"' % MOCKED_PROJECT_VERSION,
        ]

        MockFileStream.reset()

        with mock.patch("subprocess.check_output") as mock_check_output:
            mock_check_output.side_effect = make_mocked_check_output_for_get_version(
                simulate_git_command=False)
            with mock.patch("os.path.exists") as mock_exists:

                def mocked_exists(filename):
                    # Important to this test: This will return False for EB_MANIFEST_FILENAME,
                    # causing the strategy of using the version there to fall through,
                    # so we expect to try using the git version instead, which will also fail
                    # because we're simulating the absence of Git.
                    return filename in [some_template_file_name]

                mock_exists.side_effect = mocked_exists

                class MockDateTime:
                    DATETIME = datetime.datetime

                    @classmethod
                    def now(cls):
                        return cls.DATETIME(2001, 2, 3, 4, 55, 6)

                with mock.patch("io.open", side_effect=mocked_open):
                    with mock.patch.object(datetime, "datetime",
                                           MockDateTime()):
                        build_ini_file_from_template(some_template_file_name,
                                                     some_ini_file_name)

        assert MockFileStream.FILE_SYSTEM[some_ini_file_name] == [
            '[Foo]',
            'DATABASE = "snow_white"',
            'SOME_URL = "http://user@unittest:6543/"',
            'OOPS = "$NOT_AN_ENV_VAR"',
            'HMMM = "${NOT_AN_ENV_VAR_EITHER}"',
            'SHHH = "my-secret"',
            'VERSION = "unknown-version-at-20010203045506000000"',  # We mocked datetime.datetime.now() to get this
            'PROJECT_VERSION = "%s"' % MOCKED_PROJECT_VERSION,
        ]

        MockFileStream.reset()
Example #19
0
def test_c4_383_regression_action():
    """
    Check that bug C4-383 is really fixed.

    This bug involves resume_uploads not merging the uploaded file against the current directory
    when no bundle_filename or upload_folder is given. The present behavior is to merge against
    the parent directory.
    """
    output = []
    with override_environ(CGAP_KEYS_FILE=None):
        with mock.patch.object(resume_uploads_module, "print") as mock_print:
            mock_print.side_effect = lambda *args: output.append(" ".join(args)
                                                                 )
            # This is the directory we expect the uploaded file to get merged against.
            # We want to really run the code logic to make sure it does this,
            # so we have to mock out all the effects.
            current_dir = "/my/cur/dir"
            with mock.patch.object(os.path, "curdir", current_dir):
                with mock.patch.object(submission_module,
                                       "yes_or_no",
                                       return_value=True):
                    with mock.patch.object(
                            submission_module,
                            "upload_file_to_uuid") as mock_upload_file_to_uuid:
                        with mock.patch("requests.get") as mock_requests_get:

                            def mocked_requests_get(url, *args, **kwargs):
                                ignored(args, kwargs)
                                assert "ingestion-submissions" in url
                                return MockResponse(
                                    200,
                                    json=INGESTION_FRAGMENT_WITH_UPLOAD_INFO)

                            mock_requests_get.side_effect = mocked_requests_get
                            local_server = "http://localhost:8000"
                            fake_keydict = {
                                'key': 'my-key',
                                'secret': 'my-secret',
                                'server': local_server,
                            }
                            with mock.patch.object(submission_module,
                                                   "get_keydict_for_server",
                                                   return_value=fake_keydict):
                                try:
                                    # Outside of the call, we will always see the default filename for cgap keys
                                    # but inside the call, because of a decorator, the default might be different.
                                    # See additional test below.
                                    assert KeyManager.keydicts_filename(
                                    ) == KeyManager.DEFAULT_KEYDICTS_FILENAME

                                    resume_uploads_main([
                                        "2eab76cd-666c-4b04-9335-22f9c6084303",
                                        '--server', local_server
                                    ])
                                except SystemExit as e:
                                    assert e.code == 0
                                joined_filename = os.path.join(
                                    current_dir,
                                    SAMPLE_UPLOAD_INFO[-1]['filename'])
                                # Make sure this is dong what we expect.
                                assert current_dir + "/" in joined_filename
                                # Make sure the inner upload actually uploads to the current dir.
                                mock_upload_file_to_uuid.assert_called_with(
                                    auth=fake_keydict,
                                    filename=joined_filename,
                                    uuid=SAMPLE_UPLOAD_INFO[-1]['uuid'])
                                assert output == []