Friday, December 10, 2010

SBR600 Project "fedpkg Test Suite" -- release 0.3

Here we are: another semester comes to an end. If, three weeks ago, you were wondering if you could survive, now you don't have to worry any more. Live or die, you will be relived soon.

To be honest, SBR600 is a tough course. If you take it with OPS535 (I know quite a few of you do.), it's really a challenge. I, for one, failed in time management for the first time since I started Seneca. I submitted my OPS535 assignments late for both occasions. Right now, I still have two assignments not started yet (due next week). One for SEC520, another for NDD430. But I'm very glad I can wrap up release 0.3 and get a big weight out of my back. I try to make a summary of the whole project. I put it in a wiki page: Summary Document

In my release 0.3, I tested three more functions on top of my release 0.2. A big improvement is that I got the unittest functions into my test code. In my 0.2, I had a difficult time to use assertion from unittest framework. The simple reason is this: in fedpkg, the function _run_command doesn't return anything. So I can't compare the result from the fedpkg function and from the local os. Finally, I figure out a solution. I create a bash script on the fly and run the bash script. I redirect the output of the bash script to a file. Now I can extract data from the output file and compare it to the result from the fedpkg function. I clean up the temp files in the end.

One function I tested is _find_branch. This function is to find the current branch. An easy assertion is the default branch master (assertEqual). Then I use a bash script change the branch name (git branch and git checkout commands). When I run _find_branch again, I asserted that the branch is not master anymore (assertNotEqual).

Next function I tested is _hash_file. This function is to generate a hash for a file, with a providing hash type. I use a bash script to calculate the hash of a file and output to a local file. I extracted hash to a variable. Then I run _hash_file on the same file using the same hash type. The two hash values should be equal (assertEqual, again).

The third function I tested is _verify_file. The function will verity the hash value against a predetermined value. Return true is they are the same. I generate a hash for a file using a bash script. Then use the hash and the file as arguments in _verify_file. Assertion should be true (assertTrue).

To run my test, you can go to chile.proximity.on.ca. I have my test script and required packages in /var/www/html/fedpkg directory. Run ./test_fedpkg.py will go through 7 tests.

Wednesday, December 8, 2010

A little Python script

In our OPS535 class, we need to extract the records from a wiki page and generate a zone file. Normally, bash script is the choice for such a job. You use wget to download the file. Then you grep, cut, and output to another file. (You can download the page manually because the contents changes from time to time.)

Since I'm new in Python, I decide to do a little practice in Python scripting. There is a nice feature in Python in dealing with web pages. You don't have to download the page. You can just open it, process it and then write your output to a local file. You need import a package called urllib for that purpose. Anyway, below is the script I wrote for the task.

#!/usr/bin/python
'''generate a root.zone fine from the wiki page of DNS registration'''
import urllib
url = "http://zenit.senecac.on.ca/wiki/index.php/Domainreg"
#open the webpage and read the registration info into a list and then close the webpage
webpage = urllib.urlopen(url)
lst = []
lst2 = []
for line in webpage:
    line = line.strip()
    if line.startswith("</td><td>"):
        lst.append(line[9:].strip())
webpage.close()
#read the useful items from lst to lst2
for i in range(0,len(lst)):
    if i % 5 == 0 or i % 5 == 2 or i % 5 == 3:
        if lst[i] != '':
            lst2.append(lst[i])
#now generate root.zone file
output = open("root.zone", "w")
output.write("$TTL 86400\n")
output.write("@    IN   SOA  host.mydomain.com.   root.host.mydomain.com. (\n")
output.write("                42    ; serial\n")
output.write("                3H    ; refresh\n")
output.write("                15M    ; retry\n")
output.write("                1W    ; expiry\n")
output.write("                1D )    ; minimum\n")
output.write("@    IN   NS    host.mydomain.com.\n")
num = len(lst2)/3
for i in range(0, num):
    output.write(lst2[3*i])
    output.write(".        IN    NS    ")
    output.write(lst2[3*i + 2])
    output.write(".\n")
    output.write(lst2[3*i + 2])
    output.write(".    IN    A    ")
    output.write(lst2[3*i + 1])
    output.write("\n")
output.close()