Beispiel #1
0
 def test_manifest_local(self, mock_docker):
     mock_docker_client(mock_docker)
     registry = LocalRegistry('localhost')
     image = Image(self.name, registry)
     with open('tests/test_data/manifest.json') as file_:
         manifest = json.load(file_)
     self.assertEqual(image.manifest, manifest)
Beispiel #2
0
 def test_analyze_manifest_list(self):
     list_image_manifest_url = self.reg_url + \
         'org/image-name/manifests/sha256:d0fec089e611891a03f3282f10115bb186ed46093c3f083eceb250cee64b63eb'
     with open('tests/test_data/manifest.list.v2.json') as f:
         list_manifest = json.load(f)
     with open('tests/test_data/manifest.list.v2-image.json') as f:
         list_image_manifest = json.load(f)
     with open('tests/test_data/origin_vulnerabilities_list.json') as f:
         list_origin_data = json.load(f)
     responses.add(responses.GET, '%s/%s?features&vulnerabilities' %
                   (self.v1_analyze_url, list_origin_data['Layer']['Name']),
                   json=list_origin_data)
     responses.replace(responses.GET, self.manifest_url,
                       json=list_manifest, status=200)
     responses.add(responses.GET, list_image_manifest_url,
                   json=list_image_manifest, status=200)
     layers = [e['digest'] for e in list_image_manifest['layers']]
     responses.add(responses.DELETE, '%s/%s' %
                   (self.v1_analyze_url, layers[0]))
     for layer in layers:
         responses.add(responses.GET, '%s/%s' %
                       (self.v1_analyze_url, layer))
     with patch('sys.argv', ['claircli', '-d', '-c',
                             self.clair_url, self.name]):
         cli = ClairCli()
         cli.run()
     image = Image(self.name)
     self.assert_called_with_url()
     for index, layer in enumerate(image.images[0].layers, start=5):
         self.assertEqual(
             responses.calls[index].request.url, self.v1_analyze_url)
         req_body = json.loads(responses.calls[index].request.body)
         self.assertEqual(req_body['Layer']['Name'], layer)
     self.html = Report.get_report_path('{}/{}@{}'.format(self.reg, self.repo, image.manifest['manifests'][0]['digest']), '.html')
     self.assertTrue(isfile(self.html))
Beispiel #3
0
 def test_layers_local(self, mock_docker):
     mock_docker_client(mock_docker)
     registry = LocalRegistry('localhost')
     image = Image(self.name, registry)
     with open('tests/test_data/manifest.json') as file_:
         manifest = json.load(file_)
     self.assertEqual(image.layers, [e.replace(
         '/layer.tar', '') for e in manifest[0]['Layers']])
Beispiel #4
0
 def test_unsupported_manifest(self):
     with open('tests/test_data/manifest.unsupported.json') as f:
         manifest = json.load(f)
     responses.replace(responses.GET, self.manifest_url,
                       json=manifest, status=200)
     with self.assertRaises(ValueError):
         image = Image(self.name)
         image.layers
Beispiel #5
0
 def test_list_manifest(self):
     with open('tests/test_data/manifest.list.v2.json') as f:
         list_manifest = json.load(f)
     responses.replace(responses.GET, self.manifest_url,
                       json=list_manifest, status=200)
     image = Image(self.name)
     self.assertEqual(image.manifest, list_manifest)
     self.assert_called_with_url()
Beispiel #6
0
 def test_parse_image(self):
     with open('tests/test_data/images.json') as f:
         images = json.load(f)
     for i in images:
         image = Image(i['name'])
         self.assertEqual(str(image), i['name'])
         self.assertEqual(image.repository, i['repository'])
         self.assertEqual(image.tag, i['tag'])
         self.assertEqual(str(image.registry), i['registry'])
Beispiel #7
0
 def test_layers_v1(self):
     with open('tests/test_data/manifest.v1.json') as f:
         manifest = json.load(f)
     responses.replace(responses.GET, self.image_manifest_url,
                       json=manifest, status=200)
     image = Image(self.name)
     self.assertEqual(image.layers, [e['blobSum']
                                     for e in manifest['fsLayers']][::-1])
     self.assert_called_with_url()
Beispiel #8
0
 def test_analyze_remote_image(self):
     clair = Clair(self.clair_url)
     image = Image(self.name)
     layers = clair.analyze_image(image)
     self.assertEqual(layers, self.layers)
     self.assert_called_with_url()
     for index, layer in enumerate(self.layers, start=4):
         self.assertEqual(
             responses.calls[index].request.url, self.v1_analyze_url)
         req_body = json.loads(responses.calls[index].request.body)
         self.assertEqual(req_body['Layer']['Name'], layer)
         self.assertEqual(req_body['Layer']['Path'], image.registry.get_blobs_url(image, layer))
Beispiel #9
0
 def test_analyze_local_image(self, mock_docker):
     mock_docker_client(mock_docker)
     clair = Clair(self.clair_url)
     registry = LocalRegistry('localhost')
     image = Image(self.name, registry)
     responses.add(responses.DELETE, '%s/%s' %
                   (self.v1_analyze_url, image.layers[0]))
     layers = clair.analyze_image(image)
     self.assertEqual(layers, image.layers)
     for index, layer in enumerate(layers, start=1):
         self.assertEqual(
             responses.calls[index].request.url, self.v1_analyze_url)
         req_body = json.loads(responses.calls[index].request.body)
         self.assertEqual(req_body['Layer']['Name'], layer)
         self.assertEqual(req_body['Layer']['Path'], image.registry.get_blobs_url(image, layer))
Beispiel #10
0
    def test_layers_list_v2(self):
        list_image_manifest_url = self.reg_url + \
            'org/image-name/manifests/sha256:d0fec089e611891a03f3282f10115bb186ed46093c3f083eceb250cee64b63eb'

        with open('tests/test_data/manifest.list.v2.json') as f:
            list_manifest = json.load(f)
        with open('tests/test_data/manifest.list.v2-image.json') as f:
            list_image_manifest = json.load(f)
        responses.replace(responses.GET, self.manifest_url,
                          json=list_manifest, status=200)
        responses.add(responses.GET, list_image_manifest_url,
                      json=list_image_manifest, status=200)
        image = Image(self.name)
        self.assertEqual(image.images[0].layers, [e['digest']
                         for e in list_image_manifest['layers']])
        self.assertEqual(image.layers, [])
        self.assert_called_with_url()
        self.assertEqual(
            responses.calls[3].request.url, list_image_manifest_url)
Beispiel #11
0
 def test_manifest(self):
     image = Image(self.name)
     self.assertEqual(image.manifest, self.manifest)
     self.assert_called_with_url()
Beispiel #12
0
 def test_layers_v2(self):
     image = Image(self.name)
     self.assertEqual(image.layers,
                      [e['digest'] for e in self.manifest['layers']])
     self.assert_called_with_url()