summaryrefslogtreecommitdiffstats
path: root/keystone/tests/test_import_legacy.py
blob: b3b83c0f169897fa7c768b03f4ac035c6348d404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 OpenStack LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os

try:
    import sqlite3 as dbapi
except ImportError:
    from pysqlite2 import dbapi2 as dbapi

from keystone.tests import core as test

from keystone.catalog.backends import templated as catalog_templated
from keystone.common.sql import legacy
from keystone import config
from keystone import identity
from keystone.identity.backends import sql as identity_sql


CONF = config.CONF


class ImportLegacy(test.TestCase):
    def setUp(self):
        super(ImportLegacy, self).setUp()
        self.config([test.etcdir('keystone.conf.sample'),
                     test.testsdir('test_overrides.conf'),
                     test.testsdir('backend_sql.conf'),
                     test.testsdir('backend_sql_disk.conf')])
        test.setup_test_database()
        self.identity_man = identity.Manager()
        self.identity_api = identity_sql.Identity()

    def tearDown(self):
        test.teardown_test_database()
        super(ImportLegacy, self).tearDown()

    def setup_old_database(self, sql_dump):
        sql_path = test.testsdir(sql_dump)
        db_path = test.tmpdir('%s.db' % sql_dump)
        try:
            os.unlink(db_path)
        except OSError:
            pass
        script_str = open(sql_path).read().strip()
        conn = dbapi.connect(db_path)
        conn.executescript(script_str)
        conn.commit()
        return db_path

    def test_import_d5(self):
        db_path = self.setup_old_database('legacy_d5.sqlite')
        migration = legacy.LegacyMigration('sqlite:///%s' % db_path)
        migration.migrate_all()

        admin_id = '1'
        user_ref = self.identity_api.get_user(admin_id)
        self.assertEquals(user_ref['name'], 'admin')
        self.assertEquals(user_ref['enabled'], True)

        # check password hashing
        user_ref = self.identity_man.authenticate(
            user_id=admin_id, password='secrete')

        # check catalog
        self._check_catalog(migration)

    def test_import_diablo(self):
        db_path = self.setup_old_database('legacy_diablo.sqlite')
        migration = legacy.LegacyMigration('sqlite:///%s' % db_path)
        migration.migrate_all()

        admin_id = '1'
        user_ref = self.identity_api.get_user(admin_id)
        self.assertEquals(user_ref['name'], 'admin')
        self.assertEquals(user_ref['enabled'], True)

        # check password hashing
        user_ref = self.identity_man.authenticate(
            user_id=admin_id, password='secrete')

        # check catalog
        self._check_catalog(migration)

    def test_import_essex(self):
        db_path = self.setup_old_database('legacy_essex.sqlite')
        migration = legacy.LegacyMigration('sqlite:///%s' % db_path)
        migration.migrate_all()

        admin_id = 'c93b19ea3fa94484824213db8ac0afce'
        user_ref = self.identity_api.get_user(admin_id)
        self.assertEquals(user_ref['name'], 'admin')
        self.assertEquals(user_ref['enabled'], True)

        # check password hashing
        user_ref = self.identity_man.authenticate(
            user_id=admin_id, password='secrete')

        # check catalog
        self._check_catalog(migration)

    def _check_catalog(self, migration):
        catalog_lines = migration.dump_catalog()
        catalog = catalog_templated.parse_templates(catalog_lines)
        self.assert_('RegionOne' in catalog)
        self.assert_('compute' in catalog['RegionOne'])
        self.assert_('adminURL' in catalog['RegionOne']['compute'])