Commit da34e846 authored by Łukasz Nowak's avatar Łukasz Nowak

deploy-test: Cover deploy-script-controller

As deploy-script-controller is run inside of the VM and is fragile, it
is tested.

Note: In order to simplify root-run bash script testing the TEST_PREFIX
is added in deploy-script-controller, as solutions like proot seem
overkill.
parent 21b2376f
#!/bin/bash
TEST_PREFIX="${TEST_PREFIX:-}"
# Script for controlling deploy script
#
......@@ -22,11 +23,11 @@
# * post results on each try
# * use function + trap to assure posting on exit
LOG_FILE=/var/log/test-script-deployment.log
LOG_FILE=${TEST_PREFIX}/var/log/test-script-deployment.log
wget -O /tmp/test-script.cfg.$$ -q http://10.0.2.100/data
wget -O ${TEST_PREFIX}/tmp/test-script.cfg.$$ -q http://10.0.2.100/data
source /tmp/test-script.cfg.$$
source ${TEST_PREFIX}/tmp/test-script.cfg.$$
if [ -z "$LOG_FILE" ] ; then
echo "Output log file is missing"
......@@ -48,7 +49,7 @@ if [ -z "$TRIES" ] ; then
exit 1
fi
DEPLOYMENT_SCRIPT=/tmp/test-script-deployment.bash.$$
DEPLOYMENT_SCRIPT=${TEST_PREFIX}/tmp/test-script-deployment.bash.$$
wget -O $DEPLOYMENT_SCRIPT -q $URL
if [[ ! -s "$DEPLOYMENT_SCRIPT" ]] ; then
......@@ -58,7 +59,7 @@ fi
function add_log ()
{
LOG_FILE=$1
for f in /opt/slapos/log/slapos-node-{software,instance}.log ; do
for f in ${TEST_PREFIX}/opt/slapos/log/slapos-node-{software,instance}.log ; do
echo "Tail of '$f':" >> $LOG_FILE
tail -n 500 $f >> $LOG_FILE
done
......
import unittest
import os
import shutil
import subprocess
import tempfile
class TestDeployScriptController(unittest.TestCase):
deployment_script = """#!/bin/bash
echo "This is deployment script"
exit 0
"""
def createMockExecutable(self, name, content):
fname = os.path.join(self.mock_dir, name)
with open(fname, 'w') as fd:
fd.write(content)
os.chmod(fname, 0o755)
def prepareMockDir(self):
self.createMockExecutable('wget', """#!/bin/bash
if [ "$4" == "http://10.0.2.100/data" ] ; then
cat > $2 <<EOF
URL=http://fake/fake
WAITTIME=1
TRIES=1
EOF
elif [ "$4" == "http://fake/fake" ] ; then
cat > $2 <<EOF
%s
EOF
echo "no data"
fi
""" % self.deployment_script)
self.createMockExecutable('curl', """#!/bin/bash
echo $7 | cut -d @ -f 2 > $0.result
""")
def setUp(self):
self.working_dir = tempfile.mkdtemp()
os.makedirs(os.path.join(self.working_dir, 'tmp'))
os.makedirs(os.path.join(self.working_dir, 'var', 'log'))
slapos_log_dir = os.path.join(self.working_dir, 'opt', 'slapos', 'log')
os.makedirs(slapos_log_dir)
open(os.path.join(slapos_log_dir, 'slapos-node-software.log'), 'w').write(
'This is software log\n'
)
open(os.path.join(slapos_log_dir, 'slapos-node-instance.log'), 'w').write(
'This is instance log\n'
)
self.mock_dir = tempfile.mkdtemp()
def beforeTearDown(self):
shutil.rmtree(self.working_dir)
shutil.rmtree(self.mock_dir)
def runDeployScriptController(self):
code = subprocess.call(
['/bin/bash', 'deploy-script-controller'], env=dict(
TEST_PREFIX=self.working_dir,
PATH=self.mock_dir + ':' + os.environ['PATH'])
)
return code, open(
open(os.path.join(self.mock_dir, 'curl.result')).read().strip()).read()
def test(self):
self.prepareMockDir()
code, result = self.runDeployScriptController()
self.assertEqual(0, code)
self.assertTrue('This is deployment script' in result)
self.assertTrue('This is software log' in result)
self.assertTrue('This is instance log' in result)
self.assertTrue(
'deploy-script-controller: Try 1. Script executed successfully.' in result)
def test_failing(self):
self.deployment_script = """#!/bin/bash
echo "This is failing deployment script"
exit 1
"""
self.prepareMockDir()
code, result = self.runDeployScriptController()
self.assertEqual(1, code)
self.assertTrue('This is failing deployment script' in result)
self.assertTrue('This is software log' in result)
self.assertTrue('This is instance log' in result)
self.assertTrue(
'deploy-script-controller: Try 2. Amount of tries 1 exceeded, giving up.' in result)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment