def test_process_main_with_one_layer(self, super_process_main, from_json, mock_manifest): """ Test process_main() when there is only one layer. """ repo = mock.MagicMock() conduit = mock.MagicMock() config = mock.MagicMock() step = sync.DownloadManifestsStep(repo, conduit, config) step.parent = mock.MagicMock() step.parent.index_repository.get_tags.return_value = ['latest'] with open(os.path.join(TEST_DATA_PATH, 'manifest_one_layer.json')) as manifest_file: manifest = manifest_file.read() digest = 'sha256:a001e892f3ba0685184486b08cda99bf81f551513f4b56e72954a1d4404195b1' manifest = models.Manifest.from_json(manifest, digest) step.parent.index_repository.get_manifest.return_value = [ (digest, manifest, 'image') ] step.parent.available_blobs = [] step.process_main() super_process_main.assert_called_once_with() step.parent.index_repository.get_tags.assert_called_once_with() step.parent.index_repository.get_manifest.assert_called_once_with( 'latest') # since it is a manifest schema 1 version, there should no config_layer self.assertFalse(manifest.config_layer)
def test_process_main_schema2_with_one_layer(self, super_process_main, from_json, mock_manifest): """ Test process_main() when there is only one layer. """ repo = mock.MagicMock() conduit = mock.MagicMock() config = mock.MagicMock() step = sync.DownloadManifestsStep(repo, conduit, config) step.parent = mock.MagicMock() step.parent.index_repository.get_tags.return_value = ['latest'] with open( os.path.join( TEST_DATA_PATH, 'manifest_schema2_one_layer.json')) as manifest_file: manifest = manifest_file.read() digest = 'sha256:817a12c32a39bbe394944ba49de563e085f1d3c5266eb8e9723256bc4448680e' manifest = models.Manifest.from_json(manifest, digest) step.parent.index_repository.get_manifest.return_value = [ (digest, manifest, 'image') ] step.parent.available_blobs = [] step.process_main() super_process_main.assert_called_once_with() step.parent.index_repository.get_tags.assert_called_once_with() step.parent.index_repository.get_manifest.assert_called_once_with( 'latest') # since it is a manifest schema 2 version, there should a config_layer self.assertTrue(manifest.config_layer)
def test_process_main_with_repeated_layers(self, super_process_main, from_json): """ Test process_main() when the various tags contains some layers in common, which is a typical pattern. The available_blobs set on the SyncStep should only have the layers once each so that we don't try to download them more than once. """ repo = mock.MagicMock() conduit = mock.MagicMock() config = mock.MagicMock() step = sync.DownloadManifestsStep(repo, conduit, config) step.parent = mock.MagicMock() step.parent.index_repository.get_tags.return_value = ['latest'] with open(os.path.join( TEST_DATA_PATH, 'manifest_repeated_layers.json')) as manifest_file: manifest = manifest_file.read() digest = 'sha256:a001e892f3ba0685184486b08cda99bf81f551513f4b56e72954a1d4404195b1' step.parent.index_repository.get_manifest.return_value = digest, manifest step.parent.available_manifests = [] step.parent.available_blobs = [] with mock.patch('__builtin__.open') as mock_open: step.process_main() # Assert that the manifest was written to disk in the working dir mock_open.return_value.__enter__.return_value.write.assert_called_once_with( manifest) super_process_main.assert_called_once_with() step.parent.index_repository.get_tags.assert_called_once_with() step.parent.index_repository.get_manifest.assert_called_once_with( 'latest') from_json.assert_called_once_with(manifest, digest) # There should be one manifest that has the correct digest self.assertEqual(len(step.parent.available_manifests), 1) self.assertEqual(step.parent.available_manifests[0].digest, digest) # There should be two layers, but oddly one of them is used three times expected_blob_sums = ( 'sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef', 'sha256:cc8567d70002e957612902a8e985ea129d831ebe04057d88fb644857caa45d11' ) expected_digests = [expected_blob_sums[i] for i in (0, 0, 1, 0)] layer_digests = [ layer.blob_sum for layer in step.parent.available_manifests[0].fs_layers ] self.assertEqual(layer_digests, expected_digests) # The layers should have been added to the parent.available_blobs list, in no # particular order self.assertEqual(set([u.digest for u in step.parent.available_blobs]), set(expected_blob_sums))
def test_cannot_get_tags(self): """ Make sure the failure is graceful when v2 tags cannot be retrieved. """ repo = mock.MagicMock() conduit = mock.MagicMock() config = mock.MagicMock() step = sync.DownloadManifestsStep(repo, conduit, config) step.parent = mock.MagicMock() step.parent.index_repository.get_tags.side_effect = IOError step.process_main() self.assertEqual(step.parent.available_blobs.extend.call_count, 0)
def test_process_main_with_one_layer(self, super_process_main, from_json): """ Test process_main() when there is only one layer. """ repo = mock.MagicMock() conduit = mock.MagicMock() config = mock.MagicMock() step = sync.DownloadManifestsStep(repo, conduit, config) step.parent = mock.MagicMock() step.parent.index_repository.get_tags.return_value = ['latest'] with open(os.path.join(TEST_DATA_PATH, 'manifest_one_layer.json')) as manifest_file: manifest = manifest_file.read() digest = 'sha256:a001e892f3ba0685184486b08cda99bf81f551513f4b56e72954a1d4404195b1' step.parent.index_repository.get_manifest.return_value = digest, manifest step.parent.available_manifests = [] step.parent.available_blobs = [] with mock.patch('__builtin__.open') as mock_open: step.process_main() # Assert that the manifest was written to disk in the working dir mock_open.return_value.__enter__.return_value.write.assert_called_once_with( manifest) super_process_main.assert_called_once_with() step.parent.index_repository.get_tags.assert_called_once_with() step.parent.index_repository.get_manifest.assert_called_once_with( 'latest') from_json.assert_called_once_with(manifest, digest) # There should be one manifest that has the correct digest self.assertEqual(len(step.parent.available_manifests), 1) self.assertEqual(step.parent.available_manifests[0].digest, digest) # There should be one layer expected_blob_sum = ( 'sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6' 'ef') expected_layer = step.parent.available_manifests[0].fs_layers[0] self.assertEqual(expected_layer.blob_sum, expected_blob_sum) self.assertEqual(step.parent.available_manifests[0].fs_layers, [expected_layer]) # A blob with the correct digest should have been added to the parent.available_blobs # list expected_blob = step.parent.available_blobs[0] self.assertEqual(expected_blob.digest, expected_blob_sum) self.assertEqual(step.parent.available_blobs, [expected_blob])
def test___init__(self, __init__): """ Assert correct attributes and calls from __init__(). """ repo = mock.MagicMock() conduit = mock.MagicMock() config = mock.MagicMock() step = sync.DownloadManifestsStep(repo, conduit, config) self.assertEqual(step.description, _('Downloading manifests')) __init__.assert_called_once_with( step, step_type=constants.SYNC_STEP_METADATA, repo=repo, conduit=conduit, config=config, plugin_type=constants.IMPORTER_TYPE_ID)
def test_process_manifest_list(self, from_json, mock_manifest): """ Test _process_manifest_list(). """ repo = mock.MagicMock() conduit = mock.MagicMock() config = mock.MagicMock() step = sync.DownloadManifestsStep(repo, conduit, config) step.parent = mock.MagicMock() step.parent.available_manifests = [] step.parent.save_tags_step.tagged_manifests = [] step.parent.index_repository.get_manifest.side_effect = [[ ('digest1', 'manifest1', 'image') ], [('digest2', 'manifest2', 'image') ], [('digest3', 'manifest3', 'image')]] with open(os.path.join(TEST_DATA_PATH, 'manifest_list.json')) as manifest_file: manifest_list = manifest_file.read() digest = 'sha256:69fd2d3fa813bcbb3a572f1af80fe31a1710409e15dde91af79be62b37ab4f7' repo_tag = 'latest' with mock.patch('__builtin__.open') as mock_open: step._process_manifest_list(manifest_list, digest, set(), repo_tag) # Assert that the manifest was written to disk in the working dir mock_open.return_value.__enter__.return_value.write.assert_called_once_with( manifest_list) from_json.assert_called_once_with(manifest_list, digest) # There should be one manifest that has the correct digest self.assertEqual(len(step.parent.available_manifests), 1) self.assertEqual(step.parent.available_manifests[0].digest, digest) expected_man = [ 'sha256:c55544de64a01e157b9d931f5db7a16554a14be19c367f91c9a8cdc46db086bf', 'sha256:de9576aa7f9ac6aff09029293ca23136011302c02e183e856a2cd6d37b84ab92' ] self.assertEqual(step.parent.available_manifests[0].manifests, expected_man) self.assertEqual(mock_manifest.call_count, 3) self.assertEqual(step.parent.index_repository.get_manifest.call_count, 3) self.assertEqual(len(step.parent.save_tags_step.tagged_manifests), 1)
def test_process_manifest_with_unique_layers(self, from_json): """ Test _process_manifest() when the various tags all have unique layers. """ repo = mock.MagicMock() conduit = mock.MagicMock() config = mock.MagicMock() step = sync.DownloadManifestsStep(repo, conduit, config) step.parent = mock.MagicMock() with open( os.path.join(TEST_DATA_PATH, 'manifest_unique_layers.json')) as manifest_file: manifest = manifest_file.read() digest = 'sha256:a001e892f3ba0685184486b08cda99bf81f551513f4b56e72954a1d4404195b1' repo_tag = 'latest' step.parent.available_manifests = [] with mock.patch('__builtin__.open') as mock_open: step._process_manifest(manifest, digest, set(), repo_tag) # Assert that the manifest was written to disk in the working dir mock_open.return_value.__enter__.return_value.write.assert_called_once_with( manifest) from_json.assert_called_once_with(manifest, digest) # There should be one manifest that has the correct digest self.assertEqual(len(step.parent.available_manifests), 1) self.assertEqual(step.parent.available_manifests[0].digest, digest) # There should be two layers, but oddly one of them is used three times expected_blob_sums = [ 'sha256:cc8567d70002e957612902a8e985ea129d831ebe04057d88fb644857caa45d11', 'sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef' ] fs_layer_blob_sums = [ layer.blob_sum for layer in step.parent.available_manifests[0].fs_layers ] self.assertEqual(fs_layer_blob_sums, expected_blob_sums)
def test_process_manifest_schema2_with_one_layer(self, from_json): """ Test _process_manifest() when there is only one layer. """ repo = mock.MagicMock() conduit = mock.MagicMock() config = mock.MagicMock() step = sync.DownloadManifestsStep(repo, conduit, config) step.parent = mock.MagicMock() with open( os.path.join( TEST_DATA_PATH, 'manifest_schema2_one_layer.json')) as manifest_file: manifest = manifest_file.read() digest = 'sha256:817a12c32a39bbe394944ba49de563e085f1d3c5266eb8e9723256bc4448680e' repo_tag = 'latest' step.parent.available_manifests = [] with mock.patch('__builtin__.open') as mock_open: step._process_manifest(manifest, digest, set(), repo_tag) # Assert that the manifest was written to disk in the working dir mock_open.return_value.__enter__.return_value.write.assert_called_once_with( manifest) from_json.assert_called_once_with(manifest, digest) # There should be one manifest that has the correct digest self.assertEqual(len(step.parent.available_manifests), 1) self.assertEqual(step.parent.available_manifests[0].digest, digest) # There should be one layer expected_blob_sum = ( 'sha256:4b0bc1c4050b03c95ef2a8e36e25feac42fd31283e8c30b3ee5df6b043155d' '3c') expected_layer = step.parent.available_manifests[0].fs_layers[0] self.assertEqual(expected_layer.blob_sum, expected_blob_sum) self.assertEqual(step.parent.available_manifests[0].fs_layers, [expected_layer])