~jcsackett/charmworld/bac-tag-constraints

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
# Copyright 2012, 2013 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

from pyelasticsearch.exceptions import (
    ElasticHttpNotFoundError,
)
from charmworld.models import CharmSource
from charmworld.testing import MongoTestBase
from charmworld.migrations import migrate
from charmworld.migrations.migrate import Versions


def fake():
    pass


class MigrationTestBase(MongoTestBase):

    def setUp(self):
        super(MigrationTestBase, self).setUp()
        self.versions = Versions(migrate.get_migration_path())
        true_configure_logging = migrate.configure_logging
        migrate.configure_logging = fake
        self.addCleanup(
            setattr, migrate,
            'comfigure_logging', true_configure_logging)


class TestMigration010(MigrationTestBase):

    def test_migration(self):
        self.use_index_client()
        source = CharmSource.from_request(self)
        source.save({
            '_id': 'a',
            'config': None,
        })
        source.save({
            '_id': 'b',
            'config': {
                'options': {},
            },
        })
        source.save({
            '_id': 'c',
            'config': {
                'options': {'foo': {'default': 'bar'}},
            },
        })
        source.save({'_id': 'd'})
        # This is not acceptable to Elasticsearch, because config is a list.
        self.db.charms.save({
            '_id': 'e',
            'config': ['a'],
        })
        self.versions.get_exodus('010_remap_options.py')(source, source)
        for charm in source._get_all('a'):
            self.assertEqual({
            '_id': 'a',
            'config': None,
        }, charm)
        for charm in source._get_all('b'):
            self.assertEqual({
                '_id': 'b',
                'config': {'options': []},
        }, charm)
        for charm in source._get_all('c'):
            self.assertEqual({
                '_id': 'c',
                'config': {'options': [{
                    'name': 'foo',
                    'default': 'bar',
                }]}
            }, charm)
        for charm in source._get_all('d'):
            self.assertEqual({'_id': 'd'}, charm)
        with self.assertRaises(ElasticHttpNotFoundError):
            self.index_client.get('e')
        self.assertEqual({'_id': 'e', 'config': ['a']},
                         self.db.charms.find_one({'_id': 'e'}))