Ejemplo n.º 1
0
    def test_download_headers(self):
        request = Mock(uri='http://content-world.com/content/bear.rpm',
                       headers={})
        request.setHeader.side_effect = request.headers.__setitem__
        report = DownloadReport('', '')
        report.headers = {
            'A': 1,
            'B': 2,
        }

        # should be ignored.
        report.headers.update({k: '' for k in HOP_BY_HOP_HEADERS})

        config = Mock(properties={'streamer': {'cache_timeout': 100}})

        def get(s, p):
            return config.properties[s][p]

        config.get.side_effect = get
        streamer = Mock(config=config)

        # test
        listener = DownloadListener(streamer, request)
        listener.download_headers(report)

        # validation
        self.assertEqual(request.headers, {
            'Cache-Control': 'public, s-maxage=100, max-age=100',
            'A': 1,
            'B': 2,
        })
Ejemplo n.º 2
0
    def test_get_single_path_failure(self, mock_download_one):
        report = DownloadReport('http://pulpproject.org/v1/repositories/pulp/crane/images',
                                StringIO(''))
        report.headers = {}
        report.state = report.DOWNLOAD_FAILED
        mock_download_one.return_value = report

        self.assertRaises(IOError, self.repo._get_single_path, '/v1/repositories/pulp/crane/images')
Ejemplo n.º 3
0
    def test_get_single_path_failure(self, mock_download_one):
        report = DownloadReport('http://pulpproject.org/v1/repositories/pulp/crane/images',
                                StringIO(''))
        report.headers = {}
        report.state = report.DOWNLOAD_FAILED
        mock_download_one.return_value = report

        self.assertRaises(IOError, self.repo._get_single_path, '/v1/repositories/pulp/crane/images')
Ejemplo n.º 4
0
 def download_one(request):
     """
     Mock the download_one() method to manipulate the path.
     """
     self.assertEqual(request.url, 'https://registry.example.com/v2/pulp/tags/list')
     self.assertEqual(type(request.destination), type(StringIO()))
     report = DownloadReport(request.url, request.destination)
     report.download_succeeded()
     report.headers = {}
     report.destination.write('{"name": "pulp", "tags": ["best_ever", "latest", "decent"]}')
     return report
Ejemplo n.º 5
0
 def download_one(request):
     """
     Mock the download_one() method to manipulate the path.
     """
     self.assertEqual(request.url, 'https://registry.example.com/v2/')
     self.assertEqual(type(request.destination), type(StringIO()))
     report = DownloadReport(request.url, request.destination)
     report.download_succeeded()
     report.headers = {'Docker-Distribution-API-Version': 'registry/2.0'}
     report.destination.write("")
     return report
Ejemplo n.º 6
0
 def download_one(request):
     """
     Mock the download_one() method.
     """
     self.assertEqual(request.url, 'https://registry.example.com/some/path')
     self.assertEqual(type(request.destination), type(StringIO()))
     report = DownloadReport(request.url, request.destination)
     report.download_succeeded()
     report.headers = {'some': 'cool stuff'}
     report.destination.write("This is the stuff you've been waiting for.")
     return report
Ejemplo n.º 7
0
 def download_one(request):
     """
     Mock the download_one() method.
     """
     self.assertEqual(request.url, 'https://registry.example.com/some/path')
     self.assertEqual(type(request.destination), type(StringIO()))
     report = DownloadReport(request.url, request.destination)
     report.download_succeeded()
     report.headers = {'some': 'cool stuff'}
     report.destination.write("This is the stuff you've been waiting for.")
     return report
Ejemplo n.º 8
0
 def download_one(request):
     """
     Mock the download_one() method to manipulate the path.
     """
     self.assertEqual(request.url, 'https://registry.example.com/v2/')
     self.assertEqual(type(request.destination), type(StringIO()))
     report = DownloadReport(request.url, request.destination)
     report.download_succeeded()
     report.headers = {'Docker-Distribution-API-Version': 'registry/2.0'}
     report.destination.write("")
     return report
Ejemplo n.º 9
0
 def download_one(request):
     """
     Mock the download_one() method to manipulate the path.
     """
     self.assertEqual(request.url, 'https://registry.example.com/v2/pulp/tags/list')
     self.assertEqual(type(request.destination), type(StringIO()))
     report = DownloadReport(request.url, request.destination)
     report.download_succeeded()
     report.headers = {}
     report.destination.write('{"name": "pulp", "tags": ["best_ever", "latest", "decent"]}')
     return report
Ejemplo n.º 10
0
 def download_one(request):
     """
     Mock the download_one() method to manipulate the path.
     """
     self.assertEqual(request.url, 'https://registry.example.com/v2/')
     self.assertEqual(type(request.destination), type(StringIO()))
     report = DownloadReport(request.url, request.destination)
     report.download_succeeded()
     # The Version header is not present
     report.headers = {}
     report.destination.write("")
     return report
Ejemplo n.º 11
0
 def download_one(request):
     """
     Mock the download_one() method to manipulate the path.
     """
     self.assertEqual(request.url, 'https://registry.example.com/v2/')
     self.assertEqual(type(request.destination), type(StringIO()))
     report = DownloadReport(request.url, request.destination)
     report.download_succeeded()
     # The Version header is not present
     report.headers = {}
     report.destination.write("")
     return report
Ejemplo n.º 12
0
    def test_get_tags(self, mock_download_one):
        body = json.dumps({'latest': 'abc123'})
        report = DownloadReport('http://pulpproject.org/v1/repositories/pulp/crane/tags',
                                StringIO(body))
        report.headers = {}
        mock_download_one.return_value = report

        ret = self.repo._get_single_path('/v1/repositories/pulp/crane/tags')

        self.assertEqual(ret, {'latest': 'abc123'})
        self.assertEqual(mock_download_one.call_count, 1)
        self.assertTrue(isinstance(mock_download_one.call_args[0][0], DownloadRequest))
        req = mock_download_one.call_args[0][0]
        self.assertEqual(req.url, 'http://pulpproject.org/v1/repositories/pulp/crane/tags')
Ejemplo n.º 13
0
    def test_get_with_headers(self, mock_download_one):
        body = json.dumps(['abc123'])
        report = DownloadReport('http://pulpproject.org/v1/repositories/pulp/crane/images',
                                StringIO(body))
        report.headers = {
            self.repo.DOCKER_TOKEN_HEADER: 'token',
            self.repo.DOCKER_ENDPOINT_HEADER: 'endpoint',
        }
        mock_download_one.return_value = report

        self.repo._get_single_path('/v1/repositories/pulp/crane/images')

        self.assertEqual(self.repo.token, 'token')
        self.assertEqual(self.repo.endpoint, 'endpoint')
Ejemplo n.º 14
0
    def test_get_tags(self, mock_download_one):
        body = json.dumps({'latest': 'abc123'})
        report = DownloadReport('http://pulpproject.org/v1/repositories/pulp/crane/tags',
                                StringIO(body))
        report.headers = {}
        mock_download_one.return_value = report

        ret = self.repo._get_single_path('/v1/repositories/pulp/crane/tags')

        self.assertEqual(ret, {'latest': 'abc123'})
        self.assertEqual(mock_download_one.call_count, 1)
        self.assertTrue(isinstance(mock_download_one.call_args[0][0], DownloadRequest))
        req = mock_download_one.call_args[0][0]
        self.assertEqual(req.url, 'http://pulpproject.org/v1/repositories/pulp/crane/tags')
Ejemplo n.º 15
0
    def test_get_with_headers(self, mock_download_one):
        body = json.dumps(['abc123'])
        report = DownloadReport('http://pulpproject.org/v1/repositories/pulp/crane/images',
                                StringIO(body))
        report.headers = {
            self.repo.DOCKER_TOKEN_HEADER: 'token',
            self.repo.DOCKER_ENDPOINT_HEADER: 'endpoint',
        }
        mock_download_one.return_value = report

        self.repo._get_single_path('/v1/repositories/pulp/crane/images')

        self.assertEqual(self.repo.token, 'token')
        self.assertEqual(self.repo.endpoint, 'endpoint')
Ejemplo n.º 16
0
 def download_one(request):
     """
     Mock the download_one() method to manipulate the path.
     """
     self.assertEqual(request.url,
                      'https://registry.example.com/v2/pulp/manifests/best_version_ever')
     self.assertEqual(type(request.destination), type(StringIO()))
     report = DownloadReport(request.url, request.destination)
     report.download_succeeded()
     schema2 = 'application/vnd.docker.distribution.manifest.v2+json'
     report.headers = {'Docker-Distribution-API-Version': 'registry/2.0',
                       'docker-content-digest': digest,
                       'content-type': schema2}
     report.destination.write(manifest)
     return report
Ejemplo n.º 17
0
    def test_get_images(self, mock_download_one):
        body = json.dumps(['abc123'])
        report = DownloadReport('http://pulpproject.org/v1/repositories/pulp/crane/images',
                                StringIO(body))
        report.headers = {}
        mock_download_one.return_value = report

        ret = self.repo._get_single_path('/v1/repositories/pulp/crane/images')

        self.assertEqual(ret, ['abc123'])
        self.assertEqual(mock_download_one.call_count, 1)
        self.assertTrue(isinstance(mock_download_one.call_args[0][0], DownloadRequest))
        req = mock_download_one.call_args[0][0]
        self.assertEqual(req.url, 'http://pulpproject.org/v1/repositories/pulp/crane/images')
        # make sure this header is set, which is required by the docker API in order
        # to give us an auth token
        self.assertEqual(req.headers[self.repo.DOCKER_TOKEN_HEADER], 'true')
Ejemplo n.º 18
0
    def test_get_images(self, mock_download_one):
        body = json.dumps(['abc123'])
        report = DownloadReport('http://pulpproject.org/v1/repositories/pulp/crane/images',
                                StringIO(body))
        report.headers = {}
        mock_download_one.return_value = report

        ret = self.repo._get_single_path('/v1/repositories/pulp/crane/images')

        self.assertEqual(ret, ['abc123'])
        self.assertEqual(mock_download_one.call_count, 1)
        self.assertTrue(isinstance(mock_download_one.call_args[0][0], DownloadRequest))
        req = mock_download_one.call_args[0][0]
        self.assertEqual(req.url, 'http://pulpproject.org/v1/repositories/pulp/crane/images')
        # make sure this header is set, which is required by the docker API in order
        # to give us an auth token
        self.assertEqual(req.headers[self.repo.DOCKER_TOKEN_HEADER], 'true')
Ejemplo n.º 19
0
    def test__get_path_failed(self, mock_download_one, mock_request_token):
        """
        Test _get_path() for the case when an IOError is raised by the downloader.
        """
        name = 'pulp'
        download_config = DownloaderConfig(max_concurrent=25)
        registry_url = 'https://registry.example.com'
        working_dir = '/a/working/dir'
        r = registry.V2Repository(name, download_config, registry_url, working_dir)

        report = DownloadReport(registry_url + '/some/path', StringIO())
        report.error_report['response_code'] = httplib.UNAUTHORIZED
        report.state = DownloadReport.DOWNLOAD_FAILED
        report.headers = {}
        mock_download_one.return_value = report

        # The request will fail because the requested path does not exist
        self.assertRaises(IOError, r._get_path, '/some/path')
Ejemplo n.º 20
0
    def test_on_succeeded_pulp_requested(self, _insert_deferred):
        entry = Mock(url='url-a')
        request = Mock(uri='http://content-world.com/content/bear.rpm')
        request.getHeader.side_effect = {
            constants.PULP_STREAM_REQUEST_HEADER: True
        }.__getitem__
        report = DownloadReport('', '')
        report.headers = {
            'A': 1,
            'B': 2,
        }

        # test
        streamer = Streamer(Mock())
        streamer._on_succeeded(entry, request, report)

        # validation
        self.assertFalse(_insert_deferred.called)
Ejemplo n.º 21
0
    def test_get_tags_from_endpoint(self, mock_download_one):
        body = json.dumps({'latest': 'abc123'})
        report = DownloadReport('http://some-endpoint.org/v1/repositories/pulp/crane/tags',
                                StringIO(body))
        report.headers = {}
        mock_download_one.return_value = report
        self.repo.endpoint = 'some-endpoint.org'
        # this lets us test that auth was added to the request
        self.repo.token = 'letmein'

        ret = self.repo._get_single_path('/v1/repositories/pulp/crane/tags')

        self.assertEqual(ret, {'latest': 'abc123'})
        self.assertEqual(mock_download_one.call_count, 1)
        self.assertTrue(isinstance(mock_download_one.call_args[0][0], DownloadRequest))
        req = mock_download_one.call_args[0][0]
        self.assertEqual(req.url, 'http://some-endpoint.org/v1/repositories/pulp/crane/tags')
        # make sure the authorization was added, which is usually required by an endpoint
        self.assertTrue('Authorization' in req.headers)
Ejemplo n.º 22
0
    def test_get_tags_from_endpoint(self, mock_download_one):
        body = json.dumps({'latest': 'abc123'})
        report = DownloadReport('http://some-endpoint.org/v1/repositories/pulp/crane/tags',
                                StringIO(body))
        report.headers = {}
        mock_download_one.return_value = report
        self.repo.endpoint = 'some-endpoint.org'
        # this lets us test that auth was added to the request
        self.repo.token = 'letmein'

        ret = self.repo._get_single_path('/v1/repositories/pulp/crane/tags')

        self.assertEqual(ret, {'latest': 'abc123'})
        self.assertEqual(mock_download_one.call_count, 1)
        self.assertTrue(isinstance(mock_download_one.call_args[0][0], DownloadRequest))
        req = mock_download_one.call_args[0][0]
        self.assertEqual(req.url, 'http://some-endpoint.org/v1/repositories/pulp/crane/tags')
        # make sure the authorization was added, which is usually required by an endpoint
        self.assertTrue('Authorization' in req.headers)
Ejemplo n.º 23
0
    def test_download_headers(self):
        request = Mock(
            uri='http://content-world.com/content/bear.rpm',
            headers={})
        request.setHeader.side_effect = request.headers.__setitem__
        report = DownloadReport('', '')
        report.headers = {
            'A': 1,
            'B': 2,
        }

        # should be ignored.
        report.headers.update({k: '' for k in HOP_BY_HOP_HEADERS})

        config = Mock(properties={
            'streamer': {
                'cache_timeout': 100
            }
        })

        def get(s, p):
            return config.properties[s][p]

        config.get.side_effect = get
        streamer = Mock(config=config)

        # test
        listener = DownloadListener(streamer, request)
        listener.download_headers(report)

        # validation
        self.assertEqual(
            request.headers,
            {
                'Cache-Control': 'public, s-maxage=100, max-age=100',
                'A': 1,
                'B': 2,
            })