Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
add an option to do selective testing for diff builds
Browse files Browse the repository at this point in the history
Summary: This modifies build_index API to support selective testing for diff builds. Commit builds are not supported.

Test Plan: unit tests

Reviewers: anupc, kylec

Reviewed By: kylec

Subscribers: changesbot, anupc, kylec, wwu

Differential Revision: https://tails.corp.dropbox.com/D231859
  • Loading branch information
Naphat Sanguansin committed Sep 27, 2016
1 parent d8ac659 commit 4c71126
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
21 changes: 17 additions & 4 deletions changes/api/build_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

from cStringIO import StringIO
from flask.ext.restful import reqparse
from flask_restful.types import boolean
from sqlalchemy.orm import joinedload, subqueryload_all
from werkzeug.datastructures import FileStorage

from changes.api.base import APIView, error
from changes.api.validators.author import AuthorValidator
from changes.config import db, statsreporter
from changes.constants import Cause, Result, Status, ProjectStatus
from changes.constants import Cause, Result, SelectiveTestingPolicy, Status, ProjectStatus
from changes.db.utils import get_or_create
from changes.jobs.create_job import create_job
from changes.jobs.sync_build import sync_build
Expand Down Expand Up @@ -138,7 +139,8 @@ def find_green_parent_sha(project, sha):

def create_build(project, collection_id, label, target, message, author,
change=None, patch=None, cause=None, source=None, sha=None,
source_data=None, tag=None, snapshot_id=None, no_snapshot=False):
source_data=None, tag=None, snapshot_id=None, no_snapshot=False,
selective_testing_policy=None):
assert sha or source

repository = project.repository
Expand Down Expand Up @@ -178,6 +180,7 @@ def create_build(project, collection_id, label, target, message, author,
message=message,
cause=cause,
tags=[tag] if tag else [],
selective_testing_policy=selective_testing_policy,
)

db.session.add(build)
Expand Down Expand Up @@ -432,6 +435,9 @@ class BuildIndexAPIView(APIView):
"""Cause for the build (based on the Cause enum). Limited to avoid confusion."""
parser.add_argument('cause', type=unicode, choices=('unknown', 'manual'), default='unknown')

"""Optional flag, default to False."""
parser.add_argument('selective_testing', type=boolean, default=False)

get_parser = reqparse.RequestParser()

"""Optional tag to search for."""
Expand Down Expand Up @@ -640,6 +646,11 @@ def post(self):
# we won't be applying file whitelist, so there is no need to get the list of changed files.
files_changed = None

selective_testing_policy = SelectiveTestingPolicy.enabled if args.selective_testing else SelectiveTestingPolicy.disabled
if patch is None:
# don't do selective testing for commit builds yet
selective_testing_policy = SelectiveTestingPolicy.disabled

collection_id = uuid.uuid4()

builds = []
Expand Down Expand Up @@ -695,7 +706,8 @@ def post(self):
tag=tag,
cause=cause,
snapshot_id=snapshot_id,
no_snapshot=no_snapshot
no_snapshot=no_snapshot,
selective_testing_policy=selective_testing_policy,
))
else:
builds.append(potentials[0])
Expand All @@ -713,7 +725,8 @@ def post(self):
tag=tag,
cause=cause,
snapshot_id=snapshot_id,
no_snapshot=no_snapshot
no_snapshot=no_snapshot,
selective_testing_policy=selective_testing_policy,
))

return self.respond(builds)
40 changes: 39 additions & 1 deletion tests/changes/api/test_build_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mock import patch, MagicMock, Mock
from changes.api.build_index import find_green_parent_sha
from changes.config import db
from changes.constants import Cause, Status, Result
from changes.constants import Cause, SelectiveTestingPolicy, Status, Result
from changes.models.build import Build
from changes.models.job import Job
from changes.models.jobplan import JobPlan
Expand Down Expand Up @@ -1249,6 +1249,7 @@ def test_with_full_params(self, mock_find_green_parent_sha, get_vcs):
assert build.message == 'Hello world!'
assert build.label == 'Foo Bar'
assert build.target == 'D1234'
assert build.selective_testing_policy is SelectiveTestingPolicy.disabled

assert job.project == self.project
assert job.label == self.plan.label
Expand All @@ -1274,6 +1275,43 @@ def test_with_full_params(self, mock_find_green_parent_sha, get_vcs):
assert jobplans[0].plan_id == self.plan.id
assert jobplans[0].project_id == self.project.id

@patch('changes.models.repository.Repository.get_vcs')
def test_with_selective_testing_diff_build(self, get_vcs):
get_vcs.return_value = self.get_fake_vcs()

resp = self.post_sample_patch(data={
'selective_testing': 'true',
})
assert resp.status_code == 200, resp.data

data = self.unserialize(resp)
assert len(data) == 1
assert data[0]['id']

build = Build.query.get(data[0]['id'])
assert build.selective_testing_policy is SelectiveTestingPolicy.enabled

@patch('changes.models.repository.Repository.get_vcs')
def test_with_selective_testing_commit_build(self, get_vcs):
# check that selective testing is not enabled for commit builds
# TODO(naphat) remove/edit this once selective testing is enabled
# for commit builds
get_vcs.return_value = self.get_fake_vcs()

resp = self.client.post(self.path, data={
'sha': 'a' * 40,
'repository': self.project.repository.url,
'selective_testing': 'true',
})
assert resp.status_code == 200, resp.data

data = self.unserialize(resp)
assert len(data) == 1
assert data[0]['id']

build = Build.query.get(data[0]['id'])
assert build.selective_testing_policy is SelectiveTestingPolicy.disabled

def _post_for_repo(self, repo):
return self.client.post(self.path, data={
'repository': repo.url,
Expand Down

0 comments on commit 4c71126

Please sign in to comment.