new build + test scripst for ci/cd pipelines

This commit is contained in:
shockrah 2020-10-21 19:36:47 -07:00
parent 90b131c60a
commit 75a9bbe316
3 changed files with 121 additions and 0 deletions

View File

@ -4,3 +4,5 @@ target
static/css/
dev-sql/
diesel.toml
*.log

32
server-api/build.sh Executable file
View File

@ -0,0 +1,32 @@
#!/bin/bash
_help() {
cat <<EOF
Usage: ./build.sh [-h|-t|-b|-r]
h Help : shows this command
t Test : Runs All tests from cargo tests to client tests
b Build : Builds dev build with 'cargo build'
r Release : Builds release build with --release flag
EOF
}
[[ -z $1 ]] && _help && exit 0
while getopts ":htbr" arg; do
case ${arg} in
h)echo help command;;
t)
cargo build 1 > /dev/null 2>&1
cargo test 1 > /dev/null 2>&1
cargo run -- -s 1 > /dev/null 2>&1 &
echo Waiting on server to spin up && sleep 2
server=$!
export CARGO_BIN=$HOME/.cargo/bin/cargo
python3 client-tests/client.py > 'test.log'
kill -9 $server
;;
b)cargo build;;
r)cargo build --release;;
*) _help;;
esac
done

View File

@ -0,0 +1,87 @@
import subprocess, os
import json, requests
class Test:
def __init__(self, base='http://localhost:8888', create_admin=False, admin=None):
'''
@opt:base = base url string
@opt:create_admin = creates admin account directly off cargo -c <NAME>
@opt:admin = optionally pass in dictionary with some admin credentials
potentially from another instance to run multiple tests at once
'''
self.test_count = 0
if create_admin:
self.body = Test.__create_admin()
elif admin is not None:
self.body = body
else:
# for now we use this because my dev server has this fake ass acc on it
self.body = {
'secret': 'utO088fltYrTZg323NZfAGrAkArMkDfwjPmt0ooAYta2oJOYDWcAd1FnrpVVMqZtMeUX4_Hu57-LHCkXy8gedg==',
'id': 23,
'name': 'admin',
'joindate':1602385239,
'status': 0,
'permissions': 18446744073709551615
}
self.base = base
@staticmethod
def __create_admin():
# /home/$user/.cargo/bin/cargo <- normally
# prolly some awful shit on pipes
CARGO_BIN = os.getenv('CARGO_BIN')
raw_json = subprocess.run(f'{CARGO_BIN} run --release -- -c dev-test'.split(), text=True, capture_output=True)
#raw_json = raw_json_b[2:-1].replace('\\n', ' ').strip()
return json.loads(raw_json.stdout)
@staticmethod
def log(url: str, method: str, request: requests.Response):
print(f'{method} {url}')
print(f'\t[Status Code]: {request.status_code}')
print(f'\t[Body]: {request.text}')
def post(self, url, **opts):
rbody = self.body
for k in opts:
rbody[k] = opts[k]
body = json.dumps(rbody)
r = requests.post(self.base + url, data=rbody)
self.log(self.base + url, 'POST', r)
def get(self, url, **opts):
rbody = self.body
for k in opts:
rbody[k] = opts[k]
body = json.dumps(rbody)
r = requests.get(self.base + url, data=body)
self.log(self.base + url, 'GET', r)
def delete(self, url, **opts):
rbody = self.body
for k in opts:
rbody[k] = opts[k]
body =json.dumps(rbody)
r = requests.delete(self.base + url, data=body)
self.log(self.base + url, 'DELETE', r)
def creds(self):
return self.body
if __name__ == '__main__':
worker = Test(create_admin=True)
# First the invites api gets some basic tests
worker.get('/invite/create')
worker.post('/channels/create', name='something random', kind=1)
worker.get('/channels/list')
worker.delete('/channels/delete', name='something random')
worker.get('/channels/list')