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