-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from fescobar/beta
Add scripts for API with Security enabled
- Loading branch information
Showing
6 changed files
with
287 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALLURE_DOCKER_SERVICE_API_PORT=7272 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
158
allure-docker-api-usage/send_results_security_jenkins_pipeline.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
*/ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters