Permalink
Browse files

pep8 fixes

  • Loading branch information...
tobami committed Apr 4, 2012
1 parent 9c17c19 commit 29dfd78827b3320119620fbb55e38ddb3c632512
Showing with 84 additions and 60 deletions.
  1. +10 −7 littlechef/chef.py
  2. +2 −2 littlechef/lib.py
  3. +1 −1 littlechef/runner.py
  4. +14 −10 littlechef/solo.py
  5. +8 −5 setup.py
  6. +2 −0 tests/plugins/bad.py
  7. +2 −0 tests/plugins/dummy.py
  8. +14 −16 tests/test_command.py
  9. +31 −19 tests/test_lib.py
View
@@ -150,7 +150,8 @@ def update_dct(dic1, dic2):
def _add_automatic_attributes(node):
"""Adds some of Chef's automatic attributes:
- http://wiki.opscode.com/display/chef/Recipes#Recipes-CommonAutomaticAttributes
+ http://wiki.opscode.com/display/chef/Recipes#Recipes
+ -CommonAutomaticAttributes
"""
node['fqdn'] = node['name']
@@ -162,7 +163,8 @@ def _add_merged_attributes(node, all_recipes, all_roles):
"""Merges attributes from cookbooks, node and roles
Chef Attribute precedence:
- http://wiki.opscode.com/display/chef/Attributes#Attributes-AttributeTypeandPrecedence
+ http://wiki.opscode.com/display/chef/Attributes#Attributes
+ -AttributeTypeandPrecedence
LittleChef implements, in precedence order:
- Cookbook default
- Role default
@@ -235,8 +237,8 @@ def _build_node_data_bag():
All attributes found in nodes/<item>.json file
Default and override attributes from all roles
- Returns the node object of the node which is about to be configured, or None
- if this node object cannot be found.
+ Returns the node object of the node which is about to be configured, or
+ None if this node object cannot be found.
"""
current_node = None
@@ -301,12 +303,13 @@ def _node_cleanup():
def _add_search_patch():
- """ Adds chef_solo_search_lib cookbook, which provides a library to read and search
- data bags.
+ """ Adds chef_solo_search_lib cookbook, which provides a library to read
+ and search data bags
+
"""
# Create extra cookbook dir
lib_path = os.path.join(
- node_work_path, cookbook_paths[0], 'chef_solo_search_lib', 'libraries')
+ node_work_path, cookbook_paths[0], 'chef_solo_search_lib', 'libraries')
with hide('running', 'stdout'):
sudo('mkdir -p {0}'.format(lib_path))
# Add search and environment patch to the node's cookbooks
View
@@ -368,7 +368,7 @@ def get_plugins():
"""Gets available plugins by looking into the plugins/ directory"""
if os.path.exists('plugins/'):
for filename in sorted([f for f in os.listdir('plugins/')
- if not os.path.isdir(f) and f.endswith(".py")]):
+ if not os.path.isdir(f) and f.endswith(".py")]):
plugin_name = filename[:-3]
try:
plugin = import_plugin(plugin_name)
@@ -456,5 +456,5 @@ def credentials(*args, **kwargs):
# If ssh config defines a different Hostname string (be it domain or IP),
# override 'host_string'
if 'hostname' in credentials:
- credentials['host_string'] = credentials['hostname']
+ credentials['host_string'] = credentials['hostname']
return settings(*args, **credentials)
View
@@ -370,7 +370,7 @@ def _readconfig():
pass
if user_specified and not env.password and not env.ssh_config:
- abort('You need to define a password or a ssh-config file in auth.cfg.')
+ abort('You need to define a password or a ssh-config file in auth.cfg')
# Only read config if fix is being used and we are not creating a new kitchen
View
@@ -61,18 +61,18 @@ def configure(current_node=None):
with settings(hide('running', 'stdout'), warn_only=True):
output = sudo('mkdir -p {0}'.format(cache_dir))
if output.failed:
- abort(
- "Could not create {0} dir. Do you have sudo rights?".format(
- node_work_path))
+ error = "Could not create {0} dir. ".format(node_work_path)
+ error += "Do you have sudo rights?"
+ abort(error)
# Change ownership of /tmp/chef-solo/ so that we can rsync
with hide('running', 'stdout'):
with settings(warn_only=True):
output = sudo(
'chown -R {0} {1}'.format(env.user, node_work_path))
if output.failed:
- abort(
- "Could not modify {0} dir. Do you have sudo rights?".format(
- node_work_path))
+ error = "Could not modify {0} dir. ".format(node_work_path)
+ error += "Do you have sudo rights?"
+ abort(error)
# Set up chef solo configuration
if not exists(logging_path):
sudo('mkdir -p {0}'.format(logging_path))
@@ -171,8 +171,9 @@ def _gem_apt_install():
def _gem_rpm_install():
"""Install Chef from gems for rpm based distros"""
_add_rpm_repos()
+ needed_packages = "ruby ruby-shadow gcc gcc-c++ ruby-devel wget rsync"
with show('running'):
- sudo('yum -y install ruby ruby-shadow gcc gcc-c++ ruby-devel wget rsync')
+ sudo('yum -y install {0}'.format(needed_packages))
_gem_install()
@@ -226,7 +227,8 @@ def _apt_install(distro, version, stop_client='yes'):
with settings(hide('warnings'), warn_only=True):
output = sudo('apt-get update')
if output.failed:
- print(colors.red("Error while executing apt-get install chef:"))
+ print(colors.red(
+ "Error while executing 'apt-get install chef':"))
abort(output)
# Install Chef Solo
print("Installing Chef Solo")
@@ -238,7 +240,8 @@ def _apt_install(distro, version, stop_client='yes'):
with settings(hide('warnings'), warn_only=True):
output = sudo('apt-get --yes install ucf chef')
if output.failed:
- print(colors.red("Error while executing 'apt-get install chef':"))
+ print(colors.red(
+ "Error while executing 'apt-get install chef':"))
abort(output)
if stop_client == 'yes':
# We only want chef-solo, stop chef-client and remove it from init
@@ -256,7 +259,8 @@ def _apt_install(distro, version, stop_client='yes'):
def _add_rpm_repos():
"""Add RPM repositories for Chef
Opscode doesn't officially support an ELFF resporitory any longer:
- http://wiki.opscode.com/display/chef/Installation+on+RHEL+and+CentOS+5+with+RPMs
+ http://wiki.opscode.com/display/chef/Installation+on+RHEL+and+CentOS+5+with
+ +RPMs
Using http://rbel.frameos.org/
View
@@ -38,13 +38,16 @@
long_description="""\
Cook with Chef without a Chef Server
-------------------------------------
-With LittleChef you will be able to get started more quickly cooking with Chef_, the excellent Configuration Management System.
+With LittleChef you will be able to get started more quickly cooking with
+Chef_, the excellent Configuration Management System.
-You will just need your local (preferably version controled) kitchen with all your
-cookbooks, roles data bags and nodes, which will get rsynced to a node each time
-you start a Chef Solo configuration run with the bundled 'fix' command.
+You will just need your local (preferably version controled) kitchen with all
+your cookbooks, roles data bags and nodes, which will get rsynced to a node
+each time you start a Chef Solo configuration run with the bundled 'fix'
+command.
-It also adds features to Chef Solo that are currently only available for Chef Server users: data bag search, and node search.
+It also adds features to Chef Solo that are currently only available for Chef
+Server users: data bag search, and node search.
.. _Chef: http://wiki.opscode.com/display/chef/Home
"""
View
@@ -1,4 +1,6 @@
"""Bad LittleChef plugin"""
+
def execute():
+ """I am not actually valid Python code"""
I am a syntax error
View
@@ -1,4 +1,6 @@
"""Dummy LittleChef plugin"""
+
def execute():
+ """Working plugin"""
print "Worked!"
View
@@ -32,7 +32,7 @@
fix = join(littlechef_top, 'fix.cmd')
WIN32 = True
else:
- fix = join(littlechef_top,'fix')
+ fix = join(littlechef_top, 'fix')
WIN32 = False
@@ -73,7 +73,8 @@ def test_version(self):
"""Should output the correct Little Chef version"""
resp, error = self.execute([fix, '-v'])
self.assertEquals(error, "", error)
- self.assertTrue('LittleChef {0}'.format(littlechef.__version__) in resp)
+ self.assertTrue(
+ 'LittleChef {0}'.format(littlechef.__version__) in resp)
def test_list_commands(self):
"""Should output a list of available commands"""
@@ -101,7 +102,8 @@ def test_no_valid_value(self):
"""Should error out when the env value is empty or is a fabric task"""
resp, error = self.execute([fix, 'list_nodes', '--env'])
self.assertEquals(resp, "")
- self.assertTrue("error: --env option requires an argument" in error, error)
+ self.assertTrue(
+ "error: --env option requires an argument" in error, error)
resp, error = self.execute([fix, '--env', 'list_nodes'])
self.assertEquals(resp, "")
@@ -140,17 +142,10 @@ def test_several_nodes(self):
# Will try to configure *first* testnode2 and will fail DNS lookup
self.assertTrue("tal error: Name lookup failed for testnode2" in error)
- # TODO: This test will now prompt for user input
- #def test_all_nodes(self):
- #"""Should try to configure all nodes"""
- #resp, error = self.execute([fix, 'node:all'])
- #self.assertTrue("== Configuring testnode1 ==" in resp)
- ## Will try to configure all nodes and will fail DNS lookup of testnode1
- #self.assertTrue("tal error: Name lookup failed for testnode1" in error)
-
def test_recipe(self):
"""Should configure node with the given recipe"""
- resp, error = self.execute([fix, 'node:testnode1', 'recipe:subversion'])
+ resp, error = self.execute(
+ [fix, 'node:testnode1', 'recipe:subversion'])
self.assertTrue("plying recipe 'subversion' on node testnode1" in resp)
self.assertTrue("tal error: Name lookup failed for testnode1" in error)
@@ -161,7 +156,7 @@ def test_role(self):
self.assertTrue("tal error: Name lookup failed for testnode1" in error)
def test_ssh(self):
- """Should execute the given command"""
+ """Should execute the given ssh command"""
resp, error = self.execute([fix, 'node:testnode2', 'ssh:"my command"'])
expected = "Executing the command '\"my command\"' on the node"
expected += " testnode2..."
@@ -172,12 +167,12 @@ def test_ssh(self):
def test_plugin(self):
"""Should execute the given plugin"""
resp, error = self.execute([fix, 'node:testnode1', 'plugin:notthere'])
- expected = "Sorry, could not find 'notthere.py' in the plugin directory"
+ expected = ", could not find 'notthere.py' in the plugin directory"
self.assertTrue(expected in error, resp + error)
resp, error = self.execute([fix, 'node:testnode1', 'plugin:bad'])
expected = "Found plugin 'bad', but it seems to have a syntax error:"
- expected += " invalid syntax (bad.py, line 4)"
+ expected += " invalid syntax (bad.py, line 6)"
self.assertTrue(expected in error, resp + error)
resp, error = self.execute([fix, 'node:testnode1', 'plugin:dummy'])
@@ -213,7 +208,10 @@ def test_list_recipes_detailed(self):
self.assertTrue(field in resp)
def test_list_recipes_detailed_site_cookbooks(self):
- """Should show a detailed list of available recipes with site-cookbook priority"""
+ """Should show a detailed list of available recipes with site-cookbook
+ priority
+
+ """
resp, error = self.execute([fix, 'list_recipes_detailed'])
self.assertTrue('0.8.4' in resp)
View
@@ -24,7 +24,7 @@
from littlechef import runner, chef, lib
-runner.__testing__ = True
+runner.__testing__ = True
littlechef_src = os.path.split(os.path.normpath(os.path.abspath(__file__)))[0]
littlechef_top = os.path.normpath(os.path.join(littlechef_src, '..'))
@@ -67,7 +67,8 @@ def test_nodes_with_role_in_env(self):
def test_nodes_with_role_in_env_empty(self):
runner.env.chef_environment = "production"
- self.assertRaises(SystemExit, runner.nodes_with_role, "all_you_can_eat")
+ self.assertRaises(
+ SystemExit, runner.nodes_with_role, "all_you_can_eat")
self.assertEquals(runner.env.hosts, [])
def test_nodes_one(self):
@@ -118,7 +119,9 @@ def test_get_nodes(self):
{
'chef_environment': 'staging', 'name': 'testnode2',
'other_attr': {'deep_dict': {'deep_key1': 'node_value1'}},
- 'subversion': {'password': 'node_password', 'user': 'node_user'},
+ 'subversion': {
+ 'password': 'node_password', 'user': 'node_user'
+ },
'run_list': ['role[all_you_can_eat]']
},
{
@@ -208,8 +211,6 @@ def test_list_recipes(self):
'Subversion Client installs subversion and some extra svn libs')
self.assertEquals(recipes[3]['name'], 'subversion::server')
self.assertIn('subversion::testrecipe', [r['name'] for r in recipes])
-
-
def test_import_plugin(self):
"""Should import the given plugin"""
@@ -233,7 +234,8 @@ def tearDown(self):
super(TestChef, self).tearDown()
def test_save_config(self):
- """Should create a tmp_testnode4.json and a nodes/testnode4.json config file
+ """Should create a tmp_testnode4.json and a nodes/testnode4.json config
+ file
"""
# Save a new node
@@ -280,17 +282,19 @@ def test_build_node_data_bag(self):
self.assertEquals(data['roles'], [u'base', u'all_you_can_eat'])
def test_build_node_data_bag_nonalphanumeric(self):
- """Should create a node data bag when node name contains non-alphanumerical
- characters"""
+ """Should create a node data bag when node name contains invalid chars
+ """
chef._build_node_data_bag()
# A node called testnode3.mydomain.com will have the data bag id
# 'testnode3', because dots are not allowed.
- item_path = os.path.join('data_bags', 'node', 'testnode3_mydomain_com.json')
- self.assertTrue(os.path.exists(item_path))
+ filename = 'testnode3_mydomain_com'
+ nodename = filename.replace("_", ".")
+ item_path = os.path.join('data_bags', 'node', filename + '.json')
+ self.assertTrue(os.path.exists(item_path), "node file does not exist")
with open(item_path, 'r') as f:
data = json.loads(f.read())
- self.assertTrue('id' in data and data['id'] == 'testnode3_mydomain_com')
- self.assertTrue('name' in data and data['name'] == 'testnode3.mydomain.com')
+ self.assertTrue('id' in data and data['id'] == filename)
+ self.assertTrue('name' in data and data['name'] == nodename)
def test_automatic_attributes(self):
"""Should add Chef's automatic attributes"""
@@ -304,16 +308,21 @@ def test_automatic_attributes(self):
self.assertTrue('domain' in data and data['domain'] == '')
# Check node with complex fqdn
- testnode3_path = os.path.join('data_bags', 'node', 'testnode3_mydomain_com.json')
+ testnode3_path = os.path.join(
+ 'data_bags', 'node', 'testnode3_mydomain_com.json')
with open(testnode3_path, 'r') as f:
print testnode3_path
data = json.loads(f.read())
- self.assertTrue('fqdn' in data and data['fqdn'] == 'testnode3.mydomain.com')
+ self.assertTrue(
+ 'fqdn' in data and data['fqdn'] == 'testnode3.mydomain.com')
self.assertTrue('hostname' in data and data['hostname'] == 'testnode3')
self.assertTrue('domain' in data and data['domain'] == 'mydomain.com')
def test_attribute_merge_cookbook_not_found(self):
- """Should print a warning when merging a node and a cookbook is not found"""
+ """Should print a warning when merging a node and a cookbook is not
+ found
+
+ """
# Save new node with a non-existing cookbook assigned
env.host_string = 'testnode4'
chef.save_config({"run_list": ["recipe[phantom_cookbook]"]})
@@ -331,14 +340,15 @@ def test_attribute_merge_cookbook_default(self):
def test_attribute_merge_cookbook_boolean(self):
"""Should have real boolean values for default cookbook attributes"""
chef._build_node_data_bag()
- item_path = os.path.join('data_bags', 'node', 'testnode3_mydomain_com.json')
+ item_path = os.path.join(
+ 'data_bags', 'node', 'testnode3_mydomain_com.json')
with open(item_path, 'r') as f:
data = json.loads(f.read())
self.assertTrue('vim' in data)
self.assertTrue(data['vim']['sucks'] is True)
def test_attribute_merge_site_cookbook_default(self):
- """Should have the value found in
+ """Should have the value found in
site_cookbooks/xx/recipe/attributes/default.rb
"""
@@ -363,7 +373,8 @@ def test_attribute_merge_role_default(self):
with open(item_path, 'r') as f:
data = json.loads(f.read())
self.assertTrue('subversion' in data)
- self.assertEquals(data['subversion']['repo_server'], 'role_default_repo_server')
+ self.assertEquals(
+ data['subversion']['repo_server'], 'role_default_repo_server')
self.assertTrue('other_attr' in data)
self.assertEquals(data['other_attr']['other_key'], 'nada')
@@ -386,7 +397,8 @@ def test_attribute_merge_role_override(self):
self.assertEquals(data['subversion']['password'], 'role_override_pass')
def test_attribute_merge_deep_dict(self):
- """Should deep-merge a dict when it is defined in two different places"""
+ """Should deep-merge a dict when it is defined in two different places
+ """
chef._build_node_data_bag()
item_path = os.path.join('data_bags', 'node', 'testnode2.json')
with open(item_path, 'r') as f:

0 comments on commit 29dfd78

Please sign in to comment.