From 22319fa200317b930303ae3c1b63731cc4746605 Mon Sep 17 00:00:00 2001
From: Ralph Bean <rbean@redhat.com>
Date: Mon, 31 Mar 2014 12:01:37 -0400
Subject: [PATCH 2/2] Have the runner pass a config object to its directives.

---
 libtaskotron/directives/__init__.py       |  4 ++--
 libtaskotron/directives/koji_directive.py |  6 +++---
 libtaskotron/koji_utils.py                |  3 ++-
 libtaskotron/runner.py                    |  6 +++++-
 testing/functest_python_directive.py      |  2 +-
 testing/test_koji_directive.py            | 12 +++++++++---
 testing/test_koji_utils.py                | 14 ++++++++++----
 testing/test_python_directive.py          | 16 ++++++++--------
 8 files changed, 40 insertions(+), 23 deletions(-)

diff --git a/libtaskotron/directives/__init__.py b/libtaskotron/directives/__init__.py
index 96370fa..64ee3cf 100644
--- a/libtaskotron/directives/__init__.py
+++ b/libtaskotron/directives/__init__.py
@@ -1,7 +1,7 @@
 class BaseDirective(object):
 
-    def __init__(self):
-        pass
+    def __init__(self, config):
+        self.config = config
 
     def process(self, input_data, env_data):
         raise NotImplementedError
diff --git a/libtaskotron/directives/koji_directive.py b/libtaskotron/directives/koji_directive.py
index 19c4429..9e1571c 100644
--- a/libtaskotron/directives/koji_directive.py
+++ b/libtaskotron/directives/koji_directive.py
@@ -23,10 +23,10 @@ class KojiDirective(BaseDirective):
         "koji: command=download_tag tag=<tag to download> arch=<desired arch>
     """
 
-    def __init__(self, koji_session=None):
-        super(KojiDirective, self).__init__()
+    def __init__(self, config, koji_session=None):
+        super(KojiDirective, self).__init__(config)
         if koji_session is None:
-            self.koji = KojiClient(koji_session)
+            self.koji = KojiClient(koji_session, config)
         else:
             self.koji = koji_session
 
diff --git a/libtaskotron/koji_utils.py b/libtaskotron/koji_utils.py
index d7d0c58..9313696 100644
--- a/libtaskotron/koji_utils.py
+++ b/libtaskotron/koji_utils.py
@@ -8,7 +8,8 @@ KOJI_URL = 'http://koji.fedoraproject.org/kojihub'
 PKG_URL = 'http://kojipkgs.fedoraproject.org/packages'
 
 class KojiClient(object):
-    def __init__(self, koji_session=None):
+    def __init__(self, config, koji_session=None):
+        self.config = config
         self.session = koji_session
 
     def get_koji_session(self):
diff --git a/libtaskotron/runner.py b/libtaskotron/runner.py
index 4435aa6..d0b365c 100644
--- a/libtaskotron/runner.py
+++ b/libtaskotron/runner.py
@@ -6,6 +6,7 @@ import imp
 from jinja2 import Environment
 import copy
 
+from libtaskotron import config
 from libtaskotron import taskyaml
 from libtaskotron import logger
 from libtaskotron.logger import log
@@ -19,6 +20,8 @@ class Runner(object):
         self.directives = {}
         self.jinja_env = None
 
+        self.config = config.load_config()
+
         if workdir is None:
             self.workdir = tempfile.mkdtemp(dir='/var/tmp')
         else:
@@ -88,7 +91,8 @@ class Runner(object):
         directive_object = self.directives[directive_name]
         directive_classname = directive_object.directive_class
 
-        directive_callable = getattr(directive_object, directive_classname)()
+        directive_class = getattr(directive_object, directive_classname)
+        directive_callable = directive_class(config=self.config)
 
         output = directive_callable.process(rendered_action[directive_name],
                                             self.envdata)
diff --git a/testing/functest_python_directive.py b/testing/functest_python_directive.py
index 9d17d2e..137085a 100644
--- a/testing/functest_python_directive.py
+++ b/testing/functest_python_directive.py
@@ -9,7 +9,7 @@ class TestTaskMethod(object):
         self.ref_pyfile = 'some_loadable_test.py'
         self.ref_env = {'taskfile': os.path.join(os.path.dirname(
             os.path.abspath(__file__)), self.ref_taskyaml)}
-        self.test_directive = python_directive.PythonDirective()
+        self.test_directive = python_directive.PythonDirective(config=None)
 
     def testfunc_task_method(self):
         ref_input = {'file': self.ref_pyfile, 'callable': 'task_method'}
diff --git a/testing/test_koji_directive.py b/testing/test_koji_directive.py
index 5733b98..feb4ef1 100644
--- a/testing/test_koji_directive.py
+++ b/testing/test_koji_directive.py
@@ -2,6 +2,11 @@ from dingus import Dingus
 
 from libtaskotron.directives import koji_directive
 
+
+class MockConfig(object):
+    pass
+
+
 class TestKojiDirective():
     def setup_method(self, method):
         self.ref_nvr = 'foo-1.2-3.fc99'
@@ -28,7 +33,8 @@ class TestKojiDirective():
 
         self.ref_url = 'http://downloads.example.com/builds/%s.noarch.rpm'\
                        % self.ref_nvr
-        self.helper = koji_directive.KojiDirective()
+        self.config = MockConfig
+        self.helper = koji_directive.KojiDirective(self.config)
 
     def test_parse_download_command(self):
         self.ref_rpms = ['/var/tmp/fake/%s.%s.rpm'
@@ -40,7 +46,7 @@ class TestKojiDirective():
 
         stub_koji = Dingus(get_nvr_rpms__returns = self.ref_rpms)
 
-        test_helper = koji_directive.KojiDirective(stub_koji)
+        test_helper = koji_directive.KojiDirective(self.config, stub_koji)
 
         test_helper.process(ref_input, ref_envdata)
 
@@ -60,7 +66,7 @@ class TestKojiDirective():
 
         stub_koji = Dingus(get_tagged_rpms__returns=self.ref_rpms)
 
-        test_helper = koji_directive.KojiDirective(stub_koji)
+        test_helper = koji_directive.KojiDirective(self.config, stub_koji)
 
         test_helper.process(ref_input, ref_envdata)
 
diff --git a/testing/test_koji_utils.py b/testing/test_koji_utils.py
index 083de70..3ee767a 100644
--- a/testing/test_koji_utils.py
+++ b/testing/test_koji_utils.py
@@ -3,6 +3,11 @@ from dingus import Dingus
 
 from libtaskotron import koji_utils
 
+
+class MockConfig(object):
+    pass
+
+
 class TestKojiClient():
     def setup_method(self, method):
         self.ref_nvr = 'foo-1.2-3.fc99'
@@ -26,13 +31,14 @@ class TestKojiClient():
 
         self.ref_url = 'http://downloads.example.com/builds/%s.noarch.rpm' \
                        % self.ref_nvr
-        self.helper = koji_utils.KojiClient()
+        self.config = MockConfig()
+        self.helper = koji_utils.KojiClient(self.config)
 
     def test_get_noarch_rpmurls_from_nvr(self):
         stub_koji = Dingus(getBuild__returns = self.ref_build,
                            listRPMs__returns = self.ref_rpms)
 
-        test_koji = koji_utils.KojiClient(stub_koji)
+        test_koji = koji_utils.KojiClient(self.config, stub_koji)
 
         test_urls = test_koji.nvr_to_urls(self.ref_nvr)
 
@@ -51,7 +57,7 @@ class TestKojiClient():
 
         stub_koji = Dingus(getBuild__returns = self.ref_build)
 
-        test_koji = koji_utils.KojiClient(stub_koji)
+        test_koji = koji_utils.KojiClient(self.config, stub_koji)
 
         # this isn't setup to actually return urls since we don't care about
         # that for this test
@@ -70,7 +76,7 @@ class TestKojiClient():
     def should_throw_exception_norpms(self):
         stub_koji = Dingus(getBuild__returns = self.ref_build)
 
-        test_koji = koji_utils.KojiClient(stub_koji)
+        test_koji = koji_utils.KojiClient(self.config, stub_koji)
 
         with pytest.raises(Exception):
             test_koji.nvr_to_urls(self.ref_nvr, arches = [self.ref_arch])
diff --git a/testing/test_python_directive.py b/testing/test_python_directive.py
index 457806b..5b9ae80 100644
--- a/testing/test_python_directive.py
+++ b/testing/test_python_directive.py
@@ -12,7 +12,7 @@ class TestCheckfile(object):
 
         stub_ospath = Dingus(exists__returns=True)
 
-        test_directive = python_directive.PythonDirective()
+        test_directive = python_directive.PythonDirective(config=None)
         monkeypatch.setattr(os, 'path', stub_ospath)
 
         test_directive.checkfile(ref_filename)
@@ -22,7 +22,7 @@ class TestCheckfile(object):
 
         stub_ospath = Dingus(exists__returns=False)
 
-        test_directive = python_directive.PythonDirective()
+        test_directive = python_directive.PythonDirective(config=None)
         monkeypatch.setattr(os, 'path', stub_ospath)
 
         with pytest.raises(Exception):
@@ -35,7 +35,7 @@ class TestLoadPyfile(object):
         ref_taskname = 'runtask_turbo_encabulator'
         stub_imp = Dingus()
 
-        test_directive = python_directive.PythonDirective()
+        test_directive = python_directive.PythonDirective(config=None)
         monkeypatch.setattr(imp, 'load_source', stub_imp)
 
         test_name, test_task = test_directive.load_pyfile(ref_filename)
@@ -52,7 +52,7 @@ class TestExecutePyfile(object):
 
         stub_getattr = Dingus()
 
-        test_directive = python_directive.PythonDirective()
+        test_directive = python_directive.PythonDirective(config=None)
         monkeypatch.setattr(test_directive, '_do_getattr', stub_getattr)
 
         test_directive.execute(ref_modulename, ref_methodname, ref_kwargs)
@@ -67,7 +67,7 @@ class TestExecutePyfile(object):
 
         stub_getattr = Dingus()
 
-        test_directive = python_directive.PythonDirective()
+        test_directive = python_directive.PythonDirective(config=None)
         monkeypatch.setattr(test_directive, '_do_getattr', stub_getattr)
 
         test_directive.execute(ref_modulename, ref_methodname, ref_kwargs)
@@ -95,7 +95,7 @@ class TestProcess(object):
     def test_process_checks_file(self, monkeypatch):
 
 
-        test_directive = python_directive.PythonDirective()
+        test_directive = python_directive.PythonDirective(config=None)
         monkeypatch.setattr(test_directive, 'checkfile', self.stub_checkfile)
         monkeypatch.setattr(test_directive, 'load_pyfile',
                             self.stub_pyload.dostuff)
@@ -111,7 +111,7 @@ class TestProcess(object):
 
     def test_process_loads_file(self, monkeypatch):
 
-        test_directive = python_directive.PythonDirective()
+        test_directive = python_directive.PythonDirective(config=None)
         monkeypatch.setattr(test_directive, 'checkfile', self.stub_checkfile)
         monkeypatch.setattr(test_directive, 'load_pyfile',
                             self.stub_pyload.dostuff)
@@ -129,7 +129,7 @@ class TestProcess(object):
 
         ref_kwargs = {'kwarg1': 'baz'}
 
-        test_directive = python_directive.PythonDirective()
+        test_directive = python_directive.PythonDirective(config=None)
         monkeypatch.setattr(test_directive, 'checkfile', self.stub_checkfile)
         monkeypatch.setattr(test_directive, 'load_pyfile',
                             self.stub_pyload.dostuff)
-- 
1.9.0

