Skip to content

Commit

Permalink
Release version 0.1 (#1)
Browse files Browse the repository at this point in the history
Basic keywords
  • Loading branch information
okgolove authored Mar 3, 2018
1 parent 67b8a45 commit 00837b1
Show file tree
Hide file tree
Showing 15 changed files with 435 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,5 @@ ENV/

# mypy
.mypy_cache/

.pre-commit-config.yaml
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: python
python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
- "3.6"

services:
- docker

before_install:
- docker build --pull -t jenkins-test atest/docker
- docker run -p 127.0.0.1:8080:8080 -e JAVA_OPTS=-Djenkins.install.runSetupWizard=false -d jenkins-test

install:
- pip install .

script:
- robot --loglevel DEBUG atest/

notifications:
webhooks:
- https://fathomless-fjord-24024.herokuapp.com/notify
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[![Build Status](https://travis-ci.org/okgolove/robotframework-jenkins.svg?branch=master)](https://travis-ci.org/okgolove/robotframework-jenkins)

JenkinsLibrary for Robot Framework
==============================

Introduction
------------

JenkinsLibrary is a Robot Framework test
library which helps you to involve Jenkins in your tests.


Installation
------------

Just run:

pip install robotframework-jenkins
15 changes: 15 additions & 0 deletions atest/__init__.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*** Settings ***
Library JenkinsLibrary
Suite Setup Global Setup

*** Variables ***
${url} http://127.0.0.1:8080

*** Keywords ***
Wait For Container
Wait Until Keyword Succeeds 2 min 5 sec\
... Is Jenkins Up url=${jenkins_address}

Global Setup
Set Global Variable ${jenkins_address} ${url}
Wait For Container
31 changes: 31 additions & 0 deletions atest/basic.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
*** Settings ***
Library JenkinsLibrary

*** Test Cases ***
Keyword Without Init
[Tags] init auth
Run Keyword And Expect Error Jenkins server was not initialized Get Jenkins Job By Name example

Normal Init Jenkins
[Tags] init auth
Set Jenkins Server url=${jenkins_address} username=admin password=admin

Wrong Username
[Tags] init auth
Run Keyword And Expect Error Can't connect to Jenkins: Error in request. Possibly authentication failed [401]: Invalid password/token for user: nimda\
... Set Jenkins Server url=${jenkins_address} username=nimda password=admin

Wrong Password
[Tags] init auth
Run Keyword And Expect Error Can't connect to Jenkins: Error in request. Possibly authentication failed [401]: Invalid password/token for user: admin\
... Set Jenkins Server url=${jenkins_address} username=admin password=nimda

Wrong URL
[Tags] init auth
Run Keyword And Expect Error Can't connect to Jenkins: Error in request: [Errno 111] Connection refused\
... Set Jenkins Server url=http://127.1.1.1 username=admin password=admin

All Of These Are Wrong
[Tags] init auth
Run Keyword And Expect Error Can't connect to Jenkins: Error in request: [Errno 111] Connection refused\
... Set Jenkins Server url=http://127.0.0.1:11111 username=nimda password=nimda
37 changes: 37 additions & 0 deletions atest/builds.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
*** Settings ***
Library JenkinsLibrary
Suite Setup Set Jenkins Server url=${jenkins_address} username=admin password=admin

*** Variables ***
${test_job_name} test_job

*** Test Cases ***
Get Builds (inexistent job)
[Tags] job build
Run Keyword And Expect Error There is no specified job in Jenkins: ${test_job_name}\
... Get Jenkins Job Builds ${test_job_name}

Get Builds (existent jobs, multiple builds)
[Tags] job build
[Setup] Create Job And Run Multiple Builds
[Teardown] Delete Jenkins Job ${test_job_name}
${builds} = Get Jenkins Job Builds ${test_job_name}
Should Be True ${builds}
Should Be Equal As Integers 1 ${builds['builds'][2]['number']}
Should Be Equal As Integers 2 ${builds['builds'][1]['number']}
Should Be Equal As Integers 3 ${builds['builds'][0]['number']}

Get Builds (existent jobs, no builds)
[Tags] job build
[Setup] Create Jenkins Job ${test_job_name}
[Teardown] Delete Jenkins Job ${test_job_name}
${builds} = Get Jenkins Job Builds ${test_job_name}
Should Not Be True ${builds['lastBuild']}
Should Be Equal As Integers 1 ${builds['nextBuildNumber']}

*** Keywords ***
Create Job And Run Multiple Builds
Create Jenkins Job ${test_job_name}
Start Jenkins Job ${test_job_name}
Start Jenkins Job ${test_job_name}
Start Jenkins Job ${test_job_name}
3 changes: 3 additions & 0 deletions atest/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM jenkins/jenkins:latest

COPY set_password.groovy /usr/share/jenkins/ref/init.groovy.d/
20 changes: 20 additions & 0 deletions atest/docker/set_password.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!groovy
import hudson.security.*
import jenkins.model.*

def instance = Jenkins.getInstance()
def hudsonRealm = new HudsonPrivateSecurityRealm(false)
def users = hudsonRealm.getAllUsers()
users_s = users.collect { it.toString() }
if ('admin' in users_s) {
println "Admin user already exists"
} else {
println "--> creating local admin user"

hudsonRealm.createAccount('admin', 'admin')
instance.setSecurityRealm(hudsonRealm)

def strategy = new FullControlOnceLoggedInAuthorizationStrategy()
instance.setAuthorizationStrategy(strategy)
instance.save()
}
29 changes: 29 additions & 0 deletions atest/execute.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
*** Settings ***
Library Collections
Library JenkinsLibrary
Suite Setup Set Jenkins Server url=${jenkins_address} username=admin password=admin

*** Variables ***
${test_job_name} test_job

*** Test Cases ***
Wrong Params Type
[Tags] execute
[Setup] Create Jenkins Job ${test_job_name}
[Teardown] Delete Jenkins Job ${test_job_name}
Run Keyword And Expect Error Params must be a dictionary, not *\
... Start Jenkins Job ${test_job_name} test_string

Run Existent Job Without Params
[Tags] execute
[Setup] Create Jenkins Job ${test_job_name}
[Teardown] Delete Jenkins Job ${test_job_name}
Start Jenkins Job ${test_job_name}

Run Inexistent Job
[Tags] execute
Run Keyword And Expect Error There is no specified job in Jenkins: ${test_job_name}\
... Start Jenkins Job ${test_job_name}


# TODO: Test with params, also add functionality for methods (params)
89 changes: 89 additions & 0 deletions atest/jobs.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
*** Settings ***
Library Collections
Library JenkinsLibrary
Suite Setup Set Jenkins Server url=${jenkins_address} username=admin password=admin

*** Variables ***
${test_job_name} test_job
${second_test_job_name} blablabla_job

*** Test Cases ***
Get One Existent Job
[Tags] job
[Setup] Create Jenkins Job ${test_job_name}
[Teardown] Delete Jenkins Job ${test_job_name}
${job} = Get Jenkins Job By Name ${test_job_name}
Should Be True ${job}

Get Inexistent Job
[Tags] job
Run Keyword And Expect Error Can't find specified job: ${test_job_name}\
... Get Jenkins Job By Name ${test_job_name}

Get Jobs (empty Jenkins)
[Tags] job
${jobs} = Get Jenkins Jobs
Should Not Be True ${jobs}

Get Jobs (multiple jobs)
[Tags] job
[Setup] Create Multiple Jobs
[Teardown] Delete Multiple Jobs
Check Multiple Jobs

Create Existent Job
[Tags] job
[Setup] Create Jenkins Job ${test_job_name}
[Teardown] Delete Jenkins Job ${test_job_name}
Run Keyword And Expect Error Specified job already exists: ${test_job_name}\
... Create Jenkins Job ${test_job_name}

Delete Inexistent Job
[Tags] job
Run Keyword And Expect Error There is no specified job in Jenkins: ${test_job_name}\
... Delete Jenkins Job ${test_job_name}

Disable Inexistent Job
[Tags] job
Run Keyword And Expect Error There is no specified job in Jenkins: ${test_job_name}\
... Disable Jenkins Job ${test_job_name}

Enable Inexistent Job
[Tags] job
Run Keyword And Expect Error There is no specified job in Jenkins: ${test_job_name}\
... Disable Jenkins Job ${test_job_name}

Enable Disabled Job
[Tags] job
[Setup] Create And Disable Job
[Teardown] Delete Jenkins Job ${test_job_name}
Enable Jenkins Job ${test_job_name}

Disable Enabled Job
[Tags] job
[Setup] Create Jenkins Job ${test_job_name}
[Teardown] Delete Jenkins Job ${test_job_name}
Disable Jenkins Job ${test_job_name}

*** Keywords ***
Create And Disable Job
Create Jenkins Job ${test_job_name}
Disable Jenkins Job ${test_job_name}

Create Multiple Jobs
Create Jenkins Job ${test_job_name}
Create Jenkins Job ${second_test_job_name}

Delete Multiple Jobs
Delete Jenkins Job ${test_job_name}
Delete Jenkins Job ${second_test_job_name}

Check Multiple Jobs
${jobs} = Get Jenkins Jobs
Should Be True ${jobs}
${second_job} = Get From List ${jobs} 0
Should Be Equal ${second_job['name']} ${second_test_job_name}
Should Be Equal ${second_job['url']} ${jenkins_address}/job/${second_test_job_name}/
${first_job} = Get From List ${jobs} 1
Should Be Equal ${first_job['name']} ${test_job_name}
Should Be Equal ${first_job['url']} ${jenkins_address}/job/${test_job_name}/
25 changes: 25 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from os.path import join, dirname
from setuptools import setup

exec(open(join(dirname(__file__),
'src', 'JenkinsLibrary', 'version.py')).read())
PACKAGE = 'robotframework-jenkins'
DESCRIPTION = '''Library for Robot Framework for Jenkins interaction'''
REQUIREMENTS = [
'python-jenkins==0.4.15',
'robotframework==3.0.2',
'requests==2.18.4'
]

setup(
name=PACKAGE,
package_dir={'': 'src'},
packages=['JenkinsLibrary'],
version=VERSION,
description=DESCRIPTION,
author='Mikhail Naletov',
author_email='[email protected]',
url='https://github.com/okgolove/robotframework-jenkins',
keywords=['jenkins', 'robotframework', 'robot', 'testing'],
install_requires=REQUIREMENTS
)
1 change: 1 addition & 0 deletions src/JenkinsLibrary/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .library import JenkinsLibrary
51 changes: 51 additions & 0 deletions src/JenkinsLibrary/library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import requests
from .server import Server
from .version import VERSION

__version__ = VERSION


class JenkinsLibrary(object):
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
ROBOT_LIBRARY_VERSION = __version__

def __init__(self):
self.jenkins = Server()

def set_jenkins_server(self, url, username, password):
self.jenkins.initialize(url=url, username=username, password=password)

def is_jenkins_up(self, url):
try:
r = requests.get("{0}/login".format(url))
except requests.ConnectionError:
raise RuntimeError('Jenkins server {0} is not available')
else:
if r.status_code != 200:
raise RuntimeError(
'Jenkins server {0} is not available, status code:{1}'.
format(url, r.status_code))

def get_jenkins_jobs(self):
return self.jenkins.get_jobs()

def get_jenkins_job_by_name(self, name):
return self.jenkins.get_job(name)

def create_jenkins_job(self, name):
self.jenkins.create_job(name)

def delete_jenkins_job(self, name):
self.jenkins.delete_job(name)

def disable_jenkins_job(self, name):
self.jenkins.disable_job(name)

def enable_jenkins_job(self, name):
self.jenkins.enable_job(name)

def start_jenkins_job(self, name, params={}):
self.jenkins.build_job(name, params)

def get_jenkins_job_builds(self, name):
return self.jenkins.get_builds(name)
Loading

0 comments on commit 00837b1

Please sign in to comment.