Email Working Directory Utility Method

A utility method that ensures that a typo inside of the email plugin
won't ruin our day. By using this method instead we can change the
location of our working directories in one place, rather than several.

Change-Id: I3a562d839544c55deee22a85c435df629c67edc0
This commit is contained in:
Michael Krotscheck 2015-02-01 12:58:55 -08:00
parent afafdfe999
commit 45739cf02a
2 changed files with 48 additions and 1 deletions

View File

@ -15,6 +15,9 @@
from oslo.config import cfg
from oslo_log import log
from storyboard.common.working_dir import get_plugin_directory
CONF = cfg.CONF
LOG = log.getLogger(__name__)
@ -62,3 +65,11 @@ PLUGIN_OPTS = [
]
CONF.register_opts(PLUGIN_OPTS, "plugin_email")
def get_email_directory():
"""A shared utility method that always provides the same working
directory. Error handling is explicitly not provided, as the methods used
'should' be consistent about the errors they themselves raise.
"""
return get_plugin_directory("email")

View File

@ -12,15 +12,19 @@
# implied. See the License for the specific language governing permissions and
# limitations under the License.
import os
import stat
from oslo.config import cfg
from storyboard.plugin.email import get_email_directory
from storyboard.tests import base
CONF = cfg.CONF
class TestConfiguration(base.TestCase):
class TestEmailConfiguration(base.TestCase):
def test_configuration_defaults(self):
self.assertIsNotNone(CONF.plugin_email)
@ -34,3 +38,35 @@ class TestConfiguration(base.TestCase):
self.assertEqual(None, conf.smtp_ssl_certfile)
self.assertEqual(None, conf.smtp_user)
self.assertEqual(None, conf.smtp_password)
class TestGetEmailDirectory(base.WorkingDirTestCase):
def test_get_email_directory(self):
"""Can we resolve the email directory? Most of this testing also
exists in test_working_dir, however it behooves us to test it here as
well.
"""
expected_path = os.path.realpath(os.path.join(CONF.working_directory,
'plugin', 'email'))
self.assertFalse(os.path.exists(expected_path))
resolved_path = get_email_directory()
self.assertEqual(expected_path, resolved_path)
self.assertTrue(os.path.exists(CONF.working_directory))
self.assertTrue(os.path.exists(expected_path))
self.assertTrue(os.access(CONF.working_directory, os.W_OK))
self.assertTrue(os.access(expected_path, os.W_OK))
def test_get_email_directory_not_creatable(self):
"""Assert that the get_email_directory() method raises an error if
it cannot be created.
"""
# Set the permissions
os.chmod(CONF.working_directory,
stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH)
# Make sure it raises an exception.
self.assertRaises(IOError, get_email_directory)