diff options
author | Stevan Radakovic <stevan.radakovic@linaro.org> | 2012-05-15 13:01:58 +0200 |
---|---|---|
committer | Stevan Radakovic <stevan.radakovic@linaro.org> | 2012-05-15 13:01:58 +0200 |
commit | 13ee40734777ad299d6988cdcf024f5b74eefb7d (patch) | |
tree | 895173a61a13ccdbd99b838c1e0ef93f11d64ed7 | |
parent | a0f9b3bce6da30f88a128030fdb9b325178820d6 (diff) |
Add new files, first test implemented.
-rw-r--r-- | docs/snapshots.txt | 29 | ||||
-rw-r--r-- | testing/__init__.py | 2 | ||||
-rw-r--r-- | testing/doctest_production_helper.py | 112 |
3 files changed, 143 insertions, 0 deletions
diff --git a/docs/snapshots.txt b/docs/snapshots.txt new file mode 100644 index 0000000..c2a85f9 --- /dev/null +++ b/docs/snapshots.txt @@ -0,0 +1,29 @@ +Test snapshots.linaro.org production server +=========================================== + +Navigate to the regular license-protected download file +------------------------------------------------------- + +Include class we will use for this test and init fetcher. + + >>> from testing.license_protected_file_downloader import LicenseProtectedFileFetcher + >>> from testing.doctest_production_helper import DoctestProductionHelper + >>> fetcher = LicenseProtectedFileFetcher() + >>> helper = DoctestProductionHelper("http://snapshots.linaro.org") + +Visiting homepage and checking for title, android link and footer. + + >>> page = fetcher.get(helper.host_address) + >>> print page + <!DOCTYPE...<title>Linaro Snapshots</title>...<a href="android/">android/</a>...Server at snapshots.linaro.org... + ... + +Now, consecutively open links as per set guidelines and in the end return the link to the build file if there is any in the resulting directory. + >>> file_path = helper.follow_links_to_file("/", ["android/","android/","snowball"]) + >>> print file_path + /android/~linaro-android/landing-snowball/138/target/product/snowball/boot.tar.bz2 + + +Mock the file download. + >>> fetcher.get(helper.host_address) + diff --git a/testing/__init__.py b/testing/__init__.py index a02f6d3..783d38d 100644 --- a/testing/__init__.py +++ b/testing/__init__.py @@ -1,5 +1,6 @@ import os import unittest +import doctest from testing.test_click_through_license import * from testing.test_publish_to_snapshots import * @@ -12,5 +13,6 @@ def test_suite(): ] loader = unittest.TestLoader() suite = loader.loadTestsFromNames(module_names) + suite.addTest(doctest.DocFileSuite('docs/snapshots.txt', module_relative = False, optionflags = doctest.ELLIPSIS)) return suite diff --git a/testing/doctest_production_helper.py b/testing/doctest_production_helper.py new file mode 100644 index 0000000..41a01f1 --- /dev/null +++ b/testing/doctest_production_helper.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python + +from BeautifulSoup import BeautifulSoup + +from license_protected_file_downloader import LicenseProtectedFileFetcher + +class DoctestProductionHelper(): + """Doctest production testing helper class.""" + + def __init__(self, host_address): + self.host_address = host_address + self.fetcher = LicenseProtectedFileFetcher() + + def get_url_from_link(self, link): + """Append link to host and return it.""" + return self.host_address + "/" + link + + def is_dir(self, link): + """Check if the link has the slash as last char, thus pointing to the + directory. + + """ + return link[-1] == "/" + + def find_links(self, html): + """Return list of links on the page below the "Parent directory" link. + Return whole list if there is no such link. + + """ + soup = BeautifulSoup(html) + links_all = soup.findAll('a') + had_parent = False + links = [] + for link in links_all: + if had_parent: + links.append(link.get("href")) + if link.contents[0] == "Parent Directory": + had_parent = True + + if had_parent: + return links + else: + return [each.get('href') for each in links_all] + + def follow_links_to_file(self, next_link, guidelines): + """Use filefetcher to open links in the directory structure + on a specified host until the build file is reached. + + Always open the first link in the list which is a directory + and follows the guidelines criteria if possible. + + """ + next_link_is_dir = True + while (next_link_is_dir): + page = self.fetcher.get(self.get_url_from_link(next_link)) + links = self.find_links(page) + # Find link which satisfies the given guidelines. + good_link = self.find_directory_with_condition(links, guidelines) + if not good_link: + # No link matching criteria, just get first dir in list. + good_link = self.find_directory(links) + if not good_link: + # Still no good link, must be we are in the builds directory. + good_link = self.find_build_tar_bz2(links) + next_link_is_dir = False + if not good_link: + # We found page with no directories nor builds. + return None + + next_link += good_link + + return next_link + + def find_directory_with_condition(self, links, guidelines): + """Finds a directory among list of links which satisfies the first + condition in the guidelines list. + Condition is actually to contain the string from the list. + + It also removes the matching string from guidelines so it's not used + again in next iteration. + + """ + for link in links: + if self.is_dir(link): + for element in guidelines: + if element in link: + guidelines.remove(element) + return link + return None + + def find_directory(self, links): + """Finds a directory among list of links.""" + for link in links: + if self.is_dir(link): + return link + return None + + def find_build_tar_bz2(self, links): + """Finds a file list of links which ends in tar.bz2.""" + for link in links: + if link[-7:] == "tar.bz2": + return link + return None + +def main(): + helper = DoctestProductionHelper("http://snapshots.linaro.org") + helper.follow_links_to_file("/",["android/","android/","snowball"]) + + +if __name__ == "__main__": + main() + |