Commit f65ff825 authored by Alain Takoudjou's avatar Alain Takoudjou

Update Slaprunner unittest

parent c3ad77a6
...@@ -8,10 +8,13 @@ import json ...@@ -8,10 +8,13 @@ import json
import os import os
import shutil import shutil
import unittest import unittest
import hashlib
import time
from slapos.runner.utils import (getProfilePath, getSession, isInstanceRunning, isSoftwareRunning, readPid, from slapos.runner.utils import (getProfilePath, getSession, isInstanceRunning, isSoftwareRunning, readPid,
recursifKill, startProxy) recursifKill, startProxy, killRunningProcess)
from slapos.runner import views from slapos.runner import views
import slapos.slap
#Helpers #Helpers
def loadJson(response): def loadJson(response):
...@@ -54,6 +57,9 @@ class SlaprunnerTestCase(unittest.TestCase): ...@@ -54,6 +57,9 @@ class SlaprunnerTestCase(unittest.TestCase):
self.repo = 'http://git.erp5.org/repos/slapos.git' self.repo = 'http://git.erp5.org/repos/slapos.git'
self.software = "workspace/slapos/software/" #relative directory fo SR self.software = "workspace/slapos/software/" #relative directory fo SR
self.project = 'slapos' #Default project name self.project = 'slapos' #Default project name
self.template = 'template.cfg'
self.partitionPrefix = 'slappart'
self.slaposBuildout = "1.6.0-dev-SlapOS-010"
#create slaprunner configuration #create slaprunner configuration
config = Config() config = Config()
config.setConfig() config.setConfig()
...@@ -64,6 +70,8 @@ class SlaprunnerTestCase(unittest.TestCase): ...@@ -64,6 +70,8 @@ class SlaprunnerTestCase(unittest.TestCase):
if not os.path.exists(workdir): if not os.path.exists(workdir):
os.mkdir(workdir) os.mkdir(workdir)
if not os.path.exists(software_link):
os.mkdir(software_link)
views.app.config.update( views.app.config.update(
software_log=config.software_root.rstrip('/') + '.log', software_log=config.software_root.rstrip('/') + '.log',
instance_log=config.instance_root.rstrip('/') + '.log', instance_log=config.instance_root.rstrip('/') + '.log',
...@@ -85,28 +93,25 @@ class SlaprunnerTestCase(unittest.TestCase): ...@@ -85,28 +93,25 @@ class SlaprunnerTestCase(unittest.TestCase):
"""Remove all test data""" """Remove all test data"""
os.unlink(os.path.join(self.app.config['etc_dir'], '.rcode')) os.unlink(os.path.join(self.app.config['etc_dir'], '.rcode'))
project = os.path.join(self.app.config['etc_dir'], '.project') project = os.path.join(self.app.config['etc_dir'], '.project')
proxy = os.path.join(self.app.config['run_dir'], 'proxy.pid')
slapgrid_cp = os.path.join(self.app.config['run_dir'], 'slapgrid-cp.pid')
slapgrid_sr = os.path.join(self.app.config['run_dir'], 'slapgrid-sr.pid')
users = os.path.join(self.app.config['etc_dir'], '.users') users = os.path.join(self.app.config['etc_dir'], '.users')
#Stop process
#self.stopSlapproxy()
if os.path.exists(users): if os.path.exists(users):
os.unlink(users) os.unlink(users)
if os.path.exists(project): if os.path.exists(project):
os.unlink(project) os.unlink(project)
if os.path.exists(proxy):
os.unlink(proxy)
if os.path.exists(slapgrid_cp):
os.unlink(slapgrid_cp)
if os.path.exists(slapgrid_sr):
os.unlink(slapgrid_sr)
if os.path.exists(self.app.config['workspace']): if os.path.exists(self.app.config['workspace']):
shutil.rmtree(self.app.config['workspace']) shutil.rmtree(self.app.config['workspace'])
if os.path.exists(self.app.config['software_root']): if os.path.exists(self.app.config['software_root']):
shutil.rmtree(self.app.config['software_root']) shutil.rmtree(self.app.config['software_root'])
if os.path.exists(self.app.config['instance_root']): if os.path.exists(self.app.config['instance_root']):
shutil.rmtree(self.app.config['instance_root']) shutil.rmtree(self.app.config['instance_root'])
if os.path.exists(self.app.config['software_link']):
shutil.rmtree(self.app.config['software_link'])
self.logout()
#Stop process
killRunningProcess(self.app.config, 'proxy.pid')
killRunningProcess(self.app.config, 'slapgrid-cp.pid')
killRunningProcess(self.app.config, 'slapgrid-sr.pid')
def configAccount(self, username, password, email, name, rcode): def configAccount(self, username, password, email, name, rcode):
"""Helper for configAccount""" """Helper for configAccount"""
...@@ -154,18 +159,58 @@ class SlaprunnerTestCase(unittest.TestCase): ...@@ -154,18 +159,58 @@ class SlaprunnerTestCase(unittest.TestCase):
"""Helper for testslapproxy status""" """Helper for testslapproxy status"""
proxy_pid = os.path.join(self.app.config['run_dir'], 'proxy.pid') proxy_pid = os.path.join(self.app.config['run_dir'], 'proxy.pid')
pid = readPid(proxy_pid) pid = readPid(proxy_pid)
try: proxy = False
os.kill(pid, 0) if pid:
proxy = True try:
except Exception: os.kill(pid, 0)
proxy = False proxy = True
except Exception:
pass
self.assertEqual(proxy, status) self.assertEqual(proxy, status)
def setupProjectFolder(self, withSoftware=False):
"""Helper for create a project folder as for slapos.git"""
base = os.path.join(self.app.config['workspace'], 'slapos')
software = os.path.join(base, 'software')
os.mkdir(base)
os.mkdir(software)
if withSoftware:
testSoftware = os.path.join(software, 'slaprunner-test')
sr = "[buildout]\n\n"
sr += "parts = command\n\nunzip = true\nnetworkcache-section = networkcache\n\n"
sr += "find-links += http://www.nexedi.org/static/packages/source/slapos.buildout/\n\n"
sr += "[networkcache]\ndownload-cache-url = http://www.shacache.org/shacache"
sr += "\ndownload-dir-url = http://www.shacache.org/shadir\n\n"
sr += "[command]\nrecipe = zc.recipe.egg\neggs = plone.recipe.command\n\n"
sr += "[versions]\nzc.buildout = %s\n" % self.slaposBuildout
os.mkdir(testSoftware)
open(os.path.join(testSoftware, self.app.config['software_profile']),
'w').write(sr)
def setupSoftwareFolder(self):
"""Helper for setup compiled software release dir"""
self.setupProjectFolder(withSoftware=True)
md5 = hashlib.md5(os.path.join(self.app.config['workspace'],
"slapos/software/slaprunner-test", self.app.config['software_profile'])
).hexdigest()
base = os.path.join(self.app.config['software_root'], md5)
template = os.path.join(base, self.template)
content = "[buildout]\n"
content += "parts = \n create-file\n\n"
content += "eggs-directory = %s\n" % os.path.join(base, 'eggs')
content += "develop-eggs-directory = %s\n\n" % os.path.join(base, 'develop-eggs')
content += "[create-file]\nrecipe = plone.recipe.command\n"
content += "filename = ${buildout:directory}/etc\n"
content += "command = mkdir ${:filename} && echo 'simple file' > ${:filename}/testfile\n"
os.mkdir(self.app.config['software_root'])
os.mkdir(base)
open(template, "w").write(content)
def stopSlapproxy(self): def stopSlapproxy(self):
"""Kill slapproxy process""" """Kill slapproxy process"""
proxy_pid = os.path.join(self.app.config['run_dir'], 'proxy.pid') killRunningProcess(self.app.config, 'proxy.pid')
pid = readPid(proxy_pid)
recursifKill([pid])
#Begin test case here #Begin test case here
def test_wrong_login(self): def test_wrong_login(self):
...@@ -214,9 +259,11 @@ class SlaprunnerTestCase(unittest.TestCase): ...@@ -214,9 +259,11 @@ class SlaprunnerTestCase(unittest.TestCase):
def test_startProxy(self): def test_startProxy(self):
"""Test slapproxy""" """Test slapproxy"""
self.proxyStatus(False)
startProxy(self.app.config) startProxy(self.app.config)
self.proxyStatus(True) self.proxyStatus(True)
self.stopSlapproxy() self.stopSlapproxy()
self.proxyStatus(False)
def test_cloneProject(self): def test_cloneProject(self):
"""Start scenario 1 for deploying SR: Clone a project from git repository""" """Start scenario 1 for deploying SR: Clone a project from git repository"""
...@@ -247,10 +294,9 @@ class SlaprunnerTestCase(unittest.TestCase): ...@@ -247,10 +294,9 @@ class SlaprunnerTestCase(unittest.TestCase):
def test_createSR(self): def test_createSR(self):
"""Scenario 2: Create a new software release""" """Scenario 2: Create a new software release"""
self.test_cloneProject() self.setAccount()
#Login #setup project directory
self.login(self.users[0], self.users[1]) self.setupProjectFolder()
#test create SR
newSoftware = os.path.join(self.software, 'slaprunner-test') newSoftware = os.path.join(self.software, 'slaprunner-test')
response = loadJson(self.app.post('/createSoftware', response = loadJson(self.app.post('/createSoftware',
data=dict(folder=newSoftware), data=dict(folder=newSoftware),
...@@ -274,7 +320,123 @@ class SlaprunnerTestCase(unittest.TestCase): ...@@ -274,7 +320,123 @@ class SlaprunnerTestCase(unittest.TestCase):
assert software in currentSR assert software in currentSR
self.assertFalse(isInstanceRunning(self.app.config)) self.assertFalse(isInstanceRunning(self.app.config))
self.assertFalse(isSoftwareRunning(self.app.config)) self.assertFalse(isSoftwareRunning(self.app.config))
#Slapproxy process is supose to be started #Slapproxy process is supose to be startednewSoftware = os.path.join(self.software, 'slaprunner-test')
self.proxyStatus(True)
self.stopSlapproxy()
self.logout()
def test_runSoftware(self):
"""Scenario 4: CReate empty SR and save software.cfg file
then run slapgrid-sr
"""
#Call config account
#call create software Release
self.test_createSR()
#Login
self.login(self.users[0], self.users[1])
newSoftware = self.getCurrentSR()
softwareRelease = "[buildout]\n\nparts =\n test-application\n"
softwareRelease += "#Test download git web repos éè@: utf-8 caracters\n"
softwareRelease += "[test-application]\nrecipe = hexagonit.recipe.download\n"
softwareRelease += "url = http://git.erp5.org/gitweb/slapos.git\n"
softwareRelease += "filename = slapos.git\n"
softwareRelease += "download-only = true\n"
response = self.loadJson(self.app.post('/saveFileContent',
data=dict(file=newSoftware,
content=softwareRelease),
follow_redirects=True))
self.assertEqual(response['result'], "")
#Compile software and wait until slapgrid it end
#this is supose to use curent SR
response = self.loadJson(self.app.post('/runSoftwareProfile',
data=dict(),
follow_redirects=True))
self.assertTrue(response['result'])
self.assertTrue(os.path.exists(self.app.config['software_root']))
self.assertTrue(os.path.exists(self.app.config['software_log']))
assert "test-application" in open(self.app.config['software_log'], 'r').read()
sr_dir = os.listdir(self.app.config['software_root'])
self.assertEqual(len(sr_dir), 1)
createdFile = os.path.join(self.app.config['software_root'], sr_dir[0],
'parts', 'test-application', 'slapos.git')
self.assertTrue(os.path.exists(createdFile))
self.proxyStatus(True)
self.stopSlapproxy()
self.logout()
def test_updateInstanceParameter(self):
"""Scenarion 5: Update parameters of current sofware profile"""
self.setAccount()
self.setupSoftwareFolder()
#Set current projet and run Slapgrid-cp
software = os.path.join(self.software, 'slaprunner-test')
response = self.loadJson(self.app.post('/setCurrentProject',
data=dict(path=software),
follow_redirects=True))
self.assertEqual(response['result'], "")
self.proxyStatus(True)
#Send paramters for the instance
parameterDict = dict(appname='slaprunnerTest', cacountry='France')
parameterXml = '<?xml version="1.0" encoding="utf-8"?>\n<instance>'
parameterXml += '<parameter id="appname">slaprunnerTest</parameter>\n'
parameterXml += '<parameter id="cacountry">France</parameter>\n</instance>'
software_type = 'production'
response = self.loadJson(self.app.post('/saveParameterXml',
data=dict(parameter=parameterXml,
software_type=software_type),
follow_redirects=True))
self.assertEqual(response['result'], "")
slap = slapos.slap.slap()
slap.initializeConnection(self.app.config['master_url'])
computer = slap.registerComputer(self.app.config['computer_id'])
partitionList = computer.getComputerPartitionList()
self.assertNotEqual(partitionList, [])
#Assume that the requested partition is partition 0
slapParameterDict = partitionList[0].getInstanceParameterDict()
self.assertTrue(slapParameterDict.has_key('appname'))
self.assertTrue(slapParameterDict.has_key('cacountry'))
self.assertEqual(slapParameterDict['appname'], 'slaprunnerTest')
self.assertEqual(slapParameterDict['cacountry'], 'France')
self.assertEqual(slapParameterDict['slap_software_type'], 'production')
#test getParameterXml for webrunner UI
response = self.loadJson(self.app.get('/getParameterXml/xml'))
self.assertEqual(parameterXml, response['result'])
response = self.loadJson(self.app.get('/getParameterXml/dict'))
self.assertEqual(parameterDict, response['result']['instance'])
self.stopSlapproxy()
self.logout()
def test_requestInstance(self):
"""Scenarion 6: request software instance"""
self.test_updateInstanceParameter()
#Login
self.login(self.users[0], self.users[1])
self.proxyStatus(False)
#run Software profile
response = self.loadJson(self.app.post('/runSoftwareProfile',
data=dict(),
follow_redirects=True))
self.assertTrue(response['result'])
#run instance profile
response = self.loadJson(self.app.post('/runInstanceProfile',
data=dict(),
follow_redirects=True))
self.assertTrue(response['result'])
#Check that all partitions has been created
assert "create-file" in open(self.app.config['instance_log'], 'r').read()
instanceDir = os.listdir(self.app.config['instance_root'])
for num in range(int(self.app.config['partition_amount'])):
partition = os.path.join(self.app.config['instance_root'],
self.partitionPrefix + str(num))
self.assertTrue(os.path.exists(partition))
#Go to partition 0
instancePath = os.path.join(self.app.config['instance_root'],
self.partitionPrefix + '0')
createdFile = os.path.join(instancePath, 'etc', 'testfile')
self.assertTrue(os.path.exists(createdFile))
assert 'simple file' in open(createdFile).read()
self.proxyStatus(True) self.proxyStatus(True)
self.stopSlapproxy() self.stopSlapproxy()
self.logout() self.logout()
...@@ -282,7 +444,9 @@ class SlaprunnerTestCase(unittest.TestCase): ...@@ -282,7 +444,9 @@ class SlaprunnerTestCase(unittest.TestCase):
def main(): def main():
argv = ['', 'test_wrong_login', 'test_configAccount', argv = ['', 'test_wrong_login', 'test_configAccount',
'test_login_logout', 'test_updateAccount', 'test_startProxy', 'test_login_logout', 'test_updateAccount', 'test_startProxy',
'test_cloneProject', 'test_createSR', 'test_openSR'] 'test_cloneProject', 'test_createSR', 'test_openSR',
'test_runSoftware', 'test_updateInstanceParameter',
'test_requestInstance']
unittest.main(module=SlaprunnerTestCase, argv=argv) unittest.main(module=SlaprunnerTestCase, argv=argv)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -13,7 +13,7 @@ import os ...@@ -13,7 +13,7 @@ import os
import subprocess import subprocess
import time import time
import urllib import urllib
import xml.dom from xml.dom import minidom
import xml_marshaller import xml_marshaller
from flask import jsonify from flask import jsonify
...@@ -804,8 +804,8 @@ def readParameters(path): ...@@ -804,8 +804,8 @@ def readParameters(path):
a dictionnary of instance parameters.""" a dictionnary of instance parameters."""
if os.path.exists(path): if os.path.exists(path):
try: try:
xmldoc = xml.dom.minidom.parse(path) xmldoc = minidom.parse(path)
obj = {} object = {}
for elt in xmldoc.childNodes: for elt in xmldoc.childNodes:
sub_obj = {} sub_obj = {}
for subnode in elt.childNodes: for subnode in elt.childNodes:
......
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