Skip to content

Commit

Permalink
Adding api tests for container pipeline service.
Browse files Browse the repository at this point in the history
This PR adds the first set of tests for the container pipeline
service API.

Signed-off-by: Mohammed Zeeshan Ahmed <mohammed.zee1000@gmail.com>
  • Loading branch information
mohammedzee1000 committed May 17, 2018
1 parent 4acdd5f commit 915ae2d
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
85 changes: 85 additions & 0 deletions ci/tests/test_01_functional/test_04_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import os
from ci.tests.test_01_functional.test_04_api.apitest\
import APITestCase
from container_pipeline.lib import dj
from container_pipeline.models.pipeline import Project,\
Build, BuildPhase
import json
from django.utils import timezone

class ApiV1TestCase(APITestCase):
"""
Module to test api v1.
"""

def test_00_project_api_data_matches_db(self):
self.setUp()

# Create a project in database
self.project = Project.objects.create(
name=self.test_project.get('name')
)

self.project.target_file_link = self.test_project.get(
'target_file_link'
)
self.project.save()

# Get data from api end point

project_api_endpoint = str.format(
"projects/?name={}",
self.test_project.get('name')
)

res = self.query_api(project_api_endpoint)
data = res.get('results')

# Match data for validation
found = False
for item in data:
if item.get('name') == self.test_project.get('name') and \
item.get('target_file_link') == \
self.test_project.get('target_file_link'):
found = True
break
self.assertTrue(found)

def test_01_build_api_data_matches_db(self):
self.setUp()

# Setup data in db for test

self.project = Project.objects.get(
name=str(self.test_project.get('name'))
)

self.build = Build.objects.create(
uuid=str(
self.test_project.get(
'build_uuid'
)
),
project = self.project,
status = 'queued',
weekly_scan = False,
start_time = timezone.now()
)

# Get data from api endpoint
build_api_endpoint = str.format(
"builds/?name={}",
str(self.test_project.get('name'))
)

res = self.query_api(build_api_endpoint)
data = res.get('results')
found = False
for item in data:
if str(item.project) == \
self.test_project.get('name')\
and str(status) == 'queued':
found = True
break
self.assertTrue(found)

39 changes: 39 additions & 0 deletions ci/tests/test_01_functional/test_04_api/apitest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
from ci.tests.base import BaseTestCase
from container_pipeline.utils import request_url
import json
import uuid


class APITestCase(BaseTestCase):
"""
Module to test API
"""

node = "jenkins_slave"
api_server = "jenkins_slave"
test_project = {
"name": "test-project",
"target_file_link": "test_link",
"build_uuid": str(uuid.uuid4())
}
api_version = "v1"

def query_api(self, query):
api_url = str.format(
"http://{node}:9001/api/{api_version}/{query}",
node=str(self.hosts[APITestCase.api_server]['host']),
api_version=str(self.api_version),
query=str(query)
)
response = request_url(api_url)
if response:
return json.loads(
response.read()
)
raise Exception(
str.format(
"Could not get data from endpoint {}",
api_url
)
)

0 comments on commit 915ae2d

Please sign in to comment.