@@ -64,8 +64,8 @@ def setUp(self):
6464
6565 def tearDown (self ):
6666 RemoteRegistry .tokens = defaultdict (dict )
67- if isfile (self .html ):
68- os .remove (self .html )
67+ # if isfile(self.html):
68+ # os.remove(self.html)
6969
7070 def assert_called_with_url (self ):
7171 self .assertEqual (responses .calls [0 ].request .url , self .reg_url )
@@ -100,6 +100,26 @@ def test_manifest(self):
100100 self .assertEqual (image .manifest , self .manifest )
101101 self .assert_called_with_url ()
102102
103+ @responses .activate
104+ def test_list_manifest (self ):
105+ with open ('tests/test_data/manifest.list.v2.json' ) as f :
106+ list_manifest = json .load (f )
107+ responses .replace (responses .GET , self .manifest_url ,
108+ json = list_manifest , status = 200 )
109+ image = Image (self .name )
110+ self .assertEqual (image .manifest , list_manifest )
111+ self .assert_called_with_url ()
112+
113+ @responses .activate
114+ def test_unsupported_manifest (self ):
115+ with open ('tests/test_data/manifest.unsupported.json' ) as f :
116+ manifest = json .load (f )
117+ responses .replace (responses .GET , self .manifest_url ,
118+ json = manifest , status = 200 )
119+ with self .assertRaises (ValueError ):
120+ image = Image (self .name )
121+ image .layers
122+
103123 @patch ('docker.from_env' )
104124 def test_manifest_local (self , mock_docker ):
105125 mock_docker_client (mock_docker )
@@ -137,6 +157,27 @@ def test_layers_v2(self):
137157 [e ['digest' ] for e in self .manifest ['layers' ]])
138158 self .assert_called_with_url ()
139159
160+ @responses .activate
161+ def test_layers_list_v2 (self ):
162+ list_image_manifest_url = self .reg_url + \
163+ 'org/image-name/manifests/sha256:d0fec089e611891a03f3282f10115bb186ed46093c3f083eceb250cee64b63eb'
164+
165+ with open ('tests/test_data/manifest.list.v2.json' ) as f :
166+ list_manifest = json .load (f )
167+ with open ('tests/test_data/manifest.list.v2-image.json' ) as f :
168+ list_image_manifest = json .load (f )
169+ responses .replace (responses .GET , self .manifest_url ,
170+ json = list_manifest , status = 200 )
171+ responses .add (responses .GET , list_image_manifest_url ,
172+ json = list_image_manifest , status = 200 )
173+ image = Image (self .name )
174+ self .assertEqual (image .images [0 ].layers , [e ['digest' ]
175+ for e in list_image_manifest ['layers' ]])
176+ self .assertEqual (image .layers , [])
177+ self .assert_called_with_url ()
178+ self .assertEqual (
179+ responses .calls [3 ].request .url , list_image_manifest_url )
180+
140181
141182class TestClair (ClairCmdTestBase ):
142183
@@ -196,7 +237,7 @@ def test_read_white_list(self):
196237
197238 @responses .activate
198239 def test_analyze_images (self ):
199- with patch ('sys.argv' , ['claircli' , '-c' ,
240+ with patch ('sys.argv' , ['claircli' , '-d' , '-c' ,
200241 self .clair_url , self .name ]):
201242 cli = ClairCli ()
202243 cli .run ()
@@ -316,3 +357,41 @@ def test_analyze_local_images(self, mock_docker):
316357 req_body = json .loads (responses .calls [index ].request .body )
317358 self .assertEqual (req_body ['Layer' ]['Name' ], layer )
318359 self .assertTrue (isfile (self .html ))
360+
361+
362+ @responses .activate
363+ def test_analyze_manifest_list (self ):
364+ list_image_manifest_url = self .reg_url + \
365+ 'org/image-name/manifests/sha256:d0fec089e611891a03f3282f10115bb186ed46093c3f083eceb250cee64b63eb'
366+ with open ('tests/test_data/manifest.list.v2.json' ) as f :
367+ list_manifest = json .load (f )
368+ with open ('tests/test_data/manifest.list.v2-image.json' ) as f :
369+ list_image_manifest = json .load (f )
370+ with open ('tests/test_data/origin_vulnerabilities_list.json' ) as f :
371+ list_origin_data = json .load (f )
372+ responses .add (responses .GET , '%s/%s?features&vulnerabilities' %
373+ (self .v1_analyze_url , list_origin_data ['Layer' ]['Name' ]),
374+ json = list_origin_data )
375+ responses .replace (responses .GET , self .manifest_url ,
376+ json = list_manifest , status = 200 )
377+ responses .add (responses .GET , list_image_manifest_url ,
378+ json = list_image_manifest , status = 200 )
379+ layers = [e ['digest' ] for e in list_image_manifest ['layers' ]]
380+ responses .add (responses .DELETE , '%s/%s' %
381+ (self .v1_analyze_url , layers [0 ]))
382+ for layer in layers :
383+ responses .add (responses .GET , '%s/%s' %
384+ (self .v1_analyze_url , layer ))
385+ with patch ('sys.argv' , ['claircli' , '-d' , '-c' ,
386+ self .clair_url , self .name ]):
387+ cli = ClairCli ()
388+ cli .run ()
389+ image = Image (self .name )
390+ self .assert_called_with_url ()
391+ for index , layer in enumerate (image .images [0 ].layers , start = 5 ):
392+ self .assertEqual (
393+ responses .calls [index ].request .url , self .v1_analyze_url )
394+ req_body = json .loads (responses .calls [index ].request .body )
395+ self .assertEqual (req_body ['Layer' ]['Name' ], layer )
396+ self .html = Report .get_report_path ('{}/{}@{}' .format (self .reg , self .repo , image .manifest ['manifests' ][0 ]['digest' ]), '.html' )
397+ self .assertTrue (isfile (self .html ))
0 commit comments