def test_sendEvent_should_use_kwargs_as_event_items(self):
        mock_broadcaster = Mock(WampBroadcaster)
        mock_broadcaster.target = "broadcaster-target"
        mock_broadcaster.logger = Mock()
        mock_broadcaster.client = Mock()

        WampBroadcaster._sendEvent(
            mock_broadcaster,
            "event-id",
            "event-data",
            tracking_id="tracking-id",
            target="target",
            state="foobar",
            bar="baz",
        )

        actual_call = mock_broadcaster.client.publish.call_args
        self.assertEqual(
            call(
                "target",
                {
                    "payload": "event-data",
                    "type": "event",
                    "id": "event-id",
                    "tracking_id": "tracking-id",
                    "target": "target",
                    "state": "foobar",
                    "bar": "baz",
                },
            ),
            actual_call,
        )
Example #2
0
    def test_readFromBackingErrorHandling(self):
        client = Object
        vos_VOFILE = Object()
        vos_VOFILE.URLs = ["url0", "URL1"]
        vos_VOFILE.urlIndex = 0
        vos_VOFILE.open = Mock()
        mock_resp1 = Mock()
        mock_resp1.iter_content = Mock(return_value="bar")
        mock_resp2 = Mock()
        mock_resp2.iter_content = Mock(return_value="foo")
        returns = [mock_resp1, mock_resp2]

        def side_effect(*args, **kwds):
            return returns.pop(0)

        vos_VOFILE.read = MagicMock(side_effect=side_effect)
        vos_VOFILE.close = Mock()
        client.open = Mock(return_value=vos_VOFILE)
        myVofs = Mock()
        myVofs.client = client
        testProxy = vofs.MyIOProxy(myVofs, None)
        testProxy.writeToCache = Mock()
        path = "/dir1/dir2/file"
        cacheFile = Object()
        cacheFile.path = path
        cacheFile.gotHeader = True
        cacheFile.cache = Object()
        testProxy.setCacheFile(cacheFile)
        testProxy.readFromBacking()
    def test_should_not_include_files_outside_config_directory(self):

        mock_svn_service = Mock(SvnService)
        mock_svn_service.config_url = "svn://url/for/configuration/repository"
        mock_svn_service.path_to_config = "/config"
        mock_svn_service.client = Mock()
        mock_info = Mock()

        mock_path_object_1 = Mock()
        mock_path_object_1.path = "/config/foo"
        mock_path_object_1.action = "A"

        mock_path_object_2 = Mock()
        mock_path_object_2.path = "/XXXXXX/bar"
        mock_path_object_2.action = "A"

        mock_path_object_3 = Mock()
        mock_path_object_3.path = "/XXX/foobar"
        mock_path_object_3.action = "A"

        mock_info.changed_paths = [mock_path_object_1, mock_path_object_2, mock_path_object_3]
        mock_svn_service.get_logs_for_revision.return_value = [mock_info]

        actual = SvnService.get_changed_paths_with_action(mock_svn_service, "1980")

        self.assertEqual([("foo", "A")], actual)
    def test_check_connection_should_return_true_when_link_is_up(self):
        mock_broadcaster = Mock(WampBroadcaster)
        mock_broadcaster.logger = Mock()
        mock_broadcaster.url = "ws://broadcaster"
        mock_broadcaster.client = Mock()

        self.assertEqual(WampBroadcaster._check_connection(mock_broadcaster), True)
        self.assertFalse(hasattr(mock_broadcaster, "not_connected_warning_sent"))
Example #5
0
    def test_should_set_the_client_to_none(self, mock_log):
        mock_receiver = Mock(Receiver)
        mock_broadcaster = Mock()
        mock_broadcaster.client = "Test client"
        mock_receiver.broadcaster = mock_broadcaster

        Receiver.onConnectionLost(mock_receiver, "Spam eggs.")

        self.assertEquals(None, mock_broadcaster.client)
    def test_should_raise_exception_when_pysvn_client_fails_to_log(self):

        mock_svn_service = Mock(SvnService)
        mock_svn_service.config_url = "svn://url/for/configuration/repository/config"
        mock_svn_service.base_url = "svn://url/for/configuration/repository"
        mock_svn_service.path_to_config = "/config"
        mock_svn_service.client = Mock()
        mock_svn_service.client.log.side_effect = Exception("Aaarrrgggghh...")

        self.assertRaises(SvnServiceException, SvnService.get_logs_for_revision, mock_svn_service, "1980")
    def test_should_return_logs_for_revision(self):
        mock_svn_service = Mock(SvnService)
        mock_svn_service.base_url = "svn://url/for/configuration/repository"
        mock_svn_service.path_to_config = "/config"
        mock_svn_service.client = Mock()
        mock_logs = Mock()
        mock_svn_service.client.log.return_value = mock_logs

        actual = SvnService.get_logs_for_revision(mock_svn_service, "1980")

        self.assertEqual(mock_logs, actual)
 def test___init__(self):
     parser = common.CliOptions().create_optparser(False)
     common.AuthedCommandsBase.debug = True
     dbaas = Mock()
     dbaas.authenticate = Mock(return_value=None)
     dbaas.client = Mock()
     dbaas.client.auth_token = Mock()
     dbaas.client.service_url = Mock()
     dbaas.client.authenticate_with_token = Mock()
     common.AuthedCommandsBase._get_client = Mock(return_value=dbaas)
     authed_cmd = common.AuthedCommandsBase(parser)
Example #9
0
    def test_attach(self):
        i = BaseIterator(name="test123", priority=111, classname="com.test.Class")

        it_s = i.get_iterator_setting()
        scopes = set([IteratorScope.SCAN, IteratorScope.MINC, IteratorScope.MAJC])

        conn = Mock()
        conn.client = Mock()
        conn.client.attachIterator = Mock()
        conn.login = "Login"

        i.attach(conn, "mytable123", scopes)
        conn.client.attachIterator.assert_called_with("Login", "mytable123", it_s, scopes)
    def test_should_ensure_that_all_returned_host_names_are_ordinary_strings(self):

        mock_svn_service = Mock(SvnService)
        mock_svn_service.config_url = "/path to repository/config"
        mock_svn_service.client = Mock()
        item0 = Mock()
        item0.repos_path = "get_hosts removes the first element - so this will never show up"
        item1 = Mock()
        item1.repos_path = "bar"
        item2 = Mock()
        item2.repos_path = u"spam"
        mock_svn_service.client.list.return_value = [(item0,), (item1,), (item2,)]

        actual_host_names = SvnService.get_hosts(mock_svn_service, 123)

        self.assert_is_ordinary_string(actual_host_names[0])
        self.assert_is_ordinary_string(actual_host_names[1])
Example #11
0
 def testWriteToBacking(self):
     # Submit a write request for the whole file.
     with Cache(TestVOFS.testCacheDir, 100, timeout=1) as testCache:
         client = Object
         client.copy = Mock()
         client.copy = Mock(return_value=12345)
         vofsObj = Mock()
         vofsObj.client = client
         node = Object
         node.uri = "vos:/dir1/dir2/file"
         node.props = {"MD5": 12345}
         vofsObj.get_node = Mock(return_value=node)
         testProxy = vofs.MyIOProxy(vofsObj, None)
         path = "/dir1/dir2/file"
         with FileHandle(path, testCache, testProxy) as testFileHandle:
             testProxy.cacheFile = testFileHandle
             self.assertEqual(testProxy.writeToBacking(), 12345)
         client.copy.assert_called_once_with(testCache.dataDir + "/dir1/dir2/file", node.uri, send_md5=True)
    def test_login(self):
        self.auth.username = "username"
        self.auth.apikey = "apikey"
        self.auth.tenant_id = "tenant_id"
        self.auth.auth_url = "auth_url"
        dbaas = Mock()
        dbaas.authenticate = Mock(return_value=None)
        dbaas.client = Mock()
        dbaas.client.auth_token = Mock()
        dbaas.client.service_url = Mock()
        self.auth._get_client = Mock(return_value=dbaas)
        self.auth.login()

        self.auth.debug = True
        self.auth._get_client = Mock(side_effect=ValueError)
        self.assertRaises(ValueError, self.auth.login)

        self.auth.debug = False
        self.auth.login()
    def test_should_return_list_with_directory_name_and_action_for_path_to_file_when_a_file_has_been_added(self):

        mock_svn_service = Mock(SvnService)
        mock_svn_service.config_url = "svn://url/for/configuration/repository"
        mock_svn_service.path_to_config = "/config"
        mock_svn_service.client = Mock()
        mock_info = Mock()
        mock_path_object_1 = Mock()
        mock_path_object_1.path = "/config/"
        mock_path_object_1.action = "A"
        mock_info.changed_paths = [mock_path_object_1]
        mock_path_object_2 = Mock()
        mock_path_object_2.path = "/config/spam.egg"
        mock_path_object_2.action = "A"
        mock_info.changed_paths = [mock_path_object_1, mock_path_object_2]
        mock_svn_service.get_logs_for_revision.return_value = [mock_info]

        actual = SvnService.get_changed_paths_with_action(mock_svn_service, "1980")

        self.assertEqual([("", "A"), ("spam.egg", "A")], actual)
    def test_sendEvent_should_publish_expected_event_on_default_target(self):
        mock_broadcaster = Mock(WampBroadcaster)
        mock_broadcaster.target = "broadcaster-target"
        mock_broadcaster.logger = Mock()
        mock_broadcaster.client = Mock()

        WampBroadcaster._sendEvent(mock_broadcaster, "event-id", "event-data", tracking_id="tracking-id")

        actual_call = mock_broadcaster.client.publish.call_args
        self.assertEqual(
            call(
                "broadcaster-target",
                {
                    "payload": "event-data",
                    "type": "event",
                    "id": "event-id",
                    "tracking_id": "tracking-id",
                    "target": "broadcaster-target",
                },
            ),
            actual_call,
        )
    def test_should_return_list_with_tuples_including_one_tuple_which_has_a_delete_action(self):

        mock_svn_service = Mock(SvnService)
        mock_svn_service.config_url = "svn://url/for/configuration/repository"
        mock_svn_service.path_to_config = "/config"
        mock_svn_service.client = Mock()
        mock_info = Mock()
        mock_path_object_1 = Mock()
        mock_path_object_1.path = "/config/"
        mock_path_object_1.action = "A"
        mock_info.changed_paths = [mock_path_object_1]
        mock_path_object_2 = Mock()
        mock_path_object_2.path = "/config/spam.egg"
        mock_path_object_2.action = "A"
        mock_path_object_3 = Mock()
        mock_path_object_3.path = "/config/foo.bar"
        mock_path_object_3.action = "D"
        mock_info.changed_paths = [mock_path_object_1, mock_path_object_2, mock_path_object_3]
        mock_svn_service.get_logs_for_revision.return_value = [mock_info]

        actual = SvnService.get_changed_paths_with_action(mock_svn_service, "1980")

        self.assertEqual([("", "A"), ("spam.egg", "A"), ("foo.bar", "D")], actual)
Example #16
0
    def testReadFromBacking(self):
        callCount = [0]

        def mock_read(block_size):
            callCount[0] += 1
            if callCount[0] == 1:
                return "1234"
            else:
                return None

        with Cache(TestVOFS.testCacheDir, 100, timeout=1) as testCache:
            client = Object
            vos_VOFILE = Object()
            vos_VOFILE.URLs = ["url0", "URL1"]
            vos_VOFILE.urlIndex = 0
            vos_VOFILE.open = Mock()
            vos_VOFILE.read = Mock(side_effect=mock_read)
            vos_VOFILE.close = Mock()
            client.open = Mock(return_value=vos_VOFILE)
            path = "/dir1/dir2/file"
            myVofs = Mock()
            myVofs.cacheFile = Mock()
            myVofs.cacheFile.path = path
            myVofs.client = client
            client.getFileInfo = Mock(return_value=(123, "456", 45))
            testProxy = vofs.MyIOProxy(myVofs, None)

            with FileHandle(path, testCache, testProxy) as testFileHandle:
                testProxy.writeToCache = Mock(return_value=4)
                testProxy.cacheFile = testFileHandle
                testProxy.cacheFile.readThread = Mock()
                testProxy.cacheFile.readThread.aborted = False
                try:

                    # Submit a request for the whole file
                    testProxy.readFromBacking()
                    client.open.assert_called_once_with(path, mode=os.O_RDONLY, view="data", size=None, range=None)
                    self.assertEqual(vos_VOFILE.close.call_count, 1)
                    self.assertEqual(vos_VOFILE.read.call_count, 2)

                    # Submit a range request
                    vos_VOFILE.close.reset_mock()
                    vos_VOFILE.read.reset_mock()
                    callCount[0] = 0
                    testProxy.readFromBacking(100, 200)
                    self.assertEqual(client.open.call_count, 1)
                    vos_VOFILE.open.assert_called_once_with("url0", bytes="bytes=200-299")
                    self.assertEqual(vos_VOFILE.close.call_count, 1)
                    self.assertEqual(vos_VOFILE.read.call_count, 2)

                    # Submit a request which gets aborted.
                    vos_VOFILE.open.reset_mock()
                    vos_VOFILE.close.reset_mock()
                    vos_VOFILE.read.reset_mock()
                    callCount[0] = 0
                    testProxy.writeToCache.side_effect = CacheAborted("aborted")
                    testProxy.readFromBacking(150, 200)
                    self.assertEqual(client.open.call_count, 1)
                    vos_VOFILE.open.assert_called_once_with("url0", bytes="bytes=200-349")
                    self.assertEqual(vos_VOFILE.close.call_count, 1)
                    self.assertEqual(vos_VOFILE.read.call_count, 1)

                    # Submit a request with size = None and offset > 0
                    vos_VOFILE.open.reset_mock()
                    vos_VOFILE.close.reset_mock()
                    vos_VOFILE.read.reset_mock()
                    callCount[0] = 0
                    testProxy.readFromBacking(None, 1)
                    self.assertEqual(client.open.call_count, 1)
                    vos_VOFILE.open.assert_called_once_with("url0", bytes="bytes=1-")
                    self.assertEqual(vos_VOFILE.close.call_count, 1)
                    self.assertEqual(vos_VOFILE.read.call_count, 1)

                    # Do a read which fails. This should result in a
                    # renegotiation.
                    vos_VOFILE.open.reset_mock()
                    vos_VOFILE.close.reset_mock()
                    vos_VOFILE.read.reset_mock()
                    callCount[0] = 0
                    self.assertTrue(testProxy.lastVOFile is not None)
                    testProxy.lastVOFile.read = Mock(side_effect=OSError)
                    # This throws an exception because read will be called
                    # twice, the first is caught and error handling occurs, the
                    # second is not caught.
                    with self.assertRaises(OSError):
                        testProxy.readFromBacking(None, 1)
                    self.assertEqual(client.open.call_count, 2)
                    vos_VOFILE.open.assert_called_with("url0", bytes="bytes=1-")
                    self.assertEqual(vos_VOFILE.close.call_count, 1)
                    self.assertEqual(vos_VOFILE.read.call_count, 2)
                    self.assertTrue(type(testProxy.exception) is OSError)
                    testProxy.exception = None

                except Exception as e:
                    print("unexpected exception", e)

                finally:
                    testProxy.cacheFile.readThread = None