Exemplo n.º 1
0
class AsyncTaskTest(TestCase):
    def setUp(self):
        self.get_redis = patch('celery_once.backends.redis.get_redis')
        self.mocked_redis = self.get_redis.start()

        self.redis = FakeStrictRedis()
        self.mocked_redis.return_value = self.redis

    @patch('delft3dcontainermanager.tasks.call_command')
    def test_delft3dgt_pulse(self, mockCall):
        """
        Assert that de delft3dgt_pulse task
        calls the containersync_sceneupdate() only once.
        """
        delft3dgt_pulse.delay()

        # Set redis key with TTL 100 seconds from now
        # so subsequent tasks won't run
        self.redis.set('qo_delft3dcontainermanager.tasks.delft3dgt_pulse',
                       int(time()) + 100)

        delft3dgt_pulse.delay()
        delft3dgt_pulse.delay()

        mockCall.assert_called_with('containersync_sceneupdate')
        self.assertEqual(mockCall.call_count, 1)

    def tearDown(self):
        self.redis.flushall()
        self.get_redis.stop()
Exemplo n.º 2
0
def main():
    redis = StrictRedis(**config)
    fakeredis = FakeStrictRedis()
    redis.flushall()
    fakeredis.flushall()

    print '[+] FakeRedis BLPOP:'
    now = time()
    popped = fakeredis.blpop('hello_world', 10)
    print '[*] Took %.2f seconds' % (time() - now)
    print '[*] Popped value:', popped
    print

    print '[+] Redis BLPOP:'
    now = time()
    popped = redis.blpop('hello_world', 10)
    print '[*] Took %.2f seconds' % (time() - now)
    print '[*] Popped value:', popped
Exemplo n.º 3
0
def redis_storage():
    redis = FakeStrictRedis()
    yield CircuitRedisStorage(CircuitBreakerState.CLOSED, redis)
    redis.flushall()
Exemplo n.º 4
0
def redis(monkeypatch):
    fake_redis = FakeStrictRedis()
    fake_redis.flushall()
    monkeypatch.setattr("celery_once.backends.redis.Redis.redis", fake_redis)
    return fake_redis
Exemplo n.º 5
0
class ManagementTest(TestCase):
    mock_options = {
        'autospec': True,
    }

    def setUp(self):
        self.scene = Scene.objects.create(
            name='Scene',
            phase=Scene.phases.new
        )
        self.container_1_1 = Container.objects.create(
            scene=self.scene,
            container_type='preprocess',
            docker_id='abcdefg'
        )
        self.container_1_0 = Container.objects.create(
            scene=self.scene,
            container_type='delft3d',
            docker_id=''
        )
        self.container_0_1 = Container.objects.create(
            scene=self.scene,
            container_type='process',
            docker_id='hijklmn'
        )

        # a freshly created Scene
        self.scene_new = Scene.objects.create(
            name='Scene_New',
            phase=Scene.phases.fin
        )
        self.container_1_1_new = Container.objects.create(
            scene=self.scene_new,
            container_type='preprocess',
            desired_state='created',
            docker_state='non-existent',
            docker_id=''
        )
        self.container_1_0_new = Container.objects.create(
            scene=self.scene_new,
            container_type='delft3d',
            desired_state='created',
            docker_state='non-existent',
            docker_id=''
        )
        self.container_0_1_new = Container.objects.create(
            scene=self.scene_new,
            container_type='process',
            desired_state='created',
            docker_state='non-existent',
            docker_id=''
        )

        self.get_redis = patch('celery_once.backends.redis.get_redis')
        self.mocked_redis = self.get_redis.start()

        self.redis = FakeStrictRedis()
        self.mocked_redis.return_value = self.redis

    @patch('delft3dworker.management.commands.'
           'containersync_sceneupdate.Container.update_from_docker_snapshot')
    @patch('delft3dcontainermanager.tasks.Client', **mock_options)
    def test_containersync_sceneupdate(self, mockClient, mockContainerupdate):
        """
        Test match matrix for docker containers and model containers
        TODO: Add test case with timeout error as return_value
        """

        def inspect(container=''):
           return {'Id': container, 'Config': {'Labels': {'type': 'preprocess'}}}

        client = mockClient.return_value
        client.containers.return_value = [{'Id': 'abcdefg', 'Status': 'running',
                                           'Config': {'Labels': {'type': 'preprocess'}}},
                                          {'Id': 'orphan', 'Status': 'running',
                                           'Config': {'Labels': {'type': 'preprocess'}}}]
        client.inspect_container.side_effect = inspect

        out = StringIO()
        call_command('containersync_sceneupdate', stderr=out)

        # Docker container not in database
        self.assertIn(
            'Docker container orphan not found in database!', out.getvalue())
        client.remove_container.assert_called_with(
            container='orphan', force=True)

        # Docker container in database
        self.assertEqual(mockContainerupdate.call_count, 2)
        mockContainerupdate.assert_called_with(
            {'Config': {'Labels': {'type': 'preprocess'}}, 'Id': 'abcdefg'})

    @patch('delft3dworker.management.commands.'
           'containersync_sceneupdate.Container.update_from_docker_snapshot')
    @patch('delft3dcontainermanager.tasks.Client', **mock_options)
    @patch('delft3dworker.management.commands.'
           'containersync_sceneupdate.AsyncResult')
    def test_containersync_scenekill(self, mockAsync, mockClient, mockContainerupdate):
        """
        Test match matrix for docker containers and model containers
        TODO: Add test case with timeout error as return_value
        """

        client = mockClient.return_value
        client.containers.return_value = [{'Id': 'abcdefg', 'Status': 'running',
                                           'Config': {'Labels': {'type': 'notfromhere'}}},
                                          {'Id': 'orphan', 'Status': 'running',
                                           'Config': {'Labels': {'type': 'notfromhere'}}}]

        def getresult():
            return {'status': 'SUCCESS'}

        # Mock celery result
        result = mockAsync.return_value
        result._get_task_meta.side_effect = getresult
        result.result = client.containers.return_value

        out = StringIO()
        call_command('containersync_sceneupdate', stderr=out)

        # Docker container not in database
        assert not client.remove_container.called

    @patch('delft3dworker.management.commands.'
           'containersync_sceneupdate.Scene._local_scan_process')
    def test_scanbucket_command(self, mocklocalscan):
        call_command('scanbucket')

        self.assertEqual(mocklocalscan.call_count, 1)

    def tearDown(self):
        self.redis.flushall()
        self.get_redis.stop()
Exemplo n.º 6
0
class TestStart(_TestOnPost):
    # noinspection PyAttributeOutsideInit
    def before(self):
        super(TestStart, self).before()
        self.redis = FakeStrictRedis()
        self.resource = Start(self.redis)
        self.api.add_route('/start/{game_id}', self.resource)

    def after(self):
        self.redis.flushall()
        del self.redis

    def test_without_userinfo(self):
        body = self.simulate_request('/start/1', decode='utf-8', method='POST')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('run_id', data, 'run_id has been set')
        self.assertIsNone(self.redis.hget('game:1:run', data['run_id']),
                          'redis run hash should be empty')
        self.assertIsNone(self.redis.hget('game:1:start', data['run_id']),
                          'redis start hash should be empty')

    def test_with_uid(self):
        _uid = 25
        query_string = urlencode({
            'uid': _uid,
            'userinfo': 'nothing',
            'hash': '1234'
        })
        body = self.simulate_request('/start/1',
                                     method='POST',
                                     query_string=query_string,
                                     decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('run_id', data, 'run_id has been set')
        self.assertIn('uid', data, 'uid has been set')
        self.assertEquals(_uid, data['uid'], 'use the old uid')
        self.assertEquals(str(data['uid']),
                          self.redis.hget('game:1:run', data['run_id']),
                          'store run_id and uid in redis')
        self.assertIsNone(self.redis.hget('game:1:userinfo', data['uid']),
                          'should not save userinfo')
        self.assertTrue(self.redis.hexists('game:1:start', data['run_id']),
                        'should record start time')

    def test_with_userinfo_only(self):
        query_string = urlencode(
            {'userinfo': json.dumps({
                'a': '1',
                'b': '2'
            })})
        body = self.simulate_request('/start/1',
                                     method='POST',
                                     query_string=query_string,
                                     decode='utf-8')
        self.assertEquals(falcon.HTTP_400, self.srmock.status)
        data = json.loads(body)
        self.assertEquals('Missing parameter', data['title'])

    def test_with_userinfo_and_hash(self):
        _hash = 105
        _userinfo = json.dumps({'field1': u'王思聪', 'field2': '15888888888'})
        query_string = urlencode({'userinfo': _userinfo, 'hash': _hash})
        body = self.simulate_request('/start/1',
                                     method='POST',
                                     query_string=query_string,
                                     decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('run_id', data, 'run_id has been set')
        self.assertIn('uid', data, 'uid has been set')
        self.assertEquals(_hash, data['uid'], 'use the old uid')
        self.assertEquals(str(data['uid']),
                          self.redis.hget('game:1:run', data['run_id']),
                          'store run_id and uid in redis')
        self.assertEquals(_userinfo,
                          self.redis.hget('game:1:userinfo', data['uid']),
                          'store userinfo and uid in redis')
        self.assertTrue(self.redis.hexists('game:1:start', data['run_id']),
                        'should record start time')

    def test_with_userinfo_and_hash_collision(self):
        _hash = 105
        _userinfo = json.dumps({'field1': u'王思聪', 'field2': '15888888888'})
        query_string = urlencode({'userinfo': _userinfo, 'hash': _hash})
        self.redis.hset('game:1:userinfo', _hash, 'something different')
        body = self.simulate_request('/start/1',
                                     method='POST',
                                     query_string=query_string,
                                     decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('run_id', data, 'run_id has been set')
        self.assertIn('uid', data, 'uid has been set')
        self.assertNotEquals(
            _hash, data['uid'],
            'generate a new uid different from original hash')
        self.assertEquals(str(data['uid']),
                          self.redis.hget('game:1:run', data['run_id']),
                          'store run_id and uid in redis')
        self.assertEquals(_userinfo,
                          self.redis.hget('game:1:userinfo', data['uid']),
                          'store userinfo and uid in redis')
        self.assertTrue(self.redis.hexists('game:1:start', data['run_id']),
                        'should record start time')

    def test_with_used_userinfo_and_hash(self):
        _hash = 105
        _userinfo = json.dumps({'field1': u'王思聪', 'field2': '15888888888'})
        query_string = urlencode({'userinfo': _userinfo, 'hash': _hash})
        self.redis.hset('game:1:userinfo', _hash, _userinfo)
        body = self.simulate_request('/start/1',
                                     method='POST',
                                     query_string=query_string,
                                     decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('run_id', data, 'run_id has been set')
        self.assertIn('uid', data, 'uid has been set')
        self.assertEquals(_hash, data['uid'], 'use the original hash as uid')
        self.assertEquals(str(data['uid']),
                          self.redis.hget('game:1:run', data['run_id']),
                          'store run_id and uid in redis')
        self.assertEquals(_userinfo,
                          self.redis.hget('game:1:userinfo', data['uid']),
                          'store userinfo and uid in redis')
        self.assertTrue(self.redis.hexists('game:1:start', data['run_id']),
                        'should record start time')
Exemplo n.º 7
0
class TestEnd(_TestOnPost):
    # noinspection PyAttributeOutsideInit
    def before(self):
        super(TestEnd, self).before()
        self.redis = FakeStrictRedis()
        self.redis.zadd('game:1:scores', 100, 100)
        self.redis.zadd('game:1:scores', 110, 110)
        self.resource = End(self.redis)
        self.api.add_route('/end/{game_id}', self.resource)
        self.api.req_options.auto_parse_form_urlencoded = True

    def after(self):
        self.redis.flushall()
        del self.redis

    def test_without_uid(self):
        _run_id = 'some_random_thing'
        _score = 105
        query_string = urlencode({'run_id': _run_id, 'score': _score})
        body = self.simulate_request('/end/1',
                                     method='POST',
                                     query_string=query_string,
                                     decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('rank', data, 'rank has been set')
        self.assertNotIn('best_score', data, 'no best_score')
        self.assertNotIn('best_rank', data, 'no best_rank')
        self.assertEquals(1, data['rank'], 'rank should be 1')
        self.assertEquals(str(_score), self.redis.hget('game:1:final',
                                                       _run_id))
        self.assertTrue(self.redis.hexists('game:1:end', _run_id),
                        'should record end time')

    def test_with_uid(self):
        _run_id = 'some_random_thing'
        _score = 105
        _uid = 15
        query_string = urlencode({
            'run_id': _run_id,
            'score': _score,
            'uid': _uid
        })
        body = self.simulate_request('/end/1',
                                     method='POST',
                                     query_string=query_string,
                                     decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('rank', data, 'rank has been set')
        self.assertIn('best_score', data, 'best_score has been set')
        self.assertIn('best_rank', data, 'best_rank has been set')
        self.assertEquals(1, data['rank'], 'rank should be 1')
        self.assertEquals(_score, data['best_score'],
                          'score should be set as best score')
        self.assertEquals(1, data['best_rank'], 'best_rank should be 1')
        self.assertEquals(str(_score), self.redis.hget('game:1:final',
                                                       _run_id))
        self.assertTrue(self.redis.hexists('game:1:end', _run_id),
                        'should record end time')

    def test_update_best_records(self):
        _run_id = 'some_random_thing'
        _score = 105
        _uid = 15
        query_string = urlencode({
            'run_id': _run_id,
            'score': _score,
            'uid': _uid
        })
        self.redis.hset('game:1:record:scores', _uid, 100)
        self.redis.hset('game:1:record:ranks', _uid, 2)
        body = self.simulate_request('/end/1',
                                     method='POST',
                                     query_string=query_string,
                                     decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('rank', data, 'rank has been set')
        self.assertIn('best_score', data, 'best_score has been set')
        self.assertIn('best_rank', data, 'best_rank has been set')
        self.assertEquals(1, data['rank'], 'rank should be 1')
        self.assertEquals(_score, data['best_score'],
                          'score should be set as best score')
        self.assertEquals(1, data['best_rank'], 'best_rank should be 1')
        self.assertEquals(str(_score), self.redis.hget('game:1:final',
                                                       _run_id))
        self.assertTrue(self.redis.hexists('game:1:end', _run_id),
                        'should record end time')

    def test_oot_update(self):
        _run_id = 'some_random_thing'
        _score = 105
        _uid = 15
        query_string = urlencode({
            'run_id': _run_id,
            'score': _score,
            'uid': _uid
        })
        self.redis.hset('game:1:record:scores', _uid, 150)
        self.redis.hset('game:1:record:ranks', _uid, 0)
        body = self.simulate_request('/end/1',
                                     method='POST',
                                     query_string=query_string,
                                     decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('rank', data, 'rank has been set')
        self.assertIn('best_score', data, 'best_score has been set')
        self.assertIn('best_rank', data, 'best_rank has been set')
        self.assertEquals(1, data['rank'], 'rank should be 1')
        self.assertEquals(150, data['best_score'], 'score should be 150')
        self.assertEquals(0, data['best_rank'], 'best_rank should be 0')
        self.assertEquals(str(_score), self.redis.hget('game:1:final',
                                                       _run_id))
        self.assertTrue(self.redis.hexists('game:1:end', _run_id),
                        'should record end time')
Exemplo n.º 8
0
class TestStart(_TestOnPost):
    # noinspection PyAttributeOutsideInit
    def before(self):
        super(TestStart, self).before()
        self.redis = FakeStrictRedis()
        self.resource = Start(self.redis)
        self.api.add_route('/start/{game_id}', self.resource)

    def after(self):
        self.redis.flushall()
        del self.redis

    def test_without_userinfo(self):
        body = self.simulate_request('/start/1', decode='utf-8', method='POST')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('run_id', data, 'run_id has been set')
        self.assertIsNone(self.redis.hget('game:1:run', data['run_id']), 'redis run hash should be empty')
        self.assertIsNone(self.redis.hget('game:1:start', data['run_id']), 'redis start hash should be empty')

    def test_with_uid(self):
        _uid = 25
        query_string = urlencode({'uid': _uid, 'userinfo': 'nothing', 'hash': '1234'})
        body = self.simulate_request('/start/1', method='POST', query_string=query_string, decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('run_id', data, 'run_id has been set')
        self.assertIn('uid', data, 'uid has been set')
        self.assertEquals(_uid, data['uid'], 'use the old uid')
        self.assertEquals(str(data['uid']), self.redis.hget('game:1:run', data['run_id']),
                          'store run_id and uid in redis')
        self.assertIsNone(self.redis.hget('game:1:userinfo', data['uid']), 'should not save userinfo')
        self.assertTrue(self.redis.hexists('game:1:start', data['run_id']), 'should record start time')

    def test_with_userinfo_only(self):
        query_string = urlencode({'userinfo': json.dumps({'a': '1', 'b': '2'})})
        body = self.simulate_request('/start/1', method='POST', query_string=query_string, decode='utf-8')
        self.assertEquals(falcon.HTTP_400, self.srmock.status)
        data = json.loads(body)
        self.assertEquals('Missing parameter', data['title'])

    def test_with_userinfo_and_hash(self):
        _hash = 105
        _userinfo = json.dumps({'field1': u'王思聪', 'field2': '15888888888'})
        query_string = urlencode({'userinfo': _userinfo, 'hash': _hash})
        body = self.simulate_request('/start/1', method='POST', query_string=query_string, decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('run_id', data, 'run_id has been set')
        self.assertIn('uid', data, 'uid has been set')
        self.assertEquals(_hash, data['uid'], 'use the old uid')
        self.assertEquals(str(data['uid']), self.redis.hget('game:1:run', data['run_id']),
                          'store run_id and uid in redis')
        self.assertEquals(_userinfo, self.redis.hget('game:1:userinfo', data['uid']), 'store userinfo and uid in redis')
        self.assertTrue(self.redis.hexists('game:1:start', data['run_id']), 'should record start time')

    def test_with_userinfo_and_hash_collision(self):
        _hash = 105
        _userinfo = json.dumps({'field1': u'王思聪', 'field2': '15888888888'})
        query_string = urlencode({'userinfo': _userinfo, 'hash': _hash})
        self.redis.hset('game:1:userinfo', _hash, 'something different')
        body = self.simulate_request('/start/1', method='POST', query_string=query_string, decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('run_id', data, 'run_id has been set')
        self.assertIn('uid', data, 'uid has been set')
        self.assertNotEquals(_hash, data['uid'], 'generate a new uid different from original hash')
        self.assertEquals(str(data['uid']), self.redis.hget('game:1:run', data['run_id']),
                          'store run_id and uid in redis')
        self.assertEquals(_userinfo, self.redis.hget('game:1:userinfo', data['uid']), 'store userinfo and uid in redis')
        self.assertTrue(self.redis.hexists('game:1:start', data['run_id']), 'should record start time')

    def test_with_used_userinfo_and_hash(self):
        _hash = 105
        _userinfo = json.dumps({'field1': u'王思聪', 'field2': '15888888888'})
        query_string = urlencode({'userinfo': _userinfo, 'hash': _hash})
        self.redis.hset('game:1:userinfo', _hash, _userinfo)
        body = self.simulate_request('/start/1', method='POST', query_string=query_string, decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('run_id', data, 'run_id has been set')
        self.assertIn('uid', data, 'uid has been set')
        self.assertEquals(_hash, data['uid'], 'use the original hash as uid')
        self.assertEquals(str(data['uid']), self.redis.hget('game:1:run', data['run_id']),
                          'store run_id and uid in redis')
        self.assertEquals(_userinfo, self.redis.hget('game:1:userinfo', data['uid']), 'store userinfo and uid in redis')
        self.assertTrue(self.redis.hexists('game:1:start', data['run_id']), 'should record start time')
Exemplo n.º 9
0
class TestEnd(_TestOnPost):
    # noinspection PyAttributeOutsideInit
    def before(self):
        super(TestEnd, self).before()
        self.redis = FakeStrictRedis()
        self.redis.zadd('game:1:scores', 100, 100)
        self.redis.zadd('game:1:scores', 110, 110)
        self.resource = End(self.redis)
        self.api.add_route('/end/{game_id}', self.resource)
        self.api.req_options.auto_parse_form_urlencoded = True

    def after(self):
        self.redis.flushall()
        del self.redis

    def test_without_uid(self):
        _run_id = 'some_random_thing'
        _score = 105
        query_string = urlencode({'run_id': _run_id, 'score': _score})
        body = self.simulate_request('/end/1', method='POST', query_string=query_string, decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('rank', data, 'rank has been set')
        self.assertNotIn('best_score', data, 'no best_score')
        self.assertNotIn('best_rank', data, 'no best_rank')
        self.assertEquals(1, data['rank'], 'rank should be 1')
        self.assertEquals(str(_score), self.redis.hget('game:1:final', _run_id))
        self.assertTrue(self.redis.hexists('game:1:end', _run_id), 'should record end time')

    def test_with_uid(self):
        _run_id = 'some_random_thing'
        _score = 105
        _uid = 15
        query_string = urlencode({'run_id': _run_id, 'score': _score, 'uid': _uid})
        body = self.simulate_request('/end/1', method='POST', query_string=query_string, decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('rank', data, 'rank has been set')
        self.assertIn('best_score', data, 'best_score has been set')
        self.assertIn('best_rank', data, 'best_rank has been set')
        self.assertEquals(1, data['rank'], 'rank should be 1')
        self.assertEquals(_score, data['best_score'], 'score should be set as best score')
        self.assertEquals(1, data['best_rank'], 'best_rank should be 1')
        self.assertEquals(str(_score), self.redis.hget('game:1:final', _run_id))
        self.assertTrue(self.redis.hexists('game:1:end', _run_id), 'should record end time')

    def test_update_best_records(self):
        _run_id = 'some_random_thing'
        _score = 105
        _uid = 15
        query_string = urlencode({'run_id': _run_id, 'score': _score, 'uid': _uid})
        self.redis.hset('game:1:record:scores', _uid, 100)
        self.redis.hset('game:1:record:ranks', _uid, 2)
        body = self.simulate_request('/end/1', method='POST', query_string=query_string, decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('rank', data, 'rank has been set')
        self.assertIn('best_score', data, 'best_score has been set')
        self.assertIn('best_rank', data, 'best_rank has been set')
        self.assertEquals(1, data['rank'], 'rank should be 1')
        self.assertEquals(_score, data['best_score'], 'score should be set as best score')
        self.assertEquals(1, data['best_rank'], 'best_rank should be 1')
        self.assertEquals(str(_score), self.redis.hget('game:1:final', _run_id))
        self.assertTrue(self.redis.hexists('game:1:end', _run_id), 'should record end time')

    def test_oot_update(self):
        _run_id = 'some_random_thing'
        _score = 105
        _uid = 15
        query_string = urlencode({'run_id': _run_id, 'score': _score, 'uid': _uid})
        self.redis.hset('game:1:record:scores', _uid, 150)
        self.redis.hset('game:1:record:ranks', _uid, 0)
        body = self.simulate_request('/end/1', method='POST', query_string=query_string, decode='utf-8')
        self.assertEquals(falcon.HTTP_200, self.srmock.status)
        data = json.loads(body)
        self.assertIn('rank', data, 'rank has been set')
        self.assertIn('best_score', data, 'best_score has been set')
        self.assertIn('best_rank', data, 'best_rank has been set')
        self.assertEquals(1, data['rank'], 'rank should be 1')
        self.assertEquals(150, data['best_score'], 'score should be 150')
        self.assertEquals(0, data['best_rank'], 'best_rank should be 0')
        self.assertEquals(str(_score), self.redis.hget('game:1:final', _run_id))
        self.assertTrue(self.redis.hexists('game:1:end', _run_id), 'should record end time')
Exemplo n.º 10
0
class RedisworksTestCase(unittest.TestCase):

    """RedisWorks Tests."""
    def setUp(self):
        self.root = Root(redis=FakeStrictRedis)
        self.red = FakeStrictRedis()

    def tearDown(self):
        # Clear data in fakeredis.
        self.red.flushall()

    def test_nested_format(self):
        value = {1: 1, 2: {"a": "hello"}}
        result = Root.doformat(value)
        int_str = bTYPE_IDENTIFIER + b'num' + bITEM_DIVIDER + b'int' + bITEM_DIVIDER
        expected_result = {int_str + b'1': int_str + b'1',
                           int_str + b'2': bTYPE_IDENTIFIER +
                           b'dict' + bITEM_DIVIDER +
                           b'dict' + bITEM_DIVIDER + b'{"a": "hello"}'}
        self.assertEqual(result, expected_result)

    def test_numbers(self):
        today = datetime.date.today()
        now = datetime.datetime.utcnow()
        items = (10, 10.1, Decimal("10"), 10+1j, today, now)

        for val in items:
            self.root.part = val
            result = self.red.get('root.part')
            expected_result = Root.doformat(val)
            self.assertEqual(result, expected_result)
            # flushing dotobject local cache
            self.root.flush()
            self.assertEqual(self.root.part, val)

    def test_return_string(self):
        root2 = Root(redis=FakeStrictRedis, return_object=False)
        val = 11.1
        expected_result = b'11.1'
        root2.part = val
        # flushing dotobject local cache
        root2.flush()
        self.assertEqual(root2.part, expected_result)

    def test_grandchild(self):
        string = "for real?"
        self.root.haha.wahaha = string
        result = self.red.get('root.haha.wahaha')
        expected_result = Root.doformat(string)
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.haha.wahaha, string)
        self.assertEqual(self.root['haha.wahaha'], string)

    def test_child_set(self):
        value = {1, 2, 4}
        expected_result = set(Root.doformat(value))
        self.root.part_set = value
        result = self.red.smembers('root.part_set')
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.part_set, value)

    def test_child_dict(self):
        value = {1: 1, 2: 2, 3: 4}
        expected_result = Root.doformat(value)
        self.root.part_dic = value
        result = self.red.hgetall('root.part_dic')
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.part_dic, value)

    def test_child_nested_dict(self):
        value = {1: 1, 2: {"a": "hello"}, 3: 4}
        expected_result = Root.doformat(value)
        self.root.part = value
        result = self.red.hgetall('root.part')
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.part, value)

    def test_child_iterable(self):
        value = [1, 3, "a"]
        expected_result = Root.doformat(value)
        self.root.part = value
        result = self.red.lrange("root.part", 0, -1)
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.part, value)

    def test_child_nested_iterable(self):
        value = [1, 3, ["a", 3]]
        expected_result = Root.doformat(value)
        self.root.part = value
        result = self.red.lrange("root.part", 0, -1)
        self.assertEqual(result, expected_result)
        self.root.flush()
        self.assertEqual(self.root.part, value)

    def test_many_different_children_types(self):
        set_value = {1, 2, 4}
        self.root.part_set = set_value
        dict_value = {1: 1, 2: {"a": 1}}
        self.root.part_dict = dict_value
        list_value = [1, ["b", 3]]
        self.root.part_list = list_value
        self.root.flush()
        self.assertEqual(self.root.part_set, set_value)
        self.assertEqual(self.root.part_dict, dict_value)
        self.assertEqual(self.root.part_list, list_value)

    def test_many_different_children_types2(self):
        mylist = [1, 3, 4]
        self.root.my.list = mylist
        some_date = datetime.datetime(2016, 8, 22, 10, 3, 19)
        self.root.time = some_date
        mydict = {1: 1, "a": "b"}
        self.root.the.mapping.example = mydict
        self.root.flush()
        self.assertEqual(self.root.my.list, mylist)
        self.assertEqual(self.root.time, some_date)
        self.assertEqual(self.root.the.mapping.example, mydict)

    def test_change_key_type(self):
        mylist = [1, 3, 4]
        self.root.something = mylist
        st = "string"
        self.root.something = st
        self.assertEqual(self.root.something, st)

    def test_number_comparison(self):
        self.root.num = 10
        num = self.root.num
        self.assertIsInstance(num, LazyDot)
        self.assertTrue(num > 8)
        self.assertTrue(num < 11)
        self.assertTrue(num <= 12)
        self.assertTrue(num >= 10)
        self.assertFalse(num > 10)

    def test_number_math(self):
        self.root.num = 10
        num = self.root.num
        self.assertIsInstance(num, LazyDot)
        self.assertEqual(num * 2, 20)

    def test_saving_set_removed_the_old_one(self):
        self.root.myset = {1, 2, 3}
        self.root.myset = {4, 5}
        self.root.flush()
        self.assertEqual(self.root.myset, {4, 5})

    def test_saving_list_removed_the_old_one(self):
        self.root.myset = [1, 2, 3]
        self.root.myset = [4, 5]
        self.root.flush()
        self.assertEqual(self.root.myset, [4, 5])
Exemplo n.º 11
0
def redis(monkeypatch):
    fake_redis = FakeStrictRedis()
    fake_redis.flushall()
    monkeypatch.setattr("celery_once.tasks.QueueOnce.redis", fake_redis)
    return fake_redis
Exemplo n.º 12
0
class TaskTest(TestCase):
    mock_options = {
        'autospec': True,
    }

    def setUp(self):
        self.get_redis = patch('celery_once.backends.redis.get_redis')
        self.mocked_redis = self.get_redis.start()

        self.redis = FakeStrictRedis()
        self.mocked_redis.return_value = self.redis

    @patch('delft3dcontainermanager.tasks.call_command')
    def test_delft3dgt_pulse(self, mockCall):
        """
        Assert that de delft3dgt_pulse task
        calls the containersync_sceneupdate() function.
        """

        delft3dgt_pulse.delay()
        mockCall.assert_called_with('containersync_sceneupdate')

    @patch('delft3dcontainermanager.tasks.Client', **mock_options)
    @patch('delft3dcontainermanager.tasks.logging.error', **mock_options)
    def test_get_docker_ps(self, mockLogging, mockClient):
        """
        Assert that the docker_ps task
        calls the docker client.containers() function.
        """

        containers = [
            {
                'Id': 'Aaa',
                'Status': 'Running'
            },
            {
                'Id': 'Bbb',
                'Status': 'Host Down'
            },
            {
                'Id': 'Ccc',
                'Status': 'Up'
            },
        ]

        def inspect(container=''):
            if container == 'Ccc':
                raise
            else:
                return {
                    'Id': container,
                    'Config': {
                        'Labels': {
                            'type': 'preprocess'
                        }
                    }
                }

        mockClient.return_value.containers.return_value = containers
        mockClient.return_value.inspect_container.side_effect = inspect

        get_docker_ps.delay()
        # Call docker ps for all containers, but only once
        mockClient.return_value.containers.assert_called_with(all=True)
        self.assertEqual(mockClient.return_value.containers.call_count, 1)
        # Call inspect for all but Host Down container
        self.assertEqual(mockClient.return_value.inspect_container.call_count,
                         2)
        # Log error only for Ccc container
        self.assertEqual(mockLogging.call_count, 1)

    @patch('delft3dcontainermanager.tasks.Client', **mock_options)
    def test_get_docker_log(self, mockClient):
        """
        Assert that the docker_log task
        calls the docker client.logs() function.
        """
        get_docker_log.delay("id", stdout=False, stderr=True)
        mockClient.return_value.logs.assert_called_with(container="id",
                                                        stdout=False,
                                                        stderr=True,
                                                        stream=False,
                                                        timestamps=True,
                                                        tail=5)

    @patch('delft3dcontainermanager.tasks.Client', **mock_options)
    def test_do_docker_create(self, mockClient):
        """
        Assert that the docker_create task
        calls the docker client.create_container() function.
        """
        image = "IMAGENAME"
        volumes = ['/:/data/output:z', '/:/data/input:ro']
        memory_limit = '1g'
        command = "echo test"
        config = {}
        environment = {'a': 1, 'b': 2}
        label = {"type": "delft3d"}
        folder = ['input', 'output']
        name = 'test-8172318273'
        workingdir = os.path.join(os.getcwd(), 'test')
        folders = [os.path.join(workingdir, f) for f in folder]
        parameters = {u'test': {u'1': u'a', u'2': u'b', 'units': 'ignoreme'}}
        mockClient.return_value.create_host_config.return_value = config

        do_docker_create.delay(label, parameters, environment, name, image,
                               volumes, memory_limit, folders, command)

        # Assert that docker is called
        mockClient.return_value.create_container.assert_called_with(
            image,
            host_config=config,
            command=command,
            name=name,
            environment=environment,
            labels=label)

        # Assert that folders are created
        listdir = os.listdir(workingdir)
        for f in listdir:
            self.assertIn(f, listdir)

        for folder in folders:
            ini = os.path.join(folder, 'input.ini')
            self.assertTrue(os.path.isfile(ini))

            config = configparser.SafeConfigParser()
            config.readfp(open(ini))
            for key in parameters.keys():
                self.assertTrue(config.has_section(key))
                for option, value in parameters[key].items():
                    if option != 'units':
                        self.assertTrue(config.has_option(key, option))
                        self.assertEqual(config.get(key, option), value)
                    else:  # units should be ignored
                        self.assertFalse(config.has_option(key, option))

    @patch('delft3dcontainermanager.tasks.Client', **mock_options)
    def test_do_docker_start(self, mockClient):
        """
        Assert that the docker_start task
        calls the docker client.start() function
        """
        do_docker_start.delay("id")
        mockClient.return_value.start.assert_called_with(container="id")

    @patch('delft3dcontainermanager.tasks.Client', **mock_options)
    def test_do_docker_stop(self, mockClient):
        """
        Assert that the docker_stop task
        calls the docker client.stop() function
        """
        do_docker_stop.delay("id", timeout=5)
        mockClient.return_value.stop.assert_called_with(container="id",
                                                        timeout=5)

    @patch('delft3dcontainermanager.tasks.Client', **mock_options)
    def test_do_docker_remove(self, mockClient):
        """
        Assert that the docker_remove task
        calls the docker client.remove_container() function
        """
        delay = do_docker_remove.delay("id")
        mockClient.return_value.remove_container.assert_called_with(
            container="id", force=False)
        container, log = delay.result
        self.assertEqual(container, "id")
        self.assertEqual(log, "")

    def test_do_docker_sync_filesystem(self):
        """
        TODO: write test
        """
        delay = do_docker_sync_filesystem.delay("id")
        container, log = delay.result
        self.assertEqual(container, "id")
        self.assertEqual(log, "")

    def tearDown(self):
        self.redis.flushall()
        self.get_redis.stop()