def _test_required_attributes(self, resources): self.assertTrue(len(resources) > 0) for host, attributes in resources.iteritems(): for attribute in self.required_attributes: self.assertIn(attribute, attributes) self.assertIsNotNone(attributes[attribute]) self.assertNotEqual(attributes[attribute], '') if host == ResourceGenerator._server_node_name: expected = self.server_grains self.assertIn(host, resources) self.assertEqual(resources[host]['hostname'], host) else: expected = self.mine[host] self.assertIn(host, resources) self.assertEqual( resources[host]['hostname'], expected['fqdn']) self.assertEqual( resources[host]['osArch'], ResourceGenerator._os_arch(expected['cpuarch'])) self.assertEqual( resources[host]['osFamily'], ResourceGenerator._os_family(expected['kernel'])) self.assertEqual( resources[host]['osName'], expected['kernel']) self.assertEqual( resources[host]['osVersion'], expected['kernelrelease'])
def _test_required_attributes(self, resources): self.assertTrue(len(resources) > 0) for host, attributes in six.iteritems(resources): for attribute in self.required_attributes: self.assertIn(attribute, attributes) self.assertIsNotNone(attributes[attribute]) self.assertNotEqual(attributes[attribute], "") if host == ResourceGenerator._server_node_name: expected = self.server_grains self.assertIn(host, resources) self.assertEqual(resources[host]["hostname"], host) else: expected = self.mine[host] self.assertIn(host, resources) self.assertEqual(resources[host]["hostname"], expected["fqdn"]) self.assertEqual( resources[host]["osArch"], ResourceGenerator._os_arch(expected["cpuarch"]), ) self.assertEqual( resources[host]["osFamily"], ResourceGenerator._os_family(expected["kernel"]), ) self.assertEqual(resources[host]["osName"], expected["kernel"]) self.assertEqual(resources[host]["osVersion"], expected["kernelrelease"])
def _test_tags(self, resources, needed): self.assertTrue(len(resources) > 0) self.assertNotIn("!!", ResourceGenerator._dump_yaml(resources)) for _, attributes in six.iteritems(resources): self.assertIn("tags", attributes) self.assertIsNotNone(attributes["tags"]) self.assertEqual(len(attributes["tags"]), len(needed))
def _test_tags(self, resources, needed): self.assertTrue(len(resources) > 0) self.assertNotIn('!!', ResourceGenerator._dump_yaml(resources)) for host, attributes in six.iteritems(resources): self.assertIn('tags', attributes) self.assertIsNotNone(attributes['tags']) self.assertEqual(len(attributes['tags']), len(needed))
def _test_attributes(self, resources, needed): self.assertTrue(len(resources) > 0) self.assertNotIn("!!", ResourceGenerator._dump_yaml(resources)) for _, attributes in six.iteritems(resources): for attribute in needed: self.assertIn(attribute, attributes) self.assertIsNotNone(attributes[attribute]) self.assertNotEqual(attributes[attribute], "")
def test_static_attributes(self): with patch('SaltGenResource.SaltNodesCommandParser', MockParser()) as parser: with patch('salt.client.Caller', MockCaller()) as caller: call_kwargs = dict.copy(self.default_kwargs) parser.options.include_server_node = self.include_server_node call_kwargs['exclude_minion'] = self.include_server_node parser.args = ['color=yellow', 'pattern=\'polka dot\''] resources = ResourceGenerator().run() self._test_required_attributes(resources) self._test_attributes(resources, ['color', 'pattern']) for host, attributes in resources.iteritems(): self.assertEqual(resources[host]['color'], 'yellow') self.assertEqual(resources[host]['pattern'], 'polka dot') caller.cmd.assert_called_once_with(*self.default_args, **call_kwargs)
def test_unicode(self): with patch("SaltGenResource.SaltNodesCommandParser", MockParser()) as parser: with patch("salt.client.Caller", MockCaller()): call_kwargs = dict.copy(self.default_kwargs) parser.options.include_server_node = self.include_server_node call_kwargs["exclude_minion"] = self.include_server_node parser.args = [u"color=⋐⊮⊰⟒"] output = ResourceGenerator().as_yaml() self.assertNotIn("!!python/unicode", output)
def test_static_username(self): with patch('SaltGenResource.SaltNodesCommandParser', MockParser()) as parser: with patch('salt.client.Caller', MockCaller()) as caller: call_kwargs = dict.copy(self.default_kwargs) parser.options.include_server_node = self.include_server_node call_kwargs['exclude_minion'] = self.include_server_node parser.args = ['username=root'] resources = ResourceGenerator().run() self._test_required_attributes(resources) self._test_attributes(resources, ['username']) for host, attributes in resources.iteritems(): if host == ResourceGenerator._server_node_name: self.assertEqual(resources[host]['username'], parser.options.server_node_user) else: self.assertEqual(resources[host]['username'], 'root') caller.cmd.assert_called_once_with(*self.default_args, **call_kwargs)
def test_multiple_tags(self): with patch('SaltGenResource.SaltNodesCommandParser', MockParser()) as parser: with patch('salt.client.Caller', MockCaller()) as caller: call_kwargs = dict.copy(self.default_kwargs) parser.options.include_server_node = self.include_server_node call_kwargs['exclude_minion'] = self.include_server_node parser.options.tags = ['os', 'kernelrelease'] resources = ResourceGenerator().run() self._test_required_attributes(resources) self._test_tags(resources, parser.options.tags) caller.cmd.assert_called_once_with(*self.default_args, **call_kwargs)
def test_single_tag(self): with patch("SaltGenResource.SaltNodesCommandParser", MockParser()) as parser: with patch("salt.client.Caller", MockCaller()) as caller: call_kwargs = dict.copy(self.default_kwargs) parser.options.include_server_node = self.include_server_node call_kwargs["exclude_minion"] = self.include_server_node parser.options.tags = ["os"] resources = ResourceGenerator().as_dict() self._test_required_attributes(resources) self._test_tags(resources, parser.options.tags) caller.cmd.assert_called_once_with(*self.default_args, **call_kwargs)
def test_list_tag(self): with patch("SaltGenResource.SaltNodesCommandParser", MockParser()) as parser: with patch("salt.client.Caller", MockCaller()) as caller: call_kwargs = dict.copy(self.default_kwargs) parser.options.include_server_node = self.include_server_node call_kwargs["exclude_minion"] = self.include_server_node parser.options.tags = ["colors"] resources = ResourceGenerator().as_dict() self._test_required_attributes(resources) caller.cmd.assert_called_once_with(*self.default_args, **call_kwargs) for _, attributes in six.iteritems(resources): self.assertTrue(len(attributes["tags"]) > 1)
def test_falsy_attribute(self): with patch("SaltGenResource.SaltNodesCommandParser", MockParser()) as parser: with patch("salt.client.Caller", MockCaller()) as caller: call_kwargs = dict.copy(self.default_kwargs) parser.options.include_server_node = self.include_server_node call_kwargs["exclude_minion"] = self.include_server_node parser.options.attributes = ["virtual"] resources = ResourceGenerator().as_dict() self._test_required_attributes(resources) self._test_attributes(resources, parser.options.attributes) caller.cmd.assert_called_once_with(*self.default_args, **call_kwargs) for _, resource in six.iteritems(resources): self.assertIs(resource["virtual"], False)
def test_list_attribute(self): with patch('SaltGenResource.SaltNodesCommandParser', MockParser()) as parser: with patch('salt.client.Caller', MockCaller()) as caller: call_kwargs = dict.copy(self.default_kwargs) parser.options.include_server_node = self.include_server_node call_kwargs['exclude_minion'] = self.include_server_node parser.options.attributes = ['colors'] resources = ResourceGenerator().as_dict() self._test_required_attributes(resources) self._test_attributes(resources, parser.options.attributes) caller.cmd.assert_called_once_with(*self.default_args, **call_kwargs) for minion, resource in six.iteritems(resources): self.assertEqual(resource['colors'], 'red')
def test_static_attributes(self): with patch("SaltGenResource.SaltNodesCommandParser", MockParser()) as parser: with patch("salt.client.Caller", MockCaller()) as caller: call_kwargs = dict.copy(self.default_kwargs) parser.options.include_server_node = self.include_server_node call_kwargs["exclude_minion"] = self.include_server_node parser.args = ["color=yellow", "pattern='polka dot'"] resources = ResourceGenerator().as_dict() self._test_required_attributes(resources) self._test_attributes(resources, ["color", "pattern"]) for host, _ in six.iteritems(resources): self.assertEqual(resources[host]["color"], "yellow") self.assertEqual(resources[host]["pattern"], "polka dot") caller.cmd.assert_called_once_with(*self.default_args, **call_kwargs)
def test_static_username(self): with patch("SaltGenResource.SaltNodesCommandParser", MockParser()) as parser: with patch("salt.client.Caller", MockCaller()) as caller: call_kwargs = dict.copy(self.default_kwargs) parser.options.include_server_node = self.include_server_node call_kwargs["exclude_minion"] = self.include_server_node parser.args = ["username=root"] resources = ResourceGenerator().as_dict() self._test_required_attributes(resources) self._test_attributes(resources, ["username"]) for host, _ in six.iteritems(resources): if host == ResourceGenerator._server_node_name: self.assertEqual(resources[host]["username"], parser.options.server_node_user) else: self.assertEqual(resources[host]["username"], "root") caller.cmd.assert_called_once_with(*self.default_args, **call_kwargs)
def test_single_attribute(self): optional_attributes = ['os'] args = self._base_args + ['-a', optional_attributes[0], '*'] resources = ResourceGenerator(args).run() self._test_attributes(resources, self.required_attributes) self._test_attributes(resources, optional_attributes)
def test_grain_pcre_targeting(self): args = self._base_args + ['-P', 'os:.*'] resources = ResourceGenerator(args).run() self._test_attributes(resources, self.required_attributes)
def test_cidr_targeting(self): args = self._base_args + ['-S', '0.0.0.0/0'] resources = ResourceGenerator(args).run() self._test_attributes(resources, self.required_attributes)
def test_glob_targeting(self): args = self._base_args + ['*'] resources = ResourceGenerator(args).run() self._test_attributes(resources, self.required_attributes)
def test_os_arch_map2(self): os_arch = ResourceGenerator._os_arch('unknown') self.assertEqual(os_arch, 'unknown')
def test_os_arch_map1(self): os_arch = ResourceGenerator._os_arch('x86_64') self.assertEqual(os_arch, 'amd64')
def test_os_family_map2(self): os_family = ResourceGenerator._os_family('unknown') self.assertEqual(os_family, 'unknown')
def test_static_attributes(self): args = self._base_args + ['*', 'username=root', 'password=\'pw\''] resources = ResourceGenerator(args).run() self._test_attributes(resources, self.required_attributes) self._test_attributes(resources, ['username', 'password'])
def test_os_family_map1(self): os_family = ResourceGenerator._os_family("Linux") self.assertEqual(os_family, "unix")
def test_os_family_map1(self): os_family = ResourceGenerator._os_family('Linux') self.assertEqual(os_family, 'unix')
def test_multiple_attributes2(self): optional_attributes = ['os', 'os_family'] args = self._base_args + ['-a', ','.join(optional_attributes), '*'] resources = ResourceGenerator(args).run() self._test_attributes(resources, self.required_attributes) self._test_attributes(resources, optional_attributes)
def test_single_tag(self): tags = ['os'] args = self._base_args + ['-t', tags[0], '*'] resources = ResourceGenerator(args).run() self._test_attributes(resources, self.required_attributes) self._test_tags(resources, tags)
def test_os_arch_map2(self): os_arch = ResourceGenerator._os_arch("AMD64") self.assertEqual(os_arch, "amd64")
def test_multiple_tags1(self): tags = ['os', 'os_family'] args = self._base_args + ['-t', ','.join(tags), '*'] resources = ResourceGenerator(args).run() self._test_attributes(resources, self.required_attributes) self._test_tags(resources, tags)
def test_os_arch_map3(self): os_arch = ResourceGenerator._os_arch("unknown") self.assertEqual(os_arch, "unknown")