From ddd582269c22e7e616c6a2eb139e6c8a82b68818 Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Mon, 19 Aug 2013 23:28:26 +0200 Subject: [PATCH] increase test coverage of inventory by around 5% according to nose --- test/TestInventory.py | 29 ++++++++++++++++++++++++++++- test/restrict_pattern | 2 ++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/restrict_pattern diff --git a/test/TestInventory.py b/test/TestInventory.py index e1d043dacd0..e46027a5e61 100644 --- a/test/TestInventory.py +++ b/test/TestInventory.py @@ -1,6 +1,8 @@ import os import unittest +from nose.tools import raises +from ansible import errors from ansible.inventory import Inventory class TestInventory(unittest.TestCase): @@ -65,6 +67,16 @@ class TestInventory(unittest.TestCase): hosts = inventory.list_hosts('all') self.assertEqual(sorted(hosts), sorted(self.all_simple_hosts)) + def test_get_hosts(self): + inventory = Inventory('127.0.0.1,192.168.1.1') + hosts = inventory.get_hosts('!10.0.0.1') + hosts_all = inventory.get_hosts('all') + self.assertEqual(sorted(hosts), sorted(hosts_all)) + + def test_no_src(self): + inventory = Inventory('127.0.0.1,') + self.assertEqual(inventory.src(), None) + def test_simple_norse(self): inventory = self.simple_inventory() hosts = inventory.list_hosts("norse") @@ -170,6 +182,20 @@ class TestInventory(unittest.TestCase): hosts = inventory.list_hosts() self.assertEqual(sorted(hosts), sorted('bob%03i' %i for i in range(0, 143))) + def test_subset(self): + inventory = self.simple_inventory() + inventory.subset('odin;thor,loki') + self.assertEqual(sorted(inventory.list_hosts()), sorted(['thor','odin','loki'])) + + def test_subset_filename(self): + inventory = self.simple_inventory() + inventory.subset('@' + os.path.join(self.test_dir, 'restrict_pattern')) + self.assertEqual(sorted(inventory.list_hosts()), sorted(['thor','odin'])) + + @raises(errors.AnsibleError) + def testinvalid_entry(self): + Inventory('1234') + ################################################### ### INI file advanced tests @@ -227,7 +253,8 @@ class TestInventory(unittest.TestCase): inventory = self.complex_inventory() print "ALL NC=%s" % inventory.list_hosts("nc") - hosts = inventory.list_hosts("nc[0-1]") + # use "-1" instead of 0-1 to test the syntax, on purpose + hosts = inventory.list_hosts("nc[-1]") self.compare(hosts, expected1, sort=False) hosts = inventory.list_hosts("nc[2-3]") self.compare(hosts, expected2, sort=False) diff --git a/test/restrict_pattern b/test/restrict_pattern new file mode 100644 index 00000000000..fb16b4dda57 --- /dev/null +++ b/test/restrict_pattern @@ -0,0 +1,2 @@ +odin +thor