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)
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, )
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])
def setUp(self): print self.sandbox = Sandbox() self.db = self.sandbox.db self.path = self.sandbox.path
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 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 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)
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 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())
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 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 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)
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()
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 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 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)
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')
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, )
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 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)
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 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)
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 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 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 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 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 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 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 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 ' )
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' }
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 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' })
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 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)
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 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)
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': '', }