#!/usr/bin/env python # # $Id: $ # # Copyright 2007 Platform Computing Inc. # # Licensed under GPL version 2; See LICENSE file for details. # import os import sys import tempfile import datetime import cPickle from path import path from StringIO import StringIO import nose.tools import kusu.core.database as db from nagiosconfig import NagiosConfig tmp_dir = path(tempfile.mkdtemp(prefix='nagios-app_test', dir=os.environ.get('KUSU_TMP', '/tmp'))) cmd_dir = tmp_dir / 'commands' node_dir = tmp_dir / 'nodes' kusudb = tmp_dir / 'kusu.db' dbs = db.DB('sqlite', kusudb) node_test_template_filename = node_dir / 'node_test_template.tpl' node_test_template = """This is a test template. Replace \$node_name entries with the node's name: $node_name Replace \$node_alias entries with the node's alias: $node_alias Replace \$node_ip entries with the node's IP: $node_ip Replace \$service_definitions with the node's services: $service_definitions """ nodegroup_test_template_filename = node_dir / 'nodegroup_test_template.tpl' nodegroup_test_template = """Replace \$nodegroup_name entries with the nodegroup's name: $nodegroup_name Replace \$nodegroup_description entries with the nodegroup's description: $nodegroup_description Replace \$nodegroup_nodes with a comma-separated list of the names of the nodes belonging to this nodegroup: $nodegroup_nodes """ ntp_test_template_filename = cmd_dir / 'check_ntp.tpl' ntp_test_template = """define service{ use local-service host_name $node_name service_description NTP daemon check_command check_ntp!600!1800 } """ ssh_test_template_filename = cmd_dir / 'check_ssh.tpl' ssh_test_template = """define service{ use local-service host_name $node_name service_description SSH daemon check_command check_ssh } """ compute_disk_test_template_filename = cmd_dir / 'compute_check_disk.tpl' compute_disk_test_template = """define service{ use local-service host_name $node_name service_description Root Partition check_command check_nrpe!check_disk } """ installer_dns_test_template_filename = cmd_dir / 'installer_check_dns.tpl' installer_dns_test_template = """define service{ use local-service host_name $node_name service_description DNS Server check_command check_dns } """ installer_nodegroup_cfg_filename = \ node_dir / 'nodegroup_installer-fedora-6-i386.cfg' expected_installer_nodegroup_result = """Replace $nodegroup_name entries with the nodegroup's name: installer-fedora-6-i386 Replace $nodegroup_description entries with the nodegroup's description: installer-fedora-6-i386 Replace $nodegroup_nodes with a comma-separated list of the names of the nodes belonging to this nodegroup: mmm """ compute_nodegroup_cfg_filename = \ node_dir / 'nodegroup_compute-fedora-6-i386.cfg' expected_compute_nodegroup_result = """Replace $nodegroup_name entries with the nodegroup's name: compute-fedora-6-i386 Replace $nodegroup_description entries with the nodegroup's description: compute-fedora-6-i386 Replace $nodegroup_nodes with a comma-separated list of the names of the nodes belonging to this nodegroup: compute-00-00, compute-00-01 """ installer_node_cfg_filename = node_dir / 'node_mmm.cfg' expected_installer_node_result = """This is a test template. Replace $node_name entries with the node's name: mmm Replace $node_alias entries with the node's alias: mmm Replace $node_ip entries with the node's IP: mmm Replace $service_definitions with the node's services: define service{ use local-service host_name mmm service_description NTP daemon check_command check_ntp!600!1800 } define service{ use local-service host_name mmm service_description SSH daemon check_command check_ssh } define service{ use local-service host_name mmm service_description DNS Server check_command check_dns } """ compute0_node_cfg_filename = node_dir / 'node_compute-00-00.cfg' expected_compute0_node_result = """This is a test template. Replace $node_name entries with the node's name: compute-00-00 Replace $node_alias entries with the node's alias: compute-00-00 Replace $node_ip entries with the node's IP: compute-00-00 Replace $service_definitions with the node's services: define service{ use local-service host_name compute-00-00 service_description Root Partition check_command check_nrpe!check_disk } define service{ use local-service host_name compute-00-00 service_description NTP daemon check_command check_ntp!600!1800 } define service{ use local-service host_name compute-00-00 service_description SSH daemon check_command check_ssh } """ compute1_node_cfg_filename = node_dir / 'node_compute-00-01.cfg' expected_compute1_node_result = """This is a test template. Replace $node_name entries with the node's name: compute-00-01 Replace $node_alias entries with the node's alias: compute-00-01 Replace $node_ip entries with the node's IP: compute-00-01 Replace $service_definitions with the node's services: define service{ use local-service host_name compute-00-01 service_description Root Partition check_command check_nrpe!check_disk } define service{ use local-service host_name compute-00-01 service_description NTP daemon check_command check_ntp!600!1800 } define service{ use local-service host_name compute-00-01 service_description SSH daemon check_command check_ssh } """ def setup(): dbs.createTables() cmd_dir.mkdir() node_dir.mkdir() f = open(node_test_template_filename, 'w') f.write(node_test_template) f.flush() f.close() f = open(nodegroup_test_template_filename, 'w') f.write(nodegroup_test_template) f.flush() f.close() f = open(ntp_test_template_filename, 'w') f.write(ntp_test_template) f.flush() f.close() f = open(ssh_test_template_filename, 'w') f.write(ssh_test_template) f.flush() f.close() f = open(compute_disk_test_template_filename, 'w') f.write(compute_disk_test_template) f.flush() f.close() f = open(installer_dns_test_template_filename, 'w') f.write(installer_dns_test_template) f.flush() f.close() def teardown(): if tmp_dir.exists(): tmp_dir.rmtree() def reset_database(): for nic in dbs.Nics.select(): nic.delete() for node in dbs.Nodes.select(): node.delete() for network in dbs.Networks.select(): network.delete() for nodegroup in dbs.NodeGroups.select(): nodegroup.delete() for repo in dbs.Repos.select(): repo.delete() dbs.flush() repo = dbs.Repos(repoid=1000, reponame='Repo for fedora-6-i386', repository='/depot/repos/1000', installers='172.25.208.17;172.20.0.1', ostype='fedora-6-i386') repo.save() dbs.flush() installer_nodegroup = dbs.NodeGroups( repoid=repo.repoid, ngname='installer-fedora-6-i386', installtype='package', ngdesc=None, nameformat='installer-#RR=#NN', kernel='kernel-fedora-6-i386', initrd='initrd-fedora-6-i386.img', kparams='text noipv6 kssendmac selinux=0', type='installer') compute_nodegroup = dbs.NodeGroups( repoid=repo.repoid, ngname='compute-fedora-6-i386', installtype='package', ngdesc=None, nameformat='compute-#RR=#NN', kernel='kernel-fedora-6-i386', initrd='initrd-fedora-6-i386.img', kparams='text noipv6 kssendmac selinux=0', type='compute') imaged_nodegroup = dbs.NodeGroups( repoid=repo.repoid, ngname='compute-imaged-fedora-6-i386', installtype='disked', ngdesc=None, nameformat='host#NNN', kernel='kernel.disked.3', initrd='initrd.disked.3.img', kparams=None, type='compute') diskless_nodegroup = dbs.NodeGroups( repoid=repo.repoid, ngname='compute-diskless-fedora-6-i386', installtype='diskless', ngdesc=None, nameformat='host#NNN', kernel='kernel.diskless.4', initrd='initrd.diskless.4.img', kparams=None, type='compute') other_nodegroup = dbs.NodeGroups( repoid=None, ngname='unmanaged', installtype='unmanaged', ngdesc=None, nameformat='device#NNN', kernel=None, initrd=None, kparams=None, type='other') installer_nodegroup.save() compute_nodegroup.save() imaged_nodegroup.save() diskless_nodegroup.save() other_nodegroup.save() dbs.flush() public_network = dbs.Networks(network='172.25.208.0', subnet='255.255.255.0', device='eth1', suffix='-eth1', gateway='172.25.208.1', options=None, netname='public', startip='172.25.208.1', inc=1, type='public', usingdhcp=False) cluster_network = dbs.Networks(network='172.20.0.0', subnet='255.255.0.0', device='eth0', suffix='-eth0', gateway='172.20.0.1', options=None, netname='cluster', startip='172.20.0.1', inc=1, type='provision', usingdhcp=False) public_network.save() cluster_network.save() installer_nodegroup.networks.append(public_network) installer_nodegroup.networks.append(cluster_network) compute_nodegroup.networks.append(cluster_network) imaged_nodegroup.networks.append(cluster_network) diskless_nodegroup.networks.append(cluster_network) other_nodegroup.networks.append(cluster_network) dbs.flush() master = dbs.Nodes(ngid=installer_nodegroup.ngid, name='mmm', kernel=None, initrd=None, kparams=None, state='Installed', bootfrom=True, lastupdate=datetime.datetime.now(), rack=0, rank=0) compute0 = dbs.Nodes(ngid=compute_nodegroup.ngid, name='compute-00-00', kernel=None, initrd=None, kparams=None, state='Installed', bootfrom=True, lastupdate=datetime.datetime.now(), rack=0, rank=0) compute1 = dbs.Nodes(ngid=compute_nodegroup.ngid, name='compute-00-01', kernel=None, initrd=None, kparams=None, state='Installed', bootfrom=True, lastupdate=datetime.datetime.now(), rack=0, rank=1) master.save() compute0.save() compute1.save() dbs.flush() dbs.Nics(nid=master.nid, netid=public_network.netid, mac='00:0c:29:08:64:d8', ip='172.25.208.17', boot=True) dbs.Nics(nid=master.nid, netid=cluster_network.netid, mac='00:0c:29:08:64:ce', ip='172.20.0.1', boot=True) dbs.Nics(nid=compute0.nid, netid=cluster_network.netid, mac='00:0c:29:b9:19:6f', ip='172.20.0.2', boot=True) dbs.Nics(nid=compute1.nid, netid=cluster_network.netid, mac='00:0c:29:48:5f:89', ip='172.20.0.3', boot=True) dbs.flush() def remove_nodes_from_db(specified_nodes): nodes = dbs.Nodes.select_by(dbs.Nodes.c.name.in_(*specified_nodes)) for node in nodes: for nic in node.nics: nic.delete() node.delete() dbs.flush() class TestGenerateConfiguration: def setup(self): reset_database() self.nagios_config = NagiosConfig() self.nagios_config.dbs = dbs self.nagios_config.command_template_dir = cmd_dir self.nagios_config.node_cfg_dir = node_dir self.nagios_config.nodegroup_tpl = nodegroup_test_template_filename self.nagios_config.node_tpl = node_test_template_filename def teardown(self): # Clean up existing config files. for cfg_file in node_dir.walkfiles(pattern='*.cfg'): cfg_file.remove() def test_configure_all_nodegroups_and_nodes(self): self.nagios_config.generate() f = open(installer_nodegroup_cfg_filename, 'r') installer_nodegroup_result = f.read() f.close() assert installer_nodegroup_result == expected_installer_nodegroup_result f = open(compute_nodegroup_cfg_filename, 'r') compute_nodegroup_result = f.read() f.close() assert compute_nodegroup_result == expected_compute_nodegroup_result f = open(installer_node_cfg_filename, 'r') installer_node_result = f.read() f.close() assert installer_node_result == expected_installer_node_result f = open(compute0_node_cfg_filename, 'r') compute0_node_result = f.read() f.close() assert compute0_node_result == expected_compute0_node_result f = open(compute1_node_cfg_filename, 'r') compute1_node_result = f.read() f.close() assert compute1_node_result == expected_compute1_node_result # We only want nodegroups containing nodes to be configured. imaged_cfg = node_dir / 'nodegroup_compute-imaged-fedora-6-i386.cfg' assert not imaged_cfg.exists() diskless_cfg = node_dir / 'nodegroup_compute-diskless-fedora-6-i386.cfg' assert not diskless_cfg.exists() unmanaged_cfg = node_dir / 'nodegroup_unmanaged.cfg' assert not unmanaged_cfg.exists() def test_configure_two_specified_nodes(self): self.nagios_config.specified_nodes = ['compute-00-01', 'mmm'] self.nagios_config.generate() f = open(installer_nodegroup_cfg_filename, 'r') installer_nodegroup_result = f.read() f.close() assert installer_nodegroup_result == expected_installer_nodegroup_result f = open(compute_nodegroup_cfg_filename, 'r') compute_nodegroup_result = f.read() f.close() assert compute_nodegroup_result == expected_compute_nodegroup_result f = open(installer_node_cfg_filename, 'r') installer_node_result = f.read() f.close() assert installer_node_result == expected_installer_node_result f = open(compute1_node_cfg_filename, 'r') compute1_node_result = f.read() f.close() assert compute1_node_result == expected_compute1_node_result # We don't expect compute-00-00 to be generated since it wasn't # specified. assert not compute0_node_cfg_filename.exists() # We only want nodegroups containing nodes to be configured. imaged_cfg = node_dir / 'nodegroup_compute-imaged-fedora-6-i386.cfg' assert not imaged_cfg.exists() diskless_cfg = node_dir / 'nodegroup_compute-diskless-fedora-6-i386.cfg' assert not diskless_cfg.exists() unmanaged_cfg = node_dir / 'nodegroup_unmanaged.cfg' assert not unmanaged_cfg.exists() def test_configure_one_specified_node(self): self.nagios_config.specified_nodes = ['compute-00-00'] self.nagios_config.generate() f = open(compute_nodegroup_cfg_filename, 'r') compute_nodegroup_result = f.read() f.close() assert compute_nodegroup_result == expected_compute_nodegroup_result f = open(compute0_node_cfg_filename, 'r') compute1_node_result = f.read() f.close() assert compute1_node_result == expected_compute0_node_result # We don't expect compute-00-01 or mmm to be generated since they # weren't specified. assert not compute1_node_cfg_filename.exists() assert not installer_node_cfg_filename.exists() # We only want nodegroups containing nodes to be configured. assert not installer_nodegroup_cfg_filename.exists() imaged_cfg = node_dir / 'nodegroup_compute-imaged-fedora-6-i386.cfg' assert not imaged_cfg.exists() diskless_cfg = node_dir / 'nodegroup_compute-diskless-fedora-6-i386.cfg' assert not diskless_cfg.exists() unmanaged_cfg = node_dir / 'nodegroup_unmanaged.cfg' assert not unmanaged_cfg.exists() def test_nodegroups_generation(self): nodegroups = dbs.NodeGroups.select_by( dbs.NodeGroups.c.ngname.in_('installer-fedora-6-i386', 'compute-fedora-6-i386')) self.nagios_config.generate_nodegroups(nodegroups, str(nodegroup_test_template_filename)) f = open(installer_nodegroup_cfg_filename, 'r') installer_nodegroup_result = f.read() f.close() assert installer_nodegroup_result == expected_installer_nodegroup_result f = open(compute_nodegroup_cfg_filename, 'r') compute_nodegroup_result = f.read() f.close() assert compute_nodegroup_result == expected_compute_nodegroup_result def test_nodes_generation(self): nodegroups = dbs.NodeGroups.select_by( dbs.NodeGroups.c.ngname.in_('installer-fedora-6-i386', 'compute-fedora-6-i386')) nodes = [] for nodegroup in nodegroups: for node in nodegroup.nodes: nodes.append((node, nodegroup.type)) self.nagios_config.generate_nodes(nodes, str(node_test_template_filename)) f = open(installer_node_cfg_filename, 'r') installer_node_result = f.read() f.close() assert installer_node_result == expected_installer_node_result f = open(compute0_node_cfg_filename, 'r') compute0_node_result = f.read() f.close() assert compute0_node_result == expected_compute0_node_result f = open(compute1_node_cfg_filename, 'r') compute1_node_result = f.read() f.close() assert compute1_node_result == expected_compute1_node_result def test_basics(self): nodegroup = dbs.NodeGroups.selectfirst() node = dbs.Nodes.selectfirst() nodegroup_result = self.nagios_config.generate_nodegroup(nodegroup, str(nodegroup_test_template_filename)) assert nodegroup_result == expected_installer_nodegroup_result node_result = self.nagios_config.generate_node(node, 'installer', str(node_test_template_filename)) assert node_result == expected_installer_node_result class TestRemoveConfiguration: def setup(self): reset_database() self.nagios_config = NagiosConfig() self.nagios_config.dbs = dbs self.nagios_config.command_template_dir = cmd_dir self.nagios_config.node_cfg_dir = node_dir self.nagios_config.nodegroup_tpl = nodegroup_test_template_filename self.nagios_config.node_tpl = node_test_template_filename installer_nodegroup_cfg_filename.touch() compute_nodegroup_cfg_filename.touch() installer_node_cfg_filename.touch() compute0_node_cfg_filename.touch() compute1_node_cfg_filename.touch() def test_remove_one_specified_node(self): specified_nodes = ['compute-00-01'] self.nagios_config.specified_nodes = specified_nodes self.nagios_config.action = 'remove' self.nagios_config.remove() assert not compute1_node_cfg_filename.exists() def test_remove_two_specified_nodes(self): specified_nodes = ['compute-00-00', 'mmm'] self.nagios_config.specified_nodes = specified_nodes self.nagios_config.action = 'remove' self.nagios_config.remove() assert not installer_node_cfg_filename.exists() assert not compute0_node_cfg_filename.exists() assert not installer_nodegroup_cfg_filename.exists() def test_remove_all_nodes_in_nodegroup(self): specified_nodes = ['compute-00-00', 'compute-00-01'] self.nagios_config.specified_nodes = specified_nodes self.nagios_config.action = 'remove' self.nagios_config.remove() assert not compute0_node_cfg_filename.exists() assert not compute1_node_cfg_filename.exists() assert not compute_nodegroup_cfg_filename.exists() def test_clean_all(self): self.nagios_config.clean_all() assert not installer_nodegroup_cfg_filename.exists() assert not compute_nodegroup_cfg_filename.exists() assert not installer_node_cfg_filename.exists() assert not compute0_node_cfg_filename.exists() assert not compute1_node_cfg_filename.exists() class TestRunGenerate: def setup(self): self.saved_sys_argv = sys.argv sys.argv = [] reset_database() self.nagios_config = NagiosConfig() self.nagios_config.dbs = dbs self.nagios_config.command_template_dir = cmd_dir self.nagios_config.node_cfg_dir = node_dir self.nagios_config.nodegroup_tpl = nodegroup_test_template_filename self.nagios_config.node_tpl = node_test_template_filename self.nagios_config.action = 'add' def teardown(self): sys.argv = self.saved_sys_argv # Clean up existing config files. for cfg_file in node_dir.walkfiles(pattern='*.cfg'): cfg_file.remove() def test_run_generate_all(self): sys.argv = ['nagiosconfig.py', '--regen'] self.nagios_config.run() f = open(installer_nodegroup_cfg_filename, 'r') installer_nodegroup_result = f.read() f.close() assert installer_nodegroup_result == expected_installer_nodegroup_result f = open(compute_nodegroup_cfg_filename, 'r') compute_nodegroup_result = f.read() f.close() assert compute_nodegroup_result == expected_compute_nodegroup_result f = open(installer_node_cfg_filename, 'r') installer_node_result = f.read() f.close() assert installer_node_result == expected_installer_node_result f = open(compute0_node_cfg_filename, 'r') compute0_node_result = f.read() f.close() assert compute0_node_result == expected_compute0_node_result f = open(compute1_node_cfg_filename, 'r') compute1_node_result = f.read() f.close() assert compute1_node_result == expected_compute1_node_result # We only want nodegroups containing nodes to be configured. imaged_cfg = node_dir / 'nodegroup_compute-imaged-fedora-6-i386.cfg' assert not imaged_cfg.exists() diskless_cfg = node_dir / 'nodegroup_compute-diskless-fedora-6-i386.cfg' assert not diskless_cfg.exists() unmanaged_cfg = node_dir / 'nodegroup_unmanaged.cfg' assert not unmanaged_cfg.exists() def test_run_generate_two_nodes(self): sys.argv = ['nagiosconfig.py', '-a', 'compute-00-01,mmm'] self.nagios_config.run() f = open(installer_nodegroup_cfg_filename, 'r') installer_nodegroup_result = f.read() f.close() assert installer_nodegroup_result == expected_installer_nodegroup_result f = open(compute_nodegroup_cfg_filename, 'r') compute_nodegroup_result = f.read() f.close() expected_result = \ expected_compute_nodegroup_result.replace('compute-00-00, ', '') assert compute_nodegroup_result == expected_result f = open(installer_node_cfg_filename, 'r') installer_node_result = f.read() f.close() assert installer_node_result == expected_installer_node_result f = open(compute1_node_cfg_filename, 'r') compute1_node_result = f.read() f.close() assert compute1_node_result == expected_compute1_node_result # compute-00-00 node configuration files should not exist. assert not compute0_node_cfg_filename.exists() # We only want nodegroups containing nodes to be configured. imaged_cfg = node_dir / 'nodegroup_compute-imaged-fedora-6-i386.cfg' assert not imaged_cfg.exists() diskless_cfg = node_dir / 'nodegroup_compute-diskless-fedora-6-i386.cfg' assert not diskless_cfg.exists() unmanaged_cfg = node_dir / 'nodegroup_unmanaged.cfg' assert not unmanaged_cfg.exists() def test_run_generate_one_node(self): # We don't want to generate the compute nodegroup config file. remove_nodes_from_db(['compute-00-00', 'compute-00-01']) sys.argv = ['nagiosconfig.py', '-a', 'mmm'] self.nagios_config.run() f = open(installer_nodegroup_cfg_filename, 'r') installer_nodegroup_result = f.read() f.close() assert installer_nodegroup_result == expected_installer_nodegroup_result f = open(installer_node_cfg_filename, 'r') installer_node_result = f.read() f.close() assert installer_node_result == expected_installer_node_result # No compute node(group) configuration files should exist. assert not compute0_node_cfg_filename.exists() assert not compute1_node_cfg_filename.exists() assert not compute_nodegroup_cfg_filename.exists() # We only want nodegroups containing nodes to be configured. imaged_cfg = node_dir / 'nodegroup_compute-imaged-fedora-6-i386.cfg' assert not imaged_cfg.exists() diskless_cfg = node_dir / 'nodegroup_compute-diskless-fedora-6-i386.cfg' assert not diskless_cfg.exists() unmanaged_cfg = node_dir / 'nodegroup_unmanaged.cfg' assert not unmanaged_cfg.exists() class TestRunRemove: def setup(self): self.saved_sys_argv = sys.argv sys.argv = [] reset_database() self.nagios_config = NagiosConfig() self.nagios_config.dbs = dbs self.nagios_config.command_template_dir = cmd_dir self.nagios_config.node_cfg_dir = node_dir self.nagios_config.nodegroup_tpl = nodegroup_test_template_filename self.nagios_config.node_tpl = node_test_template_filename self.nagios_config.action = 'remove' installer_nodegroup_cfg_filename.touch() compute_nodegroup_cfg_filename.touch() installer_node_cfg_filename.touch() compute0_node_cfg_filename.touch() compute1_node_cfg_filename.touch() def teardown(self): sys.argv = self.saved_sys_argv def test_run_remove_one_node(self): sys.argv = ['nagiosconfig.py', '-r', 'compute-00-00'] self.nagios_config.run() assert not compute0_node_cfg_filename.exists() def test_remove_two_specified_nodes(self): sys.argv = ['nagiosconfig.py', '-r', 'compute-00-00,mmm'] self.nagios_config.run() assert not installer_node_cfg_filename.exists() assert not compute0_node_cfg_filename.exists() assert not installer_nodegroup_cfg_filename.exists() def test_remove_all_nodes_in_nodegroup(self): sys.argv = ['nagiosconfig.py', '-r', 'compute-00-00,compute-00-01'] self.nagios_config.run() assert not compute0_node_cfg_filename.exists() assert not compute1_node_cfg_filename.exists() assert not compute_nodegroup_cfg_filename.exists() class TestCommandOptions: def setup(self): self.saved_sys_argv = sys.argv self.nagios_config = NagiosConfig() def teardown(self): sys.argv = self.saved_sys_argv @nose.tools.raises(SystemExit) def test_illegal_command_options(self): sys.argv = ['nagiosconfig.py', '-r', 'compute-00-00', '-a', 'mmm'] self.nagios_config.run() @nose.tools.raises(SystemExit) def test_unspecified_nodes_to_add(self): sys.argv = ['nagiosconfig.py', '-a'] self.nagios_config.run() @nose.tools.raises(SystemExit) def test_unspecified_nodes_to_remove(self): sys.argv = ['nagiosconfig.py', '-r'] self.nagios_config.run() class TestGenerateFromFile: def setup(self): self.saved_sys_argv = sys.argv self.nagios_config = NagiosConfig() self.nagios_config.dbs = None self.nagios_config.command_template_dir = cmd_dir self.nagios_config.node_cfg_dir = node_dir self.nagios_config.nodegroup_tpl = nodegroup_test_template_filename self.nagios_config.node_tpl = node_test_template_filename self.nagios_config.action = 'add' def teardown(self): sys.argv = self.saved_sys_argv def test_generate_config_one_node_from_pickle_file(self): self.fng = {'installer-fedora-6-i386': {'type': 'installer', 'nodes': ['mmm']}} # Pickle the fake nodegroup for use in test. f = open(tmp_dir / 'pickled.txt', 'w') cPickle.dump(self.fng, f) f.close() sys.argv = ['nagiosconfig.py', '--regen', '-f', tmp_dir / 'pickled.txt'] self.nagios_config.run() f = open(installer_nodegroup_cfg_filename, 'r') installer_nodegroup_result = f.read() f.close() assert installer_nodegroup_result == expected_installer_nodegroup_result f = open(installer_node_cfg_filename, 'r') installer_node_result = f.read() f.close() assert installer_node_result == expected_installer_node_result # No compute node(group) configuration files should exist. assert not compute0_node_cfg_filename.exists() assert not compute1_node_cfg_filename.exists() assert not compute_nodegroup_cfg_filename.exists() # We only want nodegroups containing nodes to be configured. imaged_cfg = node_dir / 'nodegroup_compute-imaged-fedora-6-i386.cfg' assert not imaged_cfg.exists() diskless_cfg = node_dir / 'nodegroup_compute-diskless-fedora-6-i386.cfg' assert not diskless_cfg.exists() unmanaged_cfg = node_dir / 'nodegroup_unmanaged.cfg' assert not unmanaged_cfg.exists() def test_generate_config_three_nodes_from_pickle_file(self): self.fng = {'compute-fedora-6-i386': {'type': 'compute', 'nodes': ['compute-00-00', 'compute-00-01']}, 'installer-fedora-6-i386': {'type': 'installer', 'nodes': ['mmm']}} # Pickle the fake nodegroup for use in test. f = open(tmp_dir / 'pickled.txt', 'w') cPickle.dump(self.fng, f) f.close() sys.argv = ['nagiosconfig.py', '--regen', '-f', tmp_dir / 'pickled.txt'] self.nagios_config.run() f = open(installer_nodegroup_cfg_filename, 'r') installer_nodegroup_result = f.read() f.close() assert installer_nodegroup_result == expected_installer_nodegroup_result f = open(installer_node_cfg_filename, 'r') installer_node_result = f.read() f.close() assert installer_node_result == expected_installer_node_result f = open(compute_nodegroup_cfg_filename, 'r') compute_nodegroup_result = f.read() f.close() assert compute_nodegroup_result == expected_compute_nodegroup_result f = open(compute0_node_cfg_filename, 'r') compute0_node_result = f.read() f.close() assert compute0_node_result == expected_compute0_node_result f = open(compute1_node_cfg_filename, 'r') compute1_node_result = f.read() f.close() assert compute1_node_result == expected_compute1_node_result # We only want nodegroups containing nodes to be configured. imaged_cfg = node_dir / 'nodegroup_compute-imaged-fedora-6-i386.cfg' assert not imaged_cfg.exists() diskless_cfg = node_dir / 'nodegroup_compute-diskless-fedora-6-i386.cfg' assert not diskless_cfg.exists() unmanaged_cfg = node_dir / 'nodegroup_unmanaged.cfg' assert not unmanaged_cfg.exists() def test_existing_config_files_removed(self): self.fng = {'compute-fedora-6-i386': {'type': 'compute', 'nodes': ['compute-00-00', 'compute-00-01']}} # Pickle the fake nodegroup for use in test. f = open(tmp_dir / 'pickled.txt', 'w') cPickle.dump(self.fng, f) f.close() # Write the installer nodegroup files manually. They should be removed # when the app is run. f = open(installer_nodegroup_cfg_filename, 'w') f.write(expected_installer_nodegroup_result) f.close() f = open(installer_node_cfg_filename, 'w') f.write(expected_installer_node_result) f.close() # Run the app. sys.argv = ['nagiosconfig.py', '--regen', '-f', tmp_dir / 'pickled.txt'] self.nagios_config.run() f = open(compute_nodegroup_cfg_filename, 'r') compute_nodegroup_result = f.read() f.close() assert compute_nodegroup_result == expected_compute_nodegroup_result f = open(compute0_node_cfg_filename, 'r') compute0_node_result = f.read() f.close() assert compute0_node_result == expected_compute0_node_result f = open(compute1_node_cfg_filename, 'r') compute1_node_result = f.read() f.close() assert compute1_node_result == expected_compute1_node_result # No installer node(group) configuration files should exist. assert not installer_nodegroup_cfg_filename.exists() assert not installer_node_cfg_filename.exists() # We only want nodegroups containing nodes to be configured. imaged_cfg = node_dir / 'nodegroup_compute-imaged-fedora-6-i386.cfg' assert not imaged_cfg.exists() diskless_cfg = node_dir / 'nodegroup_compute-diskless-fedora-6-i386.cfg' assert not diskless_cfg.exists() unmanaged_cfg = node_dir / 'nodegroup_unmanaged.cfg' assert not unmanaged_cfg.exists()