def test_deploy_bitbucket_curl_connection(self): """deploy view pass bitbucket curl authenticate""" payload = self.payload.bb_payload_empty() task = TaskFactory(service=self.service_bb) deploy = Deploy('host', payload, self.service_ftp_bb, task.name) deploy.perform() service = Service.objects.get(pk=self.service_ftp_bb.pk) self.assertTrue(service.log_set.all()[0].status) self.assertEqual(service.log_set.all()[0].user, 'username') self.assertIn("<b>Bitbucket:</b> Repository %s doesn't exist" % self.service_ftp_bb.repo_name, service.status_message) self.assertIn("<b>Bitbucket:</b> Hook is not set up", service.status_message)
def test_deploy_github_curl_connection(self): """deploy view pass github curl authenticate""" payload = self.payload.gh_payload_empty() task = TaskFactory(service=self.service_gh) deploy = Deploy('host', payload, self.service_ftp_gh, task.name) deploy.perform() service = Service.objects.get(pk=self.service_ftp_gh.pk) self.assertTrue(service.log_set.all()[0].status) self.assertEqual(service.log_set.all()[0].user, 'Pusher') self.assertIn("<b>Github:</b> Repository %s doesn't exist" % self.service_ftp_gh.repo_name, service.status_message) self.assertIn("<b>Github:</b> Hook is not set up", service.status_message)
def test_service_status_with_queue_call_async_result_and_return_json( self, mock_async_result): task = TaskFactory(service=self.service) result = MagicMock(name='results', result=dict(result=22, file='file1.txt'), return_value=22) mock_async_result.return_value = result response = self.client.post( reverse('ftpdeploy_deploy_status', kwargs={'secret_key': self.service.secret_key})) mock_async_result.has_calls([call(u'factory_name_1')]) content = json.loads(response.content.decode('utf-8')) self.assertEqual(content['queue'], 1) self.assertEqual(content['result'], 22) self.assertEqual(content['file'], 'file1.txt')
def test_deploy_ftp_fail(self): """deploy view with fail ftp data capture payload, set service status to Fail""" payload = self.payload.bb_payload_empty() task = TaskFactory(service=self.service_bb) deploy = Deploy('host', payload, self.service_bb, task.name) deploy.perform() service = Service.objects.get(pk=self.service_bb.pk) self.assertEqual(service.log_set.all()[0].user, 'FTP Connection') self.assertFalse(service.log_set.all()[0].status) self.assertFalse(service.lock()) self.assertFalse(service.status) self.assertIn('Log', service.status_message) self.assertIn('FTP', service.status_message)
def test_service_has_queue_method(self): task = TaskFactory(service=self.service) self.assertTrue(self.service.has_queue())
def test_service_lock_method(self): task = TaskFactory(service=self.service) self.assertFalse(self.service.lock()) task1 = TaskFactory(service=self.service, active=True) self.assertTrue(self.service.lock())
def test_deploy_view_ftp_transfer_gh_data(self, mock_curl_connection, mock_current_task): """deploy view transfer data from bitbucket to ftp""" ftp = self.ftp_connection() mock_curl = MagicMock(name='curl') mock_curl_connection.return_value = mock_curl mock_curl.perform = MagicMock(name='curl_perform_added', side_effect=['content added file1', 'content added file2', 'content added file3']) # test create files payload = self.payload.gh_payload_added() task = TaskFactory(service=self.service_gh) deploy = Deploy('host', payload, self.service_ftp_gh, task.name) deploy.perform() service = Service.objects.get(pk=self.service_ftp_gh.pk) self.assertTrue(service.log_set.all()[0].status) # test task is removed after success deploy self.assertFalse(service.task_set.all().exists()) self.assertEqual(self.ftp_read_file(ftp, 'file1.txt'), b'content added file1') self.assertEqual(self.ftp_read_file(ftp, 'folder1/file2.txt'), b'content added file2') self.assertEqual(self.ftp_read_file(ftp, 'folder1/folder2/folder3/file3.txt'), b'content added file3') mock_curl.assert_has_calls([call.authenticate(), call.close()]) calls = ([call('https://raw.github.com/Owner/repo_slug/2fa93a45f6c4f9fe30e54036fe0cf764fae0b2a2/file1.txt'), call('https://raw.github.com/Owner/repo_slug/2fa93a45f6c4f9fe30e54036fe0cf764fae0b2a2/folder1/file2.txt'), call('https://raw.github.com/Owner/repo_slug/2fa93a45f6c4f9fe30e54036fe0cf764fae0b2a2/folder1/folder2/folder3/file3.txt')]) # test celery update progress mock_current_task.assert_has_calls([call.update_state(state='PROGRESS', meta={'status': 0, 'file': u'file1.txt'}), call.update_state( state='PROGRESS', meta={'status': 33, 'file': u'file2.txt'}), call.update_state(state='PROGRESS', meta={'status': 66, 'file': u'file3.txt'})]) mock_curl.perform.assert_has_calls(calls) # test modify files mock_curl.perform = MagicMock(name='curl_perform_modified', side_effect=['content modified file1', 'content modified file3']) payload = self.payload.gh_payload_modified() task = TaskFactory(service=self.service_gh) deploy = Deploy('host', payload, self.service_ftp_gh, task.name) deploy.perform() service = Service.objects.get(pk=self.service_ftp_gh.pk) self.assertTrue(service.log_set.all()[1].status) self.assertEqual(self.ftp_read_file(ftp, 'file1.txt'), b'content modified file1') self.assertEqual(self.ftp_read_file(ftp, 'folder1/folder2/folder3/file3.txt'), b'content modified file3') # test remove files 1 payload = self.payload.gh_payload_removed1() task = TaskFactory(service=self.service_gh) deploy = Deploy('host', payload, self.service_ftp_gh, task.name) deploy.perform() service = Service.objects.get(pk=self.service_ftp_gh.pk) self.assertTrue(service.log_set.all()[2].status) self.assertNotIn('folder2', ftp.nlst('folder1')) # test remove files 2 payload = self.payload.gh_payload_removed2() task = TaskFactory(service=self.service_gh) deploy = Deploy('host', payload, self.service_ftp_gh, task.name) deploy.perform() service = Service.objects.get(pk=self.service_ftp_gh.pk) self.assertTrue(service.log_set.all()[3].status) self.assertNotIn('folder1', ftp.nlst()) self.assertNotIn('file1.txt', ftp.nlst()) ftp.quit()