def test_apply_rules_set_fails_gracefully(self): port_id = 1 mac_address = netaddr.EUI("AA:BB:CC:DD:EE:FF") conn_err = redis.ConnectionError with mock.patch("redis.Redis") as redis_mock: client = redis_client.Client() redis_mock.set.side_effect = conn_err client.apply_rules(port_id, mac_address.value, [])
def test_redis_key(self, redis): client = redis_client.Client() device_id = str(uuid.uuid4()) mac_address = netaddr.EUI("AA:BB:CC:DD:EE:FF") redis_key = client.rule_key(device_id, mac_address.value) expected = "%s.%s" % (device_id, str(mac_address)) self.assertEqual(expected, redis_key)
def test_serialize_group_with_rules_and_remote_network(self, redis): rule_dict = {"ethertype": 0x800, "protocol": 1, "direction": "ingress", "remote_ip_prefix": "192.168.0.0/24"} client = redis_client.Client() group = models.SecurityGroup() rule = models.SecurityGroupRule() rule.update(rule_dict) group.rules.append(rule) payload = client.serialize([group]) self.assertTrue(payload.get("id") is not None) rule = payload["rules"][0] self.assertEqual(0x800, rule["ethertype"]) self.assertEqual(1, rule["protocol"]) self.assertEqual(None, rule["port start"]) self.assertEqual(None, rule["port end"]) self.assertEqual("allow", rule["action"]) self.assertEqual("ingress", rule["direction"]) self.assertEqual("::ffff:192.168.0.0/120", rule["source network"]) self.assertEqual("", rule["destination network"])
def test_serialize_group_with_rules(self, redis): rule_dict = {"ethertype": 0x800, "protocol": 6, "port_range_min": 80, "port_range_max": 443, "direction": "ingress"} client = redis_client.Client() group = models.SecurityGroup() rule = models.SecurityGroupRule() rule.update(rule_dict) group.rules.append(rule) payload = client.serialize([group]) self.assertTrue(payload.get("id") is not None) rule = payload["rules"][0] self.assertEqual(0x800, rule["ethertype"]) self.assertEqual(6, rule["protocol"]) self.assertEqual(80, rule["port start"]) self.assertEqual(443, rule["port end"]) self.assertEqual("allow", rule["action"]) self.assertEqual("ingress", rule["direction"]) self.assertEqual("", rule["source network"]) self.assertEqual("", rule["destination network"])
def test_serialize_group_no_rules(self, redis): client = redis_client.Client() group = models.SecurityGroup() payload = client.serialize([group]) self.assertTrue(payload.get("id") is not None) self.assertEqual([], payload.get("rules"))
def test_client_connection_fails_gracefully(self): conn_err = redis.ConnectionError with mock.patch("redis.Redis") as redis_mock: redis_mock.side_effect = conn_err with self.assertRaises(q_exc.SecurityGroupsCouldNotBeApplied): redis_client.Client()
def test_apply_rules(self, rule_key, redis): client = redis_client.Client() port_id = 1 mac_address = netaddr.EUI("AA:BB:CC:DD:EE:FF") client.apply_rules(port_id, mac_address.value, []) self.assertTrue(client._client.set.called)