3 # Copyright (C) 2014 Simo Sorce <simo@redhat.com>
5 # see file 'COPYING' for use and warranty information
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 from string import Template
31 class IpsilonTestBase(object):
33 def __init__(self, name, execname):
35 self.execname = execname
36 self.rootdir = os.getcwd()
38 self.testuser = pwd.getpwuid(os.getuid())[0]
41 def force_remove(self, op, name, info):
45 def setup_base(self, path, test):
46 self.testdir = os.path.join(path, test.name)
47 if os.path.exists(self.testdir):
48 shutil.rmtree(self.testdir, onerror=self.force_remove)
49 os.makedirs(self.testdir)
50 shutil.copytree(os.path.join(self.rootdir, 'templates'),
51 os.path.join(self.testdir, 'templates'))
52 os.mkdir(os.path.join(self.testdir, 'etc'))
53 os.mkdir(os.path.join(self.testdir, 'lib'))
54 os.mkdir(os.path.join(self.testdir, 'lib', test.name))
55 os.mkdir(os.path.join(self.testdir, 'log'))
57 def generate_profile(self, global_opts, args_opts, name, addr, port):
58 newconf = ConfigParser.ConfigParser()
59 newconf.add_section('globals')
60 for k in global_opts.keys():
61 newconf.set('globals', k, global_opts[k])
62 newconf.add_section('arguments')
63 for k in args_opts.keys():
64 newconf.set('arguments', k, args_opts[k])
66 profile = io.BytesIO()
67 newconf.write(profile)
69 t = Template(profile.getvalue())
70 text = t.substitute({'NAME': name, 'ADDRESS': addr, 'PORT': port,
71 'TESTDIR': self.testdir,
72 'ROOTDIR': self.rootdir,
73 'TEST_USER': self.testuser})
75 filename = os.path.join(self.testdir, '%s_profile.cfg' % name)
76 with open(filename, 'wb') as f:
81 def setup_http(self, name, addr, port):
82 httpdir = os.path.join(self.testdir, name)
84 os.mkdir(os.path.join(httpdir, 'conf.d'))
85 os.mkdir(os.path.join(httpdir, 'html'))
86 os.mkdir(os.path.join(httpdir, 'logs'))
87 os.symlink('/etc/httpd/modules', os.path.join(httpdir, 'modules'))
89 with open(os.path.join(self.rootdir, 'tests/httpd.conf')) as f:
90 t = Template(f.read())
91 text = t.substitute({'HTTPROOT': httpdir,
94 filename = os.path.join(httpdir, 'httpd.conf')
95 with open(filename, 'w+') as f:
100 def setup_idp_server(self, profile, name, addr, port, env):
101 http_conf_file = self.setup_http(name, addr, port)
102 cmd = [os.path.join(self.rootdir,
103 'ipsilon/install/ipsilon-server-install'),
104 '--config-profile=%s' % profile]
105 subprocess.check_call(cmd, env=env)
106 os.symlink(os.path.join(self.rootdir, 'ipsilon'),
107 os.path.join(self.testdir, 'lib', name, 'ipsilon'))
109 return http_conf_file
111 def setup_sp_server(self, profile, name, addr, port, env):
112 http_conf_file = self.setup_http(name, addr, port)
113 cmd = [os.path.join(self.rootdir,
114 'ipsilon/install/ipsilon-client-install'),
115 '--config-profile=%s' % profile]
116 subprocess.check_call(cmd, env=env)
118 return http_conf_file
120 def start_http_server(self, conf, env):
121 p = subprocess.Popen(['/usr/sbin/httpd', '-DFOREGROUND', '-f', conf],
122 env=env, preexec_fn=os.setsid)
123 self.processes.append(p)
126 for p in self.processes:
127 os.killpg(p.pid, signal.SIGTERM)
129 def setup_servers(self, env=None):
130 raise NotImplementedError()
134 if exe.endswith('c'):
136 return subprocess.call([exe], env=env)