2018-02-02 20:52:32 +00:00
|
|
|
#!/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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import tempfile
|
|
|
|
import os
|
|
|
|
import configparser
|
2018-02-13 19:43:02 +00:00
|
|
|
import signal
|
|
|
|
import time
|
|
|
|
import coverage
|
|
|
|
coverage.process_startup()
|
2018-02-02 20:52:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-02-13 19:43:02 +00:00
|
|
|
|
2018-02-02 20:52:32 +00:00
|
|
|
class TestManager(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2018-02-13 19:43:02 +00:00
|
|
|
self.path = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
self.program = os.path.join(self.path, 'simulate_program.py')
|
2018-02-02 20:52:32 +00:00
|
|
|
|
|
|
|
def test_client_send_json(self):
|
2018-02-13 19:43:02 +00:00
|
|
|
signal.alarm(10)
|
|
|
|
|
2018-02-02 20:52:32 +00:00
|
|
|
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()
|
2018-02-13 19:43:02 +00:00
|
|
|
port = 8000
|
2018-02-02 20:52:32 +00:00
|
|
|
config.add_section('config:server')
|
2018-02-13 19:43:02 +00:00
|
|
|
config.set('config:server', 'port', str(port))
|
2018-02-02 20:52:32 +00:00
|
|
|
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'))
|
2018-08-02 14:38:07 +00:00
|
|
|
config.set('config:server', 'method', 'https')
|
2018-02-02 20:52:32 +00:00
|
|
|
config.add_section('config:client')
|
2018-02-13 19:43:02 +00:00
|
|
|
config.set('config:server', 'port', str(port))
|
2018-02-02 20:52:32 +00:00
|
|
|
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')
|
2018-08-02 14:38:07 +00:00
|
|
|
config.set('config:client', 'method', 'https')
|
2018-02-02 20:52:32 +00:00
|
|
|
config.add_section('command:test')
|
|
|
|
config.set('command:test', 'path', workdir)
|
2018-02-13 19:43:02 +00:00
|
|
|
config.set('command:test', 'command', self.program)
|
2018-02-02 20:52:32 +00:00
|
|
|
try:
|
|
|
|
client = Client.Client(None, None, None)
|
|
|
|
client._load_config(config)
|
|
|
|
self.assertTrue(True)
|
|
|
|
except:
|
|
|
|
self.fail('Error detected on load config')
|
|
|
|
|
2018-02-13 19:43:02 +00:00
|
|
|
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)
|
2018-10-09 15:18:21 +00:00
|
|
|
self.assertEqual(msgjson['state'], 'stopped')
|
2018-02-13 19:43:02 +00:00
|
|
|
msgjson = client.send_json({'name': 'command:test'}, 'POST', "/START", show_result=False)
|
2018-10-09 15:18:21 +00:00
|
|
|
self.assertEqual(msgjson['state'], 'started')
|
2018-02-13 19:43:02 +00:00
|
|
|
msgjson = client.send_json({'name': 'command:test'}, 'GET', "/STATUS", show_result=False)
|
2018-10-09 15:18:21 +00:00
|
|
|
self.assertEqual(msgjson['state'], 'started')
|
2018-02-13 19:43:02 +00:00
|
|
|
msgjson = client.send_json({'name': 'command:test', 'action': 'test'}, 'POST', "/STDIN", show_result=False)
|
2018-10-09 15:18:21 +00:00
|
|
|
self.assertEqual(msgjson['state'], 'ok')
|
2018-02-13 19:43:02 +00:00
|
|
|
msgjson = client.send_json({'name': 'command:test'}, 'POST', "/STOP", show_result=False)
|
2018-10-09 15:18:21 +00:00
|
|
|
self.assertEqual(msgjson['state'], 'stopped')
|
2018-02-13 19:43:02 +00:00
|
|
|
msgjson = client.send_json({'name': 'command:test'}, 'GET', "/STATUS", show_result=False)
|
2018-10-09 15:18:21 +00:00
|
|
|
self.assertEqual(msgjson['state'], 'stopped')
|
2018-02-13 19:43:02 +00:00
|
|
|
|
|
|
|
manage.receive_signal(15, 1)
|
|
|
|
manage.wait_children_commands()
|
|
|
|
#Disable timeout
|
|
|
|
signal.alarm(0)
|
|
|
|
self.assertTrue
|
2018-02-02 20:52:32 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|