Jian Li | 47e7af7 | 2021-03-05 01:32:04 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.kubevirtnetworking.api; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import static junit.framework.TestCase.assertEquals; |
| 27 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 28 | |
| 29 | /** |
| 30 | * Unit tests for the default kubevirt security group class. |
| 31 | */ |
| 32 | public class DefaultKubevirtSecurityGroupTest { |
| 33 | private static final String ID_1 = "id-1"; |
| 34 | private static final String ID_2 = "id-1"; |
| 35 | private static final String NAME_1 = "sg-1"; |
| 36 | private static final String NAME_2 = "sg-2"; |
| 37 | private static final String DESCRIPTION_1 = "sg-1"; |
| 38 | private static final String DESCRIPTION_2 = "sg-2"; |
| 39 | |
| 40 | private static final String RULE_ID_1 = "rule-1"; |
| 41 | private static final String RULE_ID_2 = "rule-2"; |
| 42 | private static final String DIRECTION_1 = "ingress"; |
| 43 | private static final String DIRECTION_2 = "egress"; |
| 44 | private static final String ETHER_TYPE_1 = "IPv4"; |
| 45 | private static final String ETHER_TYPE_2 = "IPv4"; |
| 46 | private static final Integer PORT_RANGE_MAX_1 = 80; |
| 47 | private static final Integer PORT_RANGE_MAX_2 = 8080; |
| 48 | private static final Integer PORT_RANGE_MIN_1 = 80; |
| 49 | private static final Integer PORT_RANGE_MIN_2 = 8080; |
| 50 | private static final String PROTOCOL_1 = "tcp"; |
| 51 | private static final String PROTOCOL_2 = "udp"; |
| 52 | private static final IpPrefix REMOTE_IP_PREFIX_1 = IpPrefix.valueOf("10.10.10.0/24"); |
| 53 | private static final IpPrefix REMOTE_IP_PREFIX_2 = IpPrefix.valueOf("20.20.20.0/24"); |
| 54 | private static final String REMOTE_GROUP_ID_1 = "rid-1"; |
| 55 | private static final String REMOTE_GROUP_ID_2 = "rid-2"; |
| 56 | |
| 57 | private static final Set<KubevirtSecurityGroupRule> RULES_1 = ImmutableSet.of( |
| 58 | createRule( |
| 59 | RULE_ID_1, |
| 60 | ID_1, |
| 61 | DIRECTION_1, |
| 62 | ETHER_TYPE_1, |
| 63 | PORT_RANGE_MAX_1, |
| 64 | PORT_RANGE_MIN_1, |
| 65 | PROTOCOL_1, |
| 66 | REMOTE_IP_PREFIX_1, |
| 67 | REMOTE_GROUP_ID_1 |
| 68 | ) |
| 69 | ); |
| 70 | private static final Set<KubevirtSecurityGroupRule> RULES_2 = ImmutableSet.of( |
| 71 | createRule( |
| 72 | RULE_ID_2, |
| 73 | ID_2, |
| 74 | DIRECTION_2, |
| 75 | ETHER_TYPE_2, |
| 76 | PORT_RANGE_MAX_2, |
| 77 | PORT_RANGE_MIN_2, |
| 78 | PROTOCOL_2, |
| 79 | REMOTE_IP_PREFIX_2, |
| 80 | REMOTE_GROUP_ID_2 |
| 81 | ) |
| 82 | ); |
| 83 | |
| 84 | private KubevirtSecurityGroup sg1; |
| 85 | private KubevirtSecurityGroup sameAsSg1; |
| 86 | private KubevirtSecurityGroup sg2; |
| 87 | |
| 88 | /** |
| 89 | * Tests class immutability. |
| 90 | */ |
| 91 | @Test |
| 92 | public void testImmutability() { |
| 93 | assertThatClassIsImmutable(DefaultKubevirtSecurityGroup.class); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Initial setup for this unit test. |
| 98 | */ |
| 99 | @Before |
| 100 | public void setUp() { |
| 101 | sg1 = DefaultKubevirtSecurityGroup.builder() |
| 102 | .id(ID_1) |
| 103 | .name(NAME_1) |
| 104 | .description(DESCRIPTION_1) |
| 105 | .rules(RULES_1) |
| 106 | .build(); |
| 107 | |
| 108 | sameAsSg1 = DefaultKubevirtSecurityGroup.builder() |
| 109 | .id(ID_1) |
| 110 | .name(NAME_1) |
| 111 | .description(DESCRIPTION_1) |
| 112 | .rules(RULES_1) |
| 113 | .build(); |
| 114 | |
| 115 | sg2 = DefaultKubevirtSecurityGroup.builder() |
| 116 | .id(ID_2) |
| 117 | .name(NAME_2) |
| 118 | .description(DESCRIPTION_2) |
| 119 | .rules(RULES_2) |
| 120 | .build(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Tests object equality. |
| 125 | */ |
| 126 | @Test |
| 127 | public void testEquality() { |
| 128 | new EqualsTester().addEqualityGroup(sg1, sameAsSg1) |
| 129 | .addEqualityGroup(sg2) |
| 130 | .testEquals(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Test object construction. |
| 135 | */ |
| 136 | @Test |
| 137 | public void testConstruction() { |
| 138 | KubevirtSecurityGroup sg = sg1; |
| 139 | |
| 140 | assertEquals(ID_1, sg.id()); |
| 141 | assertEquals(NAME_1, sg.name()); |
| 142 | assertEquals(DESCRIPTION_1, sg.description()); |
| 143 | assertEquals(RULES_1, sg.rules()); |
| 144 | } |
| 145 | |
| 146 | private static KubevirtSecurityGroupRule createRule(String id, String sgId, |
| 147 | String direction, String etherType, |
| 148 | Integer portRangeMax, |
| 149 | Integer portRangeMin, |
| 150 | String protocol, |
| 151 | IpPrefix remoteIpPrefix, |
| 152 | String remoteGroupId) { |
| 153 | return DefaultKubevirtSecurityGroupRule.builder() |
| 154 | .id(id) |
| 155 | .securityGroupId(sgId) |
| 156 | .direction(direction) |
| 157 | .etherType(etherType) |
| 158 | .portRangeMax(portRangeMax) |
| 159 | .portRangeMin(portRangeMin) |
| 160 | .protocol(protocol) |
| 161 | .remoteIpPrefix(remoteIpPrefix) |
| 162 | .remoteGroupId(remoteGroupId) |
| 163 | .build(); |
| 164 | } |
| 165 | } |