123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- /*
- 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"
- "k8s.io/kubernetes/pkg/api"
- )
- func TestValidateEvent(t *testing.T) {
- table := []struct {
- *api.Event
- valid bool
- }{
- {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test1",
- Namespace: "foo",
- },
- InvolvedObject: api.ObjectReference{
- Namespace: "bar",
- Kind: "Pod",
- },
- },
- false,
- }, {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test2",
- Namespace: "aoeu-_-aoeu",
- },
- InvolvedObject: api.ObjectReference{
- Namespace: "aoeu-_-aoeu",
- Kind: "Pod",
- },
- },
- false,
- }, {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test3",
- Namespace: api.NamespaceDefault,
- },
- InvolvedObject: api.ObjectReference{
- APIVersion: "v1",
- Kind: "Node",
- },
- },
- true,
- }, {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test4",
- Namespace: api.NamespaceDefault,
- },
- InvolvedObject: api.ObjectReference{
- APIVersion: "v1",
- Kind: "Namespace",
- },
- },
- true,
- }, {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test5",
- Namespace: api.NamespaceDefault,
- },
- InvolvedObject: api.ObjectReference{
- APIVersion: "extensions/v1beta1",
- Kind: "NoKind",
- Namespace: api.NamespaceDefault,
- },
- },
- true,
- }, {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test6",
- Namespace: api.NamespaceDefault,
- },
- InvolvedObject: api.ObjectReference{
- APIVersion: "extensions/v1beta1",
- Kind: "Job",
- Namespace: "foo",
- },
- },
- false,
- }, {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test7",
- Namespace: api.NamespaceDefault,
- },
- InvolvedObject: api.ObjectReference{
- APIVersion: "extensions/v1beta1",
- Kind: "Job",
- Namespace: api.NamespaceDefault,
- },
- },
- true,
- }, {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test8",
- Namespace: api.NamespaceDefault,
- },
- InvolvedObject: api.ObjectReference{
- APIVersion: "other/v1beta1",
- Kind: "Job",
- Namespace: "foo",
- },
- },
- false,
- }, {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test9",
- Namespace: "foo",
- },
- InvolvedObject: api.ObjectReference{
- APIVersion: "other/v1beta1",
- Kind: "Job",
- Namespace: "foo",
- },
- },
- true,
- }, {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test10",
- Namespace: api.NamespaceDefault,
- },
- InvolvedObject: api.ObjectReference{
- APIVersion: "extensions",
- Kind: "Job",
- Namespace: "foo",
- },
- },
- false,
- }, {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test11",
- Namespace: "foo",
- },
- InvolvedObject: api.ObjectReference{
- // must register in v1beta1 to be true
- APIVersion: "extensions/v1beta1",
- Kind: "Job",
- Namespace: "foo",
- },
- },
- true,
- },
- {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test12",
- Namespace: "foo",
- },
- InvolvedObject: api.ObjectReference{
- APIVersion: "other/v1beta1",
- Kind: "FooBar",
- Namespace: "bar",
- },
- },
- false,
- },
- {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- Name: "test13",
- Namespace: "",
- },
- InvolvedObject: api.ObjectReference{
- APIVersion: "other/v1beta1",
- Kind: "FooBar",
- Namespace: "bar",
- },
- },
- false,
- },
- {
- &api.Event{
- ObjectMeta: api.ObjectMeta{
- 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)
- }
- }
- }
|