Create Jenkins Job for testapi automation 45/26045/22
authorrohitsakala <rohitsakala@gmail.com>
Thu, 15 Dec 2016 19:08:41 +0000 (00:38 +0530)
committerrohitsakala <rohitsakala@gmail.com>
Wed, 4 Jan 2017 12:11:38 +0000 (17:41 +0530)
Right now, only builder for creating testapi doc is included

JIRA: FUNCTEST-664

Change-Id: If0a34154084c1d01ed6b997d2226779da43bcb14
Signed-off-by: rohitsakala <rohitsakala@gmail.com>
jjb/releng/testapi-automate.yml [new file with mode: 0644]
utils/test/testapi/htmlize/finish.sh [new file with mode: 0644]
utils/test/testapi/htmlize/htmlize.py [new file with mode: 0644]
utils/test/testapi/htmlize/prepare.sh [new file with mode: 0644]
utils/test/testapi/htmlize/push-doc-artifact.sh [new file with mode: 0644]

diff --git a/jjb/releng/testapi-automate.yml b/jjb/releng/testapi-automate.yml
new file mode 100644 (file)
index 0000000..10b3759
--- /dev/null
@@ -0,0 +1,77 @@
+- project:
+    name: testapi-automate
+
+    project: 'releng'
+
+    stream:
+        - master:
+            branch: '{stream}'
+
+    jobs:
+        - 'testapi-automate-{stream}'
+
+- job-template:
+    name: 'testapi-automate-{stream}'
+
+    parameters:
+        - 'opnfv-build-defaults'
+
+    scm:
+        - git-scm
+
+    triggers:
+        - gerrit:
+            server-name: 'gerrit.opnfv.org'
+            trigger-on:
+                - patchset-created-event:
+                    exclude-drafts: 'false'
+                    exclude-trivial-rebase: 'false'
+                    exclude-no-code-change: 'false'
+                - draft-published-event
+                - comment-added-contains-event:
+                    comment-contains-value: 'recheck'
+                - comment-added-contains-event:
+                    comment-contains-value: 'reverify'
+            projects:
+              - project-compare-type: 'ANT'
+                project-pattern: '{project}'
+                branches:
+                  - branch-compare-type: 'ANT'
+                    branch-pattern: '**/{branch}'
+                file-paths:
+                  - compare-type: 'ANT'
+                    pattern: 'utils/**'
+
+    builders:
+        - start-testapi-server
+        - testapi-doc-build
+        - upload-doc-artifact
+        - clean-testapi-server
+
+################################
+# job builders
+################################
+
+- builder:
+    name: testapi-doc-build
+    builders:
+        - shell: |
+            python ./utils/test/testapi/htmlize/htmlize.py -o ${WORKSPACE}/
+
+- builder:
+    name: start-testapi-server
+    builders:
+        - shell: |
+            !include-raw: ../../utils/test/testapi/htmlize/prepare.sh
+
+- builder:
+    name: clean-testapi-server
+    builders:
+        - shell: |
+            !include-raw: ../../utils/test/testapi/htmlize/finish.sh
+
+- builder:
+    name: upload-doc-artifact
+    builders:
+        - shell: |
+                !include-raw: ../../utils/test/testapi/htmlize/push-doc-artifact.sh
diff --git a/utils/test/testapi/htmlize/finish.sh b/utils/test/testapi/htmlize/finish.sh
new file mode 100644 (file)
index 0000000..dc3aa86
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Stop opnfv-testapi server
+proc_number=`ps -ef | grep opnfv-testapi | grep -v grep | wc -l`
+
+if [ $proc_number -gt 0 ]; then
+    procs=`ps -ef | grep opnfv-testapi | grep -v grep`
+    echo "Kill opnfv-testapi server $procs"
+    ps -ef | grep opnfv-testapi | grep -v grep | awk '{print $2}' | xargs kill -kill &>/dev/null
+fi
diff --git a/utils/test/testapi/htmlize/htmlize.py b/utils/test/testapi/htmlize/htmlize.py
new file mode 100644 (file)
index 0000000..68d02fe
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+
+import argparse
+import requests
+import json
+import os
+
+
+def main(args):
+
+    # Merging two specs
+    api_response = requests.get(args.api_declaration_url)
+    api_response = json.loads(api_response.content)
+    resource_response = requests.get(args.resource_listing_url)
+    resource_response = json.loads(resource_response.content)
+    resource_response['models'] = api_response['models']
+    resource_response['apis'] = api_response['apis']
+
+    # Storing the swagger specs
+    with open('specs.json', 'w') as outfile:
+        json.dump(resource_response, outfile)
+
+    # Generating html page
+    cmd = 'java -jar swagger-codegen-cli.jar generate \
+        -i specs.json -l html2 -o %s' % (args.output_directory)
+    os.system(cmd)
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Create \
+                                      Swagger Spec documentation')
+    parser.add_argument('-ru', '--resource-listing-url',
+                        type=str,
+                        required=False,
+                        default='http://localhost:8000/swagger/spec.json',
+                        help='Resource Listing Spec File')
+    parser.add_argument('-au', '--api-declaration-url',
+                        type=str,
+                        required=False,
+                        default='http://localhost:8000/swagger/spec',
+                        help='API Declaration Spec File')
+    parser.add_argument('-o', '--output-directory',
+                        required=True,
+                        default='./',
+                        help='Output Directory where the \
+                                file should be stored')
+    main(parser.parse_args())
diff --git a/utils/test/testapi/htmlize/prepare.sh b/utils/test/testapi/htmlize/prepare.sh
new file mode 100644 (file)
index 0000000..3c265aa
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+
+#Creating virtual environment
+virtualenv testapi_venv
+source testapi_venv/bin/activate
+
+# Install Pre-requisites
+pip install requests
+
+# Swgger Codegen Tool
+url="http://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.2.1/swagger-codegen-cli-2.2.1.jar"
+
+#Check for jar file locally and in the repo
+if [ ! -f swagger-codegen-cli.jar ];
+then
+    wget http://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/2.2.1/swagger-codegen-cli-2.2.1.jar -O swagger-codegen-cli.jar
+fi
+
+# Start OPNFV Test API Server
+cd utils/test/testapi/
+pip install -r requirements.txt
+./install.sh
+opnfv-testapi -c ../../../testapi_venv/etc/opnfv_testapi/config.ini &
+
+deactivate
diff --git a/utils/test/testapi/htmlize/push-doc-artifact.sh b/utils/test/testapi/htmlize/push-doc-artifact.sh
new file mode 100644 (file)
index 0000000..383565d
--- /dev/null
@@ -0,0 +1,25 @@
+#!/bin/bash
+
+set -e
+set -o pipefail
+
+export PATH=$PATH:/usr/local/bin/
+
+project=$PROJECT
+workspace=$WORKSPACE
+artifact_dir="functest/docs"
+
+set +e
+gsutil&>/dev/null
+if [ $? != 0 ]; then
+    echo "Not possible to push results to artifact: gsutil not installed"
+else
+    gsutil ls gs://artifacts.opnfv.org/"$project"/ &>/dev/null
+    if [ $? != 0 ]; then
+        echo "Not possible to push results to artifact: gsutil not installed."
+    else
+        echo "Uploading document to artifact $artifact_dir"
+        gsutil cp "$workspace"/index.html gs://artifacts.opnfv.org/"$artifact_dir"/testapi.html >/dev/null 2>&1
+        echo "Document can be found at http://artifacts.opnfv.org/functest/docs/testapi.html"
+    fi
+fi