def test_machine_owned_by_one_switch(self): with database.session() as session: switch1 = model.Switch(ip='192.168.1.1') switch2 = model.Switch(ip='192.168.1.2') machine = model.Machine(mac='00:00:00:01:02:03', port='123', vlan=100) switch1.machines = [machine] switch2.machines = [machine] session.add(switch1) session.add(switch2) with database.session() as session: machine = session.query(model.Machine).first() self.assertEqual(machine.switch.ip, '192.168.1.2')
def _call(): with database.session() as session: machine1 = model.Machine(mac='00:00:00:01:02:03', port='123', vlan=100) machine2 = model.Machine(mac='00:00:00:01:02:03', port='123', vlan=100) switch = model.Switch(ip='192.168.1.1') switch.machines = [machine1, machine2] session.add(switch)
def test_del_machine(self): with database.session() as session: switch = model.Switch(ip='192.68.1.1') switch.machines = [ model.Machine(mac='00:00:00:01:02:03', port='123', vlan=100) ] session.add(switch) with database.session() as session: session.query(model.Machine).delete() with database.session() as session: switch = session.query(model.Switch).first() self.assertEqual(switch.machines.count(), 0)
def test_machine_with_switch(self): with database.session() as session: switch = model.Switch(ip='192.168.1.1') switch.machines.append( model.Machine(mac='00:00:00:01:02:03', port='123', vlan=100)) session.add(switch) with database.session() as session: machines = session.query(model.Machine).all() self.assertEqual(len(machines), 1) self.assertEqual(machines[0].mac, '00:00:00:01:02:03') self.assertEqual(machines[0].port, '123') self.assertEqual(machines[0].vlan, 100) self.assertIsNotNone(machines[0].switch)
def test_clusterhost(self): with database.session() as session: host = model.ClusterHost(hostname='host1') host.cluster = model.Cluster(name='cluster1') host.machine = model.Machine(mac='00:00:00:01:02:03', port='123', vlan=100) host.machine.switch = model.Switch(ip='192.168.1.1') session.add(host) with database.session() as session: host = session.query(model.ClusterHost).first() self.assertEqual(host.cluster.name, 'cluster1') self.assertEqual(host.machine.mac, '00:00:00:01:02:03') self.assertEqual(host.machine.switch.ip, '192.168.1.1')
def test_switch(self): with database.session() as session: switch = model.Switch(ip='10.145.88.1') switch.credential = {'version': 'v2c', 'community': 'public'} switch.vendor = 'huawei' session.add(switch) with database.session() as session: switches = session.query(model.Switch).all() self.assertEqual(len(switches), 1) self.assertEqual(switches[0].ip, '10.145.88.1') self.assertEqual(switches[0].credential, { 'version': 'v2c', 'community': 'public' }) self.assertEqual(switches[0].vendor, 'huawei')
def test_del_switch(self): with database.session() as session: switch = model.Switch(ip='192.68.1.1') switch.machines = [ model.Machine(mac='00:00:00:01:02:03', port='123', vlan=100) ] session.add(switch) with database.session() as session: session.query(model.Switch).delete() with database.session() as session: machines = session.query(model.Machine).all() self.assertEqual(len(machines), 1) self.assertEqual(machines[0].mac, '00:00:00:01:02:03') self.assertIsNone(machines[0].switch)
def test_host_config(self): with database.session() as session: cluster = model.Cluster(name='cluster1') host = model.ClusterHost(hostname='host1') host.machine = model.Machine(mac='00:00:00:01:02:03', port='123', vlan=100) host.machine.switch = model.Switch(ip='192.168.1.1') cluster.hosts = [host] host.config = { 'networking': { 'interfaces': { 'management': { 'ip': '192.168.1.100' } } } } session.add(cluster) with database.session() as session: host = session.query(model.ClusterHost).first() self.assertDictContainsSubset( { 'hostname': 'host1', 'clustername': 'cluster1', 'networking': { 'interfaces': { 'management': { 'mac': '00:00:00:01:02:03', 'ip': '192.168.1.100' } } }, 'switch_port': '123', 'vlan': 100, 'switch_ip': '192.168.1.1' }, host.config)
def _call(): with database.session() as session: session.add(model.Switch(ip='10.145.88.1')) session.add(model.Switch(ip='10.145.88.1'))