Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.k8snode.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpAddress; |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame^] | 24 | import org.onlab.packet.MacAddress; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
| 27 | import org.onosproject.codec.impl.CodecManager; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.k8snode.api.DefaultK8sNode; |
| 30 | import org.onosproject.k8snode.api.K8sNode; |
| 31 | import org.onosproject.k8snode.api.K8sNodeState; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | import java.util.HashMap; |
| 37 | import java.util.Map; |
| 38 | |
| 39 | import static junit.framework.TestCase.assertEquals; |
| 40 | import static org.easymock.EasyMock.createMock; |
| 41 | import static org.easymock.EasyMock.expect; |
| 42 | import static org.easymock.EasyMock.replay; |
| 43 | import static org.hamcrest.MatcherAssert.assertThat; |
| 44 | import static org.hamcrest.Matchers.notNullValue; |
| 45 | import static org.onosproject.k8snode.codec.K8sNodeJsonMatcher.matchesK8sNode; |
| 46 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 47 | |
| 48 | /** |
| 49 | * Unit tests for Kubernetes Node codec. |
| 50 | */ |
| 51 | public class K8sNodeCodecTest { |
| 52 | |
| 53 | MockCodecContext context; |
| 54 | |
| 55 | JsonCodec<K8sNode> k8sNodeCodec; |
| 56 | |
| 57 | final CoreService mockCoreService = createMock(CoreService.class); |
| 58 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 59 | |
| 60 | /** |
| 61 | * Initial setup for this unit test. |
| 62 | */ |
| 63 | @Before |
| 64 | public void setUp() { |
| 65 | context = new MockCodecContext(); |
| 66 | k8sNodeCodec = new K8sNodeCodec(); |
| 67 | |
| 68 | assertThat(k8sNodeCodec, notNullValue()); |
| 69 | |
| 70 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 71 | .andReturn(APP_ID).anyTimes(); |
| 72 | replay(mockCoreService); |
| 73 | context.registerService(CoreService.class, mockCoreService); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Tests the kubernetes minion node encoding. |
| 78 | */ |
| 79 | @Test |
| 80 | public void testK8sMinionNodeEncode() { |
| 81 | K8sNode node = DefaultK8sNode.builder() |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 82 | .clusterName("kubernetes") |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 83 | .hostname("minion") |
| 84 | .type(K8sNode.Type.MINION) |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 85 | .segmentId(100) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 86 | .state(K8sNodeState.INIT) |
| 87 | .managementIp(IpAddress.valueOf("10.10.10.1")) |
| 88 | .dataIp(IpAddress.valueOf("20.20.20.2")) |
| 89 | .intgBridge(DeviceId.deviceId("kbr-int")) |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 90 | .extIntf("eth1") |
| 91 | .extBridgeIp(IpAddress.valueOf("10.10.10.5")) |
| 92 | .extGatewayIp(IpAddress.valueOf("10.10.10.1")) |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame^] | 93 | .extGatewayMac(MacAddress.valueOf("FF:FF:FF:FF:FF:FF")) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 94 | .build(); |
| 95 | |
| 96 | ObjectNode nodeJson = k8sNodeCodec.encode(node, context); |
| 97 | assertThat(nodeJson, matchesK8sNode(node)); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests the kubernetes minion node decoding. |
| 102 | * |
| 103 | * @throws IOException IO exception |
| 104 | */ |
| 105 | @Test |
| 106 | public void testK8sMinionNodeDecode() throws IOException { |
| 107 | K8sNode node = getK8sNode("K8sMinionNode.json"); |
| 108 | |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 109 | assertEquals("kubernetes", node.clusterName()); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 110 | assertEquals("minion", node.hostname()); |
| 111 | assertEquals("MINION", node.type().name()); |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 112 | assertEquals(100, node.segmentId()); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 113 | assertEquals("172.16.130.4", node.managementIp().toString()); |
| 114 | assertEquals("172.16.130.4", node.dataIp().toString()); |
| 115 | assertEquals("of:00000000000000a1", node.intgBridge().toString()); |
Jian Li | 0c63272 | 2019-05-08 15:58:04 +0900 | [diff] [blame] | 116 | assertEquals("eth1", node.extIntf()); |
| 117 | assertEquals("172.16.130.5", node.extBridgeIp().toString()); |
| 118 | assertEquals("172.16.130.1", node.extGatewayIp().toString()); |
Jian Li | c2242bd | 2020-09-03 13:12:14 +0900 | [diff] [blame^] | 119 | assertEquals("FF:FF:FF:FF:FF:FF", node.extGatewayMac().toString()); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | private K8sNode getK8sNode(String resourceName) throws IOException { |
| 123 | InputStream jsonStream = K8sNodeCodecTest.class.getResourceAsStream(resourceName); |
| 124 | JsonNode json = context.mapper().readTree(jsonStream); |
| 125 | assertThat(json, notNullValue()); |
| 126 | K8sNode node = k8sNodeCodec.decode((ObjectNode) json, context); |
| 127 | assertThat(node, notNullValue()); |
| 128 | return node; |
| 129 | } |
| 130 | |
| 131 | private class MockCodecContext implements CodecContext { |
| 132 | private final ObjectMapper mapper = new ObjectMapper(); |
| 133 | private final CodecManager manager = new CodecManager(); |
| 134 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 135 | |
| 136 | /** |
| 137 | * Constructs a new mock codec context. |
| 138 | */ |
| 139 | public MockCodecContext() { |
| 140 | manager.activate(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public ObjectMapper mapper() { |
| 145 | return mapper; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | @SuppressWarnings("unchecked") |
| 150 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 151 | if (entityClass == K8sNode.class) { |
| 152 | return (JsonCodec<T>) k8sNodeCodec; |
| 153 | } |
| 154 | return manager.getCodec(entityClass); |
| 155 | } |
| 156 | |
| 157 | @SuppressWarnings("unchecked") |
| 158 | @Override |
| 159 | public <T> T getService(Class<T> serviceClass) { |
| 160 | return (T) services.get(serviceClass); |
| 161 | } |
| 162 | |
| 163 | // for registering mock services |
| 164 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 165 | services.put(serviceClass, impl); |
| 166 | } |
| 167 | } |
| 168 | } |