#!/usr/bin/python3 # -*- coding: utf-8 -*- # # create certificate (use for test) # Copyright (C) 2017 AleaJactaEst # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . import unittest import tempfile import os import configparser import signal import time import coverage coverage.process_startup() try: import pymanager.client as Client except ImportError: import sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import pymanager.client as Client try: import pymanager.certificate as cert except ImportError: import sys import os sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import pymanager.certificate as cert try: import pymanager.manager as Manager except ImportError: import sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import pymanager.manager as Manager class TestManager(unittest.TestCase): def setUp(self): self.path = os.path.dirname(os.path.abspath(__file__)) self.program = os.path.join(self.path, 'simulate_program.py') def test_client_send_json(self): signal.alarm(10) workdir = tempfile.mkdtemp(prefix='test_client_send_json') workdir_cert_root = tempfile.mkdtemp(prefix='pymanager-certificate-root-') workdir_cert_appli = tempfile.mkdtemp(prefix='pymanager-certificate-application-') args=['--workdir-cert-root', workdir_cert_root, '--workdir-cert-appli', workdir_cert_appli] cert.main(args) config = configparser.ConfigParser() port = 8000 config.add_section('config:server') config.set('config:server', 'port', str(port)) config.set('config:server', 'keyfile', os.path.join(workdir_cert_appli, 'private', 'serverkey.pem')) config.set('config:server', 'certfile', os.path.join(workdir_cert_appli, 'certs', 'servercert.pem')) config.set('config:server', 'ca_cert', os.path.join(workdir_cert_appli, 'certs', 'cachaincert.pem')) config.add_section('config:client') config.set('config:server', 'port', str(port)) config.set('config:client', 'keyfile', os.path.join(workdir_cert_appli, 'private', 'clientkey.pem')) config.set('config:client', 'certfile', os.path.join(workdir_cert_appli, 'certs', 'clientcert.pem')) config.set('config:client', 'ca_cert', os.path.join(workdir_cert_appli, 'certs', 'cachaincert.pem')) config.set('config:client', 'address', '127.0.0.1') config.add_section('command:test') config.set('command:test', 'path', workdir) config.set('command:test', 'command', self.program) print("-"*10, 'A1') try: client = Client.Client(None, None, None) client._load_config(config) self.assertTrue(True) except: self.fail('Error detected on load config') print("-"*10, 'A2') manage = Manager.Manager(False) manage._load_config(config) manage.initialize_http() manage.launch_command() manage.launch_server_http() time.sleep(1) signal.alarm(10) msgjson = client.send_json({'name': 'command:test'}, 'GET', "/STATUS", show_result=False) assert msgjson == {'state' : 'stopped'} msgjson = client.send_json({'name': 'command:test'}, 'POST', "/START", show_result=False) assert msgjson == {'state' : 'started'} msgjson = client.send_json({'name': 'command:test'}, 'GET', "/STATUS", show_result=False) assert msgjson == {'state' : 'started'} msgjson = client.send_json({'name': 'command:test', 'action': 'test'}, 'POST', "/STDIN", show_result=False) print(msgjson) assert msgjson == {'state' : 'ok'} print("-"*80) msgjson = client.send_json({'name': 'command:test'}, 'POST', "/STOP", show_result=False) assert msgjson == {'state' : 'stopped'} msgjson = client.send_json({'name': 'command:test'}, 'GET', "/STATUS", show_result=False) assert msgjson == {'state' : 'stopped'} print("-"*10, 'A3') manage.receive_signal(15, 1) manage.wait_children_commands() #Disable timeout signal.alarm(0) self.assertTrue if __name__ == '__main__': unittest.main()