Skip to content
Snippets Groups Projects
Commit fcda6a4c authored by gwenael remond's avatar gwenael remond Committed by Philippe Caseiro
Browse files

tests (pytest et jenkins)

parent 7592e47c
No related branches found
No related tags found
No related merge requests found
pipeline {
agent {
dockerfile {
filename 'Dockerfile'
dir 'ci'
}
}
triggers {
gitlab(
triggerOnPush: true,
triggerOnMergeRequest: true,
branchFilterType: 'All',
cancelPendingBuildsOnUpdate: false
)
}
options {
gitLabConnection('Gitlab MIM')
gitlabBuilds(builds: ['Launching tests'])
}
stages {
stage('Launching tests') {
steps {
script {
try {
sh 'py.test test/'
updateGitlabCommitStatus name: STAGE_NAME, state: 'success'
} catch (Exception e) {
currentBuild.result = 'UNSTABLE'
updateGitlabCommitStatus name: STAGE_NAME, state: 'failed'
}
}
}
}
}
post {
always {
rocketSend (
avatar: 'http://195.221.237.17/static/b5f67753/images/headshot.png',
message: """
${env.JOB_NAME}/${env.BUILD_ID}: ${currentBuild.currentResult}
${env.RUN_DISPLAY_URL}
@here
""".stripIndent(),
rawMessage: true
)
emailext (
subject: "[${currentBuild.currentResult}] - ${env.JOB_NAME}/${env.BUILD_NUMBER}",
body: """
${env.RUN_DISPLAY_URL}
""".stripIndent(),
recipientProviders: [developers(), requestor()],
)
}
}
}
# Zephir-Client
# Build zephir-client (for now):
* You need to have "pyinstaller" in your machine (https://www.pyinstaller.org/)
* You also need "git" installed
* Start build.sh :
For now this scripts does this :
- clone Tiramisu-cmd-line parser from https://framagit.org/tiramisu/tiramisu-cmdline-parser.git
- clone Tiramisu-json-api from https://framagit.org/tiramisu/tiramisu-json-api.git
- Run pyinstaller to create the "binary"
$ ./build.sh
This script provides a binary in "dist" directory
./dist/zcli -h (for help message)
\ No newline at end of file
build.sh 0 → 100755
#!/bin/sh
BUILD_DIR="./build"
ZDIR=$(pwd)
BINNAME=zephir-client
if [ ! -d ${BUILD_DIR} ]
then
mkdir -p ${BUILD_DIR}
fi
cd ${BUILD_DIR}
# retrieve dependencies
if [ -d tiramisu-cmdline-parser ]
then
git pull
else
git clone https://framagit.org/tiramisu/tiramisu-cmdline-parser.git
fi
cp tiramisu-cmdline-parser/tiramisu_cmdline_parser.py ${ZDIR}
if [ -d tiramisu-json-api ]
then
git pull
else
git clone https://framagit.org/tiramisu/tiramisu-json-api.git
fi
cp -Rpv tiramisu-json-api/tiramisu_json_api ${ZDIR}
cd ${ZDIR}
PYTHONOPTIMIZE=1
pyinstaller ${BINNAME} --clean --onefile --name zcli
#if [[ ${?} -eq 0 ]]
#then
# rm -rf ${ZDIR}/tiramisu_json_api
# rm -rf ${ZDIR}/tiramisu_cmdline_parser.py
#fi
\ No newline at end of file
FROM python:3.6-alpine
RUN apk update && apk upgrade && \
apk add --no-cache bash git
RUN pip install -U pytest
RUN pip install -U requests
COPY . /app
WORKDIR /app
ENV PYTHONPATH "${PYTONPATH}:/app"
RUN git clone https://framagit.org/tiramisu/tiramisu-cmdline-parser.git
RUN cp tiramisu-cmdline-parser/tiramisu_cmdline_parser.py .
RUN git clone https://framagit.org/tiramisu/tiramisu-json-api.git
RUN cp -a tiramisu-json-api/tiramisu_json_api .
#CMD [ "pytest", "./test" ]
This diff is collapsed.
import json
from tiramisu_json_api import Config
from tiramisu_cmdline_parser import TiramisuCmdlineParser
from pathlib import Path
import sys
sys.path.append("../")
from zephir_client import *
#import argparse
from py.test import raises
## rootpath = testpath.parent
testpath = Path(__file__).parent
datapath = testpath / 'data'
def test_message():
with open(datapath / 'api_v1_options.json') as fh:
jsoncontent = json.load(fh)
config = Config(jsoncontent)
message = "server.describe"
result = enables_signature(cmdline_factory(config, [message, "--serverid", "1"]), message)
expected = {'serverid': 1}
assert expected == result
def test_message_invalid_choice_argument():
with open(datapath / 'api_v1_options.json') as fh:
jsoncontent = json.load(fh)
config = Config(jsoncontent)
message = '"server.describe"'
#raises(argparse.ArgumentError, f"enables_signature(cmdline_factory(config, [{message}, '--serverid', '1', '--falsearg', '750']), {message})")
raises(SystemExit, f"enables_signature(cmdline_factory(config, [{message}, '--serverid', '1', '--falsearg', '750']), {message})")
#!/usr/bin/env python3
"""Zephir-cmd-input
"""Zephir-cmd-input script
"""
import sys
import getpass
from pathlib import Path
from json import loads, dumps
import requests
from urllib.request import urlopen, Request
from tiramisu_json_api import Config
from tiramisu_cmdline_parser import TiramisuCmdlineParser
# URL = 'lab14-eole.ac-dijon.fr'
URL = 'zephir2.ac-test.fr'
version = 'v1'
token_file = Path.home() / '.zephir-client.jwt.token'
def send(config):
# check authentication
# retrieve new message value
# (which is the name of message selected by the user)
message = config.option('message').value.get()
payload = {}
version = "v1"
if message:
# retrieve all arguments, version, returns, ... for selected message
message_od_name = message.replace('.', '_')
for option in config.option(message_od_name).list():
name = option.option.name()
if name.endswith('.version'):
api_version = option.value.get()
elif not option.owner.isdefault():
payload[name.rsplit('_', 1)[-1]] = option.value.get()
if not payload:
payload = {}
# get stored token if available
if Path.is_file(token_file):
token = open(token_file).read()
else:
token = ''
auth_ok = False
# loop on call if authentication failed
while not auth_ok:
url = f'https://{URL}/api/{version}/{message}'
headers = {'Authorization':f'Bearer {token}'}
try:
response = requests.post(url, data=dumps(payload), headers=headers, verify=False).json()
if 'error' in response and "bearer" in response["error"]["kwargs"]["reason"]:
# Token non valid :
# {"error": {"uri": "zephir.unknown_error", "kwargs": {"reason": "unexpected bearer format"}, "description": "unknown error"}}
token = authenticate()
else:
auth_ok = True
if 'error' in response:
print(f"Erreur : {response['error']['kwargs']['reason']}")
else:
print(response)
except ValueError:
token = authenticate()
continue
except Exception as err:
print(f"Erreur : {err}")
sys.exit(1)
def authenticate():
print("\nVous n'êtes pas authentifié, veuillez saisir vos identifiants Zéphir\n")
SSO_REALM = "zephir"
USERNAME = input("Utilisateur : ")
PASSWORD = getpass.getpass(prompt='Mot de passe : ', stream=None)
auth_datas = {
"username": USERNAME,
"password": PASSWORD,
"grant_type": "password",
"client_id": "zephir-api"
}
auth_url = f'https://{URL}/auth/realms/{SSO_REALM}/protocol/openid-connect/token'
response = requests.post(auth_url, data=auth_datas, verify=False).json()
token = response.get('access_token', '')
try:
with open(token_file, 'w') as t_file:
t_file.write(token)
except:
# cannot write token file, proceed anyway
pass
return token
def help():
print("""
Usage : zephir-client [-h] domain action
""")
sys.exit(1)
def main():
req = requests.get(f'https://{URL}/api/{version}', verify=False)
json = req.json()
config = Config(json['options'])
parser = TiramisuCmdlineParser(config, fullpath=False)
# bypass mandatory validation in parser
parser.parse_args(valid_mandatory=False)
config = parser.get_config()
from zephir_client import *
def main(config):
config = cmdline_factory(config)
for key in config.value.mandatory():
if not config.option(key).option.issymlinkoption():
text = input("* "+config.option(key).option.doc() + " : ")
......@@ -108,8 +13,6 @@ def main():
config.option(key).value.set(int(text))
else:
config.option(key).value.set(text)
send(config)
if __name__ == "__main__":
main()
main(remote_json_to_config())
File deleted
File deleted
File deleted
File deleted
zephir_client.py 100755 → 100644
# -*- coding: utf-8 -*-
import yaml
import json
import requests
import click
"""Zephir Client library
"""
import sys
import getpass
import os
from collections import OrderedDict
from jwt import decode, get_unverified_header
from zephir.i18n import _
ZEPHIR_URI = "http://lab14-eole.ac-dijon.fr"
API_PATH = "/api"
ZEPHIR_SSO_URI = "https://sso.lab14-eole.ac-dijon.fr"
SSO_REALM = "zephir"
TOKEN_JAR = os.environ['HOME']+"/.zephir-client.jwt.token"
TOKEN = ""
IS_AUTHENTIFICATED = False
def Authentification():
USERNAME = "yo"
PASSWORD = "yo"
#Get Auth Token from SSO
#USERNAME = input('Username :')
#PASSWORD = getpass.getpass(prompt='Password: ', stream=None)
from pathlib import Path
from json import loads, dumps
import requests
import warnings
from urllib.request import urlopen, Request
from urllib3.exceptions import InsecureRequestWarning
from tiramisu_json_api import Config
from tiramisu_cmdline_parser import TiramisuCmdlineParser
# URL = 'lab14-eole.ac-dijon.fr'
URL = 'zephir2.ac-test.fr'
version = 'v1'
token_file = Path.home() / '.zephir-client.jwt.token'
warnings.simplefilter('ignore', InsecureRequestWarning)
def enables_signature(config, message):
"enables the signature's validation of the remote message"
payload = {}
if message:
# retrieve all arguments, version, returns, ... for selected message
message_od_name = message.replace('.', '_')
for option in config.option(message_od_name).list():
name = option.option.name()
if name.endswith('.version'):
api_version = option.value.get()
elif not option.owner.isdefault():
payload[name.rsplit('_', 1)[-1]] = option.value.get()
if not payload:
payload = {}
return payload
def send(config):
# check authentication
# retrieve new message value
# (which is the name of message selected by the user)
message = config.option('message').value.get()
payload = enables_signature(config, message)
# get stored token if available
if Path.is_file(token_file):
token = open(token_file).read()
else:
token = ''
auth_ok = False
# loop on call if authentication failed
while not auth_ok:
url = f'https://{URL}/api/{version}/{message}'
headers = {'Authorization':f'Bearer {token}'}
try:
response = requests.post(url, data=dumps(payload), headers=headers, verify=False).json()
if 'error' in response and "bearer" in response["error"]["kwargs"]["reason"]:
# Token non valid :
# {"error": {"uri": "zephir.unknown_error", "kwargs": {"reason": "unexpected bearer format"}, "description": "unknown error"}}
token = authenticate()
else:
auth_ok = True
if 'error' in response:
print(f"Erreur : {response['error']['kwargs']['reason']}")
else:
print(response)
except ValueError:
token = authenticate()
continue
except Exception as err:
print(f"Erreur : {err}")
sys.exit(1)
def authenticate():
print("\nVous n'êtes pas authentifié, veuillez saisir vos identifiants Zéphir\n")
SSO_REALM = "zephir"
USERNAME = input("Utilisateur : ")
PASSWORD = getpass.getpass(prompt='Mot de passe : ', stream=None)
auth_datas = {
"username": USERNAME,
......@@ -31,25 +87,41 @@ def Authentification():
"grant_type": "password",
"client_id": "zephir-api"
}
auth_url = f'https://{URL}/auth/realms/{SSO_REALM}/protocol/openid-connect/token'
response = requests.post(auth_url, data=auth_datas, verify=False).json()
token = response.get('access_token', '')
try:
with open(token_file, 'w') as t_file:
t_file.write(token)
except:
# cannot write token file, proceed anyway
pass
return token
def help():
print("""
Usage : zephir-client [-h] domain action
""")
sys.exit(1)
def remote_json_to_config():
"retrieves the remote config from the distant api description"
req = requests.get(f'https://{URL}/api/{version}', verify=False)
json = req.json()
return Config(json['options'])
response = requests.post( '{}/auth/realms/{}/protocol/openid-connect/token'.format(ZEPHIR_SSO_URI, SSO_REALM), data=auth_datas)
TOKEN = json.loads(response.text)['access_token']
#TODO Appeller la vrai API lorqu'elle répondra sur /api
def LoadApi():
with open('./mock_api.json', 'r') as mockup:
api = json.load(mockup)
return api
def cmdline_factory(config, args=None):
parser = TiramisuCmdlineParser(config, fullpath=False)
# bypasses mandatory validation in parser
if args is None:
parser.parse_args(valid_mandatory=False)
else:
parser.parse_args(args, valid_mandatory=False)
def Define_messages(api):
messages_dic = {}
for key in api:
table = key.split('.')
messages_dic[table[0]] = []
print(messages_dic)
config = parser.get_config()
return config
Authentification()
#####
API_JSON = LoadApi()
Define_messages(API_JSON)
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment