Permalink
Browse files

move credential reading into their own function and test it

  • Loading branch information...
1 parent a9992b9 commit bfc05b247407cff6dc932c6c4a1c0c5f69bec0e1 @mapleoin committed Apr 3, 2013
Showing with 56 additions and 25 deletions.
  1. +8 −0 README.rst
  2. +16 −9 github_tarballs
  3. +32 −16 test_github_tarballs.py
View
@@ -6,6 +6,9 @@ This is an `Open Build Service`_ source service. It downloads a tarball
from a remote URL and updates the ``.spec`` and ``.changes`` files with
git commit information from the `github API`_.
+How It Works
+------------
+
The ``Version`` field will be set to ``%(tarball_version)s+git.%(timestamp)s.%(commit_sha)s``. Where ``tarball_version`` is the version as read from the parent directory inside the downloaded tarball - everything after the last dash (``-``) in the directory's name. ``timestamp`` is the current seconds from the UNIX epoch when the source service was run. ``commit_sha`` is the latest commit sha hash from the git repository.
On the first run, ``github_tarballs`` will just set the spec file's
@@ -17,6 +20,11 @@ The ``github_tarballs`` service will also change the specfile's
``Source:`` to the ``filename`` argument of the service and the ``%setup
-q`` line to match the parent folder name in the tarball.
+Rate Limiting
+-------------
+
+The github API rate limits requests. The limit can be extended by using github credentials to access the API. ``obs-service-github_tarballs`` will read and use these credentials from the ``.github_tarballs_credentials`` file in the current user's home directory if it can. The file should contain one line with the standard in standard HTTP BasicAuth format ``username:password``.
+
Dependencies
------------
View
@@ -25,18 +25,25 @@ import tarfile
import urllib
-GITHUB_CREDS = ""
-conffilebase = "github_tarballs_credentials"
-try:
- f = open(os.path.join(os.path.expanduser('~'), '.' + conffilebase))
- GITHUB_CREDS = f.read().rstrip('\n') + '@'
-except IOError:
- pass
-
+GITHUB_CREDS = ".github_tarballs_credentials"
COMMIT_HASH_SIZE = 7
GITHUB_COMPARE = "https://%(creds)sapi.github.com/repos/%(owner)s/%(repo)s/compare/%(base)s...%(head)s"
+def github_credentials():
+ """Read github credentials from a plain text file
+
+ This helps avoid rate limiting on the github API.
+ """
+ creds = ""
+ try:
+ with open(os.path.join(os.path.expanduser('~'), GITHUB_CREDS)) as f:
+ creds = f.read().rstrip()
+ except IOError:
+ pass
+ return creds
+
+
def download_tarball(url, filename):
"""Download an upstream tarball
@@ -176,7 +183,7 @@ def get_changes(package, owner, repo, target):
current_commit = get_commit_from_spec(package)
url = GITHUB_COMPARE % dict(owner=owner,
repo=repo,
- creds=GITHUB_CREDS,
+ creds=github_credentials(),
base=current_commit or target,
head=target)
try:
@@ -12,13 +12,14 @@
# License for the specific language governing permissions and limitations
# under the License.
-from contextlib import contextmanager
+from contextlib import contextmanager, nested
import imp
import io
import unittest
-import mock
+from mock import patch
+GITHUB_API = "https://api.github.com"
ghb = imp.load_source('ghb', 'github_tarballs')
@@ -36,27 +37,42 @@ def test_version_parse_comment(self):
ghb.get_commit_from_spec('example_pkg'))
def test_download_tarball(self):
- with mock.patch("urllib.urlretrieve") as urlretrieve:
- ghb.download_tarball('owner', 'repo', 'target', 'filename')
+ with patch("urllib.urlretrieve") as urlretrieve:
+ ghb.download_tarball('https://example.com/target.tar.gz',
+ 'filename')
self.assertEqual(
- (("https://github.com/owner/repo/archive/target.tar.gz",
+ (("https://example.com/target.tar.gz",
"filename"),),
urlretrieve.call_args)
def test_get_changes(self):
response = io.StringIO(u'{"commits": [1, 2, 3]}')
response.code = 200
- with mock.patch("ghb.get_commit_from_spec", return_value="1234"):
- with mock.patch("urllib.urlopen", return_value=response) as urlopen:
- self.assertEqual({'commits': [1, 2, 3]},
- ghb.get_changes("package", "owner", "repo",
- "target"))
- self.assertEqual(
- (("https://api.github.com/repos/owner/repo/compare/1234...target",),),
- urlopen.call_args)
-
-
+ with nested(patch("ghb.get_commit_from_spec", return_value="1234"),
+ patch("ghb.github_credentials", return_value=""),
+ patch("urllib.urlopen", return_value=response)
+ ) as (_, _, urlopen):
+ self.assertEqual({'commits': [1, 2, 3]},
+ ghb.get_changes("package", "owner", "repo",
+ "target"))
+ self.assertEqual(
+ ((GITHUB_API + "/repos/owner/repo/compare/1234...target",),),
+ urlopen.call_args)
+
+ def test_github_credentials_empty_file(self):
+ with mock_open(u""):
+ self.assertEqual("", ghb.github_credentials())
+
+ def test_github_credentials_ioerror(self):
+ with patch("__builtin__.open", side_effect=IOError):
+ self.assertEqual("", ghb.github_credentials())
+
+ def test_github_credentials_read_from_file(self):
+ with mock_open(u"user:pass"):
+ self.assertEqual("user:pass", ghb.github_credentials())
+
+
@contextmanager
def mock_open(contents):
- with mock.patch("__builtin__.open", return_value=io.StringIO(contents)):
+ with patch("__builtin__.open", return_value=io.StringIO(contents)):
yield

0 comments on commit bfc05b2

Please sign in to comment.