Example #1
0
def test_basic(sentry_init, capture_events):
    sentry_init(integrations=[RqIntegration()])
    events = capture_events()

    queue = rq.Queue(connection=FakeStrictRedis())
    worker = rq.SimpleWorker([queue], connection=queue.connection)

    queue.enqueue(crashing_job, foo=42)
    worker.work(burst=True)

    (event, ) = events

    (exception, ) = event["exception"]["values"]
    assert exception["type"] == "ZeroDivisionError"
    assert exception["mechanism"]["type"] == "rq"
    assert exception["stacktrace"]["frames"][-1]["vars"]["foo"] == "42"

    assert event["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
    assert event["extra"]["rq-job"] == {
        "args": [],
        "description": "tests.integrations.rq.test_rq.crashing_job(foo=42)",
        "func": "tests.integrations.rq.test_rq.crashing_job",
        "job_id": event["extra"]["rq-job"]["job_id"],
        "kwargs": {
            "foo": 42
        },
    }
Example #2
0
def test_transaction_no_error(
        sentry_init,
        capture_events,
        DictionaryContaining  # noqa:N803
):
    sentry_init(integrations=[RqIntegration()], traces_sample_rate=1.0)
    events = capture_events()

    queue = rq.Queue(connection=FakeStrictRedis())
    worker = rq.SimpleWorker([queue], connection=queue.connection)

    queue.enqueue(do_trick, "Maisey", trick="kangaroo")
    worker.work(burst=True)

    envelope = events[0]

    assert envelope["type"] == "transaction"
    assert envelope["contexts"]["trace"]["op"] == "rq.task"
    assert envelope["transaction"] == "tests.integrations.rq.test_rq.do_trick"
    assert envelope["extra"]["rq-job"] == DictionaryContaining({
        "args": ["Maisey"],
        "kwargs": {
            "trick": "kangaroo"
        },
        "func":
        "tests.integrations.rq.test_rq.do_trick",
        "description":
        "tests.integrations.rq.test_rq.do_trick('Maisey', trick='kangaroo')",
    })
Example #3
0
def test_traces_sampler_gets_correct_values_in_sampling_context(
        sentry_init,
        DictionaryContaining,
        ObjectDescribedBy  # noqa:N803
):
    traces_sampler = mock.Mock(return_value=True)
    sentry_init(integrations=[RqIntegration()], traces_sampler=traces_sampler)

    queue = rq.Queue(connection=FakeStrictRedis())
    worker = rq.SimpleWorker([queue], connection=queue.connection)

    queue.enqueue(do_trick, "Bodhi", trick="roll over")
    worker.work(burst=True)

    traces_sampler.assert_any_call(
        DictionaryContaining({
            "rq_job":
            ObjectDescribedBy(
                type=rq.job.Job,
                attrs={
                    "description":
                    "tests.integrations.rq.test_rq.do_trick('Bodhi', trick='roll over')",
                    "result": "Bodhi, can you roll over? Good dog!",
                    "func_name": "tests.integrations.rq.test_rq.do_trick",
                    "args": ("Bodhi", ),
                    "kwargs": {
                        "trick": "roll over"
                    },
                },
            ),
        }))
Example #4
0
def test_sync_worker_multiple_jobs(queue):
    jobs = []
    for i in range(3):
        jobs.append(queue.enqueue(job_add1, i))
    worker = rq.SimpleWorker([queue], connection=queue.connection)
    worker.work(burst=True)
    assert [job.result for job in jobs] == [1, 2, 3]
Example #5
0
def test_job_with_retries(sentry_init, capture_events):
    sentry_init(integrations=[RqIntegration()])
    events = capture_events()

    queue = rq.Queue(connection=FakeStrictRedis())
    worker = rq.SimpleWorker([queue], connection=queue.connection)

    queue.enqueue(crashing_job, foo=42, retry=rq.Retry(max=1))
    worker.work(burst=True)

    assert len(events) == 1
Example #6
0
def test_transaction_with_error(
        sentry_init,
        capture_events,
        DictionaryContaining  # noqa:N803
):

    sentry_init(integrations=[RqIntegration()], traces_sample_rate=1.0)
    events = capture_events()

    queue = rq.Queue(connection=FakeStrictRedis())
    worker = rq.SimpleWorker([queue], connection=queue.connection)

    queue.enqueue(chew_up_shoes, "Charlie", "Katie", shoes="flip-flops")
    worker.work(burst=True)

    error_event, envelope = events

    assert error_event[
        "transaction"] == "tests.integrations.rq.test_rq.chew_up_shoes"
    assert error_event["contexts"]["trace"]["op"] == "rq.task"
    assert error_event["exception"]["values"][0]["type"] == "Exception"
    assert (error_event["exception"]["values"][0]["value"] ==
            "Charlie!! Why did you eat Katie's flip-flops??")

    assert envelope["type"] == "transaction"
    assert envelope["contexts"]["trace"] == error_event["contexts"]["trace"]
    assert envelope["transaction"] == error_event["transaction"]
    assert envelope["extra"]["rq-job"] == DictionaryContaining({
        "args": ["Charlie", "Katie"],
        "kwargs": {
            "shoes": "flip-flops"
        },
        "func":
        "tests.integrations.rq.test_rq.chew_up_shoes",
        "description":
        "tests.integrations.rq.test_rq.chew_up_shoes('Charlie', 'Katie', shoes='flip-flops')",
    })
Example #7
0
def test_sync_worker_config_service(queue):
    job = queue.enqueue(job_add1, 10)
    with override_config("rq_worker", dict(service="my-worker-svc")):
        worker = rq.SimpleWorker([queue], connection=queue.connection)
        worker.work(burst=True)
    assert job.result == 11
Example #8
0
def test_sync_worker(queue):
    job = queue.enqueue(job_add1, 1)
    worker = rq.SimpleWorker([queue], connection=queue.connection)
    worker.work(burst=True)
    assert job.result == 2
Example #9
0
def test_worker_class_job(queue):
    queue.enqueue(JobClass().job_on_class, 2)
    queue.enqueue(JobClass(), 4)
    worker = rq.SimpleWorker([queue], connection=queue.connection)
    worker.work(burst=True)
Example #10
0
def test_worker_failing_job(queue):
    queue.enqueue(job_fail)
    worker = rq.SimpleWorker([queue], connection=queue.connection)
    worker.work(burst=True)
Example #11
0
def test_sync_worker_pin_service(queue):
    job = queue.enqueue(job_add1, 10)
    worker = rq.SimpleWorker([queue], connection=queue.connection)
    Pin.override(worker, service="my-pin-svc")
    worker.work(burst=True)
    assert job.result == 11
Example #12
0
    def setUp(self):
        super(TestEngineScenario, self).setUp()

        self.cassette_library_dir = os.path.join(CASSETTE_LIBRARY_DIR_BASE,
                                                 self._testMethodName)

        if RECORD_MODE != "none":
            if os.path.exists(self.cassette_library_dir):
                shutil.rmtree(self.cassette_library_dir)
            os.makedirs(self.cassette_library_dir)

        self.recorder = vcr.VCR(
            cassette_library_dir=self.cassette_library_dir,
            record_mode=RECORD_MODE,
            match_on=['method', 'uri'],
            filter_headers=[
                ('Authorization', '<TOKEN>'),
                ('X-Hub-Signature', '<SIGNATURE>'),
                ('User-Agent', None),
                ('Accept-Encoding', None),
                ('Connection', None),
            ],
            before_record_response=self.response_filter,
            custom_patches=((github.MainClass, 'HTTPSConnection',
                             vcr.stubs.VCRHTTPSConnection), ))

        self.useFixture(
            fixtures.MockPatchObject(
                branch_updater.utils, 'Gitter',
                lambda: GitterRecorder(self.cassette_library_dir)))

        self.useFixture(
            fixtures.MockPatchObject(
                backports.utils, 'Gitter',
                lambda: GitterRecorder(self.cassette_library_dir)))

        # Web authentification always pass
        self.useFixture(
            fixtures.MockPatch('hmac.compare_digest', return_value=True))

        reponame_path = os.path.join(self.cassette_library_dir, "reponame")

        gen_new_uuid = (RECORD_MODE == 'all'
                        or (RECORD_MODE == 'once'
                            and not os.path.exists(reponame_path)))

        if gen_new_uuid:
            REPO_UUID = str(uuid.uuid4())
            with open(reponame_path, "w") as f:
                f.write(REPO_UUID)
        else:
            with open(reponame_path, "r") as f:
                REPO_UUID = f.read()

        self.name = "repo-%s-%s" % (REPO_UUID, self._testMethodName)

        self.pr_counter = 0
        self.remaining_events = []

        utils.setup_logging()
        config.log()

        self.git = GitterRecorder(self.cassette_library_dir, "tests")
        self.addCleanup(self.git.cleanup)

        web.app.testing = True
        self.app = web.app.test_client()

        # NOTE(sileht): Prepare a fresh redis
        self.redis = utils.get_redis_for_cache()
        self.redis.flushall()
        subscription = {"token": config.MAIN_TOKEN, "subscribed": False}
        self.redis.set("subscription-cache-%s" % config.INSTALLATION_ID,
                       json.dumps(subscription))

        # Let's start recording
        cassette = self.recorder.use_cassette("http.json")
        cassette.__enter__()
        self.addCleanup(cassette.__exit__)

        self.session = requests.Session()
        self.session.trust_env = False

        # Cleanup the remote testing redis
        r = self.session.delete(
            "https://gh.mergify.io/events-testing",
            data=FAKE_DATA,
            headers={"X-Hub-Signature": "sha1=" + FAKE_HMAC})
        r.raise_for_status()

        self.g_main = github.Github(config.MAIN_TOKEN)
        self.g_fork = github.Github(config.FORK_TOKEN)

        self.u_main = self.g_main.get_user()
        self.u_fork = self.g_fork.get_user()
        assert self.u_main.login == "mergify-test1"
        assert self.u_fork.login == "mergify-test2"

        self.r_main = self.u_main.create_repo(self.name)
        self.url_main = "https://github.com/%s" % self.r_main.full_name
        self.url_fork = "https://github.com/%s/%s" % (self.u_fork.login,
                                                      self.r_main.name)

        integration = github.GithubIntegration(config.INTEGRATION_ID,
                                               config.PRIVATE_KEY)

        access_token = integration.get_access_token(
            config.INSTALLATION_ID).token
        g = github.Github(access_token)
        user = g.get_user("mergify-test1")
        repo = user.get_repo(self.name)

        # Used to access the cache with its helper
        self.engine = engine.MergifyEngine(g, config.INSTALLATION_ID,
                                           access_token, subscription, user,
                                           repo)
        self.processor = self.engine.get_processor()

        self.rq_worker = rq.SimpleWorker([
            "incoming-events", "localhost-000-high", "localhost-001-high",
            "localhost-000-low", "localhost-001-low"
        ],
                                         connection=utils.get_redis_for_rq())

        if self._testMethodName != "test_creation_pull_of_initial_config":
            self.git("init")
            self.git.configure()
            self.git.add_cred(config.MAIN_TOKEN, "", self.r_main.full_name)
            self.git.add_cred(config.FORK_TOKEN, "",
                              "%s/%s" % (self.u_fork.login, self.r_main.name))
            self.git("config", "user.name", "%s-tester" % config.CONTEXT)
            self.git("remote", "add", "main", self.url_main)
            self.git("remote", "add", "fork", self.url_fork)

            with open(self.git.tmp + "/.mergify.yml", "w") as f:
                f.write(CONFIG)
            self.git("add", ".mergify.yml")
            self.git("commit", "--no-edit", "-m", "initial commit")
            self.git("push", "--quiet", "main", "master")

            self.git("checkout", "-b", "stable", "--quiet")
            self.git("push", "--quiet", "main", "stable")

            self.git("checkout", "-b", "nostrict", "--quiet")
            self.git("push", "--quiet", "main", "nostrict")

            self.git("checkout", "-b", "disabled", "--quiet")
            self.git("push", "--quiet", "main", "disabled")

            self.git("checkout", "-b", "enabling_label", "--quiet")
            self.git("push", "--quiet", "main", "enabling_label")

            self.r_fork = self.u_fork.create_fork(self.r_main)
            self.git("fetch", "--quiet", "fork")

            # NOTE(sileht): Github looks buggy here:
            # We receive for the new repo the expected events:
            # * installation_repositories
            # * integration_installation_repositories
            # but we receive them 6 times with the same sha1...
            self.push_events([(None, {"action": "added"})] * 12)
Example #13
0
    def setUp(self):
        super(TestEngineScenario, self).setUp()
        self.session = requests.Session()
        self.session.trust_env = False
        self.recorder = betamax.Betamax(self.session)

        self.useFixture(
            fixtures.MockPatchObject(requests,
                                     'Session',
                                     return_value=self.session))

        self.useFixture(
            fixtures.MockPatchObject(
                gh_update_branch.utils, 'Gitter',
                lambda: GitterRecorder(self._testMethodName)))

        # Web authentification always pass
        self.useFixture(
            fixtures.MockPatch('hmac.compare_digest', return_value=True))

        reponame_path = (
            "mergify_engine/tests/fixtures/cassettes/reponame_%s" %
            self._testMethodName)

        gen_new_uuid = (RECORD_MODE == 'all'
                        or (RECORD_MODE == 'once'
                            and not os.path.exists(reponame_path)))

        if gen_new_uuid:
            REPO_UUID = str(uuid.uuid4())
            with open(reponame_path, "w") as f:
                f.write(REPO_UUID)
        else:
            with open(reponame_path, "r") as f:
                REPO_UUID = f.read()

        self.name = "repo-%s-%s" % (REPO_UUID, self._testMethodName)

        self.cassette_counter = 0
        self.events_handler_counter = 0
        self.events_getter_counter = 0
        self.status_counter = 0
        self.reviews_counter = 0
        self.pr_counter = 0
        self.last_event_id = None

        utils.setup_logging()
        config.log()

        self.git = GitterRecorder('%s-tests' % self._testMethodName)
        self.addCleanup(self.git.cleanup)

        web.app.testing = True
        self.app = web.app.test_client()

        # NOTE(sileht): Prepare a fresh redis
        self.redis = utils.get_redis()
        self.redis.flushall()
        self.redis.set("installation-token-%s" % INSTALLATION_ID, MAIN_TOKEN)

        with self.cassette("setUp-prepare-repo", allow_playback_repeats=True):
            # Cleanup the remote testing redis
            r = self.session.delete("https://gh.mergify.io/events-testing")
            r.raise_for_status()

            self.g_main = github.Github(MAIN_TOKEN)
            self.g_fork = github.Github(FORK_TOKEN)

            self.u_main = self.g_main.get_user()
            self.u_fork = self.g_fork.get_user()
            assert self.u_main.login == "mergify-test1"
            assert self.u_fork.login == "mergify-test2"

            self.r_main = self.u_main.create_repo(self.name)
            self.url_main = "https://%s:@github.com/%s" % (
                MAIN_TOKEN, self.r_main.full_name)
            self.git("init")
            self.git("config", "user.name", "%s-bot" % config.CONTEXT)
            self.git("config", "user.email", "*****@*****.**")
            self.git("remote", "add", "main", self.url_main)
            with open(self.git.tmp + "/.mergify.yml", "w") as f:
                f.write(CONFIG)
            self.git("add", ".mergify.yml")
            self.git("commit", "--no-edit", "-m", "initial commit")
            self.git("push", "main", "master")

            self.git("checkout", "-b", "stable")
            self.git("push", "main", "stable")

            self.git("checkout", "-b", "nostrict")
            self.git("push", "main", "nostrict")

            self.r_fork = self.u_fork.create_fork(self.r_main)
            self.url_fork = "https://%s:@github.com/%s" % (
                FORK_TOKEN, self.r_fork.full_name)
            self.git("remote", "add", "fork", self.url_fork)
            self.git("fetch", "fork")

        with self.cassette("setUp-login-engine", allow_playback_repeats=True):
            g = github.Github(ACCESS_TOKEN)
            user = g.get_user("mergify-test1")
            repo = user.get_repo(self.name)

        with self.cassette("setUp-create-engine", allow_playback_repeats=True):
            self.engine = engine.MergifyEngine(g, INSTALLATION_ID, user, repo)
            self.useFixture(
                fixtures.MockPatchObject(worker, 'real_event_handler',
                                         self.engine.handle))

        queue = rq.Queue(connection=self.redis)
        self.rq_worker = rq.SimpleWorker([queue], connection=queue.connection)

        # NOTE(sileht): Github looks buggy here:
        # We receive for the new repo the expected events:
        # * installation_repositories
        # * integration_installation_repositories
        # but we receive them 6 times with the same sha1...
        self.push_events(12)