コード例 #1
0
    def open(self, url):
        """
        Open a WSDL schema at the specified I{URL}.

        First, the WSDL schema is looked up in the I{object cache}. If not
        found, a new one constructed using the I{fn} factory function and the
        result is cached for the next open().

        @param url: A WSDL URL.
        @type url: str.
        @return: The WSDL object.
        @rtype: I{Definitions}

        """
        cache = self.__cache()
        id = self.mangle(url, "wsdl")
        wsdl = cache.get(id)
        if wsdl is None:
            wsdl = self.fn(url, self.options)
            cache.put(id, wsdl)
        else:
            # Cached WSDL Definitions objects may have been created with
            # different options so we update them here with our current ones.
            wsdl.options = self.options
            for imp in wsdl.imports:
                imp.imported.options = self.options
        return wsdl
コード例 #2
0
 def test_cache_element(self, tmpdir):
     cache_item_id = "unga1"
     cache = virtwho.virt.esx.suds.cache.DocumentCache(tmpdir.strpath)
     assert isinstance(cache, virtwho.virt.esx.suds.cache.FileCache)
     assert cache.get(cache_item_id) is None
     content, document = self.construct_XML()
     cache.put(cache_item_id, document.root())
     self.compare_document_to_content(cache.get(cache_item_id), content)
コード例 #3
0
 def test_item_expiration(self, tmpdir, monkeypatch, duration, current_time,
                          expect_remove):
     """See TestFileCache.item_expiration_test_worker() for more info."""
     cache = virtwho.virt.esx.suds.cache.FileCache(tmpdir.strpath,
                                                   **duration)
     cache.put("unga1", value_p1)
     TestFileCache.item_expiration_test_worker(cache, "unga1", monkeypatch,
                                               current_time, expect_remove)
コード例 #4
0
 def test_item_expiration(self, tmpdir, monkeypatch, duration, current_time,
                          expect_remove):
     """See TestFileCache.item_expiration_test_worker() for more info."""
     cache = virtwho.virt.esx.suds.cache.ObjectCache(
         tmpdir.strpath, **duration)
     cache.put("silly", InvisibleMan(666))
     TestFileCache.item_expiration_test_worker(cache, "silly", monkeypatch,
                                               current_time, expect_remove)
コード例 #5
0
 def test_item_expiration(self, tmpdir, monkeypatch, duration, current_time,
                          expect_remove):
     """See TestFileCache.item_expiration_test_worker() for more info."""
     cache = virtwho.virt.esx.suds.cache.DocumentCache(
         tmpdir.strpath, **duration)
     content, document = self.construct_XML()
     cache.put("willy", document)
     TestFileCache.item_expiration_test_worker(cache, "willy", monkeypatch,
                                               current_time, expect_remove)
コード例 #6
0
 def test_repeated_reads(self, tmpdir):
     cache = virtwho.virt.esx.suds.cache.DocumentCache(tmpdir.strpath)
     content, document = self.construct_XML()
     cache.put("unga1", document)
     read_XML = cache.get("unga1").str()
     assert read_XML == cache.get("unga1").str()
     assert cache.get(None) is None
     assert cache.get("") is None
     assert cache.get("unga2") is None
     assert read_XML == cache.get("unga1").str()
コード例 #7
0
 def test_basic(self, tmpdir):
     cache = virtwho.virt.esx.suds.cache.ObjectCache(tmpdir.strpath)
     assert isinstance(cache, virtwho.virt.esx.suds.cache.FileCache)
     assert cache.get("unga1") is None
     assert cache.get("unga2") is None
     cache.put("unga1", InvisibleMan(1))
     cache.put("unga2", InvisibleMan(2))
     read1 = cache.get("unga1")
     read2 = cache.get("unga2")
     assert read1.__class__ is InvisibleMan
     assert read2.__class__ is InvisibleMan
     assert read1.x == 1
     assert read2.x == 2
コード例 #8
0
    def test_file_open_failure(self, tmpdir, monkeypatch):
        """
        File open failure should cause no cached object to be found, but any
        existing underlying cache file should be kept around.

        """
        mock_open = MockFileOpener(fail_open=True)

        cache_folder = tmpdir.strpath
        cache = virtwho.virt.esx.suds.cache.DocumentCache(cache_folder)
        content1, document1 = self.construct_XML("One")
        content2, document2 = self.construct_XML("Two")
        assert content1 != content2
        cache.put("unga1", document1)

        mock_open.apply(monkeypatch)
        assert cache.get("unga1") is None
        monkeypatch.undo()
        assert mock_open.counter == 1
        _assert_empty_cache_folder(cache_folder, expected=False)
        self.compare_document_to_content(cache.get("unga1"), content1)

        mock_open.apply(monkeypatch)
        assert cache.get("unga2") is None
        monkeypatch.undo()
        assert mock_open.counter == 2
        _assert_empty_cache_folder(cache_folder, expected=False)
        self.compare_document_to_content(cache.get("unga1"), content1)
        assert cache.get("unga2") is None

        cache.put("unga2", document2)
        assert mock_open.counter == 2

        mock_open.apply(monkeypatch)
        assert cache.get("unga1") is None
        monkeypatch.undo()
        assert mock_open.counter == 3
        _assert_empty_cache_folder(cache_folder, expected=False)

        self.compare_document_to_content(cache.get("unga1"), content1)
        self.compare_document_to_content(cache.get("unga2"), content2)
        assert mock_open.counter == 3
コード例 #9
0
def test_NoCache(monkeypatch):
    cache = virtwho.virt.esx.suds.cache.NoCache()
    assert isinstance(cache, virtwho.virt.esx.suds.cache.Cache)

    assert cache.get("id") == None
    cache.put("id", "something")
    assert cache.get("id") == None

    #TODO: It should not be an error to call clear() or purge() on a NoCache
    # instance.
    monkeypatch.delitem(locals(), "e", False)
    e = pytest.raises(Exception, cache.purge, "id").value
    try:
        assert str(e) == "not-implemented"
    finally:
        del e  # explicitly break circular reference chain in Python 3
    e = pytest.raises(Exception, cache.clear).value
    try:
        assert str(e) == "not-implemented"
    finally:
        del e  # explicitly break circular reference chain in Python 3
コード例 #10
0
    def test_version(self, tmpdir):
        fake_version_info = "--- fake version info ---"
        assert virtwho.virt.esx.suds.__version__ != fake_version_info

        version_file = tmpdir.join("version")
        cache_folder = tmpdir.strpath
        cache = virtwho.virt.esx.suds.cache.FileCache(cache_folder)
        assert version_file.read() == virtwho.virt.esx.suds.__version__
        cache.put("unga1", value_p1)

        version_file.write(fake_version_info)
        assert cache.get("unga1") == value_p1

        cache2 = virtwho.virt.esx.suds.cache.FileCache(cache_folder)
        _assert_empty_cache_folder(cache_folder)
        assert cache.get("unga1") is None
        assert cache2.get("unga1") is None
        assert version_file.read() == virtwho.virt.esx.suds.__version__
        cache.put("unga1", value_p11)
        cache.put("unga2", value_p22)

        version_file.remove()
        assert cache.get("unga1") == value_p11
        assert cache.get("unga2") == value_p22

        cache3 = virtwho.virt.esx.suds.cache.FileCache(cache_folder)
        _assert_empty_cache_folder(cache_folder)
        assert cache.get("unga1") is None
        assert cache.get("unga2") is None
        assert cache2.get("unga1") is None
        assert cache3.get("unga1") is None
        assert version_file.read() == virtwho.virt.esx.suds.__version__
コード例 #11
0
    def open(self, url):
        """
        Open an XML document at the specified I{URL}.

        First, a preparsed document is looked up in the I{object cache}. If not
        found, its content is fetched from an external source and parsed using
        the SAX parser. The result is cached for the next open().

        @param url: A document URL.
        @type url: str.
        @return: The specified XML document.
        @rtype: I{Document}

        """
        cache = self.__cache()
        id = self.mangle(url, "document")
        xml = cache.get(id)
        if xml is None:
            xml = self.__fetch(url)
            cache.put(id, xml)
        self.plugins.document.parsed(url=url, document=xml.root())
        return xml
コード例 #12
0
    def test_file_open_failure(self, tmpdir, monkeypatch):
        """
        File open failure should cause no cached object to be found, but any
        existing underlying cache file should be kept around.

        """
        mock_open = MockFileOpener(fail_open=True)

        cache_folder = tmpdir.strpath
        cache = virtwho.virt.esx.suds.cache.ObjectCache(cache_folder)
        cache.put("unga1", InvisibleMan(1))

        mock_open.apply(monkeypatch)
        assert cache.get("unga1") is None
        monkeypatch.undo()
        assert mock_open.counter == 1
        _assert_empty_cache_folder(cache_folder, expected=False)
        assert cache.get("unga1").x == 1

        mock_open.apply(monkeypatch)
        assert cache.get("unga2") is None
        monkeypatch.undo()
        assert mock_open.counter == 2
        _assert_empty_cache_folder(cache_folder, expected=False)
        assert cache.get("unga1").x == 1
        assert cache.get("unga2") is None

        cache.put("unga2", InvisibleMan(2))
        assert mock_open.counter == 2

        mock_open.apply(monkeypatch)
        assert cache.get("unga1") is None
        monkeypatch.undo()
        assert mock_open.counter == 3
        _assert_empty_cache_folder(cache_folder, expected=False)

        assert cache.get("unga1").x == 1
        assert cache.get("unga2").x == 2
        assert mock_open.counter == 3
コード例 #13
0
    def test_independent_item_expirations(self, tmpdir, monkeypatch):
        cache = virtwho.virt.esx.suds.cache.FileCache(tmpdir.strpath, days=1)
        cache.put("unga1", value_p1)
        cache.put("unga2", value_p2)
        cache.put("unga3", value_f2)
        filepath1 = cache._FileCache__filename("unga1")
        filepath2 = cache._FileCache__filename("unga2")
        filepath3 = cache._FileCache__filename("unga3")
        file_timestamp1 = os.path.getctime(filepath1)
        file_timestamp2 = file_timestamp1 + 10 * 60  # in seconds
        file_timestamp3 = file_timestamp1 + 20 * 60  # in seconds
        file_time1 = datetime.datetime.fromtimestamp(file_timestamp1)
        file_time1_expiration = file_time1 + cache.duration

        original_getctime = os.path.getctime

        def mock_getctime(path):
            if path == filepath2:
                return file_timestamp2
            if path == filepath3:
                return file_timestamp3
            return original_getctime(path)

        timedelta = datetime.timedelta

        monkeypatch.setattr(os.path, "getctime", mock_getctime)
        monkeypatch.setattr(datetime, "datetime", MockDateTime)

        MockDateTime.mock_value = file_time1_expiration + timedelta(minutes=15)
        assert cache._getf("unga2") is None
        assert os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert os.path.isfile(filepath3)

        cache._getf("unga3").close()
        assert os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert os.path.isfile(filepath3)

        MockDateTime.mock_value = file_time1_expiration + timedelta(minutes=25)
        assert cache._getf("unga1") is None
        assert not os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert os.path.isfile(filepath3)

        assert cache._getf("unga3") is None
        assert not os.path.isfile(filepath1)
        assert not os.path.isfile(filepath2)
        assert not os.path.isfile(filepath3)
コード例 #14
0
    def test_file_operation_failure(self, tmpdir, monkeypatch, mock,
                                    extra_checks):
        """
        File operation failures such as reading failures or failing to parse
        data read from such a file should cause no cached object to be found
        and the related cache file to be removed.

        """
        cache_folder = tmpdir.strpath
        cache = virtwho.virt.esx.suds.cache.DocumentCache(cache_folder)
        content1, document1 = self.construct_XML("Eins")
        content2, document2 = self.construct_XML("Zwei")
        cache.put("unga1", document1)

        mock.apply(monkeypatch)
        assert cache.get("unga1") is None
        monkeypatch.undo()
        assert mock.counter == 1
        assert extra_checks[0](mock)
        _assert_empty_cache_folder(cache_folder)

        mock.reset()
        assert cache.get("unga1") is None
        cache.put("unga1", document1)
        cache.put("unga2", document2)
        assert mock.counter == 0
        assert extra_checks[1](mock)

        mock.reset()
        mock.apply(monkeypatch)
        assert cache.get("unga1") is None
        monkeypatch.undo()
        assert mock.counter == 1
        assert extra_checks[2](mock)
        _assert_empty_cache_folder(cache_folder, expected=False)

        mock.reset()
        assert cache.get("unga1") is None
        self.compare_document_to_content(cache.get("unga2"), content2)
        assert mock.counter == 0
        assert extra_checks[3](mock)
コード例 #15
0
    def test_file_operation_failure(self, tmpdir, monkeypatch, mock,
                                    extra_checks):
        """
        Open file operation failures such as reading failures or failing to
        unpickle the data read from such a file should cause no cached object
        to be found and the related cache file to be removed.

        """
        cache_folder = tmpdir.strpath
        cache = virtwho.virt.esx.suds.cache.ObjectCache(cache_folder)
        cache.put("unga1", InvisibleMan(1))

        mock.apply(monkeypatch)
        assert cache.get("unga1") is None
        monkeypatch.undo()
        assert mock.counter == 1
        assert extra_checks[0](mock)
        _assert_empty_cache_folder(cache_folder)

        mock.reset()
        assert cache.get("unga1") is None
        cache.put("unga1", InvisibleMan(1))
        cache.put("unga2", InvisibleMan(2))
        assert mock.counter == 0
        assert extra_checks[1](mock)

        mock.reset()
        mock.apply(monkeypatch)
        assert cache.get("unga1") is None
        monkeypatch.undo()
        assert mock.counter == 1
        assert extra_checks[2](mock)
        _assert_empty_cache_folder(cache_folder, expected=False)

        mock.reset()
        assert cache.get("unga1") is None
        assert cache.get("unga2").x == 2
        assert mock.counter == 0
        assert extra_checks[3](mock)
コード例 #16
0
 def test_cached_content_unicode(self, tmpdir):
     cache_folder = tmpdir.strpath
     cache = virtwho.virt.esx.suds.cache.FileCache(cache_folder)
     cache.put("unga1", value_unicode)
     assert cache.get("unga1") == value_unicode
     _assert_empty_cache_folder(cache_folder, expected=False)