io.fabric8.kubernetes.client.server.mock.KubernetesMockServer Java Examples

The following examples show how to use io.fabric8.kubernetes.client.server.mock.KubernetesMockServer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResolveServicesByLabelsConfigAndPortOKOfflineTest.java    From vxms with Apache License 2.0 7 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace(null)
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #2
Source File: KubernetesMockServerTestResource.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, String> start() {
    final Map<String, String> systemProps = new HashMap<>();
    systemProps.put(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true");
    systemProps.put(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false");
    systemProps.put(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, "false");
    systemProps.put(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test");

    mockServer = new KubernetesMockServer(useHttps());
    mockServer.init();
    try (NamespacedKubernetesClient client = mockServer.createClient()) {
        systemProps.put(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, client.getConfiguration().getMasterUrl());
    }

    configureMockServer(mockServer);

    return systemProps;
}
 
Example #3
Source File: ResolveServicesByLabelWithConfigNOKTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #4
Source File: ResolveServicesByLabelWithConfigOKTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #5
Source File: ResolveServicesByLabelsOKTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #6
Source File: ResolveServicesByLabelOKTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #7
Source File: ResolveServicesByLabelsConfigOKTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #8
Source File: ResolveServicesByLabelsTooManyNOKTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #9
Source File: ResolveServicesByLAbelsWithPortNameTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #10
Source File: KubernetesMockTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
@Before
public void init() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #11
Source File: ResolveServicesByNameTest.java    From vxms with Apache License 2.0 6 votes vote down vote up
public void initKubernetes() {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);
  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();
  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #12
Source File: KubernetesMockServerTestResource.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public void inject(Object testInstance) {
    Class<?> c = testInstance.getClass();
    while (c != Object.class) {
        for (Field f : c.getDeclaredFields()) {
            if (f.getAnnotation(MockServer.class) != null) {
                if (!KubernetesMockServer.class.isAssignableFrom(f.getType())) {
                    throw new RuntimeException("@MockServer can only be used on fields of type KubernetesMockServer");
                }

                f.setAccessible(true);
                try {
                    f.set(testInstance, mockServer);
                    return;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        c = c.getSuperclass();
    }
}
 
Example #13
Source File: CustomKubernetesMockServerTestResource.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public void configureMockServer(KubernetesMockServer mockServer) {
    mockServer.expect().get().withPath("/api/v1/namespaces/test/configmaps/cmap1")
            .andReturn(200, configMapBuilder("cmap1")
                    .addToData("dummy", "dummy")
                    .addToData("some.prop1", "val1")
                    .addToData("some.prop2", "val2")
                    .addToData("application.properties", "some.prop3=val3")
                    .addToData("application.yaml", "some:\n  prop4: val4").build())
            .once();

    mockServer.expect().get().withPath("/api/v1/namespaces/test/configmaps/cmap2")
            .andReturn(200, configMapBuilder("cmap2")
                    .addToData("application.yaml", "some:\n  prop4: val4").build())
            .once();
}
 
Example #14
Source File: ResolveServicesByNameWithPortNameTest.java    From vxms with Apache License 2.0 5 votes vote down vote up
public void initKubernetes(JsonObject conf) {
  KubernetesMockServer plainServer = new KubernetesMockServer(false);
  plainServer.init();
  String host = plainServer.getHostName();
  Integer port = plainServer.getPort();
  ClassLoader classLoader = getClass().getClassLoader();
  File ca = new File(classLoader.getResource("ca.crt").getFile());
  File clientcert = new File(classLoader.getResource("client.crt").getFile());
  File clientkey = new File(classLoader.getResource("client.key").getFile());
  System.out.println("port: " + port + "  host:" + host);

  conf.put("withMasterUrl", host + ":" + port);
  conf.put("withCaCertFile", ca.getAbsolutePath());
  conf.put("withClientCertFile", clientcert.getAbsolutePath());
  conf.put("withClientKeyFile", clientkey.getAbsolutePath());

  TestingClientConfig.config =
      new ConfigBuilder()
          .withMasterUrl(host + ":" + port)
          .withNamespace("default")
          .withCaCertFile(ca.getAbsolutePath())
          .withClientCertFile(clientcert.getAbsolutePath())
          .withClientKeyFile(clientkey.getAbsolutePath())
          .build();

  //  client = new DefaultKubernetesClient(config);
  server = plainServer;
}
 
Example #15
Source File: MixedKubernetesServer.java    From flink with Apache License 2.0 5 votes vote down vote up
public void before() {
	HashMap<ServerRequest, Queue<ServerResponse>> response = new HashMap<>();
	mock = crudMode
		? new KubernetesMockServer(new Context(), new MockWebServer(), response, new MixedDispatcher(response), true)
		: new KubernetesMockServer(https);
	mock.init();
	client = mock.createClient();
}
 
Example #16
Source File: ConfigMapStoreTest.java    From vertx-config with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp(TestContext tc) throws MalformedURLException {
  vertx = Vertx.vertx();
  vertx.exceptionHandler(tc.exceptionHandler());

  ConfigMap map1 = new ConfigMapBuilder().withMetadata(new ObjectMetaBuilder().withName("my-config-map").build())
    .addToData("my-app-json", SOME_JSON)
    .addToData("my-app-props", SOME_PROPS)
    .build();

  Map<String, String> data = new LinkedHashMap<>();
  data.put("key", "value");
  data.put("bool", "true");
  data.put("count", "3");
  ConfigMap map2 = new ConfigMapBuilder().withMetadata(new ObjectMetaBuilder().withName("my-config-map-2").build())
    .withData(data)
    .build();

  ConfigMap map3 = new ConfigMapBuilder().withMetadata(new ObjectMetaBuilder().withName("my-config-map-x").build())
    .addToData("my-app-json", SOME_JSON)
    .build();

  Secret secret = new SecretBuilder().withMetadata(new ObjectMetaBuilder().withName("my-secret").build())
    .addToData("password", Base64.getEncoder().encodeToString("secret".getBytes(UTF_8)))
    .build();

  server = new KubernetesMockServer(false);


  server.expect().get().withPath("/api/v1/namespaces/default/configmaps").andReturn(200, new
    ConfigMapListBuilder().addToItems(map1, map2).build()).always();
  server.expect().get().withPath("/api/v1/namespaces/my-project/configmaps").andReturn(200, new
    ConfigMapListBuilder().addToItems(map3).build()).always();

  server.expect().get().withPath("/api/v1/namespaces/default/configmaps/my-config-map")
    .andReturn(200, map1).always();
  server.expect().get().withPath("/api/v1/namespaces/default/configmaps/my-config-map-2")
    .andReturn(200, map2).always();

  server.expect().get().withPath("/api/v1/namespaces/my-project/configmaps/my-config-map-x")
    .andReturn(200, map3).always();

  server.expect().get().withPath("/api/v1/namespaces/default/configmaps/my-unknown-config-map")
    .andReturn(500, null).always();
  server.expect().get().withPath("/api/v1/namespaces/default/configmaps/my-unknown-map")
    .andReturn(500, null).always();

  server.expect().get().withPath("/api/v1/namespaces/my-project/secrets/my-secret").andReturn(200, secret)
    .always();

  server.init();
  client = server.createClient();
  port = new URL(client.getConfiguration().getMasterUrl()).getPort();
}
 
Example #17
Source File: OpenShiftServerTest.java    From vertx-service-discovery with Apache License 2.0 4 votes vote down vote up
@Override
public KubernetesMockServer getServer() {
  return new OpenShiftMockServer(false);
}
 
Example #18
Source File: KubernetesServerTest.java    From vertx-service-discovery with Apache License 2.0 4 votes vote down vote up
public KubernetesMockServer getServer() {
  return new KubernetesMockServer(false);
}
 
Example #19
Source File: KubernetesMockServerTestResource.java    From quarkus with Apache License 2.0 votes vote down vote up
/**
 * Can be used by subclasses of {@code KubernetesMockServerTestResource} in order to
 * setup the mock server before the Quarkus application starts
 */
public void configureMockServer(KubernetesMockServer mockServer) {

}