~bloodearnest/juju-deployer/annotate-branches

« back to all changes in this revision

Viewing changes to deployer/tests/test_charm.py

  • Committer: Kapil Thangavelu
  • Date: 2014-09-29 14:37:20 UTC
  • mfrom: (125.1.3 tvan-fix-git)
  • Revision ID: kapil@canonical.com-20140929143720-3e06t8s0v7r1x9pt
merge tvan-fix-git and incr rev

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
from deployer.charm import Charm
6
6
from deployer.utils import ErrorExit, yaml_dump
7
7
from deployer.vcs import Bzr as BaseBzr
 
8
from deployer.vcs import Git as BaseGit
8
9
from .base import Base
9
10
 
10
11
 
51
52
    branch = update = pull = None
52
53
 
53
54
 
54
 
class CharmTest(Base):
 
55
class BzrCharmTest(Base):
55
56
 
56
57
    def setUp(self):
57
58
        self.repo_path = d = self.mkdir()
120
121
        charm.fetch()
121
122
        self.assertEqual(charm.vcs.get_cur_rev(), '2')
122
123
 
123
 
    def test_store_charm(self):
124
 
        pass
125
 
 
126
124
    charms_vcs_series = [
127
125
        ({"charm": "local:precise/mongodb",
128
126
          "branch": "lp:charms/precise/couchdb"},
151
149
            "scratch", self.repo_path, "precise", params)
152
150
        self.assertRaises(ErrorExit, charm.fetch)
153
151
        self.assertIn('bzr: ERROR: Not a branch: ', self.output.getvalue())
 
152
 
 
153
 
 
154
class Git(BaseGit):
 
155
 
 
156
    def __init__(self, path):
 
157
        super(Git, self).__init__(
 
158
            path, "", logging.getLogger("deployer.repo"))
 
159
 
 
160
    def init(self):
 
161
        self._call(
 
162
            ["git", "init", self.path],
 
163
            "Could not initialize repo at  %(path)s")
 
164
 
 
165
    def write(self, files):
 
166
        for f in files:
 
167
            with open(os.path.join(
 
168
                    self.path, f), 'w') as fh:
 
169
                fh.write(files[f])
 
170
            self._call(
 
171
                ["git", "add", f],
 
172
                "Could not add file %s" % f)
 
173
 
 
174
    def commit(self, msg):
 
175
        self._call(
 
176
            ["git", "commit", "-m", msg],
 
177
            "Could not commit at %(path)s")
 
178
 
 
179
    def revert(self):
 
180
        self._call(
 
181
            ["git", "reset", "--hard"],
 
182
            "Could not revert at %(path)s")
 
183
 
 
184
    def tag(self, name):
 
185
        self._call(
 
186
            ["git", "tag", name],
 
187
            "Could not tag at %(path)s")
 
188
 
 
189
    branch = update = pull = None
 
190
 
 
191
 
 
192
class GitCharmTest(Base):
 
193
 
 
194
    def setUp(self):
 
195
        self.repo_path = d = self.mkdir()
 
196
        self.series_path = os.path.join(d, "precise")
 
197
        os.mkdir(self.series_path)
 
198
        self.output = self.capture_logging(
 
199
            "deployer.charm", level=logging.DEBUG)
 
200
 
 
201
    def setup_vcs_charm(self):
 
202
        self.branch = Git(self.mkdir())
 
203
        self.branch.init()
 
204
        self.branch.write(
 
205
            {'metadata.yaml': yaml_dump({
 
206
                'name': 'couchdb',
 
207
                'summary': 'RESTful document oriented database',
 
208
                'provides': {
 
209
                    'db': {
 
210
                        'interface': 'couchdb'}}}),
 
211
             'revision': '3'})
 
212
        self.branch.commit('initial')
 
213
        self.branch.write({'revision': '4'})
 
214
        self.branch.commit('next')
 
215
        self.branch.tag('v2')
 
216
        self.tagged_revision = self.branch.get_cur_rev()
 
217
        self.branch.write({'revision': '5'})
 
218
        self.branch.commit('next')
 
219
 
 
220
        self.charm_data = {
 
221
            "charm": "couchdb",
 
222
            "build": None,
 
223
            "branch": self.branch.path,
 
224
            "rev": None,
 
225
            "charm_url": None,
 
226
        }
 
227
 
 
228
    def test_vcs_charm(self):
 
229
        self.setup_vcs_charm()
 
230
        params = dict(self.charm_data)
 
231
        charm = Charm.from_service(
 
232
            "scratch", self.repo_path, "precise", params)
 
233
        charm.fetch()
 
234
        self.assertEqual(charm.metadata['name'],  'couchdb')
 
235
        HEAD = charm.vcs.get_cur_rev()
 
236
 
 
237
        self.assertFalse(charm.is_modified())
 
238
        with open(os.path.join(charm.path, 'revision'), 'w') as fh:
 
239
            fh.write('0')
 
240
        self.assertTrue(charm.is_modified())
 
241
        Git(charm.path).revert()
 
242
 
 
243
        charm.rev = None
 
244
        # Update goes to latest with no revision specified
 
245
        charm.update()
 
246
        self.assertEqual(charm.vcs.get_cur_rev(), HEAD)
 
247
 
 
248
    def test_vcs_fetch_with_rev(self):
 
249
        self.setup_vcs_charm()
 
250
        params = dict(self.charm_data)
 
251
        rev2 = self.branch._call(
 
252
            "git rev-parse HEAD~1".split(),
 
253
            self.branch.err_cur_rev,
 
254
        )
 
255
        params['branch'] = '{}@{}'.format(params['branch'], rev2)
 
256
        charm = Charm.from_service(
 
257
            "scratch", self.repo_path, "precise", params)
 
258
        charm.fetch()
 
259
        self.assertEqual(charm.vcs.get_cur_rev(), rev2)
 
260
 
 
261
    def test_vcs_fetch_with_tag(self):
 
262
        self.setup_vcs_charm()
 
263
        params = dict(self.charm_data)
 
264
        params['branch'] = '{}@{}'.format(params['branch'], 'v2')
 
265
        charm = Charm.from_service(
 
266
            "scratch", self.repo_path, "precise", params)
 
267
        charm.fetch()
 
268
        self.assertEqual(charm.vcs.get_cur_rev(), self.tagged_revision)
 
269
 
 
270
    def test_charm_vcs_unknown(self):
 
271
        branch = self.mkdir()
 
272
        params = {
 
273
            'charm': 'couchdb',
 
274
            'branch': "%s" % branch}
 
275
        try:
 
276
            Charm.from_service(
 
277
                "scratch", self.repo_path, "precise", params)
 
278
            self.fail("should have failed, vcs ambigious")
 
279
        except ValueError, e:
 
280
            self.assertIn("Could not determine vcs backend", str(e))