blob: c6861bef40f1c2084ff0fb29899c00614373aa63 [file] [log] [blame]
Jian Li49109b52019-01-22 00:17:28 +09001/*
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 */
16package org.onosproject.k8snode.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24import org.onosproject.codec.CodecContext;
25import org.onosproject.codec.JsonCodec;
26import org.onosproject.codec.impl.CodecManager;
27import org.onosproject.core.CoreService;
28import org.onosproject.k8snode.api.DefaultK8sNode;
29import org.onosproject.k8snode.api.K8sNode;
30import org.onosproject.k8snode.api.K8sNodeState;
31import org.onosproject.net.DeviceId;
32
33import java.io.IOException;
34import java.io.InputStream;
35import java.util.HashMap;
36import java.util.Map;
37
38import static junit.framework.TestCase.assertEquals;
39import static org.easymock.EasyMock.createMock;
40import static org.easymock.EasyMock.expect;
41import static org.easymock.EasyMock.replay;
42import static org.hamcrest.MatcherAssert.assertThat;
43import static org.hamcrest.Matchers.notNullValue;
44import static org.onosproject.k8snode.codec.K8sNodeJsonMatcher.matchesK8sNode;
45import static org.onosproject.net.NetTestTools.APP_ID;
46
47/**
48 * Unit tests for Kubernetes Node codec.
49 */
50public 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()
Jian Li58b33982020-07-01 19:07:02 +090081 .clusterName("kubernetes")
Jian Li49109b52019-01-22 00:17:28 +090082 .hostname("minion")
83 .type(K8sNode.Type.MINION)
Jian Li58b33982020-07-01 19:07:02 +090084 .segmentId(100)
Jian Li49109b52019-01-22 00:17:28 +090085 .state(K8sNodeState.INIT)
86 .managementIp(IpAddress.valueOf("10.10.10.1"))
87 .dataIp(IpAddress.valueOf("20.20.20.2"))
88 .intgBridge(DeviceId.deviceId("kbr-int"))
Jian Li7709eb42019-05-08 15:58:04 +090089 .extIntf("eth1")
90 .extBridgeIp(IpAddress.valueOf("10.10.10.5"))
91 .extGatewayIp(IpAddress.valueOf("10.10.10.1"))
Jian Li49109b52019-01-22 00:17:28 +090092 .build();
93
94 ObjectNode nodeJson = k8sNodeCodec.encode(node, context);
95 assertThat(nodeJson, matchesK8sNode(node));
96 }
97
98 /**
99 * Tests the kubernetes minion node decoding.
100 *
101 * @throws IOException IO exception
102 */
103 @Test
104 public void testK8sMinionNodeDecode() throws IOException {
105 K8sNode node = getK8sNode("K8sMinionNode.json");
106
Jian Li58b33982020-07-01 19:07:02 +0900107 assertEquals("kubernetes", node.clusterName());
Jian Li49109b52019-01-22 00:17:28 +0900108 assertEquals("minion", node.hostname());
109 assertEquals("MINION", node.type().name());
Jian Li58b33982020-07-01 19:07:02 +0900110 assertEquals(100, node.segmentId());
Jian Li49109b52019-01-22 00:17:28 +0900111 assertEquals("172.16.130.4", node.managementIp().toString());
112 assertEquals("172.16.130.4", node.dataIp().toString());
113 assertEquals("of:00000000000000a1", node.intgBridge().toString());
Jian Li7709eb42019-05-08 15:58:04 +0900114 assertEquals("eth1", node.extIntf());
115 assertEquals("172.16.130.5", node.extBridgeIp().toString());
116 assertEquals("172.16.130.1", node.extGatewayIp().toString());
Jian Li49109b52019-01-22 00:17:28 +0900117 }
118
119 private K8sNode getK8sNode(String resourceName) throws IOException {
120 InputStream jsonStream = K8sNodeCodecTest.class.getResourceAsStream(resourceName);
121 JsonNode json = context.mapper().readTree(jsonStream);
122 assertThat(json, notNullValue());
123 K8sNode node = k8sNodeCodec.decode((ObjectNode) json, context);
124 assertThat(node, notNullValue());
125 return node;
126 }
127
128 private class MockCodecContext implements CodecContext {
129 private final ObjectMapper mapper = new ObjectMapper();
130 private final CodecManager manager = new CodecManager();
131 private final Map<Class<?>, Object> services = new HashMap<>();
132
133 /**
134 * Constructs a new mock codec context.
135 */
136 public MockCodecContext() {
137 manager.activate();
138 }
139
140 @Override
141 public ObjectMapper mapper() {
142 return mapper;
143 }
144
145 @Override
146 @SuppressWarnings("unchecked")
147 public <T> JsonCodec<T> codec(Class<T> entityClass) {
148 if (entityClass == K8sNode.class) {
149 return (JsonCodec<T>) k8sNodeCodec;
150 }
151 return manager.getCodec(entityClass);
152 }
153
154 @SuppressWarnings("unchecked")
155 @Override
156 public <T> T getService(Class<T> serviceClass) {
157 return (T) services.get(serviceClass);
158 }
159
160 // for registering mock services
161 public <T> void registerService(Class<T> serviceClass, T impl) {
162 services.put(serviceClass, impl);
163 }
164 }
165}