Example #1
0
    def test_session(self):
        user = utils.get_test_user()
        oauth = self.server.auth()
        self.assertEqual(oauth.start_session(self.client.session), None)

        session = self.client.session
        self.assertFalse(oauth.is_signed_in(session))
        session[self.oauth._user_key] = 'no_user'
        session.save()
        self.assertFalse(oauth.is_signed_in(session))
        session[self.oauth._token_key] = 'token'
        session.save()
        self.assertTrue(oauth.is_signed_in(session))
        self.assertEqual(oauth.signed_in_user(user.server, session), None)
        self.assertNotEqual(oauth.start_session(session), None)

        session[self.oauth._user_key] = user.name
        session.save()
        self.assertEqual(oauth.signed_in_user(user.server, session), user)
        self.assertNotEqual(oauth.user_token_to_oauth_token(user), None)
        user2 = utils.create_user()
        self.assertEqual(oauth.user_token_to_oauth_token(user2), None)

        self.assertNotEqual(oauth.start_session_for_user(user), None)

        oauth.set_browser_session_from_user(session, user)
        session.save()
        self.assertEqual(session[self.oauth._user_key], user.name)
Example #2
0
 def create_records(self, recipe, branch):
     info = {}
     info["owner"] = test_utils.create_user(name=recipe["repository_owner"])
     info["build_user"] = test_utils.create_user_with_token(name=recipe["build_user"])
     info["repository"] = test_utils.create_repo(user=info["owner"], name=recipe["repository_name"])
     info["branch"] = test_utils.create_branch(repo=info["repository"], name=branch)
     return info
Example #3
0
 def test_bad_user(self):
     other_build_user = utils.create_user(name="bad_build_user")
     manual = self.create_data(user=other_build_user)
     # Make sure we only get recipes for the correct build user
     # This shouldn't create an event or any jobs.
     self.set_counts()
     manual.save()
     self.compare_counts()
Example #4
0
 def test_update_user(self):
     user = utils.get_test_user()
     session = {
         self.oauth._token_key: json.loads(user.token),
         self.oauth._user_key: user.name
     }
     auth = self.server.auth()
     auth.update_user(session)
     user2 = utils.create_user()
     session[self.oauth._user_key] = user2.name
     auth.update_user(session)
Example #5
0
    def create_default_recipes(self, recipes_dir, server_type=settings.GITSERVER_GITHUB):
        hostname = "github.com"
        if server_type == settings.GITSERVER_GITLAB:
            hostname = "gitlab.com"

        self.recipe_file = self.create_recipe_in_repo("recipe_all.cfg", "recipe.cfg", hostname=hostname)
        self.recipe_pr_file = self.create_recipe_in_repo("pr_dep.cfg", "pr_dep.cfg", hostname=hostname)
        self.recipe_push_file = self.create_recipe_in_repo("push_dep.cfg", "push_dep.cfg", hostname=hostname)
        self.server = test_utils.create_git_server(host_type=server_type)
        self.build_user = test_utils.create_user_with_token(name="moosebuild", server=self.server)
        self.owner = test_utils.create_user(name="idaholab", server=self.server)
        self.repo = test_utils.create_repo(name="civet", user=self.owner)
        self.branch = test_utils.create_branch(name="devel", repo=self.repo)
        return self.load_recipes(recipes_dir)
Example #6
0
    def create_default_recipes(self, server_type=settings.GITSERVER_GITHUB):
        self.set_counts()
        self.server = utils.create_git_server(host_type=server_type)
        self.build_user = utils.create_user_with_token(name="moosebuild",
                                                       server=self.server)
        self.owner = utils.create_user(name="owner", server=self.server)
        self.repo = utils.create_repo(name="repo", user=self.owner)
        self.branch = utils.create_branch(name="devel", repo=self.repo)
        pr = utils.create_recipe(name="PR Base",
                                 user=self.build_user,
                                 repo=self.repo)
        pr1 = utils.create_recipe(name="PR With Dep",
                                  user=self.build_user,
                                  repo=self.repo)
        pr1.depends_on.add(pr)
        push = utils.create_recipe(name="Push Base",
                                   user=self.build_user,
                                   repo=self.repo,
                                   branch=self.branch,
                                   cause=models.Recipe.CAUSE_PUSH)
        push1 = utils.create_recipe(name="Push With Dep",
                                    user=self.build_user,
                                    repo=self.repo,
                                    branch=self.branch,
                                    cause=models.Recipe.CAUSE_PUSH)
        push1.depends_on.add(push)
        alt_pr = utils.create_recipe(
            name="Alt PR with dep",
            user=self.build_user,
            repo=self.repo,
            cause=models.Recipe.CAUSE_PULL_REQUEST_ALT)
        alt_pr.depends_on.add(pr)

        utils.create_recipe(name="Manual",
                            user=self.build_user,
                            repo=self.repo,
                            branch=self.branch,
                            cause=models.Recipe.CAUSE_MANUAL)
        self.compare_counts(
            recipes=6,
            deps=3,
            current=6,
            num_push_recipes=2,
            num_pr_recipes=2,
            num_manual_recipes=1,
            num_pr_alt_recipes=1,
            users=2,
            repos=1,
            branches=1,
        )
Example #7
0
    def test_mooseframework(self):
        # no moose repo
        response = self.client.get(reverse('ci:mooseframework'))
        self.assertEqual(response.status_code, 200)

        user = utils.create_user(name='idaholab')
        repo = utils.create_repo(name='moose', user=user)
        utils.create_pr(repo=repo)
        # no master/devel branches
        response = self.client.get(reverse('ci:mooseframework'))
        self.assertEqual(response.status_code, 200)
        utils.create_branch(name='master', repo=repo)
        utils.create_branch(name='devel', repo=repo)
        # should be good
        response = self.client.get(reverse('ci:mooseframework'))
        self.assertEqual(response.status_code, 200)
Example #8
0
 def test_is_collaborator(self, mock_get):
     repo = utils.create_repo(user=self.user)
     # user is repo owner
     ret = self.gapi.is_collaborator(self.user, repo)
     self.assertIs(ret, True)
     user2 = utils.create_user('user2', server=self.server)
     repo = utils.create_repo(user=user2)
     # a collaborator
     mock_get.return_value = utils.Response(
         json_data={'values': [{
             'name': repo.name
         }]}, status_code=200)
     ret = self.gapi.is_collaborator(self.user, repo)
     self.assertIs(ret, True)
     # not a collaborator
     mock_get.return_value = utils.Response(status_code=404)
     ret = self.gapi.is_collaborator(self.user, repo)
     self.assertIs(ret, False)
Example #9
0
    def create_events(self):
        self.set_counts()
        self.build_user = utils.create_user_with_token(name="moosebuild")
        self.owner = utils.create_user(name="idaholab")
        self.repo = utils.create_repo(name="civet", user=self.owner)
        self.branch = utils.create_branch(name="devel", repo=self.repo)
        pre = utils.create_recipe(name="Precheck", user=self.build_user, repo=self.repo)
        test = utils.create_recipe(name="Test", user=self.build_user, repo=self.repo)
        test1 = utils.create_recipe(name="Test1", user=self.build_user, repo=self.repo)
        test.depends_on.add(pre)
        test1.depends_on.add(pre)
        merge = utils.create_recipe(name="Merge", user=self.build_user, repo=self.repo)
        merge.depends_on.add(test)
        merge.depends_on.add(test1)
        pr = utils.create_pr(title="{a, b} & <c> … somereallylongwordthatshouldgettruncated", repo=self.repo)
        pr.username = '******'
        pr.save()
        for commit in ['1234', '2345', '3456']:
            e = utils.create_event(user=self.owner, commit1=commit, branch1=self.branch, branch2=self.branch)
            e.pull_request = pr
            e.description = "some description"
            e.save()
            j = utils.create_job(recipe=pre, event=e, user=self.build_user)
            j.seconds = datetime.timedelta(seconds=10)
            j.failed_step = 'failed step'
            j.running_step = '3/5'
            j.save()
            utils.create_job(recipe=test, event=e, user=self.build_user)
            utils.create_job(recipe=test1, event=e, user=self.build_user)
            utils.create_job(recipe=merge, event=e, user=self.build_user)

        self.compare_counts(recipes=4,
                deps=4,
                current=4,
                jobs=12,
                active=12,
                num_pr_recipes=4,
                events=3,
                users=2,
                repos=1,
                branches=1,
                commits=3,
                prs=1)
Example #10
0
    def test_view_user(self):
        user = utils.create_user()
        url = reverse('ci:view_user', args=["no_exist"])
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

        url = reverse('ci:view_user', args=[user.name])
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

        ev = utils.create_event()
        pr = utils.create_pr()
        pr.closed = True
        pr.username = user.name
        pr.save()
        ev.pull_request = pr
        ev.save()
        utils.create_job(event=ev)

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
Example #11
0
    def test_is_collaborator(self, mock_get):
        # user is repo owner
        api = self.server.api()
        self.assertTrue(api.is_collaborator(self.owner, self.repo))
        user2 = utils.create_user('user2', server=self.server)

        # a collaborator
        repo = utils.create_repo(user=user2)
        mock_get.return_value = utils.Response([{
            'username':
            self.build_user.name
        }])
        self.assertTrue(api.is_collaborator(self.build_user, repo))

        # not a collaborator
        mock_get.return_value = utils.Response([{'username': '******'}])
        self.assertFalse(api.is_collaborator(self.build_user, repo))

        # some random problem
        mock_get.side_effect = Exception("Bam!")
        self.assertFalse(api.is_collaborator(self.build_user, repo))
Example #12
0
    def test_cancel_old_jobs(self, mock_post, mock_get):
        out = StringIO()
        with self.assertRaises(CommandError):
            management.call_command("cancel_old_jobs", stdout=out)

        out = StringIO()
        self.set_counts()
        management.call_command("cancel_old_jobs",
                                "--dryrun",
                                "--days",
                                "1",
                                stdout=out)
        self.compare_counts()
        self.assertIn("No jobs to cancel", out.getvalue())

        j = utils.create_job()
        created = TimeUtils.get_local_time() - timedelta(days=2)
        utils.update_job(j,
                         ready=True,
                         active=True,
                         status=models.JobStatus.NOT_STARTED,
                         created=created,
                         complete=False)

        # Make sure dryrun doesn't change anything
        out = StringIO()
        self.set_counts()
        management.call_command("cancel_old_jobs",
                                "--dryrun",
                                "--days",
                                "1",
                                stdout=out)
        self.compare_counts()
        self.assertIn(str(j), out.getvalue())
        j.refresh_from_db()
        self.assertEqual(j.status, models.JobStatus.NOT_STARTED)

        # Should update the job and event status
        out = StringIO()
        self.set_counts()
        management.call_command("cancel_old_jobs", "--days", "1", stdout=out)
        self.compare_counts(active_branches=1,
                            canceled=1,
                            events_canceled=1,
                            num_changelog=1,
                            num_events_completed=1,
                            num_jobs_completed=1)
        self.assertIn(str(j), out.getvalue())
        j.refresh_from_db()
        j.event.refresh_from_db()
        self.assertTrue(j.complete)
        self.assertEqual(j.status, models.JobStatus.CANCELED)
        self.assertEqual(j.event.status, models.JobStatus.CANCELED)
        self.assertTrue(j.event.complete)

        # Should not change anything since it isn't old enough
        utils.update_job(j,
                         status=models.JobStatus.NOT_STARTED,
                         complete=False)
        out = StringIO()
        self.set_counts()
        management.call_command("cancel_old_jobs", "--days", "3", stdout=out)
        self.compare_counts()
        self.assertIn("No jobs to cancel", out.getvalue())
        self.assertNotIn(str(j), out.getvalue())
        j.refresh_from_db()
        self.assertEqual(j.status, models.JobStatus.NOT_STARTED)

        # Should update the job and event status
        created = TimeUtils.get_local_time() - timedelta(hours=2)
        utils.update_job(j,
                         status=models.JobStatus.NOT_STARTED,
                         complete=False,
                         created=created)
        out = StringIO()
        self.set_counts()
        management.call_command("cancel_old_jobs", "--hours", "1", stdout=out)
        self.compare_counts(canceled=1, num_changelog=1, num_jobs_completed=1)
        self.assertIn(str(j), out.getvalue())
        j.refresh_from_db()
        self.assertEqual(j.status, models.JobStatus.CANCELED)

        # Should not change anything since it isn't old enough
        utils.update_job(j,
                         status=models.JobStatus.NOT_STARTED,
                         complete=False,
                         created=created)
        out = StringIO()
        self.set_counts()
        management.call_command("cancel_old_jobs", "--hours", "3", stdout=out)
        self.compare_counts()
        self.assertIn("No jobs to cancel", out.getvalue())
        self.assertNotIn(str(j), out.getvalue())
        j.refresh_from_db()
        self.assertEqual(j.status, models.JobStatus.NOT_STARTED)

        # Make sure setting allowed to fail works
        utils.update_job(j,
                         status=models.JobStatus.NOT_STARTED,
                         complete=False,
                         created=created)
        out = StringIO()
        self.set_counts()
        management.call_command("cancel_old_jobs",
                                "--hours",
                                "1",
                                "--allowed-fail",
                                stdout=out)
        self.compare_counts(events_canceled=-1,
                            num_changelog=1,
                            num_jobs_completed=1)
        self.assertIn(str(j), out.getvalue())
        j.refresh_from_db()
        self.assertEqual(j.status, models.JobStatus.FAILED_OK)

        # Check the --client-runner-user option only accepts <host>:<user> syntax
        utils.update_job(j,
                         status=models.JobStatus.NOT_STARTED,
                         complete=False,
                         created=created)
        out = StringIO()
        self.set_counts()
        with self.assertRaises(CommandError):
            management.call_command("cancel_old_jobs",
                                    "--hours",
                                    "1",
                                    '--client-runner-user',
                                    'foo',
                                    stdout=out)
        self.compare_counts()

        # Valid --client-runner-user
        self.set_counts()
        management.call_command(
            "cancel_old_jobs",
            "--hours",
            "1",
            '--client-runner-user',
            "%s:%s" %
            (j.recipe.build_user.server.name, j.recipe.build_user.name),
            stdout=out)
        self.compare_counts(canceled=1,
                            num_changelog=1,
                            num_jobs_completed=1,
                            events_canceled=1)

        # --client-runner-user with no jobs
        utils.update_job(j,
                         status=models.JobStatus.NOT_STARTED,
                         complete=False,
                         created=created)
        other_user = utils.create_user(name="other_user")
        self.set_counts()
        management.call_command("cancel_old_jobs",
                                "--hours",
                                "1",
                                '--client-runner-user',
                                "%s:%s" %
                                (other_user.server.name, other_user.name),
                                stdout=out)
        self.compare_counts()
Example #13
0
    def test_user_open_prs(self):
        user = utils.create_user()
        url = reverse('ci:ajax:user_open_prs', args=["no_exist"])
        response = self.client.get(url)
        self.assertEqual(response.status_code, 400)

        url = reverse('ci:ajax:user_open_prs', args=[user.name])
        response = self.client.get(url)
        self.assertEqual(response.status_code, 400)

        get_data = {'last_request': 10}
        response = self.client.get(url, get_data)
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertEqual(len(data["repos"]), 0)
        self.assertEqual(len(data["prs"]), 0)
        self.assertEqual(len(data["events"]), 0)
        self.assertEqual(len(data["changed_events"]), 0)
        self.assertEqual(len(data["repo_status"]), 0)

        ev = utils.create_event()
        pr = utils.create_pr()
        pr.closed = True
        pr.username = user.name
        pr.save()
        ev.pull_request = pr
        ev.save()
        utils.create_job(event=ev)

        # not open
        response = self.client.get(url, get_data)
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertEqual(len(data["repos"]), 0)
        self.assertEqual(len(data["prs"]), 0)
        self.assertEqual(len(data["events"]), 0)
        self.assertEqual(len(data["changed_events"]), 0)
        self.assertEqual(len(data["repo_status"]), 0)

        # should be OK
        pr.closed = False
        pr.save()
        response = self.client.get(url, get_data)
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertEqual(len(data["repos"]), 1)
        self.assertEqual(len(data["prs"]), 1)
        self.assertEqual(len(data["events"]), 1)
        self.assertEqual(len(data["changed_events"]), 1)
        self.assertEqual(len(data["repo_status"]), 1)

        # use the last request. The changed_* shouldn't be set
        get_data["last_request"] = data["last_request"] + 10
        response = self.client.get(url, get_data)
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertEqual(len(data["repos"]), 1)
        self.assertEqual(len(data["prs"]), 1)
        self.assertEqual(len(data["events"]), 1)
        self.assertEqual(len(data["changed_events"]), 0)
        self.assertEqual(len(data["repo_status"]), 0)
Example #14
0
    def test_ready_jobs(self):
        url = reverse('ci:client:ready_jobs', args=['123', 'client'])
        # only get allowed
        self.set_counts()
        response = self.client.post(url)
        self.compare_counts()
        self.assertEqual(response.status_code, 405)  # not allowed
        self.compare_counts()

        # valid request, but no user with build key, so no jobs
        self.set_counts()
        response = self.client.get(url)
        self.compare_counts(num_clients=1)
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertIn('jobs', data)
        self.assertEqual(len(data['jobs']), 0)

        user = utils.get_test_user()
        job = utils.create_job(user=user)
        job.ready = True
        job.active = True
        job.save()
        r2 = utils.create_recipe(name='recipe2', user=user)
        r3 = utils.create_recipe(name='recipe3', user=user)
        r4 = utils.create_recipe(name='recipe4', user=user)
        job2 = utils.create_job(recipe=r2, user=user)
        job3 = utils.create_job(recipe=r3, user=user)
        job4 = utils.create_job(recipe=r4, user=user)
        utils.update_job(job2, ready=True, active=True)
        utils.update_job(job3, ready=True, active=True)
        utils.update_job(job4, ready=True, active=True)
        r2.priority = 10
        r2.save()
        r3.priority = 5
        r3.save()
        job.recipe.priority = 1
        job.recipe.save()
        r4.priority = 1
        r4.save()

        # valid request with a ready job
        url = reverse('ci:client:ready_jobs', args=[user.build_key, 'client'])
        self.set_counts()
        response = self.client.get(url)
        self.compare_counts()
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertIn('jobs', data)
        self.assertEqual(len(data['jobs']), 4)
        self.assertEqual(data['jobs'][0]['id'], job2.pk)
        self.assertEqual(data['jobs'][1]['id'], job3.pk)
        # two jobs with the same priorty, the one created first should run first
        self.assertEqual(data['jobs'][2]['id'], job.pk)
        self.assertEqual(data['jobs'][3]['id'], job4.pk)

        # Test to see if client_runner_user is working
        # The original user should now only have 3 jobs
        # and the new user that is the client_runner_user
        # should have 1
        other_user = utils.create_user("other_user")
        r4.client_runner_user = other_user
        r4.save()
        self.set_counts()
        response = self.client.get(url)
        self.compare_counts()
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertIn('jobs', data)
        self.assertEqual(len(data['jobs']), 3)

        url = reverse('ci:client:ready_jobs',
                      args=[other_user.build_key, 'client'])
        self.set_counts()
        response = self.client.get(url)
        self.compare_counts()
        self.assertEqual(response.status_code, 200)
        data = response.json()
        self.assertIn('jobs', data)
        self.assertEqual(len(data['jobs']), 1)