1
1
# Copyright 2012, 2013 Canonical Ltd. This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
from charmworld.models import (
7
4
from charmworld.testing import (
12
9
from charmworld.migrations import migrate
13
from charmworld.migrations.migrate import Versions
10
from charmworld.migrations.migrate import (
15
from charmworld.migrations.versions.tests.data import migration_027 as mdata
17
from charmworld.models import CharmSource
19
from pyelasticsearch.exceptions import ElasticHttpError
126
132
def test_exodus_restrictions(self):
127
133
"""Ensure that deploy-from-scratch does not violate exodus rules."""
128
134
self.versions._check_exodus(self.versions.list_pending(0))
137
class TestUseNgrams027(MigrationTestBase):
140
module_name = '027_use_ngrams.py'
143
super(TestUseNgrams027, self).setUp()
144
self.use_index_client()
145
self.data_store = DataStore(self.db)
146
self.charm_source = CharmSource(self.db, self.index_client)
148
def exists_in_index(self, id_):
149
return self.index_client.get(id_) is not None
151
def sync_index(self):
152
charm_ids = self.charm_source.sync_index()
156
except StopIteration:
159
def put_old_mapping(self):
160
# Delete the temp_index.
161
self.index_client.delete_index()
162
# Create an empty index.
163
self.index_client._client.create_index(self.index_client.index_name)
164
# Put up the old mappings.
165
for type_ in ('charm', 'bundle'):
166
self.index_client._client.put_mapping(
167
self.index_client.index_name,
169
getattr(mdata, 'old_{}_mapping'.format(type_)))
171
def test_index_updated(self):
172
"""Ensure that running upgrade reindexes charms."""
173
self.put_old_mapping()
174
self.versions.ensure_initialized(self.data_store, True)
175
## Pretend upgrades through 26 are complete.
176
self.data_store.update_version(26)
177
charm = factory.get_charm_json()
178
self.db.charms.insert(charm)
179
self.assertFalse(self.exists_in_index(charm['_id']))
181
self.assertTrue(self.exists_in_index(charm['_id']))
182
self.versions.upgrade(self.data_store, self.index_client, True)
183
self.assertEquals(self.data_store.current_version, self.version)
184
self.assertTrue(self.exists_in_index(charm['_id']))
186
def test_exception_raised(self):
187
"""Putting new mapping without creating the filter and analyzer
188
raises the expected exception.
190
self.versions.ensure_initialized(self.data_store, True)
191
## Pretend upgrades through 26 are complete.
192
self.data_store.update_version(26)
193
self.put_old_mapping()
194
charm = factory.get_charm_json()
195
self.assertFalse(self.exists_in_index(charm['_id']))
196
self.index_client.index_charm(charm)
197
self.assertTrue(self.exists_in_index(charm['_id']))
198
with self.assertRaises(ElasticHttpError) as e:
199
self.index_client.put_mapping()
200
self.assertEquals(e.status_code, 400)
201
self.assertEquals(e.error,
202
u'MapperParsingException[Analyzer [n3_20grams] '
203
u'not found for field [ngrams]]')