2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
5
from pymongo import Connection
6
6
from pymongo.errors import DuplicateKeyError
7
7
from time import time
9
from charmworld.jobs.utils import (
15
from charmworld.testing import (
9
from charmworld.jobs.utils import BadDBConnection
10
from charmworld.jobs.utils import lock
11
from charmworld.jobs.utils import LockHeld
12
from charmworld.testing import MONGO_DATABASE
13
from charmworld.testing import MONGO_URL
14
from charmworld.testing import MongoTestBase
23
17
LOCK_NAME = u'test_lock'
133
127
def test_bad_connection_setup(self):
134
128
# If the DB connection is created without the parameter fsync,
135
# DuplicateKeyError is raised.
136
conn = pymongo.MongoClient(MONGO_URL)
137
self.duplicate_insert(conn, expect_duplicate_key_error=True)
129
# no DuplicateKeyError is raised.
130
conn = Connection(MONGO_URL)
131
self.duplicate_insert(conn, expect_duplicate_key_error=False)
139
133
# A DuplicateKeyError is raised if the parameter fsync is specified.
140
conn = pymongo.MongoClient(MONGO_URL, fsync=True)
134
conn = Connection(MONGO_URL, fsync=True)
141
135
self.duplicate_insert(conn, expect_duplicate_key_error=True)
142
136
# Note that the value of fsync does not matter...
143
conn = pymongo.MongoClient(MONGO_URL, fsync=False)
137
conn = Connection(MONGO_URL, fsync=False)
144
138
self.duplicate_insert(conn, expect_duplicate_key_error=True)
146
# Alternatively, using w=1 (formerly safe=True) ensures also that a
147
# DuplicateKeyError is raised. w=0 inhibits the DuplicateKeyError.
148
conn = pymongo.MongoClient(MONGO_URL, w=0)
140
# Alternatively, using safe=True ensures also that a
141
# DuplicateKeyError is raised.
142
conn = Connection(MONGO_URL, safe=False)
149
143
self.duplicate_insert(conn, expect_duplicate_key_error=False)
150
conn = pymongo.MongoClient(MONGO_URL, w=1)
144
conn = Connection(MONGO_URL, safe=True)
151
145
self.duplicate_insert(conn, expect_duplicate_key_error=True)
153
def test_lock_creation_with_w0(self):
154
# A DB connection that was created with w=0
155
# cannot be used to create a lock. So don't do that.
156
conn = pymongo.MongoClient(MONGO_URL, w=0)
147
def test_lock_creation_without_fsync_or_safe_param(self):
148
# A DB connection that was created without the fsync parameter
149
# or safe=True cannot be used to create a lock.
150
conn = Connection(MONGO_URL)
157
151
db = conn['locks']
158
152
self.assertRaises(BadDBConnection, self.use_lock, db=db)
161
class TestParse(TestCase):
163
def test_parse_branch_parse_failure(self):
164
# If the charm cannot be parsed, an error is raised.
165
repo, commit, series = [
166
u'~charmers/charms/precise/junk/someproject/trunk',
167
u'ja@appflower.com-20120329093714-s2m9e28dwotmijqc',
170
self.assertRaises(ValueError, parse_branch, repo, commit, series)