class SwitchTestsCreate(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.network = luna.Network(mongo_db=self.db, create=True, name='cluster', NETWORK='10.141.0.0', PREFIX=16) def tearDown(self): self.sandbox.cleanup() def test_create_switch_with_defaults(self): switch = luna.Switch(mongo_db=self.db, create=True, name='switch01', network=self.network.name, ip='10.141.100.1') self.assertTrue(switch)
class ClusterReadTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path def tearDown(self): self.sandbox.cleanup() def test_read_non_existing_cluster(self): self.assertRaises(RuntimeError, luna.Cluster, mongo_db=self.db) def test_cluster_read(self): luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) cluster = luna.Cluster(mongo_db=self.db) doc = self.db['cluster'].find_one({'_id': cluster._id}) expected['path'] = self.path for attr in expected: self.assertEqual(doc[attr], expected[attr])
class BMCSetupCreateTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) def tearDown(self): self.sandbox.cleanup() def test_create_bmcsetup_with_defaults(self): bmc = luna.BMCSetup(name='testbmc', mongo_db=self.db, create=True) doc = self.db['bmcsetup'].find_one({'_id': bmc._id}) expected = {'userid': 3, 'user': '******', 'password': '******', 'netchannel': 1, 'mgmtchannel': 1, 'comment': ''} for attr in expected: self.assertEqual(doc[attr], expected[attr]) def test_create_bmcsetup_with_wrong_attr_type(self): self.assertRaises(RuntimeError, luna.BMCSetup, name='testbmc', mongo_db=self.db, create=True, userid='3')
class BMCSetupReadTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.bmc = luna.BMCSetup(name='testbmc', mongo_db=self.db, create=True) def tearDown(self): self.sandbox.cleanup() def test_read_non_existing_bmcsetup(self): self.assertRaises(RuntimeError, luna.BMCSetup, mongo_db=self.db, name='non_existing') def test_read_bmcsetup(self): bmc = luna.BMCSetup(name='testbmc', mongo_db=self.db) doc = self.db['bmcsetup'].find_one({'_id': bmc._id}) expected = {'userid': 3, 'user': '******', 'password': '******', 'netchannel': 1, 'mgmtchannel': 1} for attr in expected: self.assertEqual(doc[attr], expected[attr])
class ClusterReadTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path def tearDown(self): self.sandbox.cleanup() def test_read_non_existing_cluster(self): self.assertRaises(RuntimeError, luna.Cluster, mongo_db=self.db) def test_cluster_read(self): luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) cluster = luna.Cluster(mongo_db=self.db) doc = self.db['cluster'].find_one({'_id': cluster._id}) expected['path'] = self.path for attr in expected: self.assertEqual(doc[attr], expected[attr])
class SwitchTestsCreate(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.network = luna.Network(mongo_db=self.db, create=True, name='cluster', NETWORK='10.141.0.0', PREFIX=16) def tearDown(self): self.sandbox.cleanup() def test_create_switch_with_defaults(self): switch = luna.Switch(mongo_db=self.db, create=True, name='switch01', network=self.network.name, ip='10.141.100.1') self.assertTrue(switch)
class SwitchTestsChange(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.network = luna.Network(mongo_db=self.db, create=True, name='cluster', NETWORK='10.141.0.0', PREFIX=16) self.switch = luna.Switch(mongo_db=self.db, create=True, name='switch01', network=self.network.name, ip='10.141.100.1') def tearDown(self): self.sandbox.cleanup() def test_delete_switch(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.' ) self.assertTrue(self.switch.delete()) doc = self.db['switch'].find_one({'name': self.switch.name}) self.assertIsNone(doc)
class NetworkReadTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) def tearDown(self): self.sandbox.cleanup() def test_read_non_existing_network(self): self.assertRaises(RuntimeError, luna.Network, mongo_db=self.db) def test_read_network(self): luna.Network(name='testnet', mongo_db=self.db, create=True, NETWORK='172.16.1.0', PREFIX='24') net = luna.Network(name='testnet', mongo_db=self.db) self.assertIsInstance(net, luna.Network)
class ClusterUtilsTests(unittest.TestCase): @classmethod def setUpClass(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) @classmethod def tearDownClass(self): self.sandbox.cleanup()
class ClusterUtilsTests(unittest.TestCase): @classmethod def setUpClass(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) @classmethod def tearDownClass(self): self.sandbox.cleanup()
class BMCSetupCreateTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) def tearDown(self): self.sandbox.cleanup() def test_create_bmcsetup_with_defaults(self): bmc = luna.BMCSetup(name='testbmc', mongo_db=self.db, create=True) doc = self.db['bmcsetup'].find_one({'_id': bmc._id}) expected = { 'userid': 3, 'user': '******', 'password': '******', 'netchannel': 1, 'mgmtchannel': 1, 'comment': '' } for attr in expected: self.assertEqual(doc[attr], expected[attr]) def test_create_bmcsetup_with_wrong_attr_type(self): self.assertRaises(RuntimeError, luna.BMCSetup, name='testbmc', mongo_db=self.db, create=True, userid='3')
class ClusterCreateTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path def tearDown(self): self.sandbox.cleanup() def test_init_cluster_with_defaults(self): cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) doc = self.db['cluster'].find_one({'_id': cluster._id}) expected['path'] = self.path for attr in expected: self.assertEqual(doc[attr], expected[attr])
class BMCSetupReadTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.bmc = luna.BMCSetup(name='testbmc', mongo_db=self.db, create=True) def tearDown(self): self.sandbox.cleanup() def test_read_non_existing_bmcsetup(self): self.assertRaises(RuntimeError, luna.BMCSetup, mongo_db=self.db, name='non_existing') def test_read_bmcsetup(self): bmc = luna.BMCSetup(name='testbmc', mongo_db=self.db) doc = self.db['bmcsetup'].find_one({'_id': bmc._id}) expected = { 'userid': 3, 'user': '******', 'password': '******', 'netchannel': 1, 'mgmtchannel': 1 } for attr in expected: self.assertEqual(doc[attr], expected[attr])
class ClusterCreateTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path def tearDown(self): self.sandbox.cleanup() def test_init_cluster_with_defaults(self): cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) doc = self.db['cluster'].find_one({'_id': cluster._id}) expected['path'] = self.path for attr in expected: self.assertEqual(doc[attr], expected[attr])
class SwitchTestsChange(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.network = luna.Network(mongo_db=self.db, create=True, name='cluster', NETWORK='10.141.0.0', PREFIX=16) self.switch = luna.Switch(mongo_db=self.db, create=True, name='switch01', network=self.network.name, ip='10.141.100.1') def tearDown(self): self.sandbox.cleanup() def test_delete_switch(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') self.assertTrue(self.switch.delete()) doc = self.db['switch'].find_one({'name': self.switch.name}) self.assertIsNone(doc)
class GetMacTests(unittest.TestCase): """ Test for Group.get_macs() """ @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group(name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.network = luna.Network(name="cluster", mongo_db=self.db, create=True, NETWORK='10.141.0.0', PREFIX=16) self.node = luna.Node(group=self.group.name, mongo_db=self.db, create=True) self.node.set_mac('00:11:22:33:44:55') self.group = luna.Group(name=self.group.name, mongo_db=self.db) def tearDown(self): self.sandbox.cleanup() def test_wo_net_configured(self): self.assertEqual(self.group.get_macs(self.network), {}) def test_wo_nodes(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') self.group.set_net_to_if('eth0', self.network.name) self.node.delete() self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.assertEqual(self.group.get_macs(self.network), {}) def test_w_another_net(self): net1 = luna.Network(name="net1", mongo_db=self.db, create=True, NETWORK='10.149.0.0', PREFIX=16) self.group.set_net_to_if('eth0', net1.name) self.assertEqual(self.group.get_macs(self.network), {}) def test_w_single_interface(self): self.group.set_net_to_if('eth0', self.network.name) def test_w_BOOTIF_wo_net(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') self.group.add_interface('BOOTIF') self.group.set_net_to_if('eth0', self.network.name) self.assertEqual( self.group.get_macs(self.network), {'node001': { 'ip': '10.141.0.1', 'mac': '00:11:22:33:44:55' }}) def test_w_BOOTIF_w_net(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') self.group.add_interface('BOOTIF') self.group.set_net_to_if('eth0', self.network.name) self.group.set_net_to_if('BOOTIF', self.network.name) self.assertEqual( self.group.get_macs(self.network), {'node001': { 'ip': '10.141.0.2', 'mac': '00:11:22:33:44:55' }}) def test_w_BMC(self): self.group.add_interface('BMC') self.group.set_net_to_if('BMC', self.network.name) self.assertEqual(self.group.get_macs(self.network), {}) def test_w_several_prov_ifs(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') self.group.add_interface('eth1') self.group.add_interface('BMC') self.group.set_net_to_if('eth1', self.network.name) self.group.set_net_to_if('BMC', self.network.name) self.group.set_net_to_if('eth0', self.network.name) self.assertIn( self.group.get_macs(self.network)[self.node.name]['ip'], ['10.141.0.1', '10.141.0.2', '10.141.0.3']) def test_node_wo_mac(self): self.group.set_net_to_if('eth0', self.network.name) self.node.set_mac('') self.assertEqual(self.group.get_macs(self.network), {})
class NetworkCreateTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) def tearDown(self): self.sandbox.cleanup() def test_create_network_with_defaults(self): net = luna.Network(name='testnet', mongo_db=self.db, create=True, NETWORK='172.16.1.0', PREFIX=24) self.assertIsInstance(net, luna.Network) def test_create_duplicate_network(self): net = luna.Network(name='testnet', mongo_db=self.db, create=True, NETWORK='172.16.1.0', PREFIX=24) self.assertRaises(RuntimeError, luna.Network, name='testnet2', mongo_db=self.db, create=True, NETWORK='172.16.1.1', PREFIX=24) def test_create_network_ipv6(self): net = luna.Network(name='testnet', mongo_db=self.db, create=True, NETWORK='fd12:3456:789a:1::1', PREFIX='64', version=6) self.assertIsInstance(net, luna.Network) def test_create_duplicate_network_ipv6(self): net = luna.Network(name='testnet', mongo_db=self.db, create=True, NETWORK='fd12:3456:789a:1::1', PREFIX='64', version=6) self.assertRaises(RuntimeError, luna.Network, name='testnet2', mongo_db=self.db, create=True, NETWORK='fd12:3456:789a:1::2', PREFIX=64) def test_create_network_check(self): net = luna.Network(name='testnet', mongo_db=self.db, create=True, NETWORK='172.16.1.0', PREFIX=24) net = luna.Network(name='testnet', mongo_db=self.db) self.assertIsInstance(net, luna.Network) self.assertEqual(net.maxbits, 32) self.assertEqual(net.version, 4) expected_dict = { 'name': 'testnet', 'freelist': [{ u'start': 1, u'end': 253L }], 'ns_ip': 254, 'PREFIX': 24, '_usedby_': {}, 'version': 4, 'NETWORK': 2886729984L } doc = self.db['network'].find_one({'name': net.name}) for key in expected_dict.keys(): self.assertEqual(doc[key], expected_dict[key]) def test_create_network_check_ipv6(self): net = luna.Network(name='testnet', mongo_db=self.db, create=True, NETWORK='fd12:3456:789a:1::1', PREFIX=64, version=6) net = luna.Network(name='testnet', mongo_db=self.db) self.assertIsInstance(net, luna.Network) self.assertEqual(net.maxbits, 128) self.assertEqual(net.version, 6) expected_dict = { 'name': 'testnet', 'freelist': [{ 'start': '1', 'end': '18446744073709551613' }], 'ns_ip': '18446744073709551614', 'PREFIX': 64, '_usedby_': {}, 'version': 6, 'NETWORK': '336389205813283084628800618700287770624' } doc = self.db['network'].find_one({'name': net.name}) for key in expected_dict.keys(): self.assertEqual(doc[key], expected_dict[key])
class NetworkAttributesTestsIPv6(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.net = luna.Network(name='test', mongo_db=self.db, create=True, NETWORK='fdee:172:30:128::', PREFIX=64, ns_hostname='controller', ns_ip='fdee:172:30:128::254:254', version=6) def tearDown(self): self.sandbox.cleanup() def test_get_network(self): self.assertEqual(self.net.get('NETWORK'), 'fdee:172:30:128::') def test_get_netmask(self): self.assertEqual(self.net.get('NETMASK'), 'ffff:ffff:ffff:ffff::') def test_get_PREFIX(self): self.assertEqual(self.net.get('PREFIX'), 64) def test_get_ns_ip(self): self.assertEqual(self.net.get('ns_ip'), 'fdee:172:30:128::254:254') def test_change_ns_ip(self): json = self.net._json self.assertEqual(json['ns_ip'], 39060052) self.assertEqual(json['freelist'], [{ 'start': 1, 'end': 39060051 }, { 'start': 39060053, 'end': 18446744073709551614 }]) self.assertTrue(self.net.set('ns_ip', 'fdee:172:30:128::254:253')) self.assertEqual(json['ns_ip'], 39060051) self.assertEqual(json['freelist'], [{ 'start': 1, 'end': 39060050 }, { 'start': 39060052, 'end': 18446744073709551614 }]) def test_get_other_key(self): self.assertEqual(self.net.get('name'), 'test') def test_reserve_ip(self): self.net.reserve_ip('fdee:172:30:128::253:254') net = self.net._json expected_freelist = [{ 'start': 1, 'end': 38994515 }, { 'start': 38994517, 'end': 39060051 }, { 'start': 39060053, 'end': 18446744073709551614 }] self.assertEqual(net['freelist'], expected_freelist) def test_reserve_ip_range(self): self.net.reserve_ip('fdee:172:30:128::1:4', 'fdee:172:30:128::1:6') net = self.net._json self.assertEqual(net['freelist'], [{ 'end': 65539, 'start': 1 }, { 'start': 65543, 'end': 39060051 }, { 'start': 39060053, 'end': 18446744073709551614 }]) def test_release_ip(self): self.net.release_ip('fdee:172:30:128::254:254') net = self.net._json self.assertEqual(net['freelist'], [{ 'start': 1, 'end': 18446744073709551614 }]) def test_release_ip_range(self): self.net.reserve_ip('fdee:172:30:128::1:4', 'fdee:172:30:128::1:6') self.net.release_ip('fdee:172:30:128::1:4', 'fdee:172:30:128::1:6') net = self.net._json self.assertEqual(net['freelist'], [{ 'start': 1, 'end': 39060051 }, { 'start': 39060053, 'end': 18446744073709551614 }]) def test_change_PREFIX(self): self.assertTrue(self.net.set('PREFIX', 32))
class ClusterMakeDNSTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp(self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster( mongo_db=self.db, create=True, path=self.path, user=getpass.getuser() ) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group(name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.net11 = luna.Network(name='net11', NETWORK='10.11.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net11.set('ns_hostname', 'master') self.net61 = luna.Network(name='net61', NETWORK='fe90::', PREFIX=64, mongo_db=self.db, create=True) self.net61.set('ns_hostname', 'master') self.group.set_net_to_if('eth0', self.net11.name) self.group.set_net_to_if('eth0', self.net61.name) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.node = luna.Node(group=self.group.name, mongo_db=self.db, create=True) self.switch = luna.Switch(name='sw01', network=self.net11.name, mongo_db=self.db, create=True) self.switch.set('ip', '10.11.1.1') self.otherdev = luna.OtherDev(name='pdu01', network=self.net11.name, ip="10.11.2.1", mongo_db=self.db, create=True) self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db) def tearDown(self): self.sandbox.cleanup() def test_get_allocated_ips_wrong(self): self.assertFalse(self.group.get_allocated_ips("wrong")) def test_get_allocated_ips_simple(self): self.assertEqual(self.group.get_allocated_ips(self.net11), {'node001': 1}) def test_get_allocated_ips_duplicate_ifs(self): self.group.add_interface('BMC') self.group.set_net_to_if('BMC', self.net11.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertEqual(self.group.get_allocated_ips(self.net11), {'node001-eth0': 1, 'node001-bmc': 2}) def test_get_allocated_ips_duplicate_ifs_case(self): self.group.add_interface('BMC') self.group.set_net_to_if('BMC', self.net11.name) self.group.add_interface('Eth0') self.group.set_net_to_if('Eth0', self.net11.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertEqual(self.group.get_allocated_ips(self.net11), {'node001-eth0': 1, 'node001-BMC': 2, 'node001-Eth0': 3}) def test_resolve_used_ips_simple(self): self.assertEqual( self.net11.resolve_used_ips(), { 'node001': '10.11.0.1', 'sw01': '10.11.1.1', 'pdu01': '10.11.2.1', 'master': '10.11.255.254' } )
class OsimageMethodsTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp(self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster( mongo_db=self.db, create=True, path=self.path, user=getpass.getuser(), ) self.osimage = luna.OsImage( name='testosimage', path=self.path, mongo_db=self.db, create=True, ) def tearDown(self): self.sandbox.cleanup() @mock.patch('shutil.copy') @mock.patch('os.remove') @mock.patch('os.chmod') @mock.patch('os.chown') @mock.patch('shutil.move') @mock.patch('os.close') @mock.patch('os.fchdir') @mock.patch('subprocess.Popen') @mock.patch('os.chroot') @mock.patch('os.open') def test_pack_create_tarball(self, mock_os_open, mock_os_chroot, mock_subprocess_popen, mock_os_fchdir, mock_os_close, mock_shutil_move, mock_os_chown, mock_os_chmod, mock_os_remove, mock_shutil_copy, ): mock_subprocess_popen.return_value.stderr.readline.return_value = '' self.assertTrue(self.osimage.create_tarball()) def test_create_torrent_wo_tarball(self): self.assertFalse(self.osimage.create_torrent()) @mock.patch('os.path.exists') def test_create_torrent_w_wrong_tarball(self, mock_os_path_exists): self.osimage.set('tarball', 'UUID') mock_os_path_exists.return_value = False self.assertFalse(self.osimage.create_torrent()) @mock.patch('os.path.exists') def test_create_torrent_w_wrong_frontend(self, mock_os_path_exists): self.osimage.set('tarball', 'UUID') self.assertFalse(self.osimage.create_torrent()) @mock.patch('os.path.exists') def test_create_torrent_w_wrong_frontend_port( self, mock_os_path_exists): self.osimage.set('tarball', 'UUID') self.cluster.set('frontend_address', '127.0.0.1') self.cluster.set('frontend_port', 0) self.assertFalse(self.osimage.create_torrent()) @mock.patch('libtorrent.set_piece_hashes') @mock.patch('libtorrent.add_files') @mock.patch('shutil.move') @mock.patch('os.chdir') @mock.patch('os.close') @mock.patch('os.fchdir') @mock.patch('os.chroot') @mock.patch('os.open') @mock.patch('subprocess.Popen') @mock.patch('os.chmod') @mock.patch('os.chown') @mock.patch('shutil.copy') @mock.patch('os.path.exists') def test_create_torrent_default(*args): self = args[0] args[5].return_value.stderr.readline.return_value = '' self.osimage.set('tarball', 'UUID') self.cluster.set('frontend_address', '127.0.0.1') self.cluster.set('frontend_port', 7050) self.assertTrue(self.osimage.create_torrent()) @mock.patch('os.close') @mock.patch('os.fchdir') @mock.patch('os.remove') @mock.patch('subprocess.Popen') @mock.patch('os.chroot') @mock.patch('os.open') def test_pack_image_broken(self, mock_os_open, mock_os_chroot, mock_subprocess_popen, mock_os_remove, mock_os_fchdir, mock_os_close, ): mock_subprocess_popen.side_effect = Exception('dummy excepion') self.assertFalse(self.osimage.create_tarball()) @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def test_get_package_ver(self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, {'VERSION': '3.11', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages #mock_rpm_transactionset.dbMatch = [p1] self.assertEqual( self.osimage.get_package_ver('', 'test'), ['3.10-999-el0.x86_64', '3.11-999-el0.x86_64'] ) self.assertEqual( self.osimage.list_kernels(), ['3.10-999-el0.x86_64', '3.11-999-el0.x86_64'] ) @mock.patch('os.chmod') @mock.patch('os.chown') @mock.patch('shutil.copy') @mock.patch('shutil.move') @mock.patch('os.path.isfile') @mock.patch('os.remove') @mock.patch('pwd.getpwnam') @mock.patch('subprocess.Popen') def Ztest_pack_boot(*args): self = args[0] args[2].return_value.pw_uid.return_value = 1000 args[2].return_value.pw_gid.return_value = 1000 self.assertFalse(self.osimage.pack_boot()) args[1].return_value.poll.side_effect = [None, None, True] self.assertFalse(self.osimage.pack_boot()) args[1].return_value.poll.side_effect = [None, None, True] args[1].return_value.stdout.readline.return_value = 'luna' self.assertFalse(self.osimage.pack_boot()) args[1].return_value.returncode = 0 args[1].return_value.poll.side_effect = [None, None, True] self.assertTrue(self.osimage.pack_boot()) def test_copy_boot_wo_kern(self): self.assertFalse(self.osimage.copy_boot()) @mock.patch('os.chmod') @mock.patch('os.chown') @mock.patch('shutil.copy') @mock.patch('os.path.isfile') def test_copy_boot_default(*args): self = args[0] self.assertTrue(self.osimage.copy_boot()) @mock.patch('os.remove') @mock.patch('subprocess.Popen') @mock.patch('os.path.exists') def grab_host_default(*args): self = args[0] args[2].return_value.stdout.readline.return_value = '' args[2].return_value.communicate.return_value = ('', '') args[2].return_value.returncode = 0 self.assertTrue(self.osimage.grab_host('127.0.0.1')) args[2].return_value.returncode = 255 self.assertFalse(self.osimage.grab_host('127.0.0.1'))
class ClusterMakeDHCPTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group1 = luna.Group(name='testgroup1', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.group2 = luna.Group(name='testgroup2', osimage=self.osimage.name, mongo_db=self.db, interfaces=['BOOTIF'], create=True) self.net11 = luna.Network(name='net11', NETWORK='10.11.0.0', PREFIX=16, mongo_db=self.db, create=True) self.group1.set_net_to_if('eth0', self.net11.name) self.group2.set_net_to_if('BOOTIF', self.net11.name) self.node1 = luna.Node(group=self.group1.name, mongo_db=self.db, create=True) self.node2 = luna.Node(group=self.group2.name, mongo_db=self.db, create=True) self.node1.set_mac('00:11:22:33:44:55') self.node2.set_mac('01:11:22:33:44:55') self.group1 = luna.Group(name=self.group1.name, mongo_db=self.db) self.group2 = luna.Group(name=self.group2.name, mongo_db=self.db) self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.node1 = luna.Node(name=self.node1.name, mongo_db=self.db) self.node2 = luna.Node(name=self.node2.name, mongo_db=self.db) def tearDown(self): self.sandbox.cleanup() def test_get_ip_macs(self): self.assertEqual( self.net11.get_ip_macs(), { 'node001': { 'ip': '10.11.0.1', 'mac': '00:11:22:33:44:55' }, 'node002': { 'ip': '10.11.0.2', 'mac': '01:11:22:33:44:55' }, }) def test_make_config_basic(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.11.128.1', end_ip='10.11.129.254') self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.assertEqual(self.net11._json['freelist'], [{ 'start': 3, 'end': 32768 }, { 'start': 33279, 'end': 65533 }]) hmac_key = ret.pop('hmac_key') self.assertTrue(len(hmac_key) > 1) self.assertEqual( ret, { 'network': '10.11.0.0', 'dhcp_start': '10.11.128.1', 'dhcp_end': '10.11.129.254', 'reservations': { 'node002': { 'ip': '10.11.0.2', 'mac': '01:11:22:33:44:55' }, 'node001': { 'ip': '10.11.0.1', 'mac': '00:11:22:33:44:55' }, }, 'netmask': '255.255.0.0', 'frontend_ip': '10.11.255.254', 'frontend_port': '7050', 'protocol': 'http', }) def test_make_config_empty(self): self.cluster.set('frontend_address', '10.11.255.254') self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.11.128.1', end_ip='10.11.129.254') ret = self.cluster.makedhcp_config() hmac_key = ret.pop('hmac_key') self.assertTrue(len(hmac_key) > 1) self.assertEqual( ret, { 'network': '10.11.0.0', 'dhcp_start': '10.11.128.1', 'dhcp_end': '10.11.129.254', 'reservations': { 'node002': { 'ip': '10.11.0.2', 'mac': '01:11:22:33:44:55' }, 'node001': { 'ip': '10.11.0.1', 'mac': '00:11:22:33:44:55' }, }, 'netmask': '255.255.0.0', 'frontend_ip': '10.11.255.254', 'frontend_port': '7050', 'protocol': 'http', }) def test_make_config_wo_frontend_address(self): ret = self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.11.128.1', end_ip='10.11.129.254') self.assertEqual(ret, {}) def test_make_config_start_less_end(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.11.129.254', end_ip='10.11.128.1') self.assertEqual(ret, {}) def test_set_wrong_frontend(self): self.cluster.set('frontend_address', '10.12.255.254') ret = self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.11.0.2', end_ip='10.11.0.3') self.assertEqual(ret, {}) def test_set_wrong_start(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.12.0.2', end_ip='10.11.0.3') self.assertEqual(ret, {}) def test_set_wrong_end(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.11.0.2', end_ip='10.12.0.3') self.assertEqual(ret, {}) def test_set_wrong_range(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.11.0.2', end_ip='10.11.0.3') self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.assertEqual(self.net11._json['freelist'], [{ 'start': 3, 'end': 65533 }]) self.assertEqual(ret, {}) def test_change_to_same_range(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.11.0.3', end_ip='10.11.0.4') ret = self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.11.0.3', end_ip='10.11.0.4') ret.pop('hmac_key') self.assertEqual( ret, { 'network': '10.11.0.0', 'dhcp_start': '10.11.0.3', 'dhcp_end': '10.11.0.4', 'reservations': { 'node002': { 'ip': '10.11.0.2', 'mac': '01:11:22:33:44:55' }, 'node001': { 'ip': '10.11.0.1', 'mac': '00:11:22:33:44:55' }, }, 'netmask': '255.255.0.0', 'frontend_ip': '10.11.255.254', 'frontend_port': '7050', 'protocol': 'http', }) def test_change_to_wrong_range(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.11.128.1', end_ip='10.11.129.254') ret = self.cluster.makedhcp_config(net_name=self.net11.name, start_ip='10.11.0.2', end_ip='10.11.0.4') self.assertEqual(ret, {}) self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.assertEqual(self.cluster.get('dhcp_range_start'), '10.11.128.1') self.assertEqual(self.cluster.get('dhcp_range_end'), '10.11.129.254') self.assertEqual(self.net11._json['freelist'], [{ 'start': 3, 'end': 32768 }, { 'start': 33279, 'end': 65533 }])
class GroupCreateTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) def tearDown(self): self.sandbox.cleanup() def test_create_group_with_defaults(self): group = luna.Group(name='testgroup1', osimage=str(self.osimage.name), mongo_db=self.db, interfaces=['BOOTIF'], create=True) doc = self.db['group'].find_one({'_id': group._id}) expected = { 'torrent_if': None, 'partscript': 'mount -t tmpfs tmpfs /sysroot', 'postscript': ('cat << EOF >> /sysroot/etc/fstab\n' + 'tmpfs / tmpfs defaults 0 0\n' + 'EOF'), 'name': 'testgroup1', 'bmcsetup': None, 'domain': None, '_use_': { 'cluster': { str(self.cluster._id): 1 }, 'osimage': { str(self.osimage._id): 1 } }, 'osimage': self.osimage.DBRef, 'comment': '' } for attr in expected: self.assertEqual(doc[attr], expected[attr]) # check interfaces if_dict = doc['interfaces'] self.assertEqual(len(if_dict), 1) for uuid in if_dict: self.assertEqual(if_dict[uuid]['name'], 'BOOTIF') self.assertEqual(if_dict[uuid]['params'], '') self.assertEqual(if_dict[uuid]['network'], {'4': None, '6': None}) def test_create_broken(self): self.assertRaises(RuntimeError, luna.Group, name='testgroup1', osimage=str(self.osimage.name), mongo_db=self.db, interfaces='eth0', create=True) def test_delete_group(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MondoDB as a backend.') group = luna.Group(name='testgroup', osimage=str(self.osimage.name), mongo_db=self.db, interfaces=['eth0'], create=True) groupid = group._id group.delete() doc = self.db['group'].find_one({'_id': groupid}) self.assertIsNone(doc) def test_creation_group(self): bmcsetup = luna.BMCSetup(name='bmcsetup', mongo_db=self.db, create=True) net = luna.Network(name='cluster', NETWORK='10.11.0.0', PREFIX=16, mongo_db=self.db, create=True) nics = ['eth0', 'eth1'] group = luna.Group(name='testgroup2', osimage=self.osimage.name, bmcsetup=bmcsetup.name, mongo_db=self.db, interfaces=nics, domain=net.name, torrent_if=nics[1], create=True) doc = self.db['group'].find_one({'_id': group._id}) expected = { 'torrent_if': nics[1], 'partscript': 'mount -t tmpfs tmpfs /sysroot', 'postscript': ('cat << EOF >> /sysroot/etc/fstab\n' + 'tmpfs / tmpfs defaults 0 0\n' + 'EOF'), 'name': 'testgroup2', 'bmcsetup': bmcsetup.DBRef, 'domain': net.DBRef, '_use_': { 'cluster': { str(self.cluster._id): 1 }, 'osimage': { str(self.osimage._id): 1 }, 'network': { str(net._id): 1, }, 'bmcsetup': { str(bmcsetup._id): 1 }, }, 'osimage': self.osimage.DBRef, 'comment': '', } for attr in expected: self.assertEqual(doc[attr], expected[attr]) # check interfaces if_dict = doc['interfaces'] self.assertEqual(len(if_dict), len(nics)) for uuid in if_dict: self.assertIn(if_dict[uuid]['name'], nics) self.assertEqual(if_dict[uuid]['params'], '') self.assertEqual(if_dict[uuid]['network'], {'4': None, '6': None}) nics.remove(if_dict[uuid]['name'])
class NodeChangeTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp(self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster( mongo_db=self.db, create=True, path=self.path, user=getpass.getuser() ) self.cluster.set('path', self.path) self.osimage = luna.OsImage( name='testosimage', path=self.path, mongo_db=self.db, create=True ) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.group_new = luna.Group( name='testgroup_new', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) def tearDown(self): self.sandbox.cleanup() def test_change_group(self): start_dict = self.db['node'].find_one({'_id': self.node._id}) self.node.set_group(self.group_new.name) expected_dict = copy.deepcopy(start_dict) expected_dict['group'] = self.group_new.DBRef expected_dict['_use_']['group'] = { u'' + str(self.group_new._id): 1, } actual_dict = self.db['node'].find_one({'_id': self.node._id}) if_uuid = None for uuid in self.group_new._json['interfaces']: if_uuid = uuid expected_dict['interfaces'] = {if_uuid: {'4': None, '6': None}} self.assertEqual(expected_dict, actual_dict) self.node.set_group(self.group.name) end_dict = self.db['node'].find_one({'_id': self.node._id}) self.assertEqual(start_dict, end_dict) def test_set_mac(self): self.node.set_mac('00:01:02:aa:bb:cc') d = self.db['mac'].find({'mac': '00:01:02:aa:bb:cc'}) self.assertEqual(d.count(), 1) for e in d: self.assertEqual(self.node.DBRef, e['node']) def test_change_mac(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.' ) self.node.set_mac('00:01:02:aa:bb:cc') node2 = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) node2.set_mac('00:01:02:aa:bb:cc') d = self.db['mac'].find({'mac': '00:01:02:aa:bb:cc'}) self.assertEqual(d.count(), 1) for e in d: self.assertEqual(node2.DBRef, e['node']) def test_clear_mac(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.' ) self.node.set_mac('00:01:02:aa:bb:cc') self.node.set_mac('') d = self.db['mac'].find() self.assertEqual(d.count(), 0) def test_get_mac(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.' ) mac = '00:01:02:aa:bb:cc' self.node.set_mac(mac) self.assertEqual(self.node.get_mac(), mac) def test_set_switch(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.' ) net = luna.Network( 'testnet', mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) switch = luna.Switch( 'test1', network=net.name, mongo_db=self.db, create=True, ) self.node.set_switch(switch.name) d1 = self.db['node'].find_one({'name': self.node.name}) d2 = self.db['switch'].find_one({'name': switch.name}) self.assertEqual(d1['switch'], switch.DBRef) self.assertEqual(len(d2['_usedby_']['node']), 1) self.assertEqual(d2['_usedby_']['node'][str(self.node._id)], 1) def test_change_switch(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.' ) net = luna.Network( 'testnet', mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) switch1 = luna.Switch( 'test1', network=net.name, mongo_db=self.db, create=True, ) switch2 = luna.Switch( 'test2', network=net.name, mongo_db=self.db, create=True, ) self.node.set_switch(switch1.name) self.node.set_switch(switch2.name) d1 = self.db['node'].find_one({'name': self.node.name}) d2 = self.db['switch'].find_one({'name': switch1.name}) d3 = self.db['switch'].find_one({'name': switch2.name}) self.assertEqual(d1['switch'], switch2.DBRef) self.assertEqual(len(d2['_usedby_']), 0) self.assertEqual(len(d3['_usedby_']['node']), 1) self.assertEqual(d1['switch'], switch2.DBRef) def test_node_change_group(self): # create 3 groups group1 = self.group group2 = luna.Group( name='testgroup2', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) group3 = luna.Group( name='testgroup3', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth1'], create=True, ) group4 = luna.Group( name='testgroup4', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth3'], create=True, ) # create 3 networks net1 = luna.Network( 'testnet1', mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) net2 = luna.Network( 'testnet2', mongo_db=self.db, create=True, NETWORK='10.51.0.0', PREFIX=16, ) net3 = luna.Network( 'testnet3', mongo_db=self.db, create=True, NETWORK='10.52.0.0', PREFIX=16, ) # assign 2 networks to interfaces # group1: {'eth0': net1} # group2 {'eth0': net1, 'em1': net2} # group3 {'em1': net1, 'eth1': net2} # group4 {'eth3': net3} group2.add_interface('em1') group3.add_interface('em1') group1.set_net_to_if('eth0', net1.name) group2.set_net_to_if('eth0', net1.name) group3.set_net_to_if('eth1', net2.name) group4.set_net_to_if('eth3', net3.name) group2.set_net_to_if('em1', net2.name) group3.set_net_to_if('em1', net1.name) self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) # create 9 more nodes nodes = [] for i in range(10): nodes.append(luna.Node( group=self.group, create=True, mongo_db=self.db, )) # will do tests with node005 node005 = luna.Node( 'node005', mongo_db=self.db, group=self.group ) # check did we get desired config node_json = self.db['node'].find_one({'name': node005.name}) net1_json = self.db['network'].find_one({'name': net1.name}) self.assertEqual(node_json['group'], group1.DBRef) for k in node_json['interfaces']: self.assertEqual(node_json['interfaces'][k], {'4': 5, '6': None}) self.assertEqual(len(net1_json['freelist']), 1) self.assertEqual(net1_json['freelist'][0]['start'], 12) #node_json['interfaces'], net1_json['freelist'] # # change group first time: # self.assertEqual(node005.set_group(group2.name), True) # updating group objects group1 = luna.Group( name=group1.name, mongo_db=self.db, ) group2 = luna.Group( name=group2.name, mongo_db=self.db, ) node_json = self.db['node'].find_one({'name': node005.name}) group2_json = self.db['group'].find_one({'name': group2.name}) net1_json = self.db['network'].find_one({'name': net1.name}) net2_json = self.db['network'].find_one({'name': net2.name}) # should be 2 interfaces now self.assertEqual(len(node_json['interfaces']), 2) # those interfaces should be the ones from group2 for uid in node_json['interfaces']: self.assertIn(uid, group2_json['interfaces'].keys()) # check if eth0 has the same IP adress: eth0_uuid = '' em1_uuid = '' for if_uid in group2_json['interfaces']: if group2_json['interfaces'][if_uid]['name'] == 'eth0': eth0_uuid = if_uid if group2_json['interfaces'][if_uid]['name'] == 'em1': em1_uuid = if_uid # check if we found self.assertIsNot(eth0_uuid, '') self.assertIsNot(em1_uuid, '') # should be 5 self.assertEqual(node_json['interfaces'][eth0_uuid], {'4': 5, '6': None}) # another should be 1 self.assertEqual(node_json['interfaces'][em1_uuid], {'4': 1, '6': None}) # check network self.assertEqual(net1_json['freelist'], [{'start': 12, 'end': 65533}]) self.assertEqual(net2_json['freelist'], [{'start': 2, 'end': 65533}]) # # change group second time # self.assertEqual(node005.set_group(group3.name), True) # fetch the data from DB node_json = self.db['node'].find_one({'name': node005.name}) group3_json = self.db['group'].find_one({'name': group3.name}) net1_json = self.db['network'].find_one({'name': net1.name}) net2_json = self.db['network'].find_one({'name': net2.name}) # try to find uuids of the interfaces eth1_uuid = '' em1_uuid = '' for if_uid in group3_json['interfaces']: if group3_json['interfaces'][if_uid]['name'] == 'eth1': eth1_uuid = if_uid if group3_json['interfaces'][if_uid]['name'] == 'em1': em1_uuid = if_uid # check if we found self.assertIsNot(eth1_uuid, '') self.assertIsNot(em1_uuid, '') # should be 1 self.assertEqual(node_json['interfaces'][eth1_uuid], {'4': 1, '6': None}) # another should be 5 self.assertEqual(node_json['interfaces'][em1_uuid], {'4': 5, '6': None}) # # change group third time # self.assertEqual(node005.set_group(group4.name), True) node_json = self.db['node'].find_one({'name': node005.name}) net1_json = self.db['network'].find_one({'name': net1.name}) net2_json = self.db['network'].find_one({'name': net2.name}) net3_json = self.db['network'].find_one({'name': net3.name}) self.assertEqual( net1_json['freelist'], [{'start': 5, u'end': 5}, {'start': 12, 'end': 65533}] ) self.assertEqual( net2_json['freelist'], [{'start': 1, 'end': 65533}] ) self.assertEqual( net3_json['freelist'], [{'start': 2, 'end': 65533}] ) def test_update_status(self): self.assertIsNone(self.node.update_status()) self.assertIsNone(self.node.update_status('#$#%')) self.assertTrue(self.node.update_status('status1')) doc = self.db['node'].find_one({'_id': self.node._id}) self.assertEqual(doc['status']['step'], 'status1') self.assertEqual( type(doc['status']['time']), datetime.datetime ) def test_get_status(self): self.assertIsNone(self.node.get_status()) self.assertTrue(self.node.update_status('status1')) self.assertEqual(self.node.get_status()['status'], 'status1') self.assertTrue(self.node.get_status()['time']) self.assertEqual( self.node.get_status(relative=False)['status'], 'status1', ) self.assertTrue( self.node.get_status(relative=False)['time'] ) def test_get_status_tracker(self): name = "%20s" % self.node.name peerid = ''.join(["{:02x}".format(ord(l)) for l in name]) self.assertTrue(self.node.update_status('install.download')) now = datetime.datetime.today() doc2insert = { 'peer_id': peerid, 'updated': now, 'downloaded': 2, 'left': 1 } self.db['tracker'].insert(doc2insert) self.assertEqual( self.node.get_status()['status'][:39], 'install.download (66.67% / last update ' )
class NetworkAttributesTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.net = luna.Network(name='test', mongo_db=self.db, create=True, NETWORK='172.16.1.0', PREFIX='24', ns_hostname='controller', ns_ip='172.16.1.254') def tearDown(self): self.sandbox.cleanup() def test_get_network(self): self.assertEqual(self.net.get('NETWORK'), '172.16.1.0') def test_get_netmask(self): self.assertEqual(self.net.get('NETMASK'), '255.255.255.0') def test_get_PREFIX(self): self.assertEqual(self.net.get('PREFIX'), '24') def test_get_ns_ip(self): self.assertEqual(self.net.get('ns_ip'), '172.16.1.254') def test_change_ns_ip(self): json = self.net._json self.assertEqual(json['ns_ip'], 254) self.assertEqual(json['freelist'], [{'start': 1, 'end': 253}]) self.assertTrue(self.net.set('ns_ip', '172.16.1.253')) self.assertEqual(json['ns_ip'], 253) self.assertEqual(json['freelist'], [{ 'start': 1, 'end': 252 }, { 'start': 254, 'end': 254 }]) def test_get_other_key(self): self.assertEqual(self.net.get('name'), 'test') def test_reserve_ip(self): self.net.reserve_ip('172.16.1.3') net = self.net._json self.assertEqual(net['freelist'], [{ 'start': 1, 'end': 2 }, { 'start': 4, 'end': 253 }]) def test_reserve_ip_range(self): self.net.reserve_ip('172.16.1.4', '172.16.1.6') net = self.net._json self.assertEqual(net['freelist'], [{ 'start': 1, 'end': 3 }, { 'start': 7, 'end': 253 }]) def test_release_ip(self): self.net.release_ip('172.16.1.254') net = self.net._json self.assertEqual(net['freelist'], [{'start': 1, 'end': 254}]) def test_release_ip_range(self): self.net.release_ip('172.16.1.250', '172.16.1.254') net = self.net._json self.assertEqual(net['freelist'], [{'start': 1, 'end': 254}])
class GroupBootInstallParamsTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.cluster.set("frontend_address", "127.0.0.1") self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.bmcsetup = luna.BMCSetup(name='bmcsetup', mongo_db=self.db, create=True) self.net1 = luna.Network(name='cluster', NETWORK='10.11.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net2 = luna.Network(name='external', NETWORK='10.12.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net3 = luna.Network(name='ib', NETWORK='10.13.0.0', PREFIX=16, mongo_db=self.db, create=True) self.prescript = 'pre' self.postscript = 'post' self.partscript = 'part' self.nics = {'BOOTIF': 'PARM=1', 'eth1': 'PARM=2', 'ib0': 'PARM=3'} self.group = luna.Group(name='compute', osimage=self.osimage.name, mongo_db=self.db, interfaces=['BOOTIF'], create=True) # mocking osimage boot stuff with mock.patch('os.path'), \ mock.patch('shutil.copy'), \ mock.patch('os.remove'), \ mock.patch('os.chown'), \ mock.patch('os.chmod'), \ mock.patch('subprocess.Popen') as mock_subprocess_popen, \ mock.patch('os.open'), \ mock.patch('os.chroot'), \ mock.patch('os.fchdir'), \ mock.patch('os.close'), \ mock.patch('os.chdir'), \ mock.patch('shutil.move'), \ mock.patch('libtorrent.add_files'), \ mock.patch('libtorrent.set_piece_hashes'): (mock_subprocess_popen.return_value.stderr.readline.return_value ) = '' self.osimage.copy_boot() self.osimage.create_tarball() self.osimage.create_torrent() group_json = self.db['group'].find_one({'_id': self.group._id}) osimage_json = self.db['osimage'].find_one({'_id': self.osimage._id}) self.install_expected_dict = { 'torrent_if': '', 'partscript': group_json['partscript'], 'tarball': osimage_json['tarball'] + '.tgz', 'bmcsetup': {}, 'interfaces': { 'BOOTIF': { 'options': '', '4': { 'ip': '', 'netmask': '', 'prefix': '', }, '6': { 'ip': '', 'netmask': '', 'prefix': '', } } }, 'prescript': '', 'domain': '', 'postscript': group_json['postscript'], 'kernopts': '', 'kernver': '3.10-999-el0.x86_64', 'torrent': osimage_json['torrent'] + '.torrent' } def tearDown(self): self.sandbox.cleanup() def test_boot_params_default(self): self.assertEqual( self.group.boot_params, { 'net': {}, 'kernel_file': (self.osimage.name + '-vmlinuz-3.10-999-el0.x86_64'), 'kern_opts': '', 'domain': '', 'initrd_file': (self.osimage.name + '-initramfs-3.10-999-el0.x86_64'), }) def test_boot_params_w_domain(self): self.group.set_domain(self.net1.name) self.assertEqual( self.group.boot_params, { 'net': {}, 'kernel_file': (self.osimage.name + '-vmlinuz-3.10-999-el0.x86_64'), 'kern_opts': '', 'domain': self.net1.name, 'initrd_file': (self.osimage.name + '-initramfs-3.10-999-el0.x86_64'), }) def test_boot_params_w_bootif_w_net(self): self.group.set_net_to_if('BOOTIF', self.net1.name) self.assertEqual( self.group.boot_params, { 'net': { '4': { 'prefix': '16', 'mask': '255.255.0.0' } }, 'kernel_file': (self.osimage.name + '-vmlinuz-3.10-999-el0.x86_64'), 'kern_opts': '', 'domain': '', 'initrd_file': (self.osimage.name + '-initramfs-3.10-999-el0.x86_64'), }) def test_install_params_default(self): self.maxDiff = None self.assertEqual(self.group.install_params, self.install_expected_dict) def test_install_params_w_wrong_torrent_if(self): self.group.set('torrent_if', 'eth0') self.assertEqual(self.group.install_params, self.install_expected_dict) def test_install_params_w_torr_if_w_net(self): # configure torrent_if self.group.set_net_to_if('BOOTIF', self.net1.name) self.group.set('torrent_if', 'BOOTIF') self.install_expected_dict['torrent_if'] = 'BOOTIF' self.install_expected_dict['interfaces']['BOOTIF']['4'] = { 'ip': '', 'netmask': '255.255.0.0', 'prefix': '16', } self.assertEqual(self.group.install_params, self.install_expected_dict) def test_install_params_w_domain(self): self.group.set_domain(self.net1.name) self.install_expected_dict['domain'] = self.net1.name self.assertEqual(self.group.install_params, self.install_expected_dict) def test_install_params_w_bmconfig_wo_net(self): # add bmcconfig bmc = luna.BMCSetup(name="testbmc", mongo_db=self.db, create=True) self.group.bmcsetup(bmc.name) self.install_expected_dict['bmcsetup'] = { 'mgmtchannel': 1, 'netchannel': 1, 'password': '******', 'user': '******', 'userid': 3, } self.assertEqual(self.group.install_params, self.install_expected_dict)
class SetIPTests(unittest.TestCase): """ Test for Node.get_ip """ @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) self.network = luna.Network( name="testnet", mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) self.network6 = luna.Network(name="testnet2", mongo_db=self.db, create=True, NETWORK='fe80::', PREFIX=64, version=6) def tearDown(self): self.sandbox.cleanup() def test_get_ip_wrong_name(self): self.assertFalse( self.node.set_ip(interface_name='wrong-interface', ip='10.50.0.2')) def test_get_ip_wrong_uuid(self): self.assertFalse( self.node.set_ip(interface_uuid='wrong-uuid', ip='10.50.0.2')) def test_get_ip_for_unconfigured_if(self): self.assertFalse( self.node.set_ip(interface_name='eth0', ip='10.50.0.2')) def test_set_ip_by_name(self): self.group.set_net_to_if('eth0', self.network.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertTrue(self.node.set_ip(interface_name='eth0', ip='10.50.0.2')) self.network = luna.Network(name=self.network.name, mongo_db=self.db) self.assertEqual( self.network._json['freelist'], [{ 'start': 1, 'end': 1 }, { 'start': 3, 'end': 65533 }], ) def test_set_ip_by_uuid(self): interface_uuid = None for k in self.node._json['interfaces']: interface_uuid = k self.group.set_net_to_if('eth0', self.network.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertTrue( self.node.set_ip(interface_uuid=interface_uuid, ip='10.50.0.2')) self.network = luna.Network(name=self.network.name, mongo_db=self.db) self.assertEqual( self.network._json['freelist'], [{ 'start': 1, 'end': 1 }, { 'start': 3, 'end': 65533 }], ) def test_set_both_ips(self): self.group.set_net_to_if('eth0', self.network.name) self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertTrue(self.node.set_ip(interface_name='eth0', ip='10.50.0.2')) self.assertTrue(self.node.set_ip(interface_name='eth0', ip='fe80::2')) self.network = luna.Network(name=self.network.name, mongo_db=self.db) self.network6 = luna.Network(name=self.network6.name, mongo_db=self.db) self.assertEqual( self.network._json['freelist'], [{ 'start': 1, 'end': 1 }, { 'start': 3, 'end': 65533 }], ) self.assertEqual(self.network6._json['freelist'], [{ 'start': 1, 'end': 1 }, { 'start': 3, 'end': 18446744073709551613 }]) def test_set_ip_force_wo_net_configured(self): self.assertFalse( self.node.set_ip(interface_name='eth0', ip='10.50.0.2', force=True)) def test_set_ip_force(self): self.group.set_net_to_if('eth0', self.network.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertTrue( self.node.set_ip(interface_name='eth0', ip='10.50.0.2', force=True)) self.network = luna.Network(name=self.network.name, mongo_db=self.db) self.assertEqual( self.network._json['freelist'], [{ 'start': 3, 'end': 65533 }], ) def test_set_wrong_ip(self): self.group.set_net_to_if('eth0', self.network.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertFalse( self.node.set_ip(interface_name='eth0', ip='10.40.0.1')) self.network = luna.Network(name=self.network.name, mongo_db=self.db) self.assertEqual( self.network._json['freelist'], [{ 'start': 2, 'end': 65533 }], ) self.assertEqual(self.node.get_ip(interface_name='eth0'), '10.50.0.1')
class ChangeGroupTests(unittest.TestCase): """ Test for Node.get_ip """ @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group(name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.new_group1 = luna.Group(name='new1', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.node = luna.Node(group=self.group.name, mongo_db=self.db, create=True) self.network11 = luna.Network(name="net11", mongo_db=self.db, create=True, NETWORK='10.51.0.0', PREFIX=16) self.network61 = luna.Network(name="net61", mongo_db=self.db, create=True, NETWORK='fe80::', PREFIX=64, version=6) def tearDown(self): self.sandbox.cleanup() def test_wo_interfaces_configured(self): self.assertTrue(self.node.set_group(self.new_group1.name)) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.new_group1 = luna.Group(name=self.new_group1.name, mongo_db=self.db) self.assertEqual(self.node._json['group'], self.new_group1.DBRef) self.assertEqual(self.node._json['_use_']['group'], {str(self.new_group1._id): 1}) group_if_uid = None for uuid in self.new_group1._json['interfaces']: group_if_uid = uuid self.assertEqual(self.node._json['interfaces'][group_if_uid], { '4': None, '6': None }) self.assertEqual(self.group._json['_usedby_'], {}) def test_w_interfaces_configured_in_new_group_net4(self): self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.new_group1.set_net_to_if('eth0', self.network11.name) self.assertTrue(self.node.set_group(self.new_group1.name)) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.new_group1 = luna.Group(name=self.new_group1.name, mongo_db=self.db) self.network11 = luna.Network(name=self.network11.name, mongo_db=self.db) group_if_uid = None for uuid in self.new_group1._json['interfaces']: group_if_uid = uuid self.assertEqual(self.node._json['interfaces'][group_if_uid], { '4': 1, '6': None }) self.assertEqual(self.network11._json['freelist'], [{ 'start': 2, 'end': 65533 }]) def test_w_interfaces_configured_in_new_group_net6(self): self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.new_group1.set_net_to_if('eth0', self.network61.name) self.assertTrue(self.node.set_group(self.new_group1.name)) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.new_group1 = luna.Group(name=self.new_group1.name, mongo_db=self.db) self.network61 = luna.Network(name=self.network61.name, mongo_db=self.db) group_if_uid = None for uuid in self.new_group1._json['interfaces']: group_if_uid = uuid self.assertEqual(self.node._json['interfaces'][group_if_uid], { '4': None, '6': 1 }) self.assertEqual(self.network61._json['freelist'], [{ 'start': 2, 'end': 18446744073709551613 }]) def test_w_interfaces_configured_in_old_group_net4(self): self.group.set_net_to_if('eth0', self.network11.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertTrue(self.node.set_group(self.new_group1.name)) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.new_group1 = luna.Group(name=self.new_group1.name, mongo_db=self.db) self.network11 = luna.Network(name=self.network11.name, mongo_db=self.db) group_if_uid = None for uuid in self.new_group1._json['interfaces']: group_if_uid = uuid self.assertEqual(self.node._json['interfaces'][group_if_uid], { '4': None, '6': None }) self.assertEqual(self.network11._json['freelist'], [{ 'start': 1, 'end': 65533 }]) def test_w_interfaces_configured_in_both_groups_same_net(self): self.group.set_net_to_if('eth0', self.network11.name) self.new_group1.set_net_to_if('eth0', self.network11.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertTrue(self.node.set_group(self.new_group1.name)) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.new_group1 = luna.Group(name=self.new_group1.name, mongo_db=self.db) self.network11 = luna.Network(name=self.network11.name, mongo_db=self.db) group_if_uid = None for uuid in self.new_group1._json['interfaces']: group_if_uid = uuid self.assertEqual(self.node._json['interfaces'][group_if_uid], { '4': 1, '6': None }) self.assertEqual(self.network11._json['freelist'], [{ 'start': 2, 'end': 65533 }]) def test_w_interfaces_configured_in_both_groups_same_net2(self): self.group.set_net_to_if('eth0', self.network11.name) self.new_group1.set_net_to_if('eth0', self.network11.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.node.set_ip(interface_name='eth0', ip='10.51.0.2') self.assertTrue(self.node.set_group(self.new_group1.name)) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.new_group1 = luna.Group(name=self.new_group1.name, mongo_db=self.db) self.network11 = luna.Network(name=self.network11.name, mongo_db=self.db) group_if_uid = None for uuid in self.new_group1._json['interfaces']: group_if_uid = uuid self.assertEqual(self.node._json['interfaces'][group_if_uid], { '4': 2, '6': None }) self.assertEqual(self.network11._json['freelist'], [{ 'start': 1, 'end': 1 }, { 'start': 3, 'end': 65533 }]) def test_same_net_different_ifs(self): self.new_group2 = luna.Group(name='new2', osimage=self.osimage.name, mongo_db=self.db, interfaces=['em1'], create=True) self.group.set_net_to_if('eth0', self.network11.name) self.new_group2.set_net_to_if('em1', self.network11.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.node.set_ip(interface_name='eth0', ip='10.51.0.2') self.assertTrue(self.node.set_group(self.new_group2.name)) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.new_group2 = luna.Group(name=self.new_group2.name, mongo_db=self.db) self.network11 = luna.Network(name=self.network11.name, mongo_db=self.db) group_if_uid = None for uuid in self.new_group2._json['interfaces']: group_if_uid = uuid self.assertEqual(self.node._json['interfaces'][group_if_uid], { '4': 2, '6': None }) self.assertEqual(self.network11._json['freelist'], [{ 'start': 1, 'end': 1 }, { 'start': 3, 'end': 65533 }]) def test_same_ifs_different_nets(self): self.new_group2 = luna.Group(name='new2', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.network12 = luna.Network(name="net12", mongo_db=self.db, create=True, NETWORK='10.52.0.0', PREFIX=16) self.group.set_net_to_if('eth0', self.network11.name) self.new_group2.set_net_to_if('eth0', self.network12.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.node.set_ip(interface_name='eth0', ip='10.51.0.2') self.assertTrue(self.node.set_group(self.new_group2.name)) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.new_group2 = luna.Group(name=self.new_group2.name, mongo_db=self.db) self.network11 = luna.Network(name=self.network11.name, mongo_db=self.db) self.network12 = luna.Network(name=self.network12.name, mongo_db=self.db) group_if_uid = None for uuid in self.new_group2._json['interfaces']: group_if_uid = uuid self.assertEqual(self.node._json['interfaces'][group_if_uid], { '4': 1, '6': None }) self.assertEqual(self.network11._json['freelist'], [{ 'start': 1, 'end': 65533 }]) self.assertEqual(self.network12._json['freelist'], [{ 'start': 2, 'end': 65533 }]) def test_add_del_interface_w_node(self): self.group.add_interface('eth1') self.group.del_interface('eth1') self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertEqual(len(self.node._json['interfaces']), 1)
class AddNetToGroupTests(unittest.TestCase): """ Add network to group with nodes """ @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) self.network = luna.Network( name="testnet", mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) self.network6 = luna.Network(name="testnet2", mongo_db=self.db, create=True, NETWORK='fe80::', PREFIX=64, version=6) def tearDown(self): self.sandbox.cleanup() def test_add_net_w_nodes_4(self): self.group.set_net_to_if('eth0', self.network.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.network = luna.Network(name=self.network.name, mongo_db=self.db) for key in self.node._json['interfaces']: if_dict = self.node._json['interfaces'][key] self.assertEqual(if_dict, {'4': 1, '6': None}) def test_add_net_w_nodes_6(self): self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.network = luna.Network(name=self.network6.name, mongo_db=self.db) for key in self.node._json['interfaces']: if_dict = self.node._json['interfaces'][key] self.assertEqual(if_dict, {'4': None, '6': 1}) def test_add_net_group_node_net4_net6(self): self.group.set_net_to_if('eth0', self.network.name) self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.network = luna.Network(name=self.network6.name, mongo_db=self.db) for key in self.node._json['interfaces']: if_dict = self.node._json['interfaces'][key] self.assertEqual(if_dict, {'4': 1, '6': 1}) def test_add_net_group_net4_node_net6(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MondoDB as a backend.') self.node.delete() self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.group.set_net_to_if('eth0', self.network.name) self.node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) for key in self.node._json['interfaces']: if_dict = self.node._json['interfaces'][key] self.assertEqual(if_dict, {'4': 1, '6': None}) self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) for key in self.node._json['interfaces']: if_dict = self.node._json['interfaces'][key] self.assertEqual(if_dict, {'4': 1, '6': 1})
class ZoneDataIPv6(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.cluster.set('path', self.path) self.cluster.set('frontend_address', '127.0.0.1') self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.net = luna.Network( 'testnet', mongo_db=self.db, create=True, NETWORK='fe80::', PREFIX=64, ) self.net.set('ns_hostname', 'master') self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.group.set_net_to_if('eth0', self.net.name) self.nodes = [] for i in range(10): self.nodes.append( luna.Node( group=self.group, create=True, mongo_db=self.db, )) self.net = luna.Network(name=self.net.name, mongo_db=self.db) def tearDown(self): self.sandbox.cleanup() def test_zone_data_simple(self): expected_dict = { 'ns_hostname': 'master', 'ns_ip': 'fe80::ffff:ffff:ffff:fffe', 'zone_name': 'testnet', 'version': 6, 'include': '', 'rev_include': '', 'hosts': { 'node001': 'fe80::1', 'node002': 'fe80::2', 'node003': 'fe80::3', 'node004': 'fe80::4', 'node005': 'fe80::5', 'node006': 'fe80::6', 'node007': 'fe80::7', 'node008': 'fe80::8', 'node009': 'fe80::9', 'node010': 'fe80::a', 'master': 'fe80::ffff:ffff:ffff:fffe', }, 'rev_zone_name': '0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f', 'rev_hosts': { '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0': 'node001.testnet.', '2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0': 'node002.testnet.', '3.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0': 'node003.testnet.', '4.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0': 'node004.testnet.', '5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0': 'node005.testnet.', '6.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0': 'node006.testnet.', '7.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0': 'node007.testnet.', '8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0': 'node008.testnet.', '9.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0': 'node009.testnet.', 'a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0': 'node010.testnet.', 'e.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f': 'master.testnet.', }, } self.assertEqual(self.net.zone_data, expected_dict)
class GetIPTests(unittest.TestCase): """ Test for Node.get_ip """ @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) self.network = luna.Network( name="testnet", mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) self.network6 = luna.Network(name="testnet2", mongo_db=self.db, create=True, NETWORK='fe80::', PREFIX=64, version=6) def tearDown(self): self.sandbox.cleanup() def test_get_ip_wrong_ver(self): self.assertFalse(self.node.get_ip('eth0', version=5)) def test_wo_uuid_or_name(self): self.assertFalse(self.node.get_ip(version=4)) def test_wrong_uuid(self): self.assertFalse(self.node.get_ip(interface_uuid='wrong-uuid')) def test_wrong_name(self): self.assertFalse(self.node.get_ip(interface_name='wrong-name')) def test_by_uuid_net4(self): interface_uuid = None for k in self.node._json['interfaces']: interface_uuid = k self.group.set_net_to_if('eth0', self.network.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertEqual(self.node.get_ip(interface_uuid=interface_uuid), '10.50.0.1') self.assertEqual( self.node.get_ip(interface_uuid=interface_uuid, format='num'), 1) self.assertEqual( self.node.get_ip(interface_uuid=interface_uuid, version=4), '10.50.0.1') def test_by_name_net6(self): self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertEqual(self.node.get_ip(interface_name='eth0'), 'fe80::1') self.assertEqual(self.node.get_ip(interface_name='eth0', format='num'), 1) self.assertEqual(self.node.get_ip(interface_name='eth0', version=6), 'fe80::1') def test_wrong_version6(self): interface_uuid = None for k in self.node._json['interfaces']: interface_uuid = k self.group.set_net_to_if('eth0', self.network.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertFalse( self.node.get_ip(interface_uuid=interface_uuid, version=6)) def test_wrong_version4(self): self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertFalse(self.node.get_ip(interface_name='eth0', version=4)) def test_ambiguous_ver(self): self.group.set_net_to_if('eth0', self.network.name) self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertFalse(self.node.get_ip(interface_name='eth0')) def test_both_vers_confgured(self): self.group.set_net_to_if('eth0', self.network.name) self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertEqual(self.node.get_ip(interface_name='eth0', version=6), 'fe80::1') self.assertEqual(self.node.get_ip(interface_name='eth0', version=4), '10.50.0.1')
class NodeCreateTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp(self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster( mongo_db=self.db, create=True, path=self.path, user=getpass.getuser() ) self.osimage = luna.OsImage( name='testosimage', path=self.path, mongo_db=self.db, create=True ) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) def tearDown(self): self.sandbox.cleanup() def test_create_node_with_defaults(self): node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) doc = self.db['node'].find_one({'_id': node._id}) expected = { 'name': 'node001', 'localboot': False, 'setupbmc': True, 'switch': None, 'service': False, '_use_': { 'cluster': {str(self.cluster._id): 1}, 'group': {str(self.group._id): 1}, }, 'group': self.group.DBRef, 'comment': '' } if_uuid = None for uuid in self.group._json['interfaces']: if_uuid = uuid expected['interfaces'] = {if_uuid: {'4': None, '6': None}} for attr in expected: self.assertEqual(doc[attr], expected[attr]) def test_create_named_node(self): node = luna.Node( name='n01', group=self.group.name, mongo_db=self.db, create=True, ) doc = self.db['node'].find_one({'_id': node._id}) expected = { 'name': 'n01', } for attr in expected: self.assertEqual(doc[attr], expected[attr]) def test_delete_node(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.' ) node = luna.Node( name='n02', group=self.group.name, mongo_db=self.db, create=True, ) nodeid = node._id node.delete() doc = self.db['node'].find_one({'_id': nodeid}) self.assertIsNone(doc) def test_create_node_exhausted_ips(self): net = luna.Network( 'net', mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) tight_net = luna.Network( 'tight_net', mongo_db=self.db, create=True, NETWORK='10.51.0.0', PREFIX=30, ) self.group.add_interface('eth1') self.group.set_net_to_if('eth0', net.name) self.group.set_net_to_if('eth1', tight_net.name) node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) with self.assertRaises(RuntimeError): luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) tight_net = luna.Network( name=tight_net.name, mongo_db=self.db) net = luna.Network( name=net.name, mongo_db=self.db) self.group = luna.Group( name=self.group.name, mongo_db=self.db) self.assertEqual(tight_net._json['freelist'], []) self.assertEqual(net._json['freelist'], [{'start': 2, 'end': 65533}]) self.assertEqual(len(self.group._json['_usedby_']['node']), 1)
class DeleteIPTests(unittest.TestCase): """ Test for delete ip addresses from node """ @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) self.network = luna.Network( name="testnet", mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) self.network6 = luna.Network(name="testnet2", mongo_db=self.db, create=True, NETWORK='fe80::', PREFIX=64, version=6) def tearDown(self): self.sandbox.cleanup() def test_node_del_ip_net4(self): self.group.set_net_to_if('eth0', self.network.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.node.del_ip('eth0') net_json = self.db['network'].find_one({'_id': self.network._id}) node_json = self.db['node'].find_one({'_id': self.node._id}) for if_uuid in node_json['interfaces']: if_dict = node_json['interfaces'][if_uuid] self.assertEqual(if_dict, {'4': None, '6': None}) self.assertEqual(net_json['freelist'], [{'start': 1, 'end': 65533L}]) def test_node_del_ip_net6(self): self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.node.del_ip('eth0') net_json = self.db['network'].find_one({'_id': self.network6._id}) node_json = self.db['node'].find_one({'_id': self.node._id}) for if_uuid in node_json['interfaces']: if_dict = node_json['interfaces'][if_uuid] self.assertEqual(if_dict, {'4': None, '6': None}) self.assertEqual(net_json['freelist'], [{ 'start': '1', 'end': '18446744073709551613' }]) def test_node_del_ip_mistaken(self): self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.node.del_ip('eth0') self.assertFalse(self.node.del_ip('eth0')) def test_node_del_ip_w_version(self): self.group.set_net_to_if('eth0', self.network.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertTrue(self.node.del_ip('eth0', version=4)) node_json = self.db['node'].find_one({'_id': self.node._id}) for k in node_json['interfaces']: self.assertEqual(node_json['interfaces'][k], { '4': None, '6': None }) def test_node_del_ip_wrong_version(self): self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertFalse(self.node.del_ip('eth0', version=4)) node_json = self.db['node'].find_one({'_id': self.node._id}) for k in node_json['interfaces']: self.assertEqual(node_json['interfaces'][k], {'4': None, '6': 1}) def test_node_del_ip_both_ver(self): self.group.set_net_to_if('eth0', self.network.name) self.group.set_net_to_if('eth0', self.network6.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertTrue(self.node.del_ip('eth0', version='all')) node_json = self.db['node'].find_one({'_id': self.node._id}) for k in node_json['interfaces']: self.assertEqual(node_json['interfaces'][k], { '4': None, '6': None }) net_json = self.db['network'].find_one({'_id': self.network._id}) net6_json = self.db['network'].find_one({'_id': self.network6._id}) self.assertEqual(net_json['freelist'], [{'start': 1, 'end': 65533}]) self.assertEqual(net6_json['freelist'], [{ 'start': '1', 'end': '18446744073709551613' }])
class NodeBootInstallTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp(self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_addmacro.return_value = True mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster( mongo_db=self.db, create=True, path=self.path, user=getpass.getuser() ) self.cluster.set('path', self.path) self.cluster.set('frontend_address', '127.0.0.1') self.osimage = luna.OsImage( name='testosimage', path=self.path, mongo_db=self.db, create=True ) self.net1 = luna.Network( 'testnet1', mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) self.net2 = luna.Network( 'testnet2', mongo_db=self.db, create=True, NETWORK='10.51.0.0', PREFIX=16, ) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.group_new = luna.Group( name='testgroup_new', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) self.boot_expected_dict = { 'domain': '', 'initrd_file': '', 'mac': '', 'kernel_file': '', 'localboot': 0, 'name': 'node001', 'service': 0, 'net': {}, 'hostname': 'node001', 'kern_opts': '', 'bootproto': 'dhcp' } self.install_expected_dict = { 'torrent_if': '', 'setupbmc': True, 'partscript': 'mount -t tmpfs tmpfs /sysroot', 'name': 'node001', 'tarball': '', 'bmcsetup': {}, 'interfaces': { 'BOOTIF': { 'options': '', '4': { 'ip': '', 'netmask': '', 'prefix': '', }, '6': { 'ip': '', 'netmask': '', 'prefix': '', } }, 'eth0': { 'options': '', '4': { 'ip': '', 'netmask': '', 'prefix': '', }, '6': { 'ip': '', 'netmask': '', 'prefix': '', } } }, 'prescript': '', 'domain': '', 'hostname': 'node001', 'postscript': ( 'cat << EOF >> /sysroot/etc/fstab\n' + 'tmpfs / tmpfs defaults 0 0\n' + 'EOF' ), 'kernopts': '', 'kernver': '3.10-999-el0.x86_64', 'torrent': '', 'mac': '', } def tearDown(self): self.sandbox.cleanup() def test_boot_params_default(self): self.assertEqual( self.node.boot_params, self.boot_expected_dict, ) def test_boot_params_w_net_and_mac_assigned(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.' ) mac = '00:11:22:33:44:55' self.group.set_net_to_if('eth0', self.net1.name) self.group.add_interface('BOOTIF') self.group.set_net_to_if('BOOTIF', self.net2.name) self.node.set_mac(mac) self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) self.boot_expected_dict['bootproto'] = 'static' self.boot_expected_dict['mac'] = mac self.boot_expected_dict['net']['4'] = {} self.boot_expected_dict['net']['4']['prefix'] = '16' self.boot_expected_dict['net']['4']['mask'] = '255.255.0.0' self.boot_expected_dict['net']['4']['ip'] = '10.51.0.1' self.assertEqual( self.node.boot_params, self.boot_expected_dict, ) def test_boot_params_w_bootif_wo_net(self): self.group.add_interface('BOOTIF') self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) self.assertEqual( self.node.boot_params, self.boot_expected_dict, ) def test_boot_params_w_net_wo_mac_assigned(self): self.group.set_net_to_if('eth0', self.net1.name) self.group.add_interface('BOOTIF') self.group.set_net_to_if('BOOTIF', self.net2.name) self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) self.assertEqual( self.node.boot_params, self.boot_expected_dict, ) def test_install_params_default(self): self.install_expected_dict['interfaces'].pop('BOOTIF') self.assertEqual( self.node.install_params, self.install_expected_dict, ) def test_install_params_w_nets(self): self.group.set_net_to_if('eth0', self.net1.name) self.group.add_interface('BOOTIF') self.group.set_net_to_if('BOOTIF', self.net2.name) self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) self.install_expected_dict['interfaces']['BOOTIF']['4'] = { 'ip': '10.51.0.1', 'netmask': '255.255.0.0', 'prefix': '16', } self.install_expected_dict['interfaces']['eth0']['4'] = { 'ip': '10.50.0.1', 'netmask': '255.255.0.0', 'prefix': '16', } self.assertEqual( self.node.install_params, self.install_expected_dict, ) def test_install_params_w_bmc(self): self.maxDiff = None bmcsetup = luna.BMCSetup( name='bmcsetup1', mongo_db=self.db, create=True, ) self.install_expected_dict['interfaces'].pop('BOOTIF') self.group.bmcsetup(bmcsetup.name) self.install_expected_dict['bmcsetup'] = { 'netchannel': 1, 'mgmtchannel': 1, 'userid': 3, 'user': '******', 'password': '******' } self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) self.assertEqual( self.node.install_params, self.install_expected_dict, ) def test_install_scripts(self): self.assertIsNone(self.node.render_script('non_exist')) self.assertEqual(self.node.render_script('boot').split()[0], '#!ipxe') self.assertEqual( self.node.render_script('install').split()[0], '#!/bin/bash' )
class NodeCreateTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) def tearDown(self): self.sandbox.cleanup() def test_create_node_with_defaults(self): node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) doc = self.db['node'].find_one({'_id': node._id}) expected = { 'name': 'node001', 'localboot': False, 'setupbmc': True, 'switch': None, 'service': False, '_use_': { 'cluster': { str(self.cluster._id): 1 }, 'group': { str(self.group._id): 1 }, }, 'group': self.group.DBRef, 'comment': '' } if_uuid = None for uuid in self.group._json['interfaces']: if_uuid = uuid expected['interfaces'] = {if_uuid: {'4': None, '6': None}} for attr in expected: self.assertEqual(doc[attr], expected[attr]) def test_create_named_node(self): node = luna.Node( name='n01', group=self.group.name, mongo_db=self.db, create=True, ) doc = self.db['node'].find_one({'_id': node._id}) expected = { 'name': 'n01', } for attr in expected: self.assertEqual(doc[attr], expected[attr]) def test_delete_node(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') node = luna.Node( name='n02', group=self.group.name, mongo_db=self.db, create=True, ) nodeid = node._id node.delete() doc = self.db['node'].find_one({'_id': nodeid}) self.assertIsNone(doc) def test_create_node_exhausted_ips(self): net = luna.Network( 'net', mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) tight_net = luna.Network( 'tight_net', mongo_db=self.db, create=True, NETWORK='10.51.0.0', PREFIX=30, ) self.group.add_interface('eth1') self.group.set_net_to_if('eth0', net.name) self.group.set_net_to_if('eth1', tight_net.name) node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) with self.assertRaises(RuntimeError): luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) tight_net = luna.Network(name=tight_net.name, mongo_db=self.db) net = luna.Network(name=net.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.assertEqual(tight_net._json['freelist'], []) self.assertEqual(net._json['freelist'], [{'start': 2, 'end': 65533}]) self.assertEqual(len(self.group._json['_usedby_']['node']), 1)
class OsimageCreateTests(unittest.TestCase): def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster( mongo_db=self.db, create=True, path=self.path, user=getpass.getuser(), ) def tearDown(self): self.sandbox.cleanup() @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def test_create_osimage_with_defaults(self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages osimage = luna.OsImage( name='testosimage', path=self.path, mongo_db=self.db, create=True, ) doc = self.db['osimage'].find_one({'_id': osimage._id}) expected = { 'path': self.path, 'kernmodules': 'ipmi_devintf,ipmi_si,ipmi_msghandler', 'dracutmodules': 'luna,-i18n,-plymouth', 'kernver': '3.10-999-el0.x86_64' } for attr in expected: self.assertEqual(doc[attr], expected[attr]) @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def test_create_osimage_with_same_path(self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages args = { 'name': 'testosimage', 'path': self.path, 'mongo_db': self.db, 'create': True, } luna.OsImage(**args) args['name'] = 'testosimage2' self.assertRaises(RuntimeError, luna.OsImage, **args) @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def test_create_osimage_wo_kernel(self, mock_rpm_addmacro, mock_rpm_transactionset, ): mock_rpm_transactionset.return_value.dbMatch.return_value = [] args = { 'name': 'testosimage', 'path': self.path, 'mongo_db': self.db, 'create': True, } self.assertRaises(RuntimeError, luna.OsImage, **args) def test_create_osimage_wrong_path(self): args = { 'name': 'testosimage', 'path': '/dev/null', 'mongo_db': self.db, 'create': True, } self.assertRaises(RuntimeError, luna.OsImage, **args) @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def test_create_osimage_wrong_kernver(self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages args = { 'name': 'testosimage', 'path': self.path, 'mongo_db': self.db, 'kernver': '3.10-000-el0.x86_64', 'create': True, } self.assertRaises(RuntimeError, luna.OsImage, **args) @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def test_create_osimage_wrong_grablist(self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages args = { 'name': 'testosimage', 'path': self.path, 'mongo_db': self.db, 'grab_list': 'no_grab_list', 'create': True, } self.assertRaises(RuntimeError, luna.OsImage, **args)
class NodeChangeTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.cluster.set('path', self.path) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.group_new = luna.Group( name='testgroup_new', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) def tearDown(self): self.sandbox.cleanup() def test_change_group(self): start_dict = self.db['node'].find_one({'_id': self.node._id}) self.node.set_group(self.group_new.name) expected_dict = copy.deepcopy(start_dict) expected_dict['group'] = self.group_new.DBRef expected_dict['_use_']['group'] = { u'' + str(self.group_new._id): 1, } actual_dict = self.db['node'].find_one({'_id': self.node._id}) if_uuid = None for uuid in self.group_new._json['interfaces']: if_uuid = uuid expected_dict['interfaces'] = {if_uuid: {'4': None, '6': None}} self.assertEqual(expected_dict, actual_dict) self.node.set_group(self.group.name) end_dict = self.db['node'].find_one({'_id': self.node._id}) self.assertEqual(start_dict, end_dict) def test_set_mac(self): self.node.set_mac('00:01:02:aa:bb:cc') d = self.db['mac'].find({'mac': '00:01:02:aa:bb:cc'}) self.assertEqual(d.count(), 1) for e in d: self.assertEqual(self.node.DBRef, e['node']) def test_change_mac(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') self.node.set_mac('00:01:02:aa:bb:cc') node2 = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) node2.set_mac('00:01:02:aa:bb:cc') d = self.db['mac'].find({'mac': '00:01:02:aa:bb:cc'}) self.assertEqual(d.count(), 1) for e in d: self.assertEqual(node2.DBRef, e['node']) def test_clear_mac(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') self.node.set_mac('00:01:02:aa:bb:cc') self.node.set_mac('') d = self.db['mac'].find() self.assertEqual(d.count(), 0) def test_get_mac(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') mac = '00:01:02:aa:bb:cc' self.node.set_mac(mac) self.assertEqual(self.node.get_mac(), mac) def test_set_switch(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') net = luna.Network( 'testnet', mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) switch = luna.Switch( 'test1', network=net.name, mongo_db=self.db, create=True, ) self.node.set_switch(switch.name) d1 = self.db['node'].find_one({'name': self.node.name}) d2 = self.db['switch'].find_one({'name': switch.name}) self.assertEqual(d1['switch'], switch.DBRef) self.assertEqual(len(d2['_usedby_']['node']), 1) self.assertEqual(d2['_usedby_']['node'][str(self.node._id)], 1) def test_change_switch(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') net = luna.Network( 'testnet', mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) switch1 = luna.Switch( 'test1', network=net.name, mongo_db=self.db, create=True, ) switch2 = luna.Switch( 'test2', network=net.name, mongo_db=self.db, create=True, ) self.node.set_switch(switch1.name) self.node.set_switch(switch2.name) d1 = self.db['node'].find_one({'name': self.node.name}) d2 = self.db['switch'].find_one({'name': switch1.name}) d3 = self.db['switch'].find_one({'name': switch2.name}) self.assertEqual(d1['switch'], switch2.DBRef) self.assertEqual(len(d2['_usedby_']), 0) self.assertEqual(len(d3['_usedby_']['node']), 1) self.assertEqual(d1['switch'], switch2.DBRef) def test_node_change_group(self): # create 3 groups group1 = self.group group2 = luna.Group( name='testgroup2', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) group3 = luna.Group( name='testgroup3', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth1'], create=True, ) group4 = luna.Group( name='testgroup4', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth3'], create=True, ) # create 3 networks net1 = luna.Network( 'testnet1', mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) net2 = luna.Network( 'testnet2', mongo_db=self.db, create=True, NETWORK='10.51.0.0', PREFIX=16, ) net3 = luna.Network( 'testnet3', mongo_db=self.db, create=True, NETWORK='10.52.0.0', PREFIX=16, ) # assign 2 networks to interfaces # group1: {'eth0': net1} # group2 {'eth0': net1, 'em1': net2} # group3 {'em1': net1, 'eth1': net2} # group4 {'eth3': net3} group2.add_interface('em1') group3.add_interface('em1') group1.set_net_to_if('eth0', net1.name) group2.set_net_to_if('eth0', net1.name) group3.set_net_to_if('eth1', net2.name) group4.set_net_to_if('eth3', net3.name) group2.set_net_to_if('em1', net2.name) group3.set_net_to_if('em1', net1.name) self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) # create 9 more nodes nodes = [] for i in range(10): nodes.append( luna.Node( group=self.group, create=True, mongo_db=self.db, )) # will do tests with node005 node005 = luna.Node('node005', mongo_db=self.db, group=self.group) # check did we get desired config node_json = self.db['node'].find_one({'name': node005.name}) net1_json = self.db['network'].find_one({'name': net1.name}) self.assertEqual(node_json['group'], group1.DBRef) for k in node_json['interfaces']: self.assertEqual(node_json['interfaces'][k], {'4': 5, '6': None}) self.assertEqual(len(net1_json['freelist']), 1) self.assertEqual(net1_json['freelist'][0]['start'], 12) #node_json['interfaces'], net1_json['freelist'] # # change group first time: # self.assertEqual(node005.set_group(group2.name), True) # updating group objects group1 = luna.Group( name=group1.name, mongo_db=self.db, ) group2 = luna.Group( name=group2.name, mongo_db=self.db, ) node_json = self.db['node'].find_one({'name': node005.name}) group2_json = self.db['group'].find_one({'name': group2.name}) net1_json = self.db['network'].find_one({'name': net1.name}) net2_json = self.db['network'].find_one({'name': net2.name}) # should be 2 interfaces now self.assertEqual(len(node_json['interfaces']), 2) # those interfaces should be the ones from group2 for uid in node_json['interfaces']: self.assertIn(uid, group2_json['interfaces'].keys()) # check if eth0 has the same IP adress: eth0_uuid = '' em1_uuid = '' for if_uid in group2_json['interfaces']: if group2_json['interfaces'][if_uid]['name'] == 'eth0': eth0_uuid = if_uid if group2_json['interfaces'][if_uid]['name'] == 'em1': em1_uuid = if_uid # check if we found self.assertIsNot(eth0_uuid, '') self.assertIsNot(em1_uuid, '') # should be 5 self.assertEqual(node_json['interfaces'][eth0_uuid], { '4': 5, '6': None }) # another should be 1 self.assertEqual(node_json['interfaces'][em1_uuid], { '4': 1, '6': None }) # check network self.assertEqual(net1_json['freelist'], [{'start': 12, 'end': 65533}]) self.assertEqual(net2_json['freelist'], [{'start': 2, 'end': 65533}]) # # change group second time # self.assertEqual(node005.set_group(group3.name), True) # fetch the data from DB node_json = self.db['node'].find_one({'name': node005.name}) group3_json = self.db['group'].find_one({'name': group3.name}) net1_json = self.db['network'].find_one({'name': net1.name}) net2_json = self.db['network'].find_one({'name': net2.name}) # try to find uuids of the interfaces eth1_uuid = '' em1_uuid = '' for if_uid in group3_json['interfaces']: if group3_json['interfaces'][if_uid]['name'] == 'eth1': eth1_uuid = if_uid if group3_json['interfaces'][if_uid]['name'] == 'em1': em1_uuid = if_uid # check if we found self.assertIsNot(eth1_uuid, '') self.assertIsNot(em1_uuid, '') # should be 1 self.assertEqual(node_json['interfaces'][eth1_uuid], { '4': 1, '6': None }) # another should be 5 self.assertEqual(node_json['interfaces'][em1_uuid], { '4': 5, '6': None }) # # change group third time # self.assertEqual(node005.set_group(group4.name), True) node_json = self.db['node'].find_one({'name': node005.name}) net1_json = self.db['network'].find_one({'name': net1.name}) net2_json = self.db['network'].find_one({'name': net2.name}) net3_json = self.db['network'].find_one({'name': net3.name}) self.assertEqual(net1_json['freelist'], [{ 'start': 5, u'end': 5 }, { 'start': 12, 'end': 65533 }]) self.assertEqual(net2_json['freelist'], [{'start': 1, 'end': 65533}]) self.assertEqual(net3_json['freelist'], [{'start': 2, 'end': 65533}]) def test_update_status(self): self.assertIsNone(self.node.update_status()) self.assertIsNone(self.node.update_status('#$#%')) self.assertTrue(self.node.update_status('status1')) doc = self.db['node'].find_one({'_id': self.node._id}) self.assertEqual(doc['status']['step'], 'status1') self.assertEqual(type(doc['status']['time']), datetime.datetime) def test_get_status(self): self.assertIsNone(self.node.get_status()) self.assertTrue(self.node.update_status('status1')) self.assertEqual(self.node.get_status()['status'], 'status1') self.assertTrue(self.node.get_status()['time']) self.assertEqual( self.node.get_status(relative=False)['status'], 'status1', ) self.assertTrue(self.node.get_status(relative=False)['time']) def test_get_status_tracker(self): name = "%20s" % self.node.name peerid = ''.join(["{:02x}".format(ord(l)) for l in name]) self.assertTrue(self.node.update_status('install.download')) now = datetime.datetime.today() doc2insert = { 'peer_id': peerid, 'updated': now, 'downloaded': 2, 'left': 1 } self.db['tracker'].insert(doc2insert) self.assertEqual(self.node.get_status()['status'][:39], 'install.download (66.67% / last update ')
class ClusterMakeDNSTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group = luna.Group(name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.net11 = luna.Network(name='net11', NETWORK='10.11.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net11.set('ns_hostname', 'master') self.net61 = luna.Network(name='net61', NETWORK='fe90::', PREFIX=64, mongo_db=self.db, create=True) self.net61.set('ns_hostname', 'master') self.group.set_net_to_if('eth0', self.net11.name) self.group.set_net_to_if('eth0', self.net61.name) self.group = luna.Group(name=self.group.name, mongo_db=self.db) self.node = luna.Node(group=self.group.name, mongo_db=self.db, create=True) self.switch = luna.Switch(name='sw01', network=self.net11.name, mongo_db=self.db, create=True) self.switch.set('ip', '10.11.1.1') self.otherdev = luna.OtherDev(name='pdu01', network=self.net11.name, ip="10.11.2.1", mongo_db=self.db, create=True) self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.group = luna.Group(name=self.group.name, mongo_db=self.db) def tearDown(self): self.sandbox.cleanup() def test_get_allocated_ips_wrong(self): self.assertFalse(self.group.get_allocated_ips("wrong")) def test_get_allocated_ips_simple(self): self.assertEqual(self.group.get_allocated_ips(self.net11), {'node001': 1}) def test_get_allocated_ips_duplicate_ifs(self): self.group.add_interface('BMC') self.group.set_net_to_if('BMC', self.net11.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertEqual(self.group.get_allocated_ips(self.net11), { 'node001-eth0': 1, 'node001-bmc': 2 }) def test_get_allocated_ips_duplicate_ifs_case(self): self.group.add_interface('BMC') self.group.set_net_to_if('BMC', self.net11.name) self.group.add_interface('Eth0') self.group.set_net_to_if('Eth0', self.net11.name) self.node = luna.Node(name=self.node.name, mongo_db=self.db) self.assertEqual(self.group.get_allocated_ips(self.net11), { 'node001-eth0': 1, 'node001-BMC': 2, 'node001-Eth0': 3 }) def test_resolve_used_ips_simple(self): self.assertEqual( self.net11.resolve_used_ips(), { 'node001': '10.11.0.1', 'sw01': '10.11.1.1', 'pdu01': '10.11.2.1', 'master': '10.11.255.254' })
class NodeBootInstallTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_addmacro.return_value = True mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.cluster.set('path', self.path) self.cluster.set('frontend_address', '127.0.0.1') self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.net1 = luna.Network( 'testnet1', mongo_db=self.db, create=True, NETWORK='10.50.0.0', PREFIX=16, ) self.net2 = luna.Network( 'testnet2', mongo_db=self.db, create=True, NETWORK='10.51.0.0', PREFIX=16, ) self.group = luna.Group( name='testgroup', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.group_new = luna.Group( name='testgroup_new', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True, ) self.node = luna.Node( group=self.group.name, mongo_db=self.db, create=True, ) self.boot_expected_dict = { 'domain': '', 'initrd_file': '', 'mac': '', 'kernel_file': '', 'localboot': 0, 'name': 'node001', 'service': 0, 'net': {}, 'hostname': 'node001', 'kern_opts': '', 'bootproto': 'dhcp' } self.install_expected_dict = { 'torrent_if': '', 'setupbmc': True, 'partscript': 'mount -t tmpfs tmpfs /sysroot', 'name': 'node001', 'tarball': '', 'bmcsetup': {}, 'interfaces': { 'BOOTIF': { 'options': '', '4': { 'ip': '', 'netmask': '', 'prefix': '', }, '6': { 'ip': '', 'netmask': '', 'prefix': '', } }, 'eth0': { 'options': '', '4': { 'ip': '', 'netmask': '', 'prefix': '', }, '6': { 'ip': '', 'netmask': '', 'prefix': '', } } }, 'prescript': '', 'domain': '', 'hostname': 'node001', 'postscript': ('cat << EOF >> /sysroot/etc/fstab\n' + 'tmpfs / tmpfs defaults 0 0\n' + 'EOF'), 'kernopts': '', 'kernver': '3.10-999-el0.x86_64', 'torrent': '', 'mac': '', } def tearDown(self): self.sandbox.cleanup() def test_boot_params_default(self): self.assertEqual( self.node.boot_params, self.boot_expected_dict, ) def test_boot_params_w_net_and_mac_assigned(self): if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.') mac = '00:11:22:33:44:55' self.group.set_net_to_if('eth0', self.net1.name) self.group.add_interface('BOOTIF') self.group.set_net_to_if('BOOTIF', self.net2.name) self.node.set_mac(mac) self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) self.boot_expected_dict['bootproto'] = 'static' self.boot_expected_dict['mac'] = mac self.boot_expected_dict['net']['4'] = {} self.boot_expected_dict['net']['4']['prefix'] = '16' self.boot_expected_dict['net']['4']['mask'] = '255.255.0.0' self.boot_expected_dict['net']['4']['ip'] = '10.51.0.1' self.assertEqual( self.node.boot_params, self.boot_expected_dict, ) def test_boot_params_w_bootif_wo_net(self): self.group.add_interface('BOOTIF') self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) self.assertEqual( self.node.boot_params, self.boot_expected_dict, ) def test_boot_params_w_net_wo_mac_assigned(self): self.group.set_net_to_if('eth0', self.net1.name) self.group.add_interface('BOOTIF') self.group.set_net_to_if('BOOTIF', self.net2.name) self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) self.assertEqual( self.node.boot_params, self.boot_expected_dict, ) def test_install_params_default(self): self.install_expected_dict['interfaces'].pop('BOOTIF') self.assertEqual( self.node.install_params, self.install_expected_dict, ) def test_install_params_w_nets(self): self.group.set_net_to_if('eth0', self.net1.name) self.group.add_interface('BOOTIF') self.group.set_net_to_if('BOOTIF', self.net2.name) self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) self.install_expected_dict['interfaces']['BOOTIF']['4'] = { 'ip': '10.51.0.1', 'netmask': '255.255.0.0', 'prefix': '16', } self.install_expected_dict['interfaces']['eth0']['4'] = { 'ip': '10.50.0.1', 'netmask': '255.255.0.0', 'prefix': '16', } self.assertEqual( self.node.install_params, self.install_expected_dict, ) def test_install_params_w_bmc(self): self.maxDiff = None bmcsetup = luna.BMCSetup( name='bmcsetup1', mongo_db=self.db, create=True, ) self.install_expected_dict['interfaces'].pop('BOOTIF') self.group.bmcsetup(bmcsetup.name) self.install_expected_dict['bmcsetup'] = { 'netchannel': 1, 'mgmtchannel': 1, 'userid': 3, 'user': '******', 'password': '******' } self.node = luna.Node( name=self.node.name, mongo_db=self.db, ) self.assertEqual( self.node.install_params, self.install_expected_dict, ) def test_install_scripts(self): self.assertIsNone(self.node.render_script('non_exist')) self.assertEqual(self.node.render_script('boot').split()[0], '#!ipxe') self.assertEqual( self.node.render_script('install').split()[0], '#!/bin/bash')
class GroupConfigTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp( self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path #osimage_path = self.sandbox.create_osimage() self.cluster = luna.Cluster(mongo_db=self.db, create=True, path=self.path, user=getpass.getuser()) self.cluster.set("frontend_address", "127.0.0.1") self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.bmcsetup = luna.BMCSetup(name='bmcsetup', mongo_db=self.db, create=True) self.net1 = luna.Network(name='cluster', NETWORK='10.11.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net2 = luna.Network(name='external', NETWORK='10.12.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net3 = luna.Network(name='ib', NETWORK='10.13.0.0', PREFIX=16, mongo_db=self.db, create=True) self.net6 = luna.Network(name='net6', NETWORK='fe80::', PREFIX=64, mongo_db=self.db, create=True, version=6) self.prescript = 'pre' self.postscript = 'post' self.partscript = 'part' self.nics = {'eth0': 'PARM=1', 'eth1': 'PARM=2', 'ib0': 'PARM=3'} self.group = luna.Group(name='compute', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) def tearDown(self): self.sandbox.cleanup() def test_add_remove_net_to_if(self): start_dict = self.db['group'].find_one({'_id': self.group._id}) show_if_expected = { 'name': 'eth0', 'options': '', 'network': { '4': { 'prefix': '', 'netmask': '', 'name': '', 'network': '' }, '6': { 'prefix': '', 'netmask': '', 'name': '', 'network': '' }, }, } self.group.set_net_to_if('eth0', self.net1.name) show_if_expected['network']['4']['name'] = 'cluster' show_if_expected['network']['4']['netmask'] = '255.255.0.0' show_if_expected['network']['4']['prefix'] = '16' show_if_expected['network']['4']['network'] = '10.11.0.0' self.assertEqual(self.group.show_if('eth0'), show_if_expected) self.group.del_net_from_if('eth0') # check if we get the same dictionary at the and end_dict = self.db['group'].find_one({'_id': self.group._id}) self.assertEqual(start_dict, end_dict) def test_add_remove_net6_to_if(self): start_dict = self.db['group'].find_one({'_id': self.group._id}) net6 = luna.Network( name='net6', mongo_db=self.db, ) show_if_expected = { 'name': 'eth0', 'options': '', 'network': { '4': { 'prefix': '', 'netmask': '', 'name': '', 'network': '' }, '6': { 'prefix': '', 'netmask': '', 'name': '', 'network': '' }, }, } self.group.set_net_to_if('eth0', net6.name) show_if_expected['network']['6']['name'] = 'net6' show_if_expected['network']['6']['netmask'] = 'ffff:ffff:ffff:ffff::' show_if_expected['network']['6']['prefix'] = '64' show_if_expected['network']['6']['network'] = 'fe80::' self.assertEqual(self.group.show_if('eth0'), show_if_expected) self.group.del_net_from_if('eth0') # check if we get the same dictionary at the and end_dict = self.db['group'].find_one({'_id': self.group._id}) self.assertEqual(start_dict, end_dict) @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def test_osimage( self, mock_rpm_addmacro, mock_rpm_transactionset, ): packages = [ { 'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64' }, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages start_dict = self.db['group'].find_one({'_id': self.group._id}) osimage_name = 'osimage2' osimage = luna.OsImage(name=osimage_name, path='', mongo_db=self.db, create=True) self.group.osimage(osimage.name) self.assertEqual(self.group.show()['osimage'], '[' + osimage.name + ']') self.group.osimage(self.osimage.name) self.assertEqual(self.group.show()['osimage'], '[' + self.osimage.name + ']') end_dict = self.db['group'].find_one({'_id': self.group._id}) self.assertEqual(start_dict, end_dict) def test_bmcsetup(self): start_dict = self.db['group'].find_one({'_id': self.group._id}) self.group.bmcsetup(self.bmcsetup.name) self.assertEqual(self.group.show()['bmcsetup'], '[' + self.bmcsetup.name + ']') self.group.bmcsetup() self.assertIsNone(self.group.show()['bmcsetup']) end_dict = self.db['group'].find_one({'_id': self.group._id}) self.assertEqual(start_dict, end_dict) def test_get_net_name_for_if(self): self.assertEqual(self.group.get_net_name_for_if('eth0'), '') self.assertEqual(self.group.get_net_name_for_if('unexisting_if'), '') self.group.set_net_to_if('eth0', self.net1.name) self.assertEqual(self.group.get_net_name_for_if('eth0'), self.net1.name) self.group.del_net_from_if('eth0') def test_add_del_interface(self): start_dict = self.db['group'].find_one({'_id': self.group._id}) if1 = start_dict['interfaces'] self.assertIsNone(self.group.add_interface('eth0')) if2 = self.db['group'].find_one({'_id': self.group._id})['interfaces'] # chech if dictionary did not change self.assertEqual(if1, if2) self.assertTrue(self.group.add_interface('eth1')) if3 = self.db['group'].find_one({'_id': self.group._id})['interfaces'] for e in if2.keys(): # we should pop all the interfaces except eth1 self.assertEqual(if3.pop(e)['name'], 'eth0') # last one should be eth1 self.assertEqual(if3.values()[0]['name'], 'eth1') # should be the only last self.assertEqual(len(if3.keys()), 1) self.assertTrue(self.group.del_interface('eth1')) if4 = self.db['group'].find_one({'_id': self.group._id})['interfaces'] self.assertEqual(if1, if4) self.assertFalse(self.group.del_interface('eth1')) end_dict = self.db['group'].find_one({'_id': self.group._id}) self.assertEqual(start_dict, end_dict) def test_rename_interface(self): start_dict = self.db['group'].find_one({'_id': self.group._id}) if1 = self.group.list_ifs() self.assertEqual(if1.keys()[0], 'eth0') self.assertIsNone(self.group.rename_interface('unexisting_if', 'eth1')) self.assertTrue(self.group.add_interface('ethX')) self.assertIsNone(self.group.rename_interface('ethX', 'eth0')) self.assertTrue(self.group.rename_interface('ethX', 'eth1')) self.assertTrue(self.group.del_interface('eth1')) end_dict = self.db['group'].find_one({'_id': self.group._id}) self.assertEqual(start_dict, end_dict) def test_get_ip(self): self.group.set_net_to_if('eth0', self.net1.name) group_json = self.db['group'].find_one({'_id': self.group._id}) if_uuid = "" for k in group_json['interfaces']: if_uuid = k self.assertEqual(len(if_uuid), 32) human_eth0_ip = self.group.get_ip(interface_uuid=if_uuid, ip=500, format='human') num_eth0_ip = self.group.get_ip(interface_uuid=if_uuid, ip="10.11.1.244", format='num') self.assertEqual(human_eth0_ip, "10.11.1.244") self.assertEqual(num_eth0_ip, 500) out = self.group.get_ip() self.assertIsNone(out) out = self.group.get_ip(interface_uuid=if_uuid) self.assertIsNone(out) def test_set_domain(self): self.group.set_domain(self.net1.name) group_json = self.db['group'].find_one({'_id': self.group._id}) self.assertEqual(group_json['domain'], self.net1.DBRef) self.assertEqual(group_json['_use_']['network'], {str(self.net1._id): 1}) def test_change_domain(self): self.group.set_domain(self.net1.name) self.group.set_domain(self.net2.name) group_json = self.db['group'].find_one({'_id': self.group._id}) self.assertEqual(group_json['domain'], self.net2.DBRef) self.assertEqual(group_json['_use_']['network'], {str(self.net2._id): 1}) def test_delete_domain(self): self.group.set_domain(self.net1.name) self.group.set_domain() group_json = self.db['group'].find_one({'_id': self.group._id}) self.assertIsNone(group_json['domain']) self.assertNotIn('network', group_json['_use_'].keys()) def test_set_net_to_if_4(self): ret = self.group.set_net_to_if('eth0', self.net1.name) self.assertTrue(ret) group_json = self.db['group'].find_one({'_id': self.group._id}) net_json = self.db['network'].find_one({'_id': self.net1._id}) interfaces = group_json['interfaces'] for k in interfaces: self.assertEqual(interfaces[k]['network']['4'], self.net1.DBRef) self.assertEqual(group_json['_use_']['network'], {str(self.net1._id): 1}) self.assertEqual(net_json['_usedby_']['group'], {str(self.group._id): 1}) def test_set_net_to_if_6(self): ret = self.group.set_net_to_if('eth0', self.net6.name) self.assertTrue(ret) group_json = self.db['group'].find_one({'_id': self.group._id}) net_json = self.db['network'].find_one({'_id': self.net6._id}) interfaces = group_json['interfaces'] for k in interfaces: self.assertEqual(interfaces[k]['network']['6'], self.net6.DBRef) self.assertEqual(group_json['_use_']['network'], {str(self.net6._id): 1}) self.assertEqual(net_json['_usedby_']['group'], {str(self.group._id): 1}) def test_set_net_to_if_wrong_if(self): ret = self.group.set_net_to_if('eth1', self.net1.name) self.assertFalse(ret) group_json = self.db['group'].find_one({'_id': self.group._id}) interfaces = group_json['interfaces'] for k in interfaces: self.assertEqual(interfaces[k]['network']['4'], None) self.assertEqual(interfaces[k]['network']['6'], None) def test_set_net_to_if_4_occupied(self): ret = self.group.set_net_to_if('eth0', self.net1.name) self.assertTrue(ret) ret = self.group.set_net_to_if('eth0', self.net2.name) group_json = self.db['group'].find_one({'_id': self.group._id}) net_json = self.db['network'].find_one({'_id': self.net1._id}) interfaces = group_json['interfaces'] for k in interfaces: self.assertEqual(interfaces[k]['network']['4'], self.net1.DBRef) self.assertEqual(group_json['_use_']['network'], {str(self.net1._id): 1}) self.assertEqual(net_json['_usedby_']['group'], {str(self.group._id): 1}) def test_manage_ip_wrong_ver(self): if_uuid = None group_json = self.db['group'].find_one({'_id': self.group._id}) for k in group_json['interfaces']: if_uuid = k ret = self.group.manage_ip(if_uuid, version=5) self.assertFalse(ret) def test_manage_ip_wo_net_reserve(self): if_uuid = None group_json = self.db['group'].find_one({'_id': self.group._id}) for k in group_json['interfaces']: if_uuid = k self.assertFalse(self.group.manage_ip(if_uuid)) def test_manage_ip_w_both_nets_wo_ver_specified(self): if_uuid = None group_json = self.db['group'].find_one({'_id': self.group._id}) for k in group_json['interfaces']: if_uuid = k self.group.set_net_to_if('eth0', self.net1.name) self.group.set_net_to_if('eth0', self.net6.name) ret = self.group.manage_ip(if_uuid) self.assertFalse(ret) def test_manage_ip_w_net_4_reserve(self): if_uuid = None group_json = self.db['group'].find_one({'_id': self.group._id}) for k in group_json['interfaces']: if_uuid = k self.group.set_net_to_if('eth0', self.net1.name) ret = self.group.manage_ip(if_uuid) self.assertEqual(ret, 1) ret = self.group.manage_ip(if_uuid) self.assertEqual(ret, 2) net_json = self.db['network'].find_one({'_id': self.net1._id}) self.assertEqual(net_json['freelist'], [{'start': 3, 'end': 65533}]) def test_manage_ip_w_net_4_reserve_specific(self): if_uuid = None group_json = self.db['group'].find_one({'_id': self.group._id}) for k in group_json['interfaces']: if_uuid = k self.group.set_net_to_if('eth0', self.net1.name) ret = self.group.manage_ip(if_uuid, '10.11.0.2') self.assertEqual(ret, 2) ret = self.group.manage_ip(if_uuid, 3) self.assertEqual(ret, 3) net_json = self.db['network'].find_one({'_id': self.net1._id}) self.assertEqual(net_json['freelist'], [{ 'end': 1, 'start': 1 }, { 'end': 65533, 'start': 4 }]) def test_manage_ip_w_net_4_reserve_first_free(self): if_uuid = None group_json = self.db['group'].find_one({'_id': self.group._id}) for k in group_json['interfaces']: if_uuid = k self.group.set_net_to_if('eth0', self.net1.name) ret = self.group.manage_ip(if_uuid, '10.11.0.2') ret = self.group.manage_ip(if_uuid, '10.11.0.3') ret = self.group.manage_ip(if_uuid) self.assertEqual(ret, 1) net_json = self.db['network'].find_one({'_id': self.net1._id}) self.assertEqual(net_json['freelist'], [{'end': 65533, 'start': 4}]) def test_manage_ip_w_net_wrong_ver(self): if_uuid = None group_json = self.db['group'].find_one({'_id': self.group._id}) for k in group_json['interfaces']: if_uuid = k self.group.set_net_to_if('eth0', self.net1.name) ret = self.group.manage_ip(if_uuid, '10.11.0.3', version=6) self.assertFalse(ret) def test_manage_ip_w_net_6_reserve(self): if_uuid = None group_json = self.db['group'].find_one({'_id': self.group._id}) for k in group_json['interfaces']: if_uuid = k self.group.set_net_to_if('eth0', self.net6.name) ret = self.group.manage_ip(if_uuid) self.assertEqual(ret, 1) ret = self.group.manage_ip(if_uuid) self.assertEqual(ret, 2) net_json = self.db['network'].find_one({'_id': self.net6._id}) self.assertEqual(net_json['freelist'], [{ 'start': '3', 'end': '18446744073709551613' }])
class ClusterMakeDHCPTests(unittest.TestCase): @mock.patch('rpm.TransactionSet') @mock.patch('rpm.addMacro') def setUp(self, mock_rpm_addmacro, mock_rpm_transactionset, ): print packages = [ {'VERSION': '3.10', 'RELEASE': '999-el0', 'ARCH': 'x86_64'}, ] mock_rpm_transactionset.return_value.dbMatch.return_value = packages self.sandbox = Sandbox() if self.sandbox.dbtype != 'mongo': raise unittest.SkipTest( 'This test can be run only with MongoDB as a backend.' ) self.db = self.sandbox.db self.path = self.sandbox.path self.cluster = luna.Cluster( mongo_db=self.db, create=True, path=self.path, user=getpass.getuser() ) self.osimage = luna.OsImage(name='testosimage', path=self.path, mongo_db=self.db, create=True) self.group1 = luna.Group(name='testgroup1', osimage=self.osimage.name, mongo_db=self.db, interfaces=['eth0'], create=True) self.group2 = luna.Group(name='testgroup2', osimage=self.osimage.name, mongo_db=self.db, interfaces=['BOOTIF'], create=True) self.net11 = luna.Network(name='net11', NETWORK='10.11.0.0', PREFIX=16, mongo_db=self.db, create=True) self.group1.set_net_to_if('eth0', self.net11.name) self.group2.set_net_to_if('BOOTIF', self.net11.name) self.node1 = luna.Node(group=self.group1.name, mongo_db=self.db, create=True) self.node2 = luna.Node(group=self.group2.name, mongo_db=self.db, create=True) self.node1.set_mac('00:11:22:33:44:55') self.node2.set_mac('01:11:22:33:44:55') self.group1 = luna.Group(name=self.group1.name, mongo_db=self.db) self.group2 = luna.Group(name=self.group2.name, mongo_db=self.db) self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.node1 = luna.Node(name=self.node1.name, mongo_db=self.db) self.node2 = luna.Node(name=self.node2.name, mongo_db=self.db) def tearDown(self): self.sandbox.cleanup() def test_get_ip_macs(self): self.assertEqual( self.net11.get_ip_macs(), { 'node001': {'ip': '10.11.0.1', 'mac': '00:11:22:33:44:55'}, 'node002': {'ip': '10.11.0.2', 'mac': '01:11:22:33:44:55'}, } ) def test_make_config_basic(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.11.128.1', end_ip='10.11.129.254' ) self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.assertEqual( self.net11._json['freelist'], [ {'start': 3, 'end': 32768}, {'start': 33279, 'end': 65533} ] ) hmac_key = ret.pop('hmac_key') self.assertTrue(len(hmac_key) > 1) self.assertEqual( ret, { 'network': '10.11.0.0', 'dhcp_start': '10.11.128.1', 'dhcp_end': '10.11.129.254', 'reservations': { 'node002': { 'ip': '10.11.0.2', 'mac': '01:11:22:33:44:55'}, 'node001': { 'ip': '10.11.0.1', 'mac': '00:11:22:33:44:55'}, }, 'netmask': '255.255.0.0', 'frontend_ip': '10.11.255.254', 'frontend_port': '7050', 'protocol': 'http', } ) def test_make_config_empty(self): self.cluster.set('frontend_address', '10.11.255.254') self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.11.128.1', end_ip='10.11.129.254' ) ret = self.cluster.makedhcp_config() hmac_key = ret.pop('hmac_key') self.assertTrue(len(hmac_key) > 1) self.assertEqual( ret, { 'network': '10.11.0.0', 'dhcp_start': '10.11.128.1', 'dhcp_end': '10.11.129.254', 'reservations': { 'node002': { 'ip': '10.11.0.2', 'mac': '01:11:22:33:44:55'}, 'node001': { 'ip': '10.11.0.1', 'mac': '00:11:22:33:44:55'}, }, 'netmask': '255.255.0.0', 'frontend_ip': '10.11.255.254', 'frontend_port': '7050', 'protocol': 'http', } ) def test_make_config_wo_frontend_address(self): ret = self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.11.128.1', end_ip='10.11.129.254' ) self.assertEqual(ret, {}) def test_make_config_start_less_end(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.11.129.254', end_ip='10.11.128.1' ) self.assertEqual(ret, {}) def test_set_wrong_frontend(self): self.cluster.set('frontend_address', '10.12.255.254') ret = self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.11.0.2', end_ip='10.11.0.3' ) self.assertEqual(ret, {}) def test_set_wrong_start(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.12.0.2', end_ip='10.11.0.3' ) self.assertEqual(ret, {}) def test_set_wrong_end(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.11.0.2', end_ip='10.12.0.3' ) self.assertEqual(ret, {}) def test_set_wrong_range(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.11.0.2', end_ip='10.11.0.3' ) self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.assertEqual( self.net11._json['freelist'], [{'start': 3, 'end': 65533}] ) self.assertEqual(ret, {}) def test_change_to_same_range(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.11.0.3', end_ip='10.11.0.4' ) ret = self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.11.0.3', end_ip='10.11.0.4' ) ret.pop('hmac_key') self.assertEqual( ret, { 'network': '10.11.0.0', 'dhcp_start': '10.11.0.3', 'dhcp_end': '10.11.0.4', 'reservations': { 'node002': { 'ip': '10.11.0.2', 'mac': '01:11:22:33:44:55'}, 'node001': { 'ip': '10.11.0.1', 'mac': '00:11:22:33:44:55'}, }, 'netmask': '255.255.0.0', 'frontend_ip': '10.11.255.254', 'frontend_port': '7050', 'protocol': 'http', } ) def test_change_to_wrong_range(self): self.cluster.set('frontend_address', '10.11.255.254') ret = self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.11.128.1', end_ip='10.11.129.254' ) ret = self.cluster.makedhcp_config( net_name=self.net11.name, start_ip='10.11.0.2', end_ip='10.11.0.4' ) self.assertEqual(ret, {}) self.net11 = luna.Network(name=self.net11.name, mongo_db=self.db) self.assertEqual(self.cluster.get('dhcp_range_start'), '10.11.128.1') self.assertEqual(self.cluster.get('dhcp_range_end'), '10.11.129.254') self.assertEqual( self.net11._json['freelist'], [ {'start': 3, 'end': 32768}, {'start': 33279, 'end': 65533} ] )