コード例 #1
0
ファイル: test_lookaside.py プロジェクト: tkdchen/rpkg2
    def test_download_kwargs(self, mock_curl):
        def mock_getinfo(info):
            return 200 if info == pycurl.RESPONSE_CODE else 0

        def mock_perform():
            with open(self.filename, 'rb') as f:
                curlopts[pycurl.WRITEDATA].write(f.read())

        def mock_setopt(opt, value):
            curlopts[opt] = value

        curlopts = {}
        curl = mock_curl.return_value
        curl.getinfo.side_effect = mock_getinfo
        curl.perform.side_effect = mock_perform
        curl.setopt.side_effect = mock_setopt

        with open(self.filename, 'wb') as f:
            f.write(b'content')

        name = 'pyrpkg'
        filename = 'pyrpkg-0.0.tar.xz'
        branch = 'f22'
        hash = hashlib.sha512(b'content').hexdigest()
        outfile = os.path.join(self.workdir, 'pyrpkg-0.0.tar.xz')

        path = '%(name)s/%(filename)s/%(branch)s/%(hashtype)s/%(hash)s'
        full_url = 'http://example.com/%s' % (path % {
            'name': name,
            'filename': filename,
            'branch': branch,
            'hashtype': 'sha512',
            'hash': hash
        })

        lc = CGILookasideCache('sha512', 'http://example.com', '_')

        # Modify the download path, to try arbitrary kwargs
        lc.download_path = path

        lc.download(name,
                    filename,
                    hash,
                    outfile,
                    hashtype='sha512',
                    branch=branch)
        self.assertEqual(curl.perform.call_count, 1)
        self.assertEqual(curlopts[pycurl.URL].decode('utf-8'), full_url)
コード例 #2
0
ファイル: test_lookaside.py プロジェクト: tkdchen/rpkg2
    def test_download(self, mock_curl):
        def mock_getinfo(info):
            return 200 if info == pycurl.RESPONSE_CODE else 0

        def mock_perform():
            with open(self.filename, 'rb') as f:
                curlopts[pycurl.WRITEDATA].write(f.read())

        def mock_setopt(opt, value):
            curlopts[opt] = value

        curlopts = {}
        curl = mock_curl.return_value
        curl.getinfo.side_effect = mock_getinfo
        curl.perform.side_effect = mock_perform
        curl.setopt.side_effect = mock_setopt

        with open(self.filename, 'wb') as f:
            f.write(b'content')

        name = 'pyrpkg'
        filename = 'pyrpkg-0.0.tar.xz'
        hash = hashlib.sha512(b'content').hexdigest()
        outfile = os.path.join(self.workdir, 'pyrpkg-0.0.tar.xz')
        full_url = 'http://example.com/%s/%s/%s/%s' % (name, filename, hash,
                                                       filename)

        lc = CGILookasideCache('sha512', 'http://example.com', '_')
        lc.download(name, filename, hash, outfile, hashtype='sha512')
        self.assertEqual(curl.perform.call_count, 1)
        self.assertEqual(curlopts[pycurl.URL].decode('utf-8'), full_url)
        self.assertEqual(os.path.getmtime(outfile), 0)

        with open(outfile) as f:
            self.assertEqual(f.read(), 'content')

        # Try a second time
        lc.download(name, filename, hash, outfile)
        self.assertEqual(curl.perform.call_count, 1)

        # Try a third time
        os.remove(outfile)
        lc.download(name, filename, hash, outfile)
        self.assertEqual(curl.perform.call_count, 2)