Skip to content

Commit

Permalink
system_tests.py: Add auto update test
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Knöppler <[email protected]>
  • Loading branch information
theCalcaholic committed Oct 25, 2023
1 parent eb35f83 commit 92c3833
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bin/ncp-provisioning.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fi
cohorte_id=$((1 + RANDOM % 100))
cat > /usr/local/etc/instance.cfg <<EOF
{
"cohorteId": ${cohorte_id},
"cohorteId": ${cohorte_id}
}
EOF
}
Expand Down
64 changes: 62 additions & 2 deletions tests/system_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
More at https://ownyourbits.com
"""
import json

pre_cmd = []

import sys
import getopt
import os
import signal
from subprocess import run, getstatusoutput, PIPE
from subprocess import run, getstatusoutput, PIPE, CompletedProcess
from typing import Optional

processes_must_be_running = [
'apache2',
Expand Down Expand Up @@ -174,6 +176,63 @@ def signal_handler(sig, frame):
sys.exit(0)


class ProcessExecutionException(Exception):
pass


def test_autoupdates():
def handle_error(r: CompletedProcess) -> CompletedProcess:
if r.returncode != 0:
print(f"{tc.red}error{tc.normal}\n{r.stdout.decode('utf-8') if r.stdout else ''}\n{r.stderr.decode('utf-8') if r.stderr else ''}"
f" -- command failed: '{' '.join(r.args)}'")
raise ProcessExecutionException()
return CompletedProcess(r.args,
r.returncode,
r.stdout.decode('utf-8') if r.stdout else '',
r.stderr.decode('utf-8') if r.stderr else '')

def set_cohorte_id(cohorte_id: int) -> CompletedProcess:
r = handle_error(run(pre_cmd + ['cat', '/usr/local/etc/instance.cfg'], stdout=PIPE, stderr=PIPE))
instance_cfg = json.loads(r.stdout)
instance_cfg['cohorteId'] = cohorte_id
return handle_error(run(pre_cmd + ['bash', '-c', f'echo \'{json.dumps(instance_cfg)}\' > /usr/local/etc/instance.cfg'], stdout=PIPE, stderr=PIPE))

print(f"[updates] {tc.brown}staged rollouts{tc.normal}", end=' ')
try:
result = handle_error(run(pre_cmd + ['cat', '/usr/local/etc/ncp-version'], stdout=PIPE, stderr=PIPE))
if 'v99.99.99' in result.stdout:
print(f"{tc.yellow}skipped{tc.normal} (already updated to v99.99.99)")
return True
handle_error(run(pre_cmd + ['rm', '-f', '/var/run/.ncp-latest-version']))
result = handle_error(run(pre_cmd + ['sed', '-i', 's|BRANCH="master"|BRANCH="testing/staged-rollouts-1"|', '/usr/local/bin/ncp-check-version'], stdout=PIPE, stderr=PIPE))
set_cohorte_id(1)
result = run(pre_cmd + ['test', '-f', '/var/run/.ncp-latest-version'], stdout=PIPE, stderr=PIPE)
if result.returncode == 0:
result = handle_error(run(pre_cmd + ['cat', '/var/run/.ncp-latest-version'], stdout=PIPE, stderr=PIPE))
if 'v99.99.99' in result.stdout:
print(f"{tc.red}error{tc.normal} Auto update to v99.99.99 was unexpectedly not prevented by disabled cohorte id")
return False

set_cohorte_id(99)
handle_error(run(pre_cmd + ['/usr/local/bin/ncp-check-version'], stdout=PIPE, stderr=PIPE))
result = handle_error(run(pre_cmd + ['cat', '/var/run/.ncp-latest-version'], stdout=PIPE, stderr=PIPE))
if 'v99.99.99' not in result.stdout:
print(f"{tc.red}error{tc.normal} Expected latest detected version to be v99.99.99, was {result.stdout}")
return False

handle_error(run(pre_cmd + ['/usr/local/bin/ncp-test-updates']))
handle_error(run(pre_cmd + ['ncp-update', 'testing/staged-rollouts-1'], stdout=PIPE, stderr=PIPE))
result = handle_error(run(pre_cmd + ['cat', '/usr/local/etc/v99.99.99.success'], stdout=PIPE, stderr=PIPE))
if 'updated' not in result.stdout:
print(f"{tc.red}error{tc.normal} update to v99.99.99 did not succeed")
return False
print(f"{tc.green}ok{tc.normal}")

except ProcessExecutionException:
return False

return True

if __name__ == "__main__":

signal.signal(signal.SIGINT, signal_handler)
Expand Down Expand Up @@ -282,8 +341,9 @@ def signal_handler(sig, frame):
files1_result = check_files_exist(files_must_exist)
files2_result = check_files_dont_exist(files_must_not_exist)
notify_push_result = check_notify_push()
update_test_result = test_autoupdates()

if running_result and install_result and files1_result and files2_result and notify_push_result:
if running_result and install_result and files1_result and files2_result and notify_push_result and update_test_result:
sys.exit(0)
else:
sys.exit(1)
Expand Down
2 changes: 1 addition & 1 deletion updates/1.52.5.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
cohorte_id=$((RANDOM % 100))
cat > /usr/local/etc/instance.cfg <<EOF
{
"cohorteId": ${cohorte_id},
"cohorteId": ${cohorte_id}
}
EOF
}
Expand Down

0 comments on commit 92c3833

Please sign in to comment.