11import pytest
2- from anchorecli .cli import image
2+
3+ from anchorecli .cli import image , utils
4+ from click import Context
35from click .testing import CliRunner
46
57
@@ -81,6 +83,14 @@ def apply(success=True, httpcode=200, has_error=None):
8183 "/usr/local/lib64/python3.6/site-packages/aubio" ,
8284]
8385
86+ def mock_empty_callback ():
87+ pass
88+
89+ def get_mock_parent_ctx ():
90+ # The command supplied here will not be used in a way tht impacts the tests, and can
91+ # be any arbitrary, non-root command
92+ return Context (image .query_vuln , obj = utils .ContextObject (None , mock_empty_callback ))
93+
8494
8595class TestQueryVuln :
8696 def test_is_analyzing (self , monkeypatch , response ):
@@ -92,7 +102,10 @@ def test_is_analyzing(self, monkeypatch, response):
92102 monkeypatch .setattr (image , "config" , {"jsonmode" : False })
93103 runner = CliRunner ()
94104 response (success = False )
95- result = runner .invoke (image .query_vuln , ["centos/centos:8" , "all" ])
105+ # image.query_vuln expects a context containing a parent context with a callback for it to run.
106+ # In production this is provided by the image group command, and should never be skipped.
107+ # Since that doesn't exist here, just mock it with a no-op callback
108+ result = runner .invoke (image .query_vuln , ["centos/centos:8" , "all" ], parent = get_mock_parent_ctx ())
96109 assert result .exit_code == 100
97110
98111 def test_not_yet_analyzed (self , monkeypatch , response ):
@@ -111,7 +124,7 @@ def test_not_yet_analyzed(self, monkeypatch, response):
111124 "message" : "image is not analyzed - analysis_status: not_analyzed" ,
112125 },
113126 )
114- result = runner .invoke (image .query_vuln , ["centos/centos:8" , "all" ])
127+ result = runner .invoke (image .query_vuln , ["centos/centos:8" , "all" ], parent = get_mock_parent_ctx () )
115128 assert result .exit_code == 101
116129
117130 @pytest .mark .parametrize ("item" , headers )
@@ -124,7 +137,7 @@ def test_success_headers(self, monkeypatch, response, item):
124137 monkeypatch .setattr (image , "config" , {"jsonmode" : False })
125138 runner = CliRunner ()
126139 response (success = True )
127- result = runner .invoke (image .query_vuln , ["centos/centos:8" , "all" ])
140+ result = runner .invoke (image .query_vuln , ["centos/centos:8" , "all" ], parent = get_mock_parent_ctx () )
128141 assert result .exit_code == 0
129142 assert item in result .stdout
130143
@@ -138,7 +151,7 @@ def test_success_info(self, monkeypatch, response, item):
138151 monkeypatch .setattr (image , "config" , {"jsonmode" : False })
139152 runner = CliRunner ()
140153 response (success = True )
141- result = runner .invoke (image .query_vuln , ["centos/centos:8" , "all" ])
154+ result = runner .invoke (image .query_vuln , ["centos/centos:8" , "all" ], parent = get_mock_parent_ctx () )
142155 assert result .exit_code == 0
143156 assert item in result .stdout
144157
@@ -163,7 +176,7 @@ def test_deleted_pre_v080(self, monkeypatch, response):
163176 )
164177 runner = CliRunner ()
165178 response (success = True )
166- result = runner .invoke (image .delete , ["centos/centos:8" ])
179+ result = runner .invoke (image .delete , ["centos/centos:8" ], parent = get_mock_parent_ctx () )
167180 assert result .exit_code == 0
168181
169182 def test_delete_failed_pre_v080 (self , monkeypatch , response ):
@@ -185,7 +198,7 @@ def test_delete_failed_pre_v080(self, monkeypatch, response):
185198 )
186199 runner = CliRunner ()
187200 response (success = True )
188- result = runner .invoke (image .delete , ["centos/centos:8" ])
201+ result = runner .invoke (image .delete , ["centos/centos:8" ], parent = get_mock_parent_ctx () )
189202 assert result .exit_code == 1
190203
191204 def test_is_deleting (self , monkeypatch , response ):
@@ -207,7 +220,7 @@ def test_is_deleting(self, monkeypatch, response):
207220 )
208221 runner = CliRunner ()
209222 response (success = True )
210- result = runner .invoke (image .delete , ["centos/centos:8" ])
223+ result = runner .invoke (image .delete , ["centos/centos:8" ], parent = get_mock_parent_ctx () )
211224 assert result .exit_code == 0
212225
213226 def test_delete_failed (self , monkeypatch , response ):
@@ -233,7 +246,7 @@ def test_delete_failed(self, monkeypatch, response):
233246 )
234247 runner = CliRunner ()
235248 response (success = True )
236- result = runner .invoke (image .delete , ["centos/centos:8" ])
249+ result = runner .invoke (image .delete , ["centos/centos:8" ], parent = get_mock_parent_ctx () )
237250 assert result .exit_code == 1
238251
239252
0 commit comments