k8s supports a few mechanisms to stub Kubernetes HTTP requests when developing or testing locally.

Mocking API Version and Resource Discovery

K8s does resource discovery of the Kubernetes API before running K8s.Operations. When testing if a Kubernetes cluster isn't available you can use the module K8s.Discovery.Driver.File to stub Kubernetes api-resource and api-versions responses.

The mock used in the test suite is here.

The driver can be set for all clusters or per-cluster:

config :k8s,
  discovery_driver: K8s.Discovery.Driver.File,
  discovery_opts: [config: "test/support/discovery/example.json"],
  clusters: %{
    test: %{
      conn: "test/support/kube-config.yaml",
      conn_opts: [
        discovery_driver: K8s.Discovery.Driver.File,
        discovery_opts: [config: "test/support/discovery/example.json"]

Mocking HTTP Responses of K8s.Operations in your application

K8s ships with a K8s.Client.DynamicHTTPProvider for stubbing HTTP responses to kubernetes API requests.

This provider is used throughout the test suite for mocking HTTP responses.

To enable the dynamic HTTP provider it must be turned on in your config.exs:

config :k8s, http_provider: K8s.Client.DynamicHTTPProvider
defmodule MyApp.ResourceTest do
  use ExUnit.Case, async: true

  defmodule K8sMock do
    @base_url "https://localhost:6443"
    @namespaces_url @base_url <> "/api/v1/namespaces"

    def request(:get, @namespaces_url, _, _, _) do
      namespaces = [%{"metadata" => %{"name" => "default"}}]
      body = Jason.encode!(namespaces)
      {:ok, %HTTPoison.Response{status_code: 200, body: body}}

  setup do
    DynamicHTTPProvider.register(self(), __MODULE__.K8sMock)

  test "gets namespaces" do
    conn = %K8s.Conn{} # set up your conn
    operation = K8s.Client.get("v1", :namespaces)
    assert {:ok, namespaces} = K8s.Client.run(operation, conn)
    assert namespaces == [%{"metadata" => %{"name" => "default"}}]

To see advanced examples of usage, check out these examples in the test suite: