Skip to content

Commit

Permalink
Fix environment filtering for list_nodes_with_role
Browse files Browse the repository at this point in the history
  • Loading branch information
tobami committed Dec 15, 2011
1 parent f51441c commit 7f99e57
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ MANIFEST
build
dist
littlechef.egg-info
.coverage
15 changes: 2 additions & 13 deletions littlechef/lib.py
Expand Up @@ -16,7 +16,6 @@
import os
import simplejson as json
import subprocess
import re

from fabric import colors
from fabric.api import env, settings
Expand Down Expand Up @@ -271,15 +270,15 @@ 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
"""
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):
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion littlechef/runner.py
Expand Up @@ -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)


Expand Down
10 changes: 9 additions & 1 deletion tests/test_command.py
Expand Up @@ -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'])
Expand Down
40 changes: 18 additions & 22 deletions tests/test_lib.py
Expand Up @@ -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'))
Expand All @@ -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):
Expand Down

0 comments on commit 7f99e57

Please sign in to comment.