2017-02-01 00:45:59 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-02-03 13:41:32 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-02-01 00:45:59 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateEvent(t *testing.T) {
|
|
|
|
table := []struct {
|
|
|
|
*api.Event
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test1",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
Namespace: "bar",
|
|
|
|
Kind: "Pod",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
}, {
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test2",
|
|
|
|
Namespace: "aoeu-_-aoeu",
|
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
Namespace: "aoeu-_-aoeu",
|
|
|
|
Kind: "Pod",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
}, {
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test3",
|
2017-02-03 13:41:32 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
APIVersion: "v1",
|
|
|
|
Kind: "Node",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
}, {
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test4",
|
2017-02-03 13:41:32 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
APIVersion: "v1",
|
|
|
|
Kind: "Namespace",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
}, {
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test5",
|
2017-02-03 13:41:32 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
APIVersion: "extensions/v1beta1",
|
|
|
|
Kind: "NoKind",
|
2017-02-03 13:41:32 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
}, {
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test6",
|
2017-02-03 13:41:32 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
APIVersion: "extensions/v1beta1",
|
|
|
|
Kind: "Job",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
}, {
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test7",
|
2017-02-03 13:41:32 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
APIVersion: "extensions/v1beta1",
|
|
|
|
Kind: "Job",
|
2017-02-03 13:41:32 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
}, {
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test8",
|
2017-02-03 13:41:32 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
APIVersion: "other/v1beta1",
|
|
|
|
Kind: "Job",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
}, {
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test9",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
APIVersion: "other/v1beta1",
|
|
|
|
Kind: "Job",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
}, {
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test10",
|
2017-02-03 13:41:32 +00:00
|
|
|
Namespace: metav1.NamespaceDefault,
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
APIVersion: "extensions",
|
|
|
|
Kind: "Job",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
}, {
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test11",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
// must register in v1beta1 to be true
|
|
|
|
APIVersion: "extensions/v1beta1",
|
|
|
|
Kind: "Job",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test12",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
APIVersion: "other/v1beta1",
|
|
|
|
Kind: "FooBar",
|
|
|
|
Namespace: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test13",
|
|
|
|
Namespace: "",
|
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
APIVersion: "other/v1beta1",
|
|
|
|
Kind: "FooBar",
|
|
|
|
Namespace: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&api.Event{
|
2017-02-03 13:41:32 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2017-02-01 00:45:59 +00:00
|
|
|
Name: "test14",
|
|
|
|
Namespace: "foo",
|
|
|
|
},
|
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
APIVersion: "other/v1beta1",
|
|
|
|
Kind: "FooBar",
|
|
|
|
Namespace: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range table {
|
|
|
|
if e, a := item.valid, len(ValidateEvent(item.Event)) == 0; e != a {
|
|
|
|
t.Errorf("%v: expected %v, got %v", item.Event.Name, e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|