Skip to content

Commit

Permalink
Merge pull request #125 from fescobar/beta
Browse files Browse the repository at this point in the history
Add scripts for API with Security enabled
  • Loading branch information
fescobar authored Sep 3, 2020
2 parents b8176cd + 90e5723 commit 537cfc4
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 4 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALLURE_DOCKER_SERVICE_API_PORT=7272
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Table of contents
* [Multiple Project - Docker Compose](#multiple-project---docker-compose)
* [Creating our first project](#creating-our-first-project)
* [PORT 4040 Deprecated](#port-4040-deprecated)
* [Known Issues](#known-issues)
* [Opening & Refreshing Report](#opening--refreshing-report)
* [New User Interface](#new-user-interface)
* [Deploy using Kubernetes](#deploy-using-kubernetes)
Expand Down Expand Up @@ -407,6 +408,8 @@ The only issue you will face will be when you try to navigate the HISTORY from t

Check the new commands to start the container for a single project or for multiple projects: [ALLURE DOCKER SERVICE](#allure-docker-service-1)

### Known Issues
- `Permission denied` --> https://github.com/fescobar/allure-docker-service/issues/108

### Opening & Refreshing Report
If everything was OK, you will see this:
Expand Down Expand Up @@ -593,8 +596,17 @@ You have 2 options to send results:
python send_results.py
```

- Python script with security enabled: [allure-docker-api-usage/send_results_security.py](allure-docker-api-usage/send_results_security.py)

```sh
python send_results_security.py
```

- Declarative Pipeline script for JENKINS: [allure-docker-api-usage/send_results_jenkins_pipeline.groovy](allure-docker-api-usage/send_results_jenkins_pipeline.groovy)

- Declarative Pipeline script for JENKINS with security enabled: [allure-docker-api-usage/send_results_security_jenkins_pipeline.groovy](allure-docker-api-usage/send_results_security_jenkins_pipeline.groovy)


- PowerShell script: [allure-docker-api-usage/send_results.ps1](allure-docker-api-usage/send_results.ps1)

```sh
Expand Down Expand Up @@ -1070,6 +1082,15 @@ Set-Cookie: csrf_refresh_token=; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Path=/
./send_results_security.sh
```

- Python script with security enabled: [allure-docker-api-usage/send_results_security.py](allure-docker-api-usage/send_results_security.py)

```sh
python send_results_security.py
```

- Declarative Pipeline script for JENKINS with security enabled: [allure-docker-api-usage/send_results_security_jenkins_pipeline.groovy](allure-docker-api-usage/send_results_security_jenkins_pipeline.groovy)


#### Add Custom URL Prefix
`Available from Allure Docker Service version 2.13.5`

Expand Down
4 changes: 1 addition & 3 deletions allure-docker-api-usage/send_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@
json_prettier_response_body = json.dumps(json_response_body, indent=4, sort_keys=True)
print(json_prettier_response_body)



# If you want to generate reports on demand use the endpoint `GET /generate-report` and disable the Automatic Execution >> `CHECK_RESULTS_EVERY_SECONDS: NONE`
"""
print("------------------GENERATE-REPORT------------------")
execution_name = 'execution from my script'
execution_from = 'http://google.com'
execution_type = 'teamcity'
response = requests.get(allure_server + '/allure-docker-service/generate-report?project_id=' + project_id + '&execution_name=' + execution_name + '&execution_from=' + execution_from, '&execution_type=' + execution_type, headers=headers, data=json_request_body, verify=ssl_verification)
response = requests.get(allure_server + '/allure-docker-service/generate-report?project_id=' + project_id + '&execution_name=' + execution_name + '&execution_from=' + execution_from + '&execution_type=' + execution_type, headers=headers, verify=ssl_verification)
print("STATUS CODE:")
print(response.status_code)
print("RESPONSE:")
Expand Down
97 changes: 97 additions & 0 deletions allure-docker-api-usage/send_results_security.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import os, requests, json, base64

# This directory is where you have all your results locally, generally named as `allure-results`
allure_results_directory = '/allure-results-example'
# This url is where the Allure container is deployed. We are using localhost as example
allure_server = 'http://localhost:5050'
# Project ID according to existent projects in your Allure container - Check endpoint for project creation >> `[POST]/projects`
project_id = 'default'
#project_id = 'my-project-id'
# Set security_user & security_password according to Allure container configuration
security_user='my_username'
security_password='my_password'

current_directory = os.path.dirname(os.path.realpath(__file__))
results_directory = current_directory + allure_results_directory
print('RESULTS DIRECTORY PATH: ' + results_directory)

files = os.listdir(results_directory)

print('FILES:')
results = []
for file in files:
result = {}

file_path = results_directory + "/" + file
print(file_path)

if os.path.isfile(file_path):
try:
with open(file_path, "rb") as f:
content = f.read()
if content.strip():
b64_content = base64.b64encode(content)
result['file_name'] = file
result['content_base64'] = b64_content.decode('UTF-8')
results.append(result)
else:
print('Empty File skipped: '+ file_path)
finally :
f.close()
else:
print('Directory skipped: '+ file_path)

headers = {'Content-type': 'application/json'}
request_body = {
"results": results
}
json_request_body = json.dumps(request_body)

ssl_verification = True

print("------------------LOGIN-----------------")
credentials_body = {
"username": security_user,
"password": security_password
}
json_credentials_body = json.dumps(credentials_body)

session = requests.Session()
response = session.post(allure_server + '/allure-docker-service/login', headers=headers, data=json_credentials_body, verify=ssl_verification)

print("STATUS CODE:")
print(response.status_code)
print("RESPONSE COOKIES:")
json_prettier_response_body = json.dumps(session.cookies.get_dict(), indent=4, sort_keys=True)
print(json_prettier_response_body)
csrf_access_token = session.cookies['csrf_access_token']
print("CSRF-ACCESS-TOKEN: " + csrf_access_token)


print("------------------SEND-RESULTS------------------")
headers['X-CSRF-TOKEN'] = csrf_access_token
response = session.post(allure_server + '/allure-docker-service/send-results?project_id=' + project_id, headers=headers, data=json_request_body, verify=ssl_verification)
print("STATUS CODE:")
print(response.status_code)
print("RESPONSE:")
json_response_body = json.loads(response.content)
json_prettier_response_body = json.dumps(json_response_body, indent=4, sort_keys=True)
print(json_prettier_response_body)

# If you want to generate reports on demand use the endpoint `GET /generate-report` and disable the Automatic Execution >> `CHECK_RESULTS_EVERY_SECONDS: NONE`
"""
print("------------------GENERATE-REPORT------------------")
execution_name = 'execution from my script'
execution_from = 'http://google.com'
execution_type = 'teamcity'
response = session.get (allure_server + '/allure-docker-service/generate-report?project_id=' + project_id + '&execution_name=' + execution_name + '&execution_from=' + execution_from + '&execution_type=' + execution_type, headers=headers, verify=ssl_verification)
print("STATUS CODE:")
print(response.status_code)
print("RESPONSE:")
json_response_body = json.loads(response.content)
json_prettier_response_body = json.dumps(json_response_body, indent=4, sort_keys=True)
print(json_prettier_response_body)
print('ALLURE REPORT URL:')
print(json_response_body['data']['report_url'])
"""
158 changes: 158 additions & 0 deletions allure-docker-api-usage/send_results_security_jenkins_pipeline.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Required Jenkins plugins:
// https://plugins.jenkins.io/http_request/
// https://plugins.jenkins.io/pipeline-utility-steps/

// Documentation:
// https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/
// https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/

import groovy.json.JsonOutput;

class Result { String file_name; String content_base64 }

// This url is where the Allure container is deployed. We are using localhost as example
allure_server_url = 'http://localhost:5050'
// Project ID according to existent projects in your Allure container - Check endpoint for project creation >> `[POST]/projects`
project_id = 'default'
//project_id = 'my-project-id'
// Set security_user & security_password according to Allure container configuration
security_user = 'my_username'
security_password = 'my_password'

// This directory is where you have all your results, generally named as `allure-results`
// For the example we are using the results located in 'allure-docker-service/allure-docker-api-usage/allure-results-example'
// Finish the pattern just with 1 asterisk. On this way you avoid to include recursive directories and only you are including files from the first directory level.
pattern_allure_results_directory = '**/**/allure-results-example/*'

automation_repository = 'https://github.com/fescobar/allure-docker-service.git'
default_branch = 'master'

String build_allure_results_json(pattern) {
def results = []
def files = findFiles(glob: pattern)
files.each {
def b64_content = readFile file: "${it.path}", encoding: 'Base64'
if (!b64_content.trim().isEmpty()) {
results.add(new Result(file_name: "${it.name}", content_base64: b64_content))
} else {
print("Empty File skipped: ${it.path}")
}
}
JsonOutput.toJson(results: results)
}

Object get_cookies(response) {
def cookies_map = [:]
def cookies = response.headers.get("Set-Cookie")
cookies.each{
def cookie = it.substring(0, it.indexOf(';'))
def cookie_key = cookie.substring(0, cookie.indexOf('='))
cookies_map[cookie_key] = it
}
cookies_map
}

Object get_cookie_value(cookie) {
def simple_cookie = cookie.substring(0, cookie.indexOf(';'))
return simple_cookie.substring(simple_cookie.indexOf('=') + 1, simple_cookie.length())
}

Object login_to_allure_docker_service(allure_server_url, username, password) {
def json_credential = JsonOutput.toJson(username: username, password: password)
httpRequest url: "${allure_server_url}/allure-docker-service/login",
httpMode: 'POST',
contentType: 'APPLICATION_JSON',
requestBody: json_credential,
consoleLogResponseBody: true,
validResponseCodes: '200'
}

Object send_results_to_allure_docker_service(allure_server_url, cookies, csrf_access_token, project_id, results_json) {
httpRequest url: "${allure_server_url}/allure-docker-service/send-results?project_id=${project_id}",
httpMode: 'POST',
contentType: 'APPLICATION_JSON',
customHeaders: [
[ name: 'Cookie', value: cookies['access_token_cookie'] ],
[ name: 'X-CSRF-TOKEN', value: csrf_access_token ]
],
requestBody: results_json,
consoleLogResponseBody: true,
validResponseCodes: '200'
}

Object generate_allure_report(allure_server_url, cookies, csrf_access_token, project_id, execution_name, execution_from, execution_type) {
execution_name = URLEncoder.encode(execution_name, 'UTF-8')
execution_from = URLEncoder.encode(execution_from, 'UTF-8')
execution_type = URLEncoder.encode(execution_type, 'UTF-8')

httpRequest url: "${allure_server_url}/allure-docker-service/generate-report?project_id=${project_id}&execution_name=${execution_name}&execution_from=${execution_from}&execution_type=${execution_type}",
httpMode: 'GET',
contentType: 'APPLICATION_JSON',
customHeaders: [
[ name: 'Cookie', value: cookies['access_token_cookie'] ],
[ name: 'X-CSRF-TOKEN', value: csrf_access_token ]
],
consoleLogResponseBody: true,
validResponseCodes: '200'
}

pipeline {
agent any
options {
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '10'))
}

stages {
stage('Clone Project Testing Repository') {
steps {
cleanWs()
git(url: automation_repository, branch: default_branch)
}
}

stage('Run Tests') {
steps {
warnError('Unstable Tests') {
print('This stage should be use it to run tests generating allure-results directory')
}
}
}

stage('Login to Allure Docker Service Server') {
steps {
script {
def response = login_to_allure_docker_service(allure_server_url, security_user, security_password)
cookies = get_cookies(response)
print "cookies: ${cookies}"
csrf_access_token = get_cookie_value(cookies['csrf_access_token'])
print "csrf_access_token: ${csrf_access_token}"
}
}
}

stage('Post Results to Allure Docker Service Server') {
steps {
script {
def results_json = build_allure_results_json(pattern_allure_results_directory)
send_results_to_allure_docker_service(allure_server_url, cookies, csrf_access_token, project_id, results_json)
}
}
}
/*
stage('Generate Report in Allure Docker Service Server') {
steps {
script {
// If you want to generate reports on demand use the endpoint `GET /generate-report` and disable the Automatic Execution >> `CHECK_RESULTS_EVERY_SECONDS: NONE`
def execution_name = 'execution from my jenkins'
def execution_from = "$BUILD_URL"
def execution_type = 'jenkins'
def response = generate_allure_report(allure_server_url, cookies, csrf_access_token, project_id, execution_name, execution_from, execution_type)
def response_body = readJSON text: response.content
print "ALLURE REPORT URL: $response_body.data.report_url"
}
}
}
*/
}
}
10 changes: 9 additions & 1 deletion docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ services:
SECURITY_PASS: "my_password"
SECURITY_ENABLED: 0
ports:
- "7272:5050"
- "${ALLURE_DOCKER_SERVICE_API_PORT}:5050"
volumes:
- ./.data/allure-docker-service/allure-results:/app/allure-results

allure-ui:
image: "frankescobar/allure-docker-service-ui"
environment:
ALLURE_DOCKER_PUBLIC_API_URL: "http://localhost:${ALLURE_DOCKER_SERVICE_API_PORT}"
ALLURE_DOCKER_PUBLIC_API_URL_PREFIX: ""
ports:
- "7474:5252"

0 comments on commit 537cfc4

Please sign in to comment.