aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_db_migrate_versions_015_remove_bad_master_objectid.py
blob: 1ee7be070421a6b2b9c1b9f6289c021dccb71d5b (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# 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 General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

from twisted.trial import unittest
from buildbot.test.util import migration
import sqlalchemy as sa

class Migration(migration.MigrateTestMixin, unittest.TestCase):

    def setUp(self):
        return self.setUpMigrateTest()

    def tearDown(self):
        return self.tearDownMigrateTest()

    def create_tables_thd(self, conn):
        metadata = sa.MetaData()
        self.objects = sa.Table("objects", metadata,
            sa.Column("id", sa.Integer, primary_key=True),
            sa.Column('name', sa.String(128), nullable=False),
            sa.Column('class_name', sa.String(128), nullable=False),
            sa.UniqueConstraint('name', 'class_name', name='object_identity'),
        )
        self.object_state = sa.Table("object_state", metadata,
            sa.Column("objectid", sa.Integer, sa.ForeignKey('objects.id'),
                nullable=False),
            sa.Column("name", sa.String(length=256), nullable=False),
            sa.Column("value_json", sa.Text, nullable=False),
            sa.UniqueConstraint('objectid', 'name', name='name_per_object'),
        )
        self.objects.create(bind=conn)
        self.object_state.create(bind=conn)

    def insert_old_obj(self, conn):
        conn.execute(self.objects.insert(),
                id=21,
                name='master',
                class_name='buildbot.master.BuildMaster')
        conn.execute(self.object_state.insert(),
                objectid=21,
                name='last_processed_change',
                value_json='938')

    def insert_new_objs(self, conn, count):
        for id in range(50, 50+count):
            conn.execute(self.objects.insert(),
                    id=id,
                    name='some_hostname:/base/dir/%d' % id,
                    class_name='BuildMaster')
            # (this id would be referenced from buildrequests, but that table
            # doesn't change)

    def assertObjectState_thd(self, conn, exp_objects=[],
                            exp_object_state=[]):
        tbl = self.objects
        res = conn.execute(tbl.select(order_by=tbl.c.id))
        got_objects = res.fetchall()

        tbl = self.object_state
        res = conn.execute(tbl.select(
            order_by=[tbl.c.objectid, tbl.c.name]))
        got_object_state = res.fetchall()

        self.assertEqual(
                dict(objects=exp_objects, object_state=exp_object_state),
                dict(objects=got_objects, object_state=got_object_state))

    # tests

    def test_no_old_id(self):
        def setup_thd(conn):
            self.create_tables_thd(conn)
            self.insert_new_objs(conn, 2)

        def verify_thd(conn):
            self.assertObjectState_thd(conn, [
                (50, 'some_hostname:/base/dir/50',
                    'buildbot.master.BuildMaster'),
                (51, 'some_hostname:/base/dir/51',
                    'buildbot.master.BuildMaster'),
            ], [])

        return self.do_test_migration(14, 15, setup_thd, verify_thd)

    def test_no_new_id(self):
        def setup_thd(conn):
            self.create_tables_thd(conn)
            self.insert_old_obj(conn)

        def verify_thd(conn):
            self.assertObjectState_thd(conn, [], [])

        return self.do_test_migration(14, 15, setup_thd, verify_thd)

    def test_one_new_id(self):
        def setup_thd(conn):
            self.create_tables_thd(conn)
            self.insert_old_obj(conn)
            self.insert_new_objs(conn, 1)

        def verify_thd(conn):
            self.assertObjectState_thd(conn, [
                (50, 'some_hostname:/base/dir/50',
                    'buildbot.master.BuildMaster'),
            ], [
                (50, 'last_processed_change', '938'),
            ])

        return self.do_test_migration(14, 15, setup_thd, verify_thd)

    def test_two_new_ids(self):
        def setup_thd(conn):
            self.create_tables_thd(conn)
            self.insert_old_obj(conn)
            self.insert_new_objs(conn, 2)

        def verify_thd(conn):
            self.assertObjectState_thd(conn, [
                (50, 'some_hostname:/base/dir/50',
                    'buildbot.master.BuildMaster'),
                (51, 'some_hostname:/base/dir/51',
                    'buildbot.master.BuildMaster'),
            ], [
                # last_processed_change is just deleted
            ])

        return self.do_test_migration(14, 15, setup_thd, verify_thd)