Skip to content

Commit

Permalink
use minikube for testing as its permissions are more standard (rigoro…
Browse files Browse the repository at this point in the history
…us) by default compared to docker-desktop
  • Loading branch information
wr0ngway committed Feb 24, 2021
1 parent a07f603 commit ef16657
Show file tree
Hide file tree
Showing 11 changed files with 531 additions and 2,247 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

150 changes: 95 additions & 55 deletions spec/fixtures/vcr/Kubetruth_KubeApi/secrets/can_crud_secrets.yml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

54 changes: 41 additions & 13 deletions spec/kubetruth/kubeapi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,63 @@
require 'kubetruth/kubeapi'

module Kubetruth
# describe KubeApi, :vcr => {:record => :all} do
describe KubeApi, :vcr do

describe KubeApi, :vcr => {
# uncomment to force record of new fixtures
# :record => :all,
# minikube has variable port for api server url, so use match_requests_on to not match against port
:match_requests_on => [:method, :host, :path]
} do

def namespace; "kubetruth-test-ns"; end
def helm_name; "kubetruth-test-app"; end

if ! ENV['CI']

def check_deps
@deps_checked ||= begin
system("helm version >/dev/null 2>&1") || fail("test dependency not installed - helm ")
system("minikube version >/dev/null 2>&1") || fail("test dependency not installed - minikube")
system("minikube status >/dev/null 2>&1") || fail("test dependency nor running - minikube")
true
end
end

def teardown
sysrun("helm delete --namespace #{namespace} #{helm_name}", output_on_fail: false, allow_fail: true)
existing_namespaces.each do |ns|
sysrun("kubectl --context docker-desktop delete namespace #{ns}", output_on_fail: false, allow_fail: true)
sysrun("minikube kubectl -- delete namespace #{ns}", output_on_fail: false, allow_fail: true)
end
end

def setup
check_deps
teardown
root = File.expand_path('../..', __dir__)
sysrun("helm install --create-namespace --namespace #{namespace} --set appSettings.apiKey=#{ENV['CT_API_KEY']} #{helm_name} #{root}/helm/kubetruth/")
end

def token
@token ||= begin
secret_names = sysrun("kubectl --context docker-desktop --namespace #{namespace} get secret").lines
secret_names = sysrun("minikube kubectl -- --namespace #{namespace} get secret").lines
secret_names = secret_names.grep(/#{helm_name}-token/)
secret_name = secret_names.first.split.first
token_lines = sysrun("kubectl --context docker-desktop --namespace #{namespace} describe secret #{secret_name}").lines
token_lines = sysrun("minikube kubectl -- --namespace #{namespace} describe secret #{secret_name}").lines
token_lines = token_lines.grep(/token:/)
token_lines.first.split[1]
end
end

def apiserver
@apiserver ||= begin
config = YAML.load(sysrun("minikube kubectl -- config view"))
cluster = config["clusters"].find {|c| c["name"] == "minikube" }
cluster["cluster"]["server"]
end
end

def existing_namespaces
names = sysrun("kubectl --context docker-desktop get namespace").lines
names = sysrun("minikube kubectl -- get namespace").lines
names = names.grep(/#{namespace}/)
names = names.collect {|n| n.split.first }
names
Expand All @@ -47,11 +71,15 @@ def existing_namespaces
after(:all) do
teardown
end

else

def token; ""; end
def apiserver; "https://127.0.0.1"; end

end

let(:kubeapi) { described_class.new(namespace: namespace, token: token, api_url: "https://kubernetes.docker.internal:6443") }
let(:kubeapi) { described_class.new(namespace: namespace, token: token, api_url: apiserver) }

describe "initialize" do

Expand All @@ -64,15 +92,15 @@ def token; ""; end
describe "ensure_namespace" do

it "creates namespace if not present" do
kapi = described_class.new(namespace: "#{namespace}-newns", token: token, api_url: "https://kubernetes.docker.internal:6443")
kapi = described_class.new(namespace: "#{namespace}-newns", token: token, api_url: apiserver)
expect { kapi.create_config_map("foo", {}) }.to raise_error(Kubeclient::ResourceNotFoundError, /namespaces.*not found/)
ns = kapi.ensure_namespace
kapi.create_config_map("foo", {bar: "baz"})
expect(kapi.get_config_map("foo").data["bar"]).to eq("baz")
end

it "sets labels when creating namespace" do
kapi = described_class.new(namespace: "#{namespace}-newns2", token: token, api_url: "https://kubernetes.docker.internal:6443")
kapi = described_class.new(namespace: "#{namespace}-newns2", token: token, api_url: apiserver)
expect { kapi.create_config_map("foo", {}) }.to raise_error(Kubeclient::ResourceNotFoundError, /namespaces.*not found/)
ns = kapi.ensure_namespace
expect(ns.metadata.labels.to_h).to eq({:"app.kubernetes.io/managed-by" => "kubetruth"})
Expand Down Expand Up @@ -132,9 +160,9 @@ def token; ""; end
end

it "can use multiple namespaces for config maps" do
ns1_kapi = described_class.new(namespace: "#{namespace}-cmns1", token: token, api_url: "https://kubernetes.docker.internal:6443")
ns1_kapi = described_class.new(namespace: "#{namespace}-cmns1", token: token, api_url: apiserver)
ns1_kapi.ensure_namespace
ns2_kapi = described_class.new(namespace: "#{namespace}-cmns2", token: token, api_url: "https://kubernetes.docker.internal:6443")
ns2_kapi = described_class.new(namespace: "#{namespace}-cmns2", token: token, api_url: apiserver)
ns2_kapi.ensure_namespace

expect { ns1_kapi.get_config_map("foo") }.to raise_error(Kubeclient::ResourceNotFoundError)
Expand Down Expand Up @@ -193,10 +221,10 @@ def token; ""; end
end

it "can use multiple namespaces for secrets" do
ns1_kapi = described_class.new(namespace: "#{namespace}-secretns1", token: token, api_url: "https://kubernetes.docker.internal:6443")
ns1_kapi = described_class.new(namespace: "#{namespace}-secretns1", token: token, api_url: apiserver)
ns1_kapi.ensure_namespace
ns1_kapi.ensure_namespace
ns2_kapi = described_class.new(namespace: "#{namespace}-secretns2", token: token, api_url: "https://kubernetes.docker.internal:6443")
ns2_kapi = described_class.new(namespace: "#{namespace}-secretns2", token: token, api_url: apiserver)
ns2_kapi.ensure_namespace

expect { ns1_kapi.get_secret("foo") }.to raise_error(Kubeclient::ResourceNotFoundError)
Expand Down

0 comments on commit ef16657

Please sign in to comment.