Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix environment filtering for list_nodes_with_role

  • Loading branch information...
commit 7f99e5733e5253282adf8919de6aa408e9aaf6dd 1 parent f51441c
@tobami authored
View
1  .gitignore
@@ -3,3 +3,4 @@ MANIFEST
build
dist
littlechef.egg-info
+.coverage
View
15 littlechef/lib.py
@@ -16,7 +16,6 @@
import os
import simplejson as json
import subprocess
-import re
from fabric import colors
from fabric.api import env, settings
@@ -271,7 +270,7 @@ def get_roles():
return sorted(roles, key=lambda x: x['fullname'])
-def get_nodes_with_role(rolename):
+def get_nodes_with_role(rolename, environment=None):
"""Get all nodes which include a given role,
prefix-searches are also supported
@@ -279,7 +278,7 @@ def get_nodes_with_role(rolename):
prefix_search = rolename.endswith("*")
if prefix_search:
rolename = rolename.rstrip("*")
- for n in get_nodes():
+ for n in get_nodes(environment):
if prefix_search:
roles = get_roles_in_node(n)
if any(role.startswith(rolename) for role in roles):
@@ -343,16 +342,6 @@ def get_margin(length):
return margin_left
-def parse_ip(text):
- """Extract an IPv4 IP from a text string
- Uses an IP Address Regex: http://www.regular-expressions.info/examples.html
-
- """
- ip_matches = re.findall(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b', text)
- ip = ip_matches[0] if ip_matches else None
- return ip
-
-
def credentials(*args, **kwargs):
"""Override default credentials with contents of .ssh/config,
View
2  littlechef/runner.py
@@ -278,7 +278,7 @@ def list_nodes_with_recipe(recipe):
@hosts('api')
def list_nodes_with_role(role):
"""Show all nodes which have asigned a given role"""
- for node in lib.get_nodes_with_role(role):
+ for node in lib.get_nodes_with_role(role, env.chef_environment):
lib.print_node(node)
View
10 tests/test_command.py
@@ -240,9 +240,17 @@ class TestListNodes(BaseTest):
def test_list_nodes(self):
"""Should list all nodes"""
resp, error = self.execute([fix, 'list_nodes'])
- self.assertTrue('testnode1' in resp)
+ for node in ['testnode1', 'testnode2', 'testnode3.mydomain.com']:
+ self.assertTrue(node in resp)
self.assertTrue('Recipes: subversion' in resp)
+ def test_list_nodes_in_env(self):
+ """Should list all nodes in an environment"""
+ resp, error = self.execute([fix, '--env', 'staging', 'list_nodes'])
+ self.assertTrue('testnode2' in resp)
+ self.assertFalse('testnode1' in resp)
+ self.assertFalse('testnode3.mydomain.com' in resp)
+
def test_list_nodes_detailed(self):
"""Should show a detailed list of all nodes"""
resp, error = self.execute([fix, 'list_nodes_detailed'])
View
40 tests/test_lib.py
@@ -134,17 +134,6 @@ def test_get_nodes_in_env(self):
self.assertEquals(len(lib.get_nodes("production")), 2)
self.assertEquals(len(lib.get_nodes("staging")), 1)
- def test_list_recipes(self):
- recipes = lib.get_recipes()
- self.assertEquals(len(recipes), 5)
- self.assertEquals(recipes[1]['name'], 'subversion')
- self.assertEquals(recipes[1]['description'],
- 'Includes the client recipe. Modified by site-cookbooks')
- self.assertEquals(recipes[2]['name'], 'subversion::client')
- self.assertEquals(recipes[2]['description'],
- 'Subversion Client installs subversion and some extra svn libs')
- self.assertEquals(recipes[3]['name'], 'subversion::server')
-
def test_nodes_with_role(self):
"""Should return all nodes with a given role in their run_list"""
nodes = list(lib.get_nodes_with_role('all_you_can_eat'))
@@ -161,19 +150,26 @@ def test_nodes_with_role(self):
nodes = list(lib.get_nodes_with_role(''))
self.assertEquals(len(nodes), 0)
- def test_parse_ip(self):
- """Should return an IP when the given text contains one IPv4"""
- text = "127.0.0.1"
- self.assertEquals(lib.parse_ip(text), "127.0.0.1")
-
- text = "blabla(127.0.0.1)sdfasdf"
- self.assertEquals(lib.parse_ip(text), "127.0.0.1")
+ def test_nodes_with_role_in_env(self):
+ """Should return all nodes with a given role and in the given env"""
+ nodes = list(lib.get_nodes_with_role('all_you_can_eat', 'staging'))
+ self.assertEquals(len(nodes), 1)
+ self.assertEquals(nodes[0]['name'], 'testnode2')
+ # No nodes in production with this role
+ nodes = list(lib.get_nodes_with_role('all_you_can_eat', 'production'))
+ self.assertFalse(len(nodes))
- text = "\nblabla 216.34.94.184 sdfasdf"
- self.assertEquals(lib.parse_ip(text), "216.34.94.184")
+ def test_list_recipes(self):
+ recipes = lib.get_recipes()
+ self.assertEquals(len(recipes), 5)
+ self.assertEquals(recipes[1]['name'], 'subversion')
+ self.assertEquals(recipes[1]['description'],
+ 'Includes the client recipe. Modified by site-cookbooks')
+ self.assertEquals(recipes[2]['name'], 'subversion::client')
+ self.assertEquals(recipes[2]['description'],
+ 'Subversion Client installs subversion and some extra svn libs')
+ self.assertEquals(recipes[3]['name'], 'subversion::server')
- text = "216.34.94"
- self.assertEquals(lib.parse_ip(text), None)
class TestChef(BaseTest):
def tearDown(self):
Please sign in to comment.
Something went wrong with that request. Please try again.