77
88from cloudinit .atomic_helper import write_json
99from cloudinit .cmd import status
10- from cloudinit .util import write_file
10+ from cloudinit .util import ensure_file
1111from cloudinit .tests .helpers import CiTestCase , wrap_and_call , mock
1212
1313mypaths = namedtuple ('MyPaths' , 'run_dir' )
@@ -36,7 +36,7 @@ def read_cfg(self):
3636
3737 def test__is_cloudinit_disabled_false_on_sysvinit (self ):
3838 '''When not in an environment using systemd, return False.'''
39- write_file (self .disable_file , '' ) # Create the ignored disable file
39+ ensure_file (self .disable_file ) # Create the ignored disable file
4040 (is_disabled , reason ) = wrap_and_call (
4141 'cloudinit.cmd.status' ,
4242 {'uses_systemd' : False },
@@ -47,7 +47,7 @@ def test__is_cloudinit_disabled_false_on_sysvinit(self):
4747
4848 def test__is_cloudinit_disabled_true_on_disable_file (self ):
4949 '''When using systemd and disable_file is present return disabled.'''
50- write_file (self .disable_file , '' ) # Create observed disable file
50+ ensure_file (self .disable_file ) # Create observed disable file
5151 (is_disabled , reason ) = wrap_and_call (
5252 'cloudinit.cmd.status' ,
5353 {'uses_systemd' : True },
@@ -58,7 +58,7 @@ def test__is_cloudinit_disabled_true_on_disable_file(self):
5858
5959 def test__is_cloudinit_disabled_false_on_kernel_cmdline_enable (self ):
6060 '''Not disabled when using systemd and enabled via commandline.'''
61- write_file (self .disable_file , '' ) # Create ignored disable file
61+ ensure_file (self .disable_file ) # Create ignored disable file
6262 (is_disabled , reason ) = wrap_and_call (
6363 'cloudinit.cmd.status' ,
6464 {'uses_systemd' : True ,
@@ -96,7 +96,7 @@ def test__is_cloudinit_disabled_true_when_generator_disables(self):
9696 def test__is_cloudinit_disabled_false_when_enabled_in_systemd (self ):
9797 '''Report enabled when systemd generator creates the enabled file.'''
9898 enabled_file = os .path .join (self .paths .run_dir , 'enabled' )
99- write_file (enabled_file , '' )
99+ ensure_file (enabled_file )
100100 (is_disabled , reason ) = wrap_and_call (
101101 'cloudinit.cmd.status' ,
102102 {'uses_systemd' : True ,
@@ -149,8 +149,25 @@ def fakeexists(filepath):
149149 ''' )
150150 self .assertEqual (expected , m_stdout .getvalue ())
151151
152+ def test_status_returns_running_on_no_results_json (self ):
153+ '''Report running when status.json exists but result.json does not.'''
154+ result_file = self .tmp_path ('result.json' , self .new_root )
155+ write_json (self .status_file , {})
156+ self .assertFalse (
157+ os .path .exists (result_file ), 'Unexpected result.json found' )
158+ cmdargs = myargs (long = False , wait = False )
159+ with mock .patch ('sys.stdout' , new_callable = StringIO ) as m_stdout :
160+ retcode = wrap_and_call (
161+ 'cloudinit.cmd.status' ,
162+ {'_is_cloudinit_disabled' : (False , '' ),
163+ 'Init' : {'side_effect' : self .init_class }},
164+ status .handle_status_args , 'ignored' , cmdargs )
165+ self .assertEqual (0 , retcode )
166+ self .assertEqual ('status: running\n ' , m_stdout .getvalue ())
167+
152168 def test_status_returns_running (self ):
153169 '''Report running when status exists with an unfinished stage.'''
170+ ensure_file (self .tmp_path ('result.json' , self .new_root ))
154171 write_json (self .status_file ,
155172 {'v1' : {'init' : {'start' : 1 , 'finished' : None }}})
156173 cmdargs = myargs (long = False , wait = False )
@@ -164,10 +181,11 @@ def test_status_returns_running(self):
164181 self .assertEqual ('status: running\n ' , m_stdout .getvalue ())
165182
166183 def test_status_returns_done (self ):
167- '''Reports done when stage is None and all stages are finished.'''
184+ '''Report done results.json exists no stages are unfinished.'''
185+ ensure_file (self .tmp_path ('result.json' , self .new_root ))
168186 write_json (
169187 self .status_file ,
170- {'v1' : {'stage' : None ,
188+ {'v1' : {'stage' : None , # No current stage running
171189 'datasource' : (
172190 'DataSourceNoCloud [seed=/var/.../seed/nocloud-net]'
173191 '[dsmode=net]' ),
@@ -187,6 +205,7 @@ def test_status_returns_done(self):
187205
188206 def test_status_returns_done_long (self ):
189207 '''Long format of done status includes datasource info.'''
208+ ensure_file (self .tmp_path ('result.json' , self .new_root ))
190209 write_json (
191210 self .status_file ,
192211 {'v1' : {'stage' : None ,
@@ -303,6 +322,8 @@ def fake_sleep(interval):
303322 write_json (self .status_file , running_json )
304323 elif self .sleep_calls == 3 :
305324 write_json (self .status_file , done_json )
325+ result_file = self .tmp_path ('result.json' , self .new_root )
326+ ensure_file (result_file )
306327
307328 cmdargs = myargs (long = False , wait = True )
308329 with mock .patch ('sys.stdout' , new_callable = StringIO ) as m_stdout :
0 commit comments