Exemple #1
0
def test_tee_output_with_SystemExit():
    fileobj = StringIO()
    with assert_raises(SystemExit), stdfake() as fake, tee_output(fileobj):
        raise SystemExit(1)
    eq(fake.stdout.getvalue(), "")
    eq(fake.stderr.getvalue(), "")
    eq(fileobj.getvalue(), "")
 def test_process_change_with_deleted_document(self):
     self.obj.put_attachment("content", "name")
     change = Config(id=self.obj._id, deleted=True)
     self.processor.process_change(None, change)
     msg = "FakeCouchDocument attachment: 'name'"
     with assert_raises(ResourceNotFound, msg=msg):
         self.obj.fetch_attachment("name")
Exemple #3
0
def test_tee_output_with_KeyboardInterrupt():
    fileobj = StringIO()
    with assert_raises(KeyboardInterrupt), stdfake() as fake, tee_output(fileobj):
        raise KeyboardInterrupt("errrt")
    eq(fake.stdout.getvalue(), "")
    eq(fake.stderr.getvalue(), "")
    eq(sanitize_tb(fileobj.getvalue()),
        "Traceback (most recent call last):\n"
        "  ...\n"
        "KeyboardInterrupt: errrt\n")
Exemple #4
0
def test_heartbeat():
    hb = Heartbeat('celery_periodic')
    hb.clear_last_seen()

    with assert_raises(HeartbeatNeverRecorded):
        hb.get_last_seen()

    with assert_raises(HeartbeatNeverRecorded):
        hb.get_blockage_duration()

    seen_time = datetime.datetime.utcnow()

    with freeze_time(seen_time):
        hb.mark_seen()
        eq(hb.get_last_seen(), seen_time)
        eq(hb.get_blockage_duration(), datetime.timedelta(seconds=0))

    with freeze_time(seen_time + datetime.timedelta(minutes=10)):
        eq(hb.get_last_seen(), seen_time)
        eq(hb.get_blockage_duration(), datetime.timedelta(minutes=10) - HEARTBEAT_FREQUENCY)
Exemple #5
0
def test_time_to_start_timer():
    task_id = 'abc123'
    delay = datetime.timedelta(seconds=6)

    start_time = datetime.datetime.utcnow()

    # starts empty
    with assert_raises(TimingNotAvailable):
        TimeToStartTimer(task_id).stop_and_pop_timing()

    with freeze_time(start_time):
        TimeToStartTimer(task_id).start_timing(datetime.datetime.utcnow())

    with freeze_time(start_time + delay):
        time_to_start = TimeToStartTimer(task_id).stop_and_pop_timing()

    eq(time_to_start, delay)

    # can only pop once, second time empty
    with assert_raises(TimingNotAvailable):
        TimeToStartTimer(task_id).stop_and_pop_timing()
Exemple #6
0
def test_heartbeat():
    hb = Heartbeat('celery_periodic')
    hb.clear_last_seen()

    with assert_raises(HeartbeatNeverRecorded):
        hb.get_last_seen()

    with assert_raises(HeartbeatNeverRecorded):
        hb.get_blockage_duration()

    seen_time = datetime.datetime.utcnow()

    with freeze_time(seen_time):
        hb.mark_seen()
        eq(hb.get_last_seen(), seen_time)
        eq(hb.get_blockage_duration(), datetime.timedelta(seconds=0))

    with freeze_time(seen_time + datetime.timedelta(minutes=10)):
        eq(hb.get_last_seen(), seen_time)
        eq(hb.get_blockage_duration(),
           datetime.timedelta(minutes=10) - HEARTBEAT_FREQUENCY)
def test_existing_users():
    user_specs = [
        {'username': '******'},
        {'username': '******'},
    ]

    with patch('corehq.apps.user_importer.validation.get_existing_usernames',
               return_value=['*****@*****.**']):
        validator = ExistingUserValidator('domain', user_specs)

    validator(user_specs[0])
    with assert_raises(UserUploadError):
        validator(user_specs[1])
Exemple #8
0
 def test_tee_output(self):
     fileobj = StringIO()
     with assert_raises(Error), stdfake() as fake, tee_output(fileobj):
         print("testing...")
         sys.stderr.write("fail.\n")
         raise Error("stop")
     eq(fake.stdout.getvalue(), "testing...\n")
     eq(fake.stderr.getvalue(), "fail.\n")
     eq(
         sanitize_tb(fileobj.getvalue()), "testing...\n"
         "fail.\n"
         "Traceback (most recent call last):\n"
         "  ...\n" + "corehq.util.tests.test_teeout.Error: stop\n")
Exemple #9
0
async def test_CommandParser_incomplete():
    field = Choice('arg', 'all')
    parser = create_parser(field)
    arg = await Arg(field, 'a', 0, Options())

    def check(err):
        eq_(err.options, Options(arg=arg))
        eq_(err.errors, [
            ParseError("'a' is ambiguous: arg, all", Choice('arg', 'all'), 0,
                       2)
        ])

    with assert_raises(ArgumentError, msg=check):
        await parser.parse('a')
Exemple #10
0
def test_produce_error():
    class Error(Exception):
        pass

    def producer():
        yield 1
        yield 2
        raise Error

    results = set()
    with assert_raises(Error):
        for result in mod.Pool().imap_unordered(square, producer()):
            results.add(result)
    eq(results, {1, 4})
Exemple #11
0
def test_late_extension_definition():
    ext = CommCareExtensions()

    @ext.extension_point
    def ext_point_b():
        """testing..."""

    ext.load_extensions([])
    with testil.assert_raises(ExtensionError,
                              msg=re.compile("Late extension definition")):

        @ext_point_b.extend
        def impl():
            pass
Exemple #12
0
    async def test_parser(argstr, options, parser):
        if isinstance(options, Exception):

            def check(err):
                eq_(type(err), type(options))
                eq_(str(err), str(options))
                eq_(err.errors, options.errors)
                eq_(err.parse_index, options.parse_index)

            with assert_raises(type(options), msg=check):
                await parser.parse(argstr)
        else:
            opts = parser.default_options()
            opts.__dict__.update(options)
            eq_(await parser.parse(argstr), opts)
Exemple #13
0
def test_tee_output():
    fileobj = StringIO()
    with assert_raises(Error), stdfake() as fake, tee_output(fileobj):
        print("testing...")
        sys.stderr.write("fail.\n")
        raise Error("stop")
    eq(fake.stdout.getvalue(), "testing...\n")
    eq(fake.stderr.getvalue(), "fail.\n")
    eq(sanitize_tb(fileobj.getvalue()),
        "testing...\n"
        "fail.\n"
        "Traceback (most recent call last):\n"
        "  ...\n" +
        ("corehq.util.tests.test_teeout.Error" if six.PY3 else "Error") +
        ": stop\n")
Exemple #14
0
def test_get_blobdb(self, msg, root=True, blob_dir=None):
    with tempdir() as tmp:
        if root == "file":
            tmp = join(tmp, "file")
            with open(tmp, "w") as fh:
                fh.write("x")
        conf = SharedDriveConfiguration(
            shared_drive_path=tmp if root else root,
            restore_dir=None,
            transfer_dir=None,
            temp_dir=None,
            blob_dir=blob_dir,
        )
        with override_settings(SHARED_DRIVE_CONF=conf, S3_BLOB_DB_SETTINGS=None):
            with assert_raises(mod.Error, msg=re.compile(msg)):
                mod.get_blob_db()
Exemple #15
0
def test_get_blobdb(self, msg, root=True, blob_dir=None):
    with tempdir() as tmp:
        if (root == "file" and six.PY3) or (root == b"file" and six.PY2):
            tmp = join(tmp, "file" if six.PY3 else b"file")
            with open(tmp, "w", encoding='utf-8') as fh:
                fh.write("x")
        conf = SharedDriveConfiguration(
            shared_drive_path=tmp if root else root,
            restore_dir=None,
            transfer_dir=None,
            temp_dir=None,
            blob_dir=blob_dir,
        )
        with patch("corehq.blobs._db", new=[]):
            with override_settings(SHARED_DRIVE_CONF=conf, S3_BLOB_DB_SETTINGS=None):
                with assert_raises(mod.Error, msg=re.compile(msg)):
                    mod.get_blob_db()
Exemple #16
0
def test_get_blobdb(self, msg, root=True, blob_dir=None):
    with tempdir() as tmp:
        if root == "file":
            tmp = join(tmp, "file")
            with open(tmp, "w", encoding='utf-8') as fh:
                fh.write("x")
        conf = SharedDriveConfiguration(
            shared_drive_path=tmp if root else root,
            restore_dir=None,
            transfer_dir=None,
            temp_dir=None,
            blob_dir=blob_dir,
        )
        with patch("corehq.blobs._db", new=[]):
            with override_settings(SHARED_DRIVE_CONF=conf, S3_BLOB_DB_SETTINGS=None):
                with assert_raises(mod.Error, msg=re.compile(msg)):
                    mod.get_blob_db()
Exemple #17
0
async def test_CommandParser_with_SubParser_errors():
    sub = SubArgs("num", Int("num"), abc="xyz")
    arg = SubParser("var", sub)
    parser = create_parser(arg)

    def check(err):
        arg = Arg(None, 'num x', 0, Options())
        eq_(
            str(err), "invalid arguments: num x\n"
            "invalid literal for int() with base 10: 'x'")
        eq_(err.options, Options(var=arg))
        eq_(err.errors, [
            ParseError("invalid literal for int() with base 10: 'x'",
                       Int("num"), 4, 5)
        ])

    with assert_raises(ArgumentError, msg=check):
        await parser.parse('num x')
Exemple #18
0
    async def regex_test(text, start, expect, flags=0):
        if isinstance(expect, Exception):

            def check(err):
                eq_(err, expect)

            with assert_raises(type(expect), msg=check):
                await field.consume(text, start)
            return
        value = await field.consume(text, start)
        if expect[0] in [None, (None, None)]:
            eq_(value, expect)
            return
        expr, index = value
        if field.replace:
            (expr, replace) = expr
            got = ((expr, replace), index)
        else:
            got = (expr, index)
        eq_(got, expect)
        eq_(expr.flags, flags | re.UNICODE | re.MULTILINE)
Exemple #19
0
 def test(path):
     with assert_raises(TypeError):
         {}[path] = None
Exemple #20
0
 def test_pig_index_not_registered():
     with assert_raises(ESRegistryError):
         verify_registered(PIGS)
def test_retry_on_couch_error_too_many_retries():
    func, calls = make_couch_retry_function(6)
    with mock_retry_sleep() as sleeps, assert_raises(RequestException):
        func(1)
    eq(sleeps, [0.1, 1, 2, 4, 8])
    eq(len(calls), 6)
Exemple #22
0
def test_nested_reentrant_redis_locks_is_not_allowed():
    with assert_raises(RuntimeError):
        with reentrant_redis_locks():
            pass
Exemple #23
0
 def test(path):
     with assert_raises(TypeError):
         {}[path] = None
Exemple #24
0
 def tearDownClass(cls):
     super().tearDownClass()
     # non-standard: test that the instance is torn down
     with assert_raises(ESRegistryError):
         verify_registered(CATS)
     eq(cls.state_log, ["instance_up", "instance_down"])
 def _run_test(config, exception, message):
     settings = {
         'DATABASES': TEST_DATABASES,
     }
     with override_settings(**settings), assert_raises(exception, msg=message):
         PlProxyConfig.from_dict(config)
Exemple #26
0
def test_parameterized_permission_validation():
    # no exception raised
    PermissionInfo(Permissions.view_apps.name, allow=PermissionInfo.ALLOW_ALL)
    with assert_raises(TypeError):
        PermissionInfo(Permissions.view_apps.name, allow=["app1"])
Exemple #27
0
 async def test(text, result):
     if isinstance(result, Options):
         eq_(await parser.parse(text), result)
     else:
         with assert_raises(result):
             await parser.parse(text)
Exemple #28
0
def test_generator_update_create_index_not_found():
    builder = IntentCaseBuilder().create_index("case2", "parent_type", "child")

    with assert_raises(DataRegistryCaseUpdateError, msg="Index case not found: case2"):
        with patch.object(CommCareCase.objects, 'get_case', side_effect=CaseNotFound):
            _test_payload_generator(intent_case=builder.get_case())
Exemple #29
0
def test_generator_update_remove_index_bad_relationship():
    builder = IntentCaseBuilder().remove_index("case2", "parent", relationship="cousin")
    msg = "Index relationships must be either 'child' or 'extension'"
    with assert_raises(DataRegistryCaseUpdateError, msg=msg):
        _test_payload_generator(intent_case=builder.get_case())
Exemple #30
0
def test_generator_update_remove_index_check_relationship():
    builder = IntentCaseBuilder().remove_index("parent_case_id", "parent_c", relationship="extension")
    msg = "Index relationship does not match for index to remove"
    with assert_raises(DataRegistryCaseUpdateError, msg=msg):
        _test_payload_generator(intent_case=builder.get_case())
Exemple #31
0
def test_generator_required_fields():
    intent_case = IntentCaseBuilder().get_case({})
    expect_missing = ["target_data_registry", "target_domain", "target_case_id"]
    expected_message = f"Missing required case properties: {', '.join(expect_missing)}"
    with assert_raises(DataRegistryCaseUpdateError, msg=expected_message):
        _test_payload_generator(intent_case=intent_case)
 def _test(spec):
     if spec.get('name') in duplicates:
         with assert_raises(UserUploadError, msg=validator.error_message):
             validator(spec)
     else:
         validator(spec)
Exemple #33
0
def test_generator_create_case_target_exists():
    builder = IntentCaseBuilder().case_properties(new_prop="new_prop_val").create_case("123")

    with assert_raises(DataRegistryCaseUpdateError, msg="Unable to create case as it already exists: 1"):
        _test_payload_generator(intent_case=builder.get_case())
Exemple #34
0
def test_generator_fail_if_case_domain_mismatch():
    builder = IntentCaseBuilder().include_props([]).target_case(domain="other")

    with assert_raises(DataRegistryCaseUpdateError, msg="Case not found: 1"):
        _test_payload_generator(intent_case=builder.get_case())
Exemple #35
0
def test_public_only_session__simple_invalid_url_local():
    session = _set_up_session()
    with assert_raises(PossibleSSRFAttempt):
        session.get('http://localhost')
Exemple #36
0
def test_public_only_session__simple_invalid_url_private():
    session = _set_up_session()
    with assert_raises(PossibleSSRFAttempt):
        session.get('http://10.0.0.0')
Exemple #37
0
def test_wait_for_one_task_to_complete_on_empty_pool():
    pool = Pool(size=3)
    with assert_raises(ValueError):
        mod.wait_for_one_task_to_complete(pool)
 def _check(query, msg):
     node = parse_xpath(query)
     with assert_raises(XPathFunctionException, msg=msg):
         _parse_normalize_subcase_query(node)
Exemple #39
0
async def test_CommandParser_too_many_args():
    with assert_raises(ArgumentError,
                       msg="unexpected argument(s): unexpected"):
        await arg_parser.parse('yes unexpected')