Align to OpenStack Hacking rules.

This commit is contained in:
Monty Taylor 2013-03-28 03:49:12 +01:00
parent 74929e70cc
commit 82489a11ba
6 changed files with 39 additions and 29 deletions

View File

@ -13,15 +13,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from argparse import ArgumentParser
import argparse
import json
import logging
import os
import subprocess
import sys
from tempfile import NamedTemporaryFile
import tempfile
from pystache.context import KeyNotFoundError
from pystache import context
from config_exception import ConfigException
from renderers import JsonRenderer
@ -57,7 +57,7 @@ def write_file(path, contents):
logger.info("writing %s", path)
d = os.path.dirname(path)
os.path.exists(d) or os.makedirs(d)
with NamedTemporaryFile(dir=d, delete=False) as newfile:
with tempfile.NamedTemporaryFile(dir=d, delete=False) as newfile:
newfile.write(contents)
os.chmod(newfile.name, 0644)
os.rename(newfile.name, path)
@ -78,7 +78,7 @@ def render_template(template, config):
else:
try:
return render_moustache(open(template).read(), config)
except KeyNotFoundError as e:
except context.KeyNotFoundError as e:
raise ConfigException(
"key '%s' from template '%s' does not exist in metadata file."
% (e.key, template))
@ -113,7 +113,7 @@ def render_executable(path, config):
def read_config(path):
try:
return json.loads(open(path).read())
except:
except Exception:
raise ConfigException("invalid metadata file: %s" % path)
@ -145,7 +145,7 @@ def strip_hash(h, keys):
def parse_opts(argv):
parser = ArgumentParser()
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--templates', metavar='TEMPLATE_ROOT',
help="""path to template root directory (default:
%(default)s)""",

View File

@ -15,10 +15,10 @@
import json
from pystache import Renderer
import pystache
class JsonRenderer(Renderer):
class JsonRenderer(pystache.Renderer):
def __init__(self,
file_encoding=None,
string_encoding=None,

View File

@ -18,7 +18,7 @@ import json
import testtools
from testtools import content
from os_config_applier.renderers import JsonRenderer
from os_config_applier import renderers
TEST_JSON = '{"a":{"b":[1,2,3,"foo"],"c": "the quick brown fox"}}'
@ -27,7 +27,7 @@ class JsonRendererTestCase(testtools.TestCase):
def test_json_renderer(self):
context = json.loads(TEST_JSON)
x = JsonRenderer()
x = renderers.JsonRenderer()
result = x.render('{{a.b}}', context)
self.addDetail('result', content.text_content(result))
result_structure = json.loads(result)

View File

@ -19,10 +19,10 @@ import subprocess
import tempfile
import fixtures
from pystache.context import KeyNotFoundError
from pystache import context
import testtools
from os_config_applier.config_exception import ConfigException
from os_config_applier import config_exception
from os_config_applier import os_config_applier as oca
# example template tree
@ -90,13 +90,13 @@ class TestRunOSConfigApplier(testtools.TestCase):
def test_print_key_missing(self):
self.assertRaises(
subprocess.CalledProcessError,
subprocess.CalledProcessError, oca.main,
['os-config-applier.py', '--metadata', self.path, '--key',
'does.not.exist'])
def test_print_key_wrong_type(self):
self.assertRaises(
subprocess.CalledProcessError,
subprocess.CalledProcessError, oca.main,
['os-config-applier.py', '--metadata', self.path, '--key',
'x', '--type', 'int'])
@ -110,6 +110,7 @@ class OSConfigApplierTestCase(testtools.TestCase):
def setUp(self):
super(OSConfigApplierTestCase, self).setUp()
self.useFixture(fixtures.FakeLogger('os-config-applier'))
self.useFixture(fixtures.NestedTempfile())
def test_install_config(self):
@ -145,15 +146,16 @@ class OSConfigApplierTestCase(testtools.TestCase):
# execute executable files, moustache non-executables
self.assertEqual(oca.render_template(template(
"/etc/glance/script.conf"), {"x": "abc"}), "abc\n")
self.assertRaises(ConfigException, oca.render_template, template(
"/etc/glance/script.conf"), {})
self.assertRaises(
config_exception.ConfigException, oca.render_template, template(
"/etc/glance/script.conf"), {})
def test_render_moustache(self):
self.assertEqual(oca.render_moustache("ab{{x.a}}cd", {
"x": {"a": "123"}}), "ab123cd")
def test_render_moustache_bad_key(self):
self.assertRaises(KeyNotFoundError,
self.assertRaises(context.KeyNotFoundError,
oca.render_moustache, "{{badkey}}", {})
def test_render_executable(self):
@ -163,7 +165,7 @@ class OSConfigApplierTestCase(testtools.TestCase):
def test_render_executable_failure(self):
self.assertRaises(
ConfigException,
config_exception.ConfigException,
oca.render_executable, template("/etc/glance/script.conf"), {})
def test_template_paths(self):
@ -184,13 +186,17 @@ class OSConfigApplierTestCase(testtools.TestCase):
with tempfile.NamedTemporaryFile() as t:
t.write("{{{{")
t.flush()
self.assertRaises(ConfigException, oca.read_config, t.name)
self.assertRaises(config_exception.ConfigException,
oca.read_config, t.name)
def test_read_config_no_file(self):
self.assertRaises(ConfigException, oca.read_config, "/nosuchfile")
self.assertRaises(config_exception.ConfigException,
oca.read_config, "/nosuchfile")
def test_strip_hash(self):
h = {'a': {'b': {'x': 'y'}}, "c": [1, 2, 3]}
self.assertEqual(oca.strip_hash(h, 'a.b'), {'x': 'y'})
self.assertRaises(ConfigException, oca.strip_hash, h, 'a.nonexistent')
self.assertRaises(ConfigException, oca.strip_hash, h, 'a.c')
self.assertRaises(config_exception.ConfigException,
oca.strip_hash, h, 'a.nonexistent')
self.assertRaises(config_exception.ConfigException,
oca.strip_hash, h, 'a.c')

View File

@ -15,20 +15,23 @@
import testtools
from os_config_applier.config_exception import ConfigException
from os_config_applier.value_types import ensure_type
from os_config_applier import config_exception
from os_config_applier import value_types
class ValueTypeTestCase(testtools.TestCase):
def test_unknown_type(self):
self.assertRaises(ValueError, ensure_type, "foo", "badtype")
self.assertRaises(
ValueError, value_types.ensure_type, "foo", "badtype")
def test_int(self):
self.assertEqual("123", ensure_type("123", "int"))
self.assertEqual("123", value_types.ensure_type("123", "int"))
def test_default(self):
self.assertEqual("foobar", ensure_type("foobar", "default"))
self.assertEqual("foobar",
value_types.ensure_type("foobar", "default"))
def test_default_bad(self):
self.assertRaises(ConfigException, ensure_type, "foo\nbar", "default")
self.assertRaises(config_exception.ConfigException,
value_types.ensure_type, "foo\nbar", "default")

View File

@ -1,6 +1,7 @@
coverage>=3.6
discover
fixtures>=0.3.12
hacking
python-subunit
sphinx>=1.1.2
testrepository>=0.0.13