Jian Li | 3defa84 | 2019-02-12 00:31:35 +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; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.IpAddress; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 25 | import org.onlab.packet.IpPrefix; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 26 | import org.onosproject.codec.CodecContext; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.codec.impl.CodecManager; |
| 29 | import org.onosproject.core.CoreService; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 30 | import org.onosproject.k8snode.api.DefaultHostNodesInfo; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 31 | import org.onosproject.k8snode.api.DefaultK8sApiConfig; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 32 | import org.onosproject.k8snode.api.HostNodesInfo; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 33 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.util.HashMap; |
| 38 | import java.util.Map; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 39 | import java.util.Set; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 40 | |
| 41 | import static junit.framework.TestCase.assertEquals; |
Jian Li | b121844 | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 42 | import static junit.framework.TestCase.assertTrue; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 43 | import static org.easymock.EasyMock.createMock; |
| 44 | import static org.easymock.EasyMock.expect; |
| 45 | import static org.easymock.EasyMock.replay; |
| 46 | import static org.hamcrest.MatcherAssert.assertThat; |
| 47 | import static org.hamcrest.Matchers.notNullValue; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 48 | import static org.onosproject.k8snode.api.K8sApiConfig.State.CONNECTED; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 49 | import static org.onosproject.k8snode.codec.K8sApiConfigJsonMatcher.matchesK8sApiConfig; |
| 50 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 51 | |
| 52 | /** |
| 53 | * Unit tests for kubernetes API config codec. |
| 54 | */ |
| 55 | public class K8sApiConfigCodecTest { |
| 56 | |
| 57 | MockCodecContext context; |
| 58 | |
| 59 | JsonCodec<K8sApiConfig> k8sApiConfigCodec; |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 60 | JsonCodec<HostNodesInfo> hostNodesInfoCodec; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 61 | |
| 62 | final CoreService mockCoreService = createMock(CoreService.class); |
| 63 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 64 | |
| 65 | /** |
| 66 | * Initial setup for this unit test. |
| 67 | */ |
| 68 | @Before |
| 69 | public void setUp() { |
| 70 | context = new MockCodecContext(); |
| 71 | k8sApiConfigCodec = new K8sApiConfigCodec(); |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 72 | hostNodesInfoCodec = new HostNodesInfoCodec(); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 73 | |
| 74 | assertThat(k8sApiConfigCodec, notNullValue()); |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 75 | assertThat(hostNodesInfoCodec, notNullValue()); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 76 | |
| 77 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 78 | .andReturn(APP_ID).anyTimes(); |
| 79 | replay(mockCoreService); |
| 80 | context.registerService(CoreService.class, mockCoreService); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Tests the kubernetes API config encoding. |
| 85 | */ |
| 86 | @Test |
| 87 | public void testK8sApiConfigEncode() { |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 88 | HostNodesInfo info = new DefaultHostNodesInfo.Builder() |
| 89 | .hostIp(IpAddress.valueOf("192.168.10.10")) |
| 90 | .nodes(ImmutableSet.of("master", "worker")) |
| 91 | .build(); |
| 92 | |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 93 | K8sApiConfig config = DefaultK8sApiConfig.builder() |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 94 | .clusterName("kubernetes") |
| 95 | .segmentId(1) |
| 96 | .extNetworkCidr(IpPrefix.valueOf("192.168.200.0/24")) |
| 97 | .mode(K8sApiConfig.Mode.NORMAL) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 98 | .scheme(K8sApiConfig.Scheme.HTTPS) |
| 99 | .ipAddress(IpAddress.valueOf("10.10.10.23")) |
| 100 | .port(6443) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 101 | .state(CONNECTED) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 102 | .token("token") |
| 103 | .caCertData("caCertData") |
| 104 | .clientCertData("clientCertData") |
| 105 | .clientKeyData("clientKeyData") |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 106 | .infos(ImmutableSet.of(info)) |
Jian Li | b121844 | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 107 | .dvr(true) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 108 | .build(); |
| 109 | |
| 110 | ObjectNode configJson = k8sApiConfigCodec.encode(config, context); |
| 111 | assertThat(configJson, matchesK8sApiConfig(config)); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Tests the kubernetes API config decoding. |
| 116 | * |
| 117 | * @throws IOException IO exception |
| 118 | */ |
| 119 | @Test |
| 120 | public void testK8sApiConfigDecode() throws IOException { |
| 121 | K8sApiConfig config = getK8sApiConfig("K8sApiConfig.json"); |
| 122 | |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 123 | assertEquals("kubernetes", config.clusterName()); |
| 124 | assertEquals(1, config.segmentId()); |
| 125 | assertEquals("192.168.200.0/24", config.extNetworkCidr().toString()); |
| 126 | assertEquals("NORMAL", config.mode().name()); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 127 | assertEquals("HTTPS", config.scheme().name()); |
| 128 | assertEquals("10.134.34.223", config.ipAddress().toString()); |
| 129 | assertEquals(6443, config.port()); |
| 130 | assertEquals("token", config.token()); |
| 131 | assertEquals("caCertData", config.caCertData()); |
| 132 | assertEquals("clientCertData", config.clientCertData()); |
| 133 | assertEquals("clientKeyData", config.clientKeyData()); |
Jian Li | b121844 | 2020-09-03 13:12:14 +0900 | [diff] [blame] | 134 | assertTrue(config.dvr()); |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 135 | |
| 136 | Set<HostNodesInfo> infos = config.infos(); |
| 137 | assertEquals(1, infos.size()); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | private K8sApiConfig getK8sApiConfig(String resourceName) throws IOException { |
| 141 | InputStream jsonStream = K8sNodeCodecTest.class.getResourceAsStream(resourceName); |
| 142 | JsonNode json = context.mapper().readTree(jsonStream); |
| 143 | assertThat(json, notNullValue()); |
| 144 | K8sApiConfig config = k8sApiConfigCodec.decode((ObjectNode) json, context); |
| 145 | assertThat(config, notNullValue()); |
| 146 | return config; |
| 147 | } |
| 148 | |
| 149 | private class MockCodecContext implements CodecContext { |
| 150 | private final ObjectMapper mapper = new ObjectMapper(); |
| 151 | private final CodecManager manager = new CodecManager(); |
| 152 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 153 | |
| 154 | /** |
| 155 | * Constructs a new mock codec context. |
| 156 | */ |
| 157 | public MockCodecContext() { |
| 158 | manager.activate(); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public ObjectMapper mapper() { |
| 163 | return mapper; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | @SuppressWarnings("unchecked") |
| 168 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 169 | if (entityClass == K8sApiConfig.class) { |
| 170 | return (JsonCodec<T>) k8sApiConfigCodec; |
| 171 | } |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 172 | if (entityClass == HostNodesInfo.class) { |
| 173 | return (JsonCodec<T>) hostNodesInfoCodec; |
| 174 | } |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 175 | return manager.getCodec(entityClass); |
| 176 | } |
| 177 | |
| 178 | @SuppressWarnings("unchecked") |
| 179 | @Override |
| 180 | public <T> T getService(Class<T> serviceClass) { |
| 181 | return (T) services.get(serviceClass); |
| 182 | } |
| 183 | |
| 184 | // for registering mock services |
| 185 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 186 | services.put(serviceClass, impl); |
| 187 | } |
| 188 | } |
| 189 | } |