def test_get_filtered_by_organization(self, mock_get_pr_by_number, mock_get_commit_id_from_ref): """ GET+POST - A user (instance manager) can only manage PRs from WF which belong to the user's organization. """ wpr1 = make_watched_pr_and_instance(username='******', organization=self.organization) wpr2 = make_watched_pr_and_instance(username='******', organization=self.organization2) self.assertEqual(WatchedPullRequest.objects.count(), 2) # We'll log in with user4, and we should only see pr2, but not pr1 self.api_client.login(username='******', password='******') # Check the PR list response = self.api_client.get('/api/v1/pr_watch/') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertNotIn(('id', wpr1.pk), response.data[0].items()) self.assertIn(('id', wpr2.pk), response.data[0].items()) # Also check the detailed view response = self.api_client.get('/api/v1/pr_watch/{pk}/'.format(pk=wpr1.pk)) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) response = self.api_client.get('/api/v1/pr_watch/{pk}/'.format(pk=wpr2.pk)) self.assertEqual(response.status_code, status.HTTP_200_OK) # Also check update_instance mock_get_pr_by_number.return_value = PRFactory(number=wpr1.github_pr_number) response = self.api_client.post('/api/v1/pr_watch/{pk}/update_instance/'.format(pk=wpr1.pk)) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) mock_get_pr_by_number.return_value = PRFactory(number=wpr2.github_pr_number) response = self.api_client.post('/api/v1/pr_watch/{pk}/update_instance/'.format(pk=wpr2.pk)) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_watch_pr_rate_limit_exceeded(self, mock_get_pr_list_from_usernames, mock_spawn_appserver, mock_get_commit_id_from_ref): """ New PR created on the watched repo """ ansible_extra_settings = textwrap.dedent("""\ WATCH: true edx_ansible_source_repo: https://github.com/open-craft/configuration configuration_version: named-release/elder """) pr = PRFactory( number=234, source_fork_name='fork/repo', target_fork_name='source/repo', branch_name='watch-branch', title='Watched PR title which is very long', username='******', body= 'Hello watcher!\n- - -\r\n**Settings**\r\n```\r\n{}```\r\nMore...'. format(ansible_extra_settings), ) pr_url = 'https://github.com/source/repo/pull/234' self.assertEqual(pr.github_pr_url, pr_url) mock_get_pr_list_from_usernames.side_effect = RateLimitExceeded mock_get_commit_id_from_ref.return_value = '7' * 40 tasks.watch_pr() self.assertEqual(mock_spawn_appserver.call_count, 0)
def test_update_instance(self, username, mock_get_pr_by_number, mock_consul): """ POST /pr_watch/:id/update_instance/ - Update instance with latest settings from the PR """ self.api_client.login(username=username, password='******') # Create a WatchedPullRequest, and OpenEdXInstance: watched_pr = make_watched_pr_and_instance( username='******', organization=self.organization2) instance = OpenEdXInstance.objects.get(pk=watched_pr.instance_id) self.assertIn('fork/master (5555555)', instance.name) self.assertEqual(instance.edx_platform_commit, '5' * 40) # Now mock the PR being updated on GitHub mock_get_pr_by_number.return_value = PRFactory( number=watched_pr.github_pr_number, title="Updated Title", ) with patch('pr_watch.github.get_commit_id_from_ref', return_value=('6' * 40)): response = self.api_client.post( '/api/v1/pr_watch/{pk}/update_instance/'.format( pk=watched_pr.pk)) self.assertEqual(response.status_code, status.HTTP_200_OK) instance.refresh_from_db() self.assertEqual( instance.name, 'PR#{}: Updated Title (edx) - fork/master (6666666)'.format( watched_pr.github_pr_number)) self.assertEqual(instance.edx_platform_commit, '6' * 40)
def test_disabled_watchedfork(self, mock_get_pr_list_from_usernames, mock_spawn_appserver, mock_get_commit_id_from_ref): """ Creates WatchedFork with the 'enabled' field set to false and checks that its PRs are not watched. """ ansible_extra_settings = textwrap.dedent("""\ WATCH: true edx_ansible_source_repo: https://github.com/open-craft/configuration configuration_version: named-release/elder """) _, organization = make_user_and_organization() wf = WatchedForkFactory( organization=organization, fork='source/repo', enabled=False, ) pr = PRFactory( number=234, source_fork_name='fork/repo', target_fork_name=wf.fork, branch_name='watch-branch', title='Watched PR title which is very long', username='******', body='Hello watcher!\n- - -\r\n**Settings**\r\n```\r\n{}```\r\nMore...'.format( ansible_extra_settings ), ) mock_get_commit_id_from_ref.return_value = '7' * 40 mock_get_pr_list_from_usernames.return_value = [pr] tasks.watch_pr() self.assertEqual(mock_spawn_appserver.call_count, 0) self.assertEqual(WatchedPullRequest.objects.count(), 0)
def test_create_from_pr_and_watchedfork_values(self): """ Create an instance from a pull request, and check that the default values from the watched fork are used. """ pr = PRFactory() _, organization = make_user_and_organization() watched_fork = WatchedForkFactory( fork=pr.fork_name, organization=organization, configuration_source_repo_url= 'https://github.com/open-craft/configuration-fromwatchedfork', configuration_version='named-release/elder-fromwatchedfork', configuration_extra_settings=textwrap.dedent("""\ PHRASE: "Hello" """), openedx_release='ginkgo.8', ) with patch( 'instance.models.openedx_instance.OpenEdXInstance._write_metadata_to_consul', return_value=(1, True)): instance, created = WatchedPullRequest.objects.get_or_create_from_pr( pr, watched_fork) self.assertTrue(created) self.assertEqual( instance.configuration_source_repo_url, 'https://github.com/open-craft/configuration-fromwatchedfork') self.assertEqual(instance.configuration_version, 'named-release/elder-fromwatchedfork') self.assertEqual(instance.openedx_release, 'ginkgo.8') self.assertEqual( yaml.load(instance.configuration_extra_settings, Loader=yaml.SafeLoader), {'PHRASE': 'Hello'})
def test_create_from_pr(self): """ Create an instance from a pull request """ pr = PRFactory() _, organization = make_user_and_organization() watched_fork = WatchedForkFactory(fork=pr.fork_name, organization=organization) instance, created = WatchedPullRequest.objects.get_or_create_from_pr(pr, watched_fork) self.assertTrue(created) watched_pr = instance.watchedpullrequest self.assertEqual(watched_pr.instance, instance) self.assertEqual(watched_pr.github_pr_number, pr.number) self.assertEqual(watched_pr.fork_name, pr.fork_name) self.assertEqual(watched_pr.branch_name, pr.branch_name) internal_lms_domain = 'pr{}.sandbox.basedomain.com'.format(pr.number) self.assertEqual(instance.internal_lms_domain, internal_lms_domain) self.assertEqual(instance.internal_lms_preview_domain, 'lms-preview.{}'.format(internal_lms_domain)) self.assertEqual(instance.internal_studio_domain, 'studio-{}'.format(internal_lms_domain)) self.assertRegex(instance.name, r'^PR') self.assertEqual(instance.edx_platform_commit, '9' * 40) same_instance, created = WatchedPullRequest.objects.get_or_create_from_pr(pr, watched_fork) self.assertEqual(instance, same_instance) self.assertFalse(created)
def test_create_from_pr_persistent_databases(self): """ Instances should use persistent databases if requested in the PR """ pr = PRFactory(body='pr123.sandbox.example.com (persistent databases)', number=123) instance, _ = WatchedPullRequest.objects.get_or_create_from_pr(pr) self.assertFalse(instance.use_ephemeral_databases)
def test_unique_constraints_allow_multiple_per_branch(self): """ Verifies that the unique constraint on a pull request allows for multiple copies of the same branch, but different URLs. """ pr = PRFactory() _, organization = make_user_and_organization() watched_fork = WatchedForkFactory( fork=pr.fork_name, organization=organization, configuration_source_repo_url= 'https://github.com/open-craft/configuration-fromwatchedfork', ) watched_pr1 = WatchedPullRequest.objects.create( github_organization_name='get-by', github_repository_name='fork-name', branch_name='test', github_pr_url='https://github.com/open-craft/opencraft/pull/123/', watched_fork=watched_fork, ) watched_pr2 = WatchedPullRequest.objects.create( github_organization_name='get-by', github_repository_name='fork-name', branch_name='test', github_pr_url='https://github.com/open-craft/opencraft/pull/1234/', watched_fork=watched_fork, ) self.assertNotEqual(watched_pr1, watched_pr2)
def test_unique_constraints(self): """ Verifies that we cannot create multiple database entries for following a specific pull request. """ pr = PRFactory() _, organization = make_user_and_organization() watched_fork = WatchedForkFactory( fork=pr.fork_name, organization=organization, configuration_source_repo_url= 'https://github.com/open-craft/configuration-fromwatchedfork', ) WatchedPullRequest.objects.create( github_organization_name='get-by', github_repository_name='fork-name', branch_name='test', github_pr_url='https://github.com/open-craft/opencraft/pull/123/', watched_fork=watched_fork, ) self.assertRaises( IntegrityError, WatchedPullRequest.objects.create, github_organization_name='get-by', github_repository_name='fork-name', branch_name='test', github_pr_url='https://github.com/open-craft/opencraft/pull/123/', watched_fork=watched_fork, )
def test_watch_pr_new(self, mock_get_pr_list_from_usernames, mock_spawn_appserver, mock_get_commit_id_from_ref): """ New PR created on the watched repo """ ansible_extra_settings = textwrap.dedent("""\ WATCH: true edx_ansible_source_repo: https://github.com/open-craft/configuration configuration_version: named-release/elder """) _, organization = make_user_and_organization(github_username='******') WatchedForkFactory(organization=organization, fork='source/repo') pr = PRFactory( number=234, source_fork_name='fork/repo', target_fork_name='source/repo', branch_name='watch-branch', title='Watched PR title which is very long', username='******', body='Hello watcher!\n- - -\r\n**Settings**\r\n```\r\n{}```\r\nMore...'.format( ansible_extra_settings ), ) pr_url = 'https://github.com/source/repo/pull/234' self.assertEqual(pr.github_pr_url, pr_url) mock_get_pr_list_from_usernames.return_value = [pr] mock_get_commit_id_from_ref.return_value = '7' * 40 tasks.watch_pr() self.assertEqual(mock_spawn_appserver.call_count, 1) new_instance_ref_id = mock_spawn_appserver.mock_calls[0][1][0] instance = OpenEdXInstance.objects.get(ref_set__pk=new_instance_ref_id) self.assertEqual(instance.internal_lms_domain, 'pr234.sandbox.awesome.hosting.org') self.assertEqual(instance.internal_lms_preview_domain, 'preview.pr234.sandbox.awesome.hosting.org') self.assertEqual(instance.internal_studio_domain, 'studio.pr234.sandbox.awesome.hosting.org') self.assertEqual(instance.edx_platform_repository_url, 'https://github.com/fork/repo.git') self.assertEqual(instance.edx_platform_commit, '7' * 40) self.assertEqual(instance.openedx_release, 'master') self.assertEqual( yaml.load(instance.configuration_extra_settings, Loader=yaml.SafeLoader), yaml.load(ansible_extra_settings, Loader=yaml.SafeLoader)) self.assertEqual(instance.configuration_source_repo_url, 'https://github.com/open-craft/configuration') self.assertEqual(instance.configuration_version, 'named-release/elder') self.assertEqual( instance.name, 'PR#234: Watched PR title which … (bradenmacdonald) - fork/watch-branch (7777777)') # Also check the WatchedPullRequest object: watched_pr = WatchedPullRequest.objects.get(github_pr_url=pr_url) self.assertEqual(watched_pr.github_pr_number, 234) self.assertEqual(watched_pr.github_pr_url, 'https://github.com/source/repo/pull/234') self.assertEqual(watched_pr.github_base_url, 'https://github.com/fork/repo') self.assertEqual(watched_pr.branch_name, 'watch-branch') self.assertEqual(watched_pr.instance_id, instance.id) # Once the new instance/appserver has been spawned, it shouldn't spawn again: tasks.watch_pr() self.assertEqual(mock_spawn_appserver.call_count, 1)
def create_test_data(number): """ Return some data about a fork and repository. The data is almost the same, but values have a "1" or "2" (or the number you pass) appended. Also, the PR's id is 23001 for PR1, 23002 for PR2 etc. """ pr_extra_settings = textwrap.dedent("""\ PHRASE: "I am the value defined in PR{}" edx_ansible_source_repo: https://github.com/open-craft/configuration configuration_version: named-release/elder """.format(number)) _, organization = make_user_and_organization( github_username='******') wf = WatchedForkFactory( organization=organization, fork='source/repo{}'.format(number), # These 2 values will be replaced by the ones from the PR because the PR ones have more precedence configuration_source_repo_url= 'https://github.com/open-craft/configuration-fromwatchedfork', configuration_version='named-release/elder-fromwatchedfork', configuration_extra_settings=textwrap.dedent("""\ PHRASE: "I am a setting which was set up the watched fork {} (but will be overriden by the PR)" FORK_SPECIFIC_PHRASE: "I am another setting which was set up in watched fork {}" """.format(number, number)), openedx_release='ginkgo.8', ) pr_number = 23000 + number pr = PRFactory( number=pr_number, source_fork_name='fork/repo', target_fork_name=wf.fork, branch_name='watch-branch', title='Watched PR title which is very long', username='******', body= 'Hello watcher!\n- - -\r\n**Settings**\r\n```\r\n{}```\r\nMore...' .format(pr_extra_settings), ) pr_url = 'https://github.com/{}/pull/{}'.format(wf.fork, pr_number) pr_expected_resulting_settings = { 'PHRASE': "I am the value defined in PR{}".format(number), 'FORK_SPECIFIC_PHRASE': "I am another setting which was set up in watched fork {}". format(number), 'edx_ansible_source_repo': 'https://github.com/open-craft/configuration', 'configuration_version': 'named-release/elder', } return { 'wf': wf, 'pr': pr, 'url': pr_url, 'expected_settings': pr_expected_resulting_settings }
def test_create_from_pr_and_watchedfork_values(self): """ Create an instance from a pull request, and check that the default values from the watched fork are used. """ pr = PRFactory() _, organization = make_user_and_organization() watched_fork = WatchedForkFactory( fork=pr.fork_name, organization=organization, ansible_appserver_repo_url= 'https://github.com/open-craft/ansible-playbooks.git', ansible_appserver_playbook='playbooks/appserver.yml', ansible_appserver_requirements_path='requirements.txt', ansible_appserver_version='ansible2.8.17', openstack_server_base_image= '{"name_or_id":"focal-20.04-unmodified"}', configuration_source_repo_url= 'https://github.com/open-craft/configuration-fromwatchedfork', configuration_version='named-release/elder-fromwatchedfork', configuration_extra_settings=textwrap.dedent("""\ PHRASE: "Hello" """), openedx_release='ginkgo.8', ) with patch( 'instance.models.openedx_instance.OpenEdXInstance._write_metadata_to_consul', return_value=(1, True)): instance, created = WatchedPullRequest.objects.get_or_create_from_pr( pr, watched_fork) self.assertTrue(created) self.assertEqual( instance.ansible_appserver_repo_url, 'https://github.com/open-craft/ansible-playbooks.git') self.assertEqual(instance.ansible_appserver_playbook, 'playbooks/appserver.yml') self.assertEqual(instance.ansible_appserver_requirements_path, 'requirements.txt') self.assertEqual(instance.ansible_appserver_version, 'ansible2.8.17') self.assertEqual(json.loads(instance.openstack_server_base_image), {"name_or_id": "focal-20.04-unmodified"}) self.assertEqual( instance.configuration_source_repo_url, 'https://github.com/open-craft/configuration-fromwatchedfork') self.assertEqual(instance.configuration_version, 'named-release/elder-fromwatchedfork') self.assertEqual(instance.openedx_release, 'ginkgo.8') self.assertEqual( yaml.load(instance.configuration_extra_settings, Loader=yaml.SafeLoader), {'PHRASE': 'Hello'})
def test_update_instance_branch_delete(self, mock_get_pr_by_number, mock_get_commit_id_from_ref): """ Test what happens when we try to update an instance for a PR whose branch has been deleted. Note: Once WatchedPullRequest.update_instance_from_pr() has been refactored so that it first queries GitHub for PR details (rather than accepting a PR parameter), it can get the commit ID from the PR details response, rather than using get_branch_tip(), and then this test won't be necessary since the PR API always contains the commit information (in ["head"]["sha"]) even if the branch has been deleted. """ self.api_client.login(username='******', password='******') watched_pr = make_watched_pr_and_instance() mock_get_pr_by_number.return_value = PRFactory(number=watched_pr.github_pr_number) response = self.api_client.post('/api/v1/pr_watch/{pk}/update_instance/'.format(pk=watched_pr.pk)) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data, {'error': 'Could not fetch updated details from GitHub.'})