Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 1 | /* |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame] | 2 | * Copyright 2022-present Open Networking Foundation |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 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.Sets; |
| 19 | import com.google.common.testing.EqualsTester; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 24 | |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import static junit.framework.TestCase.assertEquals; |
| 28 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 29 | |
| 30 | /** |
| 31 | * Unit tests for the default Kubernetes external lb class. |
| 32 | */ |
| 33 | public class DefaultKubernetesExternalLbTest { |
| 34 | private static final String SERVICE_NAME_1 = "service_name_1"; |
| 35 | private static final String SERVICE_NAME_2 = "service_name_2"; |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame] | 36 | private static final IpAddress LOADBALANCER_IP_1 = IpAddress.valueOf("1.1.1.2"); |
| 37 | private static final IpAddress LOADBALANCER_IP_2 = IpAddress.valueOf("2.2.2.2"); |
Daniel Park | 6a83ee6 | 2022-10-26 11:57:34 +0900 | [diff] [blame^] | 38 | private static final KubernetesServicePort SERVICE_PORT_1 = DefaultKubernetesServicePort.builder() |
| 39 | .port(Integer.valueOf(8080)) |
| 40 | .nodePort(Integer.valueOf(31080)) |
| 41 | .build(); |
| 42 | private static final KubernetesServicePort SERVICE_PORT_2 = DefaultKubernetesServicePort.builder() |
| 43 | .port(Integer.valueOf(8081)) |
| 44 | .nodePort(Integer.valueOf(31081)) |
| 45 | .build(); |
| 46 | private static final Set<KubernetesServicePort> SERVICE_PORT_SET_1 = Sets.newHashSet(SERVICE_PORT_1); |
| 47 | private static final Set<KubernetesServicePort> SERVICE_PORT_SET_2 = Sets.newHashSet(SERVICE_PORT_2); |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 48 | private static final Set<String> ENDPOINT_SET_1 = Sets.newHashSet(String.valueOf("1.1.2.1")); |
| 49 | private static final Set<String> ENDPOINT_SET_2 = Sets.newHashSet(String.valueOf("1.1.2.2")); |
| 50 | private static final String ELECTED_GATEWAY_1 = "gateway1"; |
| 51 | private static final String ELECTED_GATEWAY_2 = "gateway2"; |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame] | 52 | private static final String ELECTED_WORKER_1 = "worker1"; |
| 53 | private static final String ELECTED_WORKER_2 = "worker2"; |
| 54 | |
| 55 | private static final IpAddress LOADBALANCER_GW_IP_1 = IpAddress.valueOf("1.1.1.1"); |
| 56 | private static final IpAddress LOADBALANCER_GW_IP_2 = IpAddress.valueOf("2.2.2.1"); |
| 57 | private static final MacAddress LOADBALANCER_GW_MAC_1 = MacAddress.valueOf("aa:bb:cc:dd:ee:ff"); |
| 58 | private static final MacAddress LOADBALANCER_GW_MAC_2 = MacAddress.valueOf("ff:ee:dd:cc:bb:aa"); |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 59 | |
| 60 | private KubernetesExternalLb lb1; |
| 61 | private KubernetesExternalLb sameAsLb1; |
| 62 | private KubernetesExternalLb lb2; |
| 63 | |
| 64 | /** |
| 65 | * Tests class immutability. |
| 66 | */ |
| 67 | @Test |
| 68 | public void testImmutability() { |
| 69 | assertThatClassIsImmutable(DefaultKubernetesExternalLb.class); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Initial setup for this unit test. |
| 74 | */ |
| 75 | @Before |
| 76 | public void setUp() { |
| 77 | lb1 = DefaultKubernetesExternalLb.builder() |
| 78 | .serviceName(SERVICE_NAME_1) |
| 79 | .loadBalancerIp(LOADBALANCER_IP_1) |
Daniel Park | 6a83ee6 | 2022-10-26 11:57:34 +0900 | [diff] [blame^] | 80 | .servicePorts(SERVICE_PORT_SET_1) |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 81 | .endpointSet(ENDPOINT_SET_1) |
| 82 | .electedGateway(ELECTED_GATEWAY_1) |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame] | 83 | .electedWorker(ELECTED_WORKER_1) |
| 84 | .loadBalancerGwIp(LOADBALANCER_GW_IP_1) |
| 85 | .loadBalancerGwMac(LOADBALANCER_GW_MAC_1) |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 86 | .build(); |
| 87 | |
| 88 | sameAsLb1 = DefaultKubernetesExternalLb.builder() |
| 89 | .serviceName(SERVICE_NAME_1) |
| 90 | .loadBalancerIp(LOADBALANCER_IP_1) |
Daniel Park | 6a83ee6 | 2022-10-26 11:57:34 +0900 | [diff] [blame^] | 91 | .servicePorts(SERVICE_PORT_SET_1) |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 92 | .endpointSet(ENDPOINT_SET_1) |
| 93 | .electedGateway(ELECTED_GATEWAY_1) |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame] | 94 | .electedWorker(ELECTED_WORKER_1) |
| 95 | .loadBalancerGwIp(LOADBALANCER_GW_IP_1) |
| 96 | .loadBalancerGwMac(LOADBALANCER_GW_MAC_1) |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 97 | .build(); |
| 98 | |
| 99 | lb2 = DefaultKubernetesExternalLb.builder() |
| 100 | .serviceName(SERVICE_NAME_2) |
| 101 | .loadBalancerIp(LOADBALANCER_IP_2) |
Daniel Park | 6a83ee6 | 2022-10-26 11:57:34 +0900 | [diff] [blame^] | 102 | .servicePorts(SERVICE_PORT_SET_2) |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 103 | .endpointSet(ENDPOINT_SET_2) |
| 104 | .electedGateway(ELECTED_GATEWAY_2) |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame] | 105 | .electedWorker(ELECTED_WORKER_2) |
| 106 | .loadBalancerGwIp(LOADBALANCER_GW_IP_2) |
| 107 | .loadBalancerGwMac(LOADBALANCER_GW_MAC_2) |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 108 | .build(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Tests object equality. |
| 113 | */ |
| 114 | @Test |
| 115 | public void testEquality() { |
| 116 | new EqualsTester().addEqualityGroup(lb1, sameAsLb1) |
| 117 | .addEqualityGroup(lb2) |
| 118 | .testEquals(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Test object construction. |
| 123 | */ |
| 124 | @Test |
| 125 | public void testConstruction() { |
| 126 | KubernetesExternalLb lb = lb1; |
| 127 | |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame] | 128 | assertEquals(SERVICE_NAME_1, lb.serviceName()); |
| 129 | assertEquals(LOADBALANCER_IP_1, lb.loadBalancerIp()); |
Daniel Park | 6a83ee6 | 2022-10-26 11:57:34 +0900 | [diff] [blame^] | 130 | assertEquals(SERVICE_PORT_SET_1, lb.servicePorts()); |
Daniel Park | 5ff76b7 | 2022-09-26 22:58:53 +0900 | [diff] [blame] | 131 | assertEquals(ENDPOINT_SET_1, lb.endpointSet()); |
Daniel Park | 734b553 | 2022-09-26 15:13:59 +0900 | [diff] [blame] | 132 | } |
| 133 | } |