3 # Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
5 from helpers.common import IpsilonTestBase # pylint: disable=relative-import
6 from helpers.http import HttpSessions # pylint: disable=relative-import
12 import ipsilon.util.data
14 idp_g = {'TEMPLATES': '${TESTDIR}/templates/install',
15 'CONFDIR': '${TESTDIR}/etc',
16 'DATADIR': '${TESTDIR}/lib',
17 'CACHEDIR': '${TESTDIR}/cache',
18 'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
19 'STATICDIR': '${ROOTDIR}',
20 'BINDIR': '${ROOTDIR}/ipsilon',
21 'WSGI_SOCKET_PREFIX': '${TESTDIR}/${NAME}/logs/wsgi'}
24 idp_a = {'hostname': '${ADDRESS}:${PORT}',
25 'admin_user': '${TEST_USER}',
26 'system_user': '${TEST_USER}',
27 'instance': '${NAME}',
33 'server_debugging': 'True'}
36 class IpsilonTest(IpsilonTestBase):
39 super(IpsilonTest, self).__init__('dbupgrades', __file__)
41 def setup_servers(self, env=None):
44 def test_upgrade_from(self, env, old_version):
46 print "Installing IDP server to test upgrade from %i" % old_version
47 name = 'idp_v%i' % old_version
48 addr = '127.0.0.%i' % (10 + old_version)
49 port = str(45080 + old_version)
50 idp = self.generate_profile(idp_g, idp_a, name, addr, port)
51 conf = self.setup_idp_server(idp, name, addr, port, env)
53 # Move database of old_version into place
54 cfgfile = os.path.join(self.testdir, 'etc', name, 'ipsilon.conf')
55 db_indir = os.path.join(self.rootdir, 'tests', 'blobs', 'old_dbs',
57 db_outdir = os.path.join(self.testdir, 'lib', name)
60 for database in ['adminconfig',
65 db_in = os.path.join(db_indir, '%s.sqlite.dump' % database)
66 db_out = os.path.join(db_outdir, '%s.sqlite' % database)
68 cmd = ['/bin/sqlite3', db_out, '.read %s' % db_in]
69 subprocess.check_call(cmd)
71 # Upgrade that database
72 cmd = [os.path.join(self.rootdir,
73 'ipsilon/install/ipsilon-upgrade-database'),
75 subprocess.check_call(cmd,
76 cwd=os.path.join(self.testdir, 'lib', name),
79 # Check some version-specific changes, to see if the upgrade went OK
81 # Check all features in a newly created database
82 # Let's verify if at least one index was created
83 test_db = os.path.join(db_outdir, 'adminconfig.sqlite')
84 p = subprocess.Popen(['/bin/sqlite3', test_db, '.dump'],
85 stdout=subprocess.PIPE)
86 output, _ = p.communicate()
88 print 'Sqlite dump failed'
90 if 'CREATE INDEX' not in output:
91 raise Exception('Database upgrade did not introduce index')
92 if 'PRIMARY KEY' not in output:
93 raise Exception('Database upgrade did not introduce primary ' +
95 elif old_version == 1:
96 # In 1 -> 2, we added indexes and primary keys
97 # Let's verify if at least one index was created
98 test_db = os.path.join(db_outdir, 'adminconfig.sqlite')
99 p = subprocess.Popen(['/bin/sqlite3', test_db, '.dump'],
100 stdout=subprocess.PIPE)
101 output, _ = p.communicate()
103 print 'Sqlite dump failed'
105 if 'CREATE INDEX' not in output:
106 raise Exception('Database upgrade did not introduce index')
107 # SQLite did not support creating primary keys, so we can't test
109 # Start the httpd server
110 http_server = self.start_http_server(conf, env)
112 # Now attempt to use the upgraded database
114 if exe.endswith('c'):
117 exe.append(str(old_version))
119 exe.append('%s:%s' % (addr, port))
120 exit_code = subprocess.call(exe, env=env)
124 # Now kill the last http server
125 os.killpg(http_server.pid, signal.SIGTERM)
126 self.processes.remove(http_server)
129 for version in range(ipsilon.util.data.CURRENT_SCHEMA_VERSION):
130 self.test_upgrade_from(env, version)
133 if __name__ == '__main__':
134 from_version = sys.argv[1]
135 idpname = sys.argv[2]
138 user = pwd.getpwuid(os.getuid())[0]
140 sess = HttpSessions()
141 sess.add_server(idpname, 'http://%s' % url, user,
144 print "dbupgrades: From v%s: Authenticate to IDP ..." % from_version,
146 sess.auth_to_idp(idpname)
147 except Exception, e: # pylint: disable=broad-except
148 print >> sys.stderr, " ERROR: %s" % repr(e)