Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
3a506be6
Commit
3a506be6
authored
Aug 07, 2018
by
jennybuckley
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add test cases for webhook dry run
parent
aa36dc94
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
11 deletions
+47
-11
plugin_test.go
...rver/pkg/admission/plugin/webhook/mutating/plugin_test.go
+3
-3
testcase.go
...piserver/pkg/admission/plugin/webhook/testing/testcase.go
+42
-6
plugin_test.go
...er/pkg/admission/plugin/webhook/validating/plugin_test.go
+2
-2
No files found.
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/mutating/plugin_test.go
View file @
3a506be6
...
...
@@ -79,9 +79,9 @@ func TestAdmit(t *testing.T) {
var
attr
admission
.
Attributes
if
tt
.
IsCRD
{
attr
=
webhooktesting
.
NewAttributeUnstructured
(
ns
,
tt
.
AdditionalLabels
)
attr
=
webhooktesting
.
NewAttributeUnstructured
(
ns
,
tt
.
AdditionalLabels
,
tt
.
IsDryRun
)
}
else
{
attr
=
webhooktesting
.
NewAttribute
(
ns
,
tt
.
AdditionalLabels
)
attr
=
webhooktesting
.
NewAttribute
(
ns
,
tt
.
AdditionalLabels
,
tt
.
IsDryRun
)
}
err
=
wh
.
Admit
(
attr
)
...
...
@@ -147,7 +147,7 @@ func TestAdmitCachedClient(t *testing.T) {
continue
}
err
=
wh
.
Admit
(
webhooktesting
.
NewAttribute
(
ns
,
nil
))
err
=
wh
.
Admit
(
webhooktesting
.
NewAttribute
(
ns
,
nil
,
false
))
if
tt
.
ExpectAllow
!=
(
err
==
nil
)
{
t
.
Errorf
(
"%s: expected allowed=%v, but got err=%v"
,
tt
.
Name
,
tt
.
ExpectAllow
,
err
)
}
...
...
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/testing/testcase.go
View file @
3a506be6
...
...
@@ -76,7 +76,7 @@ func NewFakeDataSource(name string, webhooks []registrationv1beta1.Webhook, muta
return
client
,
informerFactory
}
func
newAttributesRecord
(
object
metav1
.
Object
,
oldObject
metav1
.
Object
,
kind
schema
.
GroupVersionKind
,
namespace
string
,
name
string
,
resource
string
,
labels
map
[
string
]
string
)
admission
.
Attributes
{
func
newAttributesRecord
(
object
metav1
.
Object
,
oldObject
metav1
.
Object
,
kind
schema
.
GroupVersionKind
,
namespace
string
,
name
string
,
resource
string
,
labels
map
[
string
]
string
,
dryRun
bool
)
admission
.
Attributes
{
object
.
SetName
(
name
)
object
.
SetNamespace
(
namespace
)
objectLabels
:=
map
[
string
]
string
{
resource
+
".name"
:
name
}
...
...
@@ -95,11 +95,11 @@ func newAttributesRecord(object metav1.Object, oldObject metav1.Object, kind sch
UID
:
"webhook-test"
,
}
return
admission
.
NewAttributesRecord
(
object
.
(
runtime
.
Object
),
oldObject
.
(
runtime
.
Object
),
kind
,
namespace
,
name
,
gvr
,
subResource
,
admission
.
Update
,
false
,
&
userInfo
)
return
admission
.
NewAttributesRecord
(
object
.
(
runtime
.
Object
),
oldObject
.
(
runtime
.
Object
),
kind
,
namespace
,
name
,
gvr
,
subResource
,
admission
.
Update
,
dryRun
,
&
userInfo
)
}
// NewAttribute returns static admission Attributes for testing.
func
NewAttribute
(
namespace
string
,
labels
map
[
string
]
string
)
admission
.
Attributes
{
func
NewAttribute
(
namespace
string
,
labels
map
[
string
]
string
,
dryRun
bool
)
admission
.
Attributes
{
// Set up a test object for the call
object
:=
corev1
.
Pod
{
TypeMeta
:
metav1
.
TypeMeta
{
...
...
@@ -111,11 +111,11 @@ func NewAttribute(namespace string, labels map[string]string) admission.Attribut
kind
:=
corev1
.
SchemeGroupVersion
.
WithKind
(
"Pod"
)
name
:=
"my-pod"
return
newAttributesRecord
(
&
object
,
&
oldObject
,
kind
,
namespace
,
name
,
"pod"
,
labels
)
return
newAttributesRecord
(
&
object
,
&
oldObject
,
kind
,
namespace
,
name
,
"pod"
,
labels
,
dryRun
)
}
// NewAttributeUnstructured returns static admission Attributes for testing with custom resources.
func
NewAttributeUnstructured
(
namespace
string
,
labels
map
[
string
]
string
)
admission
.
Attributes
{
func
NewAttributeUnstructured
(
namespace
string
,
labels
map
[
string
]
string
,
dryRun
bool
)
admission
.
Attributes
{
// Set up a test object for the call
object
:=
unstructured
.
Unstructured
{}
object
.
SetKind
(
"TestCRD"
)
...
...
@@ -126,7 +126,7 @@ func NewAttributeUnstructured(namespace string, labels map[string]string) admiss
kind
:=
object
.
GroupVersionKind
()
name
:=
"my-test-crd"
return
newAttributesRecord
(
&
object
,
&
oldObject
,
kind
,
namespace
,
name
,
"crd"
,
labels
)
return
newAttributesRecord
(
&
object
,
&
oldObject
,
kind
,
namespace
,
name
,
"crd"
,
labels
,
dryRun
)
}
type
urlConfigGenerator
struct
{
...
...
@@ -149,6 +149,7 @@ type Test struct {
Webhooks
[]
registrationv1beta1
.
Webhook
Path
string
IsCRD
bool
IsDryRun
bool
AdditionalLabels
map
[
string
]
string
ExpectLabels
map
[
string
]
string
ExpectAllow
bool
...
...
@@ -349,6 +350,30 @@ func NewNonMutatingTestCases(url *url.URL) []Test {
}},
ErrorContains
:
"Webhook response was absent"
,
},
{
Name
:
"no match dry run"
,
Webhooks
:
[]
registrationv1beta1
.
Webhook
{{
Name
:
"nomatch"
,
ClientConfig
:
ccfgSVC
(
"disallow"
),
Rules
:
[]
registrationv1beta1
.
RuleWithOperations
{{
Operations
:
[]
registrationv1beta1
.
OperationType
{
registrationv1beta1
.
Create
},
}},
NamespaceSelector
:
&
metav1
.
LabelSelector
{},
}},
IsDryRun
:
true
,
ExpectAllow
:
true
,
},
{
Name
:
"match dry run"
,
Webhooks
:
[]
registrationv1beta1
.
Webhook
{{
Name
:
"allow"
,
ClientConfig
:
ccfgSVC
(
"allow"
),
Rules
:
matchEverythingRules
,
NamespaceSelector
:
&
metav1
.
LabelSelector
{},
}},
IsDryRun
:
true
,
ErrorContains
:
"does not support dry run"
,
},
// No need to test everything with the url case, since only the
// connection is different.
}
...
...
@@ -417,6 +442,17 @@ func NewMutatingTestCases(url *url.URL) []Test {
}},
ErrorContains
:
"invalid character"
,
},
{
Name
:
"match & remove label dry run"
,
Webhooks
:
[]
registrationv1beta1
.
Webhook
{{
Name
:
"removeLabel"
,
ClientConfig
:
ccfgSVC
(
"removeLabel"
),
Rules
:
matchEverythingRules
,
NamespaceSelector
:
&
metav1
.
LabelSelector
{},
}},
IsDryRun
:
true
,
ErrorContains
:
"does not support dry run"
,
},
// No need to test everything with the url case, since only the
// connection is different.
}
...
...
staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/validating/plugin_test.go
View file @
3a506be6
...
...
@@ -72,7 +72,7 @@ func TestValidate(t *testing.T) {
continue
}
err
=
wh
.
Validate
(
webhooktesting
.
NewAttribute
(
ns
,
nil
))
err
=
wh
.
Validate
(
webhooktesting
.
NewAttribute
(
ns
,
nil
,
tt
.
IsDryRun
))
if
tt
.
ExpectAllow
!=
(
err
==
nil
)
{
t
.
Errorf
(
"%s: expected allowed=%v, but got err=%v"
,
tt
.
Name
,
tt
.
ExpectAllow
,
err
)
}
...
...
@@ -130,7 +130,7 @@ func TestValidateCachedClient(t *testing.T) {
continue
}
err
=
wh
.
Validate
(
webhooktesting
.
NewAttribute
(
ns
,
nil
))
err
=
wh
.
Validate
(
webhooktesting
.
NewAttribute
(
ns
,
nil
,
false
))
if
tt
.
ExpectAllow
!=
(
err
==
nil
)
{
t
.
Errorf
(
"%s: expected allowed=%v, but got err=%v"
,
tt
.
Name
,
tt
.
ExpectAllow
,
err
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment