Skip to content

Commit

Permalink
Add more test cases (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelee2012 authored Dec 31, 2020
1 parent 2a6d5e6 commit faacbdc
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 29 deletions.
4 changes: 2 additions & 2 deletions api4jenkins/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def term(self):
def kill(self):
self.handle_req('POST', 'kill', allow_redirects=False)

def retrigger(self):
pass
# def retrigger(self):
# pass

def get_next_build(self):
item = self.api_json(tree='nextBuild[url]')['nextBuild']
Expand Down
14 changes: 5 additions & 9 deletions api4jenkins/node.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: utf-8

import json
import re

from .exceptions import ItemNotFoundError
from .item import Item
Expand Down Expand Up @@ -36,10 +37,7 @@ def create(self, name, **kwargs):
def get(self, name):
for item in self.api_json(tree='computer[displayName]')['computer']:
if name == item['displayName']:
if name == 'master':
item['url'] = f'{self.url}(master)/'
else:
item['url'] = f"{self.url}{item['displayName']}/"
item['url'] = f"{self.url}{item['displayName']}/"
return self._new_instance_by_item(__name__, item)
return None

Expand All @@ -52,10 +50,7 @@ def iter_builds(self):

def __iter__(self):
for item in self.api_json(tree='computer[displayName]')['computer']:
if item['displayName'] == 'master':
item['url'] = f'{self.url}(master)/'
else:
item['url'] = f"{self.url}{item['displayName']}/"
item['url'] = f"{self.url}{item['displayName']}/"
yield self._new_instance_by_item(__name__, item)


Expand All @@ -79,7 +74,8 @@ def iter_builds(self):


class MasterComputer(Node):
pass
def __init__(self, jenkins, url):
super().__init__(jenkins, re.sub(r'/master/$', '/(master)/', url))


class SlaveComputer(Node):
Expand Down
5 changes: 2 additions & 3 deletions api4jenkins/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def quiet_down(self):
def cancel_quiet_down(self):
self.handle_req('POST', 'cancelQuietDown', allow_redirects=False)

def show_credential(self):
pass

# TODO add groovy to print credential
# def show_credential(self):
# pass
7 changes: 4 additions & 3 deletions api4jenkins/view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: utf-8

from .item import Item
from .mix import ConfigrationMix, DeletionMix
from .mix import ConfigrationMix, DeletionMix, DescriptionMix


class Views(Item):
Expand Down Expand Up @@ -31,7 +31,7 @@ def __iter__(self):
yield self._new_instance_by_item(__name__, item)


class View(Item, ConfigrationMix, DeletionMix):
class View(Item, ConfigrationMix, DescriptionMix, DeletionMix):

def get_job(self, name):
for item in self.api_json(tree='jobs[name,url]')['jobs']:
Expand All @@ -51,7 +51,8 @@ def exclude(self, name):


class AllView(View):
pass
def __init__(self, jenkins, url):
super().__init__(jenkins, url + 'view/all/')


class MyView(View):
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from api4jenkins import PluginsManager
from api4jenkins import Queue
from api4jenkins.node import Nodes
from api4jenkins.view import AllView

DATA = Path(__file__).with_name('tests_data')

Expand All @@ -35,6 +36,8 @@ def _api_json(self, tree='', depth=0):
return load_json('queue/queue.json')
elif isinstance(self, Nodes):
return load_json('node/computer.json')
elif isinstance(self, AllView):
return load_json('view/allview.json')
raise TypeError(f'unknow item: {type(self)}')


Expand Down Expand Up @@ -80,6 +83,10 @@ def credential(jenkins):
return Credential(jenkins, f'{jenkins.url}credentials/store/system/domain/_/test-user/')


@pytest.fixture(scope='module')
def view(jenkins):
return AllView(jenkins, jenkins.url)

@pytest.fixture
def mock_resp():
with responses.RequestsMock() as r:
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/test_build.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# encoding: utf-8

import pytest
from api4jenkins.build import WorkflowRun


Expand All @@ -26,3 +26,10 @@ def test_get_previous_build(self, workflowrun):

def test_get_job(self, workflowrun, workflow):
assert workflow == workflowrun.get_job()

@pytest.mark.parametrize('action', ['stop', 'term', 'kill'])
def test_stop_term_kill(self, workflowrun, mock_resp, action):
req_url = f'{workflowrun.url}{action}'
mock_resp.add('POST', req_url)
getattr(workflowrun, action)()
assert mock_resp.calls[0].request.url == req_url
18 changes: 16 additions & 2 deletions tests/unit/test_jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,22 @@ def test_create_job_succ(self, jenkins, mock_resp):
assert mock_resp.calls[0].response.status_code == 200
assert mock_resp.calls[0].request.url == req_url

def test_copy_job(self, jenkins, mock_resp):
pass
@pytest.mark.parametrize('headers', [{'X-Error': "A job already exists "
"with the name 'Level2_Folder1'"},
{'X-Error': 'No such job: xxxx'}],
ids=['job exist', 'no source job'])
def test_copy_fail(self, jenkins, mock_resp, headers):
mock_resp.add('POST', f'{jenkins.url}createItem',
headers=headers, status=400)
with pytest.raises(BadRequestError):
jenkins.copy_job('not exist', 'Level2_Folder1')

def test_copy_succ(self, jenkins, mock_resp):
req_url = f'{jenkins.url}createItem?name=new_job&mode=copy&from=src_job'
mock_resp.add('POST', req_url)
jenkins.copy_job('src_job', 'new_job')
assert mock_resp.calls[0].response.status_code == 200
assert mock_resp.calls[0].request.url == req_url

def test_delete_job(self, jenkins, mock_resp):
req_url = f'{jenkins.url}job/Level1_Folder1/doDelete'
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,13 @@ def test_get_(self, workflow, key):
def test_iter_builds(self, workflow):
builds = list(workflow.iter_builds())
assert len(builds) == 8

@pytest.mark.parametrize('action', ['enable', 'disable'])
def test_enable_disable(self, workflow, mock_resp, action):
req_url = f'{workflow.url}{action}'
mock_resp.add('POST', req_url)
getattr(workflow, action)()
assert mock_resp.calls[0].request.url == req_url

def test_building(self, workflow):
assert workflow.building is False
12 changes: 12 additions & 0 deletions tests/unit/test_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
from api4jenkins.item import snake


class TestSystem:

@pytest.mark.parametrize('action', ['restart', 'safeRestart', 'quietDown', 'cancelQuietDown'])
def test_enable_disable(self, jenkins, mock_resp, action):
req_url = f'{jenkins.system.url}{action}'
mock_resp.add('POST', req_url)
getattr(jenkins.system, snake(action))()
assert mock_resp.calls[0].request.url == req_url
31 changes: 30 additions & 1 deletion tests/unit/test_view.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# encoding: utf-8
import pytest
from api4jenkins.view import AllView
class TestViews:
pass

def test_get(self, jenkins):
view = jenkins.views.get('all')
assert isinstance(view, AllView)
assert view.url == jenkins.url + 'view/all/'
assert jenkins.views.get('not exist') is None

def test_views(self, jenkins):
assert len(list(jenkins.views)) == 1


class TestView:

def test_get_job(self, view):
job_in_view = view.get_job('Level1_Folder1')
job = view.jenkins.get_job('Level1_Folder1')
assert job_in_view == job
assert view.get_job('not exist') is None

def test_iter(self, view):
assert len(list(view)) == 2

@pytest.mark.parametrize('action, entry', [('include', 'addJobToView'), ('exclude', 'removeJobFromView')])
def test_include_exclude(self, view, mock_resp, action, entry):
req_url = f'{view.url}{entry}?name=folder1'
mock_resp.add('POST', req_url, json={'name': 'folder1'})
getattr(view, action)('folder1')
assert mock_resp.calls[0].request.url == req_url
24 changes: 16 additions & 8 deletions tests/unit/tests_data/job/pipeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,50 @@
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"number": 52,
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/52/"
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/52/",
"building": false
},
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"number": 51,
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/51/"
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/51/",
"building": false
},
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"number": 50,
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/50/"
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/50/",
"building": false
},
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"number": 49,
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/49/"
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/49/",
"building": false
},
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"number": 48,
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/48/"
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/48/",
"building": false
},
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"number": 47,
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/47/"
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/47/",
"building": false
},
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"number": 46,
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/46/"
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/46/",
"building": false
},
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"number": 45,
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/45/"
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob/45/",
"building": false
}
],
"color": "red",
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/tests_data/view/allview.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"_class": "hudson.model.AllView",
"description": null,
"jobs": [
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowJob",
"name": "Level1_WorkflowJob1",
"url": "http://0.0.0.0:8080/job/Level1_WorkflowJob1/",
"color": "red"
},
{
"_class": "com.cloudbees.hudson.plugins.folder.Folder",
"name": "Level1_Folder1",
"url": "http://0.0.0.0:8080/job/Level1_Folder1/"
}
],
"name": "all",
"property": [],
"url": "http://http://0.0.0.0:8080/"
}

0 comments on commit faacbdc

Please sign in to comment.