PageRenderTime 91ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/utils/LockTestServer.java

https://gitlab.com/matticala/apache-camel
Java | 239 lines | 173 code | 46 blank | 20 comment | 17 complexity | 181c43835aef0918be507e9896072efa MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.kubernetes.cluster.utils;
  18. import java.io.IOException;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.Set;
  22. import java.util.TreeSet;
  23. import java.util.stream.Collectors;
  24. import com.fasterxml.jackson.databind.ObjectMapper;
  25. import io.fabric8.kubernetes.api.model.ConfigMap;
  26. import io.fabric8.kubernetes.api.model.PodBuilder;
  27. import io.fabric8.kubernetes.api.model.PodListBuilder;
  28. import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
  29. import io.fabric8.mockwebserver.utils.ResponseProvider;
  30. import okhttp3.Headers;
  31. import okhttp3.mockwebserver.RecordedRequest;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. /**
  35. * A Test server to interact with Kubernetes for locking on a ConfigMap.
  36. */
  37. public class LockTestServer extends KubernetesMockServer {
  38. private static final Logger LOG = LoggerFactory.getLogger(LockTestServer.class);
  39. private boolean refuseRequests;
  40. private Long delayRequests;
  41. private Set<String> pods;
  42. public LockTestServer(ConfigMapLockSimulator lockSimulator) {
  43. this(lockSimulator, Collections.emptySet());
  44. }
  45. public LockTestServer(ConfigMapLockSimulator lockSimulator, Collection<String> initialPods) {
  46. this.pods = new TreeSet<>(initialPods);
  47. expect().get().withPath("/api/v1/namespaces/test/configmaps/" + lockSimulator.getConfigMapName()).andReply(new ResponseProvider<Object>() {
  48. ThreadLocal<Integer> responseCode = new ThreadLocal<>();
  49. private Headers headers = new Headers.Builder().build();
  50. @Override
  51. public int getStatusCode() {
  52. return responseCode.get();
  53. }
  54. @Override
  55. public Object getBody(RecordedRequest recordedRequest) {
  56. delayIfNecessary();
  57. if (refuseRequests) {
  58. responseCode.set(500);
  59. return "";
  60. }
  61. ConfigMap map = lockSimulator.getConfigMap();
  62. if (map != null) {
  63. responseCode.set(200);
  64. return map;
  65. } else {
  66. responseCode.set(404);
  67. return "";
  68. }
  69. }
  70. @Override
  71. public Headers getHeaders() {
  72. return headers;
  73. }
  74. @Override
  75. public void setHeaders(Headers headers) {
  76. this.headers = headers;
  77. }
  78. }).always();
  79. expect().post().withPath("/api/v1/namespaces/test/configmaps").andReply(new ResponseProvider<Object>() {
  80. ThreadLocal<Integer> responseCode = new ThreadLocal<>();
  81. private Headers headers = new Headers.Builder().build();
  82. @Override
  83. public int getStatusCode() {
  84. return responseCode.get();
  85. }
  86. @Override
  87. public Object getBody(RecordedRequest recordedRequest) {
  88. delayIfNecessary();
  89. if (refuseRequests) {
  90. responseCode.set(500);
  91. return "";
  92. }
  93. ConfigMap map = convert(recordedRequest);
  94. if (map == null || map.getMetadata() == null || !lockSimulator.getConfigMapName().equals(map.getMetadata().getName())) {
  95. throw new IllegalArgumentException("Illegal configMap received");
  96. }
  97. boolean done = lockSimulator.setConfigMap(map, true);
  98. if (done) {
  99. responseCode.set(201);
  100. return lockSimulator.getConfigMap();
  101. } else {
  102. responseCode.set(500);
  103. return "";
  104. }
  105. }
  106. @Override
  107. public Headers getHeaders() {
  108. return headers;
  109. }
  110. @Override
  111. public void setHeaders(Headers headers) {
  112. this.headers = headers;
  113. }
  114. }).always();
  115. expect().put().withPath("/api/v1/namespaces/test/configmaps/" + lockSimulator.getConfigMapName()).andReply(new ResponseProvider<Object>() {
  116. ThreadLocal<Integer> responseCode = new ThreadLocal<>();
  117. private Headers headers = new Headers.Builder().build();
  118. @Override
  119. public int getStatusCode() {
  120. return responseCode.get();
  121. }
  122. @Override
  123. public Object getBody(RecordedRequest recordedRequest) {
  124. delayIfNecessary();
  125. if (refuseRequests) {
  126. responseCode.set(500);
  127. return "";
  128. }
  129. ConfigMap map = convert(recordedRequest);
  130. boolean done = lockSimulator.setConfigMap(map, false);
  131. if (done) {
  132. responseCode.set(200);
  133. return lockSimulator.getConfigMap();
  134. } else {
  135. responseCode.set(409);
  136. return "";
  137. }
  138. }
  139. @Override
  140. public Headers getHeaders() {
  141. return headers;
  142. }
  143. @Override
  144. public void setHeaders(Headers headers) {
  145. this.headers = headers;
  146. }
  147. }).always();
  148. // Other resources
  149. expect().get().withPath("/api/v1/namespaces/test/pods")
  150. .andReply(200,
  151. request -> new PodListBuilder().withNewMetadata().withResourceVersion("1").and().withItems(getCurrentPods()
  152. .stream().map(name -> new PodBuilder().withNewMetadata().withName(name).and().build()).collect(Collectors.toList())).build())
  153. .always();
  154. }
  155. public boolean isRefuseRequests() {
  156. return refuseRequests;
  157. }
  158. public void setRefuseRequests(boolean refuseRequests) {
  159. this.refuseRequests = refuseRequests;
  160. }
  161. public synchronized Collection<String> getCurrentPods() {
  162. return new TreeSet<>(this.pods);
  163. }
  164. public synchronized void removePod(String pod) {
  165. this.pods.remove(pod);
  166. }
  167. public synchronized void addPod(String pod) {
  168. this.pods.add(pod);
  169. }
  170. public Long getDelayRequests() {
  171. return delayRequests;
  172. }
  173. public void setDelayRequests(Long delayRequests) {
  174. this.delayRequests = delayRequests;
  175. }
  176. private void delayIfNecessary() {
  177. if (delayRequests != null) {
  178. try {
  179. Thread.sleep(delayRequests);
  180. } catch (InterruptedException e) {
  181. throw new RuntimeException(e);
  182. }
  183. }
  184. }
  185. private ConfigMap convert(RecordedRequest request) {
  186. try {
  187. ObjectMapper mapper = new ObjectMapper();
  188. return mapper.readValue(request.getBody().readByteArray(), ConfigMap.class);
  189. } catch (IOException e) {
  190. throw new IllegalArgumentException("Erroneous data", e);
  191. }
  192. }
  193. }