blob: e70afce85504761fa2ff90715c7a458a6593678c [file] [log] [blame]
Jian Lib9642832021-01-08 03:18:35 +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.ImmutableSet;
19import com.google.common.testing.EqualsTester;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.PortNumber;
26
27import static junit.framework.TestCase.assertEquals;
28import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
29
30/**
31 * Unit tests for the default kubevirt instance class.
32 */
33public class DefaultKubevirtInstanceTest {
34
35 private static final String UID_1 = "1";
36 private static final String UID_2 = "2";
37 private static final String NAME_1 = "instance-1";
38 private static final String NAME_2 = "instance-2";
Jian Li3ba5c582021-01-14 11:30:36 +090039 private static final String NETWORK_ID_1 = "net-1";
40 private static final String NETWORK_ID_2 = "net-2";
Jian Li9557e902021-06-08 10:12:52 +090041 private static final String VM_NAME_1 = "test-vm-1";
42 private static final String VM_NAME_2 = "test-vm-2";
Jian Lib9642832021-01-08 03:18:35 +090043 private static final MacAddress MAC_1 = MacAddress.valueOf("11:22:33:44:55:66");
44 private static final MacAddress MAC_2 = MacAddress.valueOf("66:55:44:33:22:11");
45 private static final IpAddress IP_1 = IpAddress.valueOf("10.10.10.10");
46 private static final IpAddress IP_2 = IpAddress.valueOf("20.20.20.20");
47 private static final DeviceId DID_1 = DeviceId.deviceId("did1");
48 private static final DeviceId DID_2 = DeviceId.deviceId("did2");
49 private static final PortNumber PN_1 = PortNumber.portNumber(1);
50 private static final PortNumber PN_2 = PortNumber.portNumber(2);
Jian Li9557e902021-06-08 10:12:52 +090051 private static final KubevirtPort PORT_1 = createPort(VM_NAME_1, NETWORK_ID_1, MAC_1, IP_1, DID_1, PN_1);
52 private static final KubevirtPort PORT_2 = createPort(VM_NAME_2, NETWORK_ID_2, MAC_2, IP_2, DID_2, PN_2);
Jian Lib9642832021-01-08 03:18:35 +090053
54 private KubevirtInstance instance1;
55 private KubevirtInstance sameAsInstance1;
56 private KubevirtInstance instance2;
57
58 /**
59 * Tests class immutability.
60 */
61 @Test
62 public void testImmutability() {
63 assertThatClassIsImmutable(DefaultKubevirtInstance.class);
64 }
65
66 /**
67 * Initial setup for this unit test.
68 */
69 @Before
70 public void setUp() {
71 instance1 = DefaultKubevirtInstance.builder()
72 .uid(UID_1)
73 .name(NAME_1)
74 .ports(ImmutableSet.of(PORT_1))
75 .build();
76
77 sameAsInstance1 = DefaultKubevirtInstance.builder()
78 .uid(UID_1)
79 .name(NAME_1)
80 .ports(ImmutableSet.of(PORT_1))
81 .build();
82
83 instance2 = DefaultKubevirtInstance.builder()
84 .uid(UID_2)
85 .name(NAME_2)
86 .ports(ImmutableSet.of(PORT_2))
87 .build();
88 }
89
90 /**
91 * Tests object equality.
92 */
93 @Test
94 public void testEquality() {
95 new EqualsTester().addEqualityGroup(instance1, sameAsInstance1)
96 .addEqualityGroup(instance2)
97 .testEquals();
98 }
99
100 /**
101 * Test object construction.
102 */
103 @Test
104 public void testConstruction() {
105 KubevirtInstance instance = instance1;
106
107 assertEquals(UID_1, instance1.uid());
108 assertEquals(NAME_1, instance1.name());
109 assertEquals(ImmutableSet.of(PORT_1), instance1.ports());
110 }
111
Jian Li9557e902021-06-08 10:12:52 +0900112 static KubevirtPort createPort(String vmName, String networkId, MacAddress mac,
113 IpAddress ip, DeviceId did, PortNumber pn) {
Jian Lib9642832021-01-08 03:18:35 +0900114 return DefaultKubevirtPort.builder()
Jian Li9557e902021-06-08 10:12:52 +0900115 .vmName(vmName)
Jian Li3ba5c582021-01-14 11:30:36 +0900116 .networkId(networkId)
Jian Lib9642832021-01-08 03:18:35 +0900117 .macAddress(mac)
118 .ipAddress(ip)
119 .deviceId(did)
120 .portNumber(pn)
121 .build();
122 }
123}