thanks for the responses.<div><br></div><div><br></div><div>I run the application ever few weeks or so...its currently setup like this</div><div><br></div><div><br></div><div>#!/usr/bin/env bash</div><div><br></div><div>function testAfile() {</div>
<div> ..</div><div> ..</div><div> return 0</div><div>}</div><div><br></div><div>then if all tests are OK then I do </div><div>exec program "$@"</div><div><br></div><div>Unittest is for code it seems but I though I could bolt it on for system testing also :p</div>
<div><br></div><div><br></div><div><br></div><div><br><br><div class="gmail_quote">On Thu, Oct 18, 2012 at 2:55 AM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve+comp.lang.python@pearwood.info" target="_blank">steve+comp.lang.python@pearwood.info</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Sorry for breaking threading, but the original post has not come through<br>
to me.<br>
<div class="im"><br>
> On 18/10/2012 01:22, Rita wrote:<br>
</div><div class="im">> Hi,<br>
><br>
> Currently, I use a shell script to test how my system behaves before I<br>
> deploy an application. For instance, I check if fileA, fileB, and fileC<br>
> exist and if they do I go and start up my application.<br>
<br>
</div>Do you run the shell script once, before installing the application, or<br>
every time the application launches?<br>
<br>
Do you realise that this is vulnerable to race conditions? E.g:<br>
<br>
Time = 10am exactly: shell script runs, fileA etc exist;<br>
<br>
Time = 10am and 1 millisecond: another process deletes fileA etc;<br>
<br>
Time = 10am and 2 milliseconds: application launches, cannot find<br>
fileA etc and crashes.<br>
<br>
<br>
Depending on what your application does, this could be a security hole.<br>
<br>
Regardless of what the shell script reports, to be robust your Python<br>
application needs to protect against the case that fileA etc are missing.<br>
Even if all it does is report an error, save the user's work and exit.<br>
<div class="im"><br>
<br>
> This works great BUT<br>
><br>
> I would like to use python and in particular unittest module to test my<br>
> system and then deploy my app. I understand unittest is for functional<br>
> testing but I think this too would be a case for it. Any thoughts? I am<br>
> not looking for code in particular but just some ideas on how to use<br>
> python better in situations like this.<br>
<br>
</div>Well, you *could* use unittest, but frankly I think that's a case of<br>
using a hammer to nail in screws. Unittest is awesome for what it does.<br>
It's not so well suited for this.<br>
<br>
Compare these two pieces of code (untested, so they probably won't work<br>
exactly as given):<br>
<br>
# sample 1<br>
import os<br>
import sys<br>
for name in ['fileA', 'fileB', 'fileC']:<br>
if not os.path.exists(name):<br>
print('missing essential file %s' % name)<br>
sys.exit(1)<br>
<br>
run_application()<br>
<br>
<br>
<br>
# sample 2<br>
import os<br>
import sys<br>
import unittest<br>
<br>
class PreRunTest(unittest.TestCase):<br>
list_of_files = ['fileA', 'fileB', 'fileC']<br>
def testFilesExist(self):<br>
for name in self.list_of_files:<br>
assertTrue(os.path.exists(name)<br>
<br>
total_tests, failed_tests = unittest.testmod() # I think...<br>
<br>
if failed_tests != 0:<br>
sys.exit(1)<br>
<br>
run_application()<br>
<br>
<br>
<br>
I think the first sample is much to be preferred, and not just because it<br>
is a couple of lines shorter. There's less magic involved.<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
--<br>
Steven<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br>--- <span>Get your facts first, then you can distort them as you please.</span>--<br>
</div>