-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
173 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
defmodule Bonny.AdmissionControl.PlugTest do | ||
use ExUnit.Case, async: true | ||
use Plug.Test | ||
|
||
alias Bonny.Test.AdmissionControlHelper | ||
alias Bonny.AdmissionControl.AdmissionReview | ||
alias Bonny.AdmissionControl.Plug, as: MUT | ||
|
||
describe "init/1" do | ||
test "raises if webhook_type is not declared" do | ||
assert_raise(CompileError, ~r/requires you to define the :webhook_type option/, fn -> | ||
MUT.init(webhook_handler: SomeModule) | ||
end) | ||
end | ||
|
||
test "raises if webhook_type is not :validating or :mutating" do | ||
assert_raise(CompileError, ~r/requires you to define the :webhook_type option/, fn -> | ||
MUT.init(webhook_handler: SomeModule, webhook_type: :invalid) | ||
end) | ||
end | ||
|
||
test "raises if webhook_handler is not declared" do | ||
assert_raise(CompileError, ~r/requires you to set the :webhook_handler option/, fn -> | ||
MUT.init(webhook_type: :validating) | ||
end) | ||
end | ||
|
||
test "turns webhook_handler into {module, opts} tuple" do | ||
opts = MUT.init(webhook_type: :validating, webhook_handler: SomeModule) | ||
assert opts.webhook_handler == {SomeModule, []} | ||
end | ||
|
||
defmodule InitTestHandler do | ||
def init(:foo), do: :bar | ||
end | ||
|
||
test "calls handler's init function if tuple is given" do | ||
opts = MUT.init(webhook_type: :validating, webhook_handler: {InitTestHandler, :foo}) | ||
assert opts.webhook_handler == {InitTestHandler, :bar} | ||
end | ||
end | ||
|
||
defmodule CallTestHandler do | ||
def call(admission_webhook, opts) do | ||
case opts[:result] do | ||
:deny -> AdmissionReview.deny(admission_webhook) | ||
_ -> admission_webhook | ||
end | ||
end | ||
end | ||
|
||
describe "call/2" do | ||
test "calls the handler and returns plug" do | ||
response = | ||
AdmissionControlHelper.webhook_request_conn() | ||
|> MUT.call(%{webhook_type: :validation, webhook_handler: {CallTestHandler, []}}) | ||
|> Map.get(:resp_body) | ||
|> Jason.decode!() | ||
|
||
assert %{ | ||
"apiVersion" => "admission.k8s.io/v1", | ||
"kind" => "AdmissionReview", | ||
"response" => %{ | ||
"allowed" => true | ||
} | ||
} = response | ||
end | ||
|
||
test "calls the handler and returns allowed false" do | ||
response = | ||
AdmissionControlHelper.webhook_request_conn() | ||
|> MUT.call(%{ | ||
webhook_type: :validation, | ||
webhook_handler: {CallTestHandler, [result: :deny]} | ||
}) | ||
|> Map.get(:resp_body) | ||
|> Jason.decode!() | ||
|
||
assert false == response["response"]["allowed"] | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
defmodule Bonny.Test.AdmissionControlHelper do | ||
use Plug.Test | ||
|
||
def webhook_request_conn() do | ||
body = """ | ||
{ | ||
"apiVersion": "admission.k8s.io/v1", | ||
"kind": "AdmissionReview", | ||
"request": { | ||
"uid": "705ab4f5-6393-11e8-b7cc-42010a800002", | ||
"kind": { | ||
"group": "example.com", | ||
"version": "v1alpha1", | ||
"kind": "SomeCRD" | ||
}, | ||
"resource": { | ||
"group": "example.com", | ||
"version": "v1alpha1", | ||
"resource": "somecrds" | ||
}, | ||
"requestKind": { | ||
"group": "example.com", | ||
"version": "v1alpha1", | ||
"kind": "SomeCRD" | ||
}, | ||
"requestResource": { | ||
"group": "example.com", | ||
"version": "v1alpha1", | ||
"resource": "somecrds" | ||
}, | ||
"name": "my-deployment", | ||
"namespace": "my-namespace", | ||
"operation": "UPDATE", | ||
"userInfo": { | ||
"username": "admin", | ||
"uid": "014fbff9a07c", | ||
"groups": [ | ||
"system:authenticated", | ||
"my-admin-group" | ||
], | ||
"extra": { | ||
"some-key": [ | ||
"some-value1", | ||
"some-value2" | ||
] | ||
} | ||
}, | ||
"object": { | ||
"apiVersion": "autoscaling/v1", | ||
"kind": "Scale" | ||
}, | ||
"oldObject": { | ||
"apiVersion": "autoscaling/v1", | ||
"kind": "Scale" | ||
}, | ||
"options": { | ||
"apiVersion": "meta.k8s.io/v1", | ||
"kind": "UpdateOptions" | ||
}, | ||
"dryRun": false | ||
} | ||
} | ||
""" | ||
|
||
conn("POST", "/webhook", body) | ||
|> put_req_header("content-type", "application/json") | ||
end | ||
end |
Oops, something went wrong.