blob: 1ad8d8715890c3f4eb695f83277fba0690bf6fe4 [file] [log] [blame]
Jian Licaa03922021-02-26 18:15:20 +09001/*
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 */
16package org.onosproject.kubevirtnetworking.api;
17
18import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.testing.EqualsTester;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.MacAddress;
25
26import static junit.framework.TestCase.assertEquals;
27import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
28import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.FLAT;
29import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.VXLAN;
30
31/**
32 * Unit tests for the default kubevirt router class.
33 */
34public class DefaultKubevirtRouterTest {
35
36 private static final String NAME_1 = "router-1";
37 private static final String NAME_2 = "router-2";
38 private static final String DESCRIPTION_1 = "dummy router 1";
39 private static final String DESCRIPTION_2 = "dummy router 2";
40 private static final boolean ENABLE_SNAT_1 = false;
41 private static final boolean ENABLE_SNAT_2 = true;
42 private static final KubevirtNetwork.Type TYPE_1 = FLAT;
43 private static final KubevirtNetwork.Type TYPE_2 = VXLAN;
44 private static final String NETWORK_NAME_1 = "net-1";
45 private static final String NETWORK_NAME_2 = "net-2";
Daniel Park2884b232021-03-04 18:58:47 +090046 private static final String GATEWAY_HOST_1 = "gateway-1";
47 private static final String GATEWAY_HOST_2 = "gateway-2";
Jian Licaa03922021-02-26 18:15:20 +090048
49 private static final KubevirtPeerRouter PEER_ROUTER_1 =
50 new KubevirtPeerRouter(IpAddress.valueOf("192.168.10.10"),
51 MacAddress.valueOf("11:22:33:44:55:66"));
52 private static final KubevirtPeerRouter PEER_ROUTER_2 =
53 new KubevirtPeerRouter(IpAddress.valueOf("192.168.20.20"),
54 MacAddress.valueOf("22:33:44:55:66:77"));
55
56 private KubevirtRouter router1;
57 private KubevirtRouter sameAsRouter1;
58 private KubevirtRouter router2;
59
60 /**
61 * Tests class immutability.
62 */
63 @Test
64 public void testImmutability() {
65 assertThatClassIsImmutable(DefaultKubevirtRouter.class);
66 }
67
68 /**
69 * Initial setup for this unit test.
70 */
71 @Before
72 public void setUp() {
73 router1 = DefaultKubevirtRouter.builder()
74 .name(NAME_1)
75 .description(DESCRIPTION_1)
76 .enableSnat(ENABLE_SNAT_1)
77 .internal(ImmutableSet.of(NETWORK_NAME_1))
Jian Li810f58c2021-02-27 01:10:50 +090078 .external(ImmutableMap.of("10.10.10.10", NETWORK_NAME_1))
Jian Licaa03922021-02-26 18:15:20 +090079 .peerRouter(PEER_ROUTER_1)
Daniel Park2884b232021-03-04 18:58:47 +090080 .electedGateway(GATEWAY_HOST_1)
Jian Licaa03922021-02-26 18:15:20 +090081 .build();
82 sameAsRouter1 = DefaultKubevirtRouter.builder()
83 .name(NAME_1)
84 .description(DESCRIPTION_1)
85 .enableSnat(ENABLE_SNAT_1)
86 .internal(ImmutableSet.of(NETWORK_NAME_1))
Jian Li810f58c2021-02-27 01:10:50 +090087 .external(ImmutableMap.of("10.10.10.10", NETWORK_NAME_1))
Jian Licaa03922021-02-26 18:15:20 +090088 .peerRouter(PEER_ROUTER_1)
Daniel Park2884b232021-03-04 18:58:47 +090089 .electedGateway(GATEWAY_HOST_1)
Jian Licaa03922021-02-26 18:15:20 +090090 .build();
91 router2 = DefaultKubevirtRouter.builder()
92 .name(NAME_2)
93 .description(DESCRIPTION_2)
94 .enableSnat(ENABLE_SNAT_2)
95 .internal(ImmutableSet.of(NETWORK_NAME_2))
Jian Li810f58c2021-02-27 01:10:50 +090096 .external(ImmutableMap.of("20.20.20.20", NETWORK_NAME_2))
Jian Licaa03922021-02-26 18:15:20 +090097 .peerRouter(PEER_ROUTER_2)
Daniel Park2884b232021-03-04 18:58:47 +090098 .electedGateway(GATEWAY_HOST_2)
Jian Licaa03922021-02-26 18:15:20 +090099 .build();
100 }
101
102 /**
103 * Tests object equality.
104 */
105 @Test
106 public void testEquality() {
107 new EqualsTester().addEqualityGroup(router1, sameAsRouter1)
108 .addEqualityGroup(router2)
109 .testEquals();
110 }
111
112 /**
113 * Test object construction.
114 */
115 @Test
116 public void testConstruction() {
117 KubevirtRouter router = router1;
118
119 assertEquals(NAME_1, router.name());
120 assertEquals(DESCRIPTION_1, router.description());
121 assertEquals(ENABLE_SNAT_1, router.enableSnat());
122 assertEquals(ImmutableSet.of(NETWORK_NAME_1), router.internal());
Jian Li810f58c2021-02-27 01:10:50 +0900123 assertEquals(ImmutableMap.of("10.10.10.10", NETWORK_NAME_1), router.external());
Jian Licaa03922021-02-26 18:15:20 +0900124 assertEquals(PEER_ROUTER_1, router.peerRouter());
125 }
126}