2014-10-11 00:46:38 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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 record_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2014-10-30 00:27:11 +00:00
|
|
|
"strings"
|
2014-10-11 00:46:38 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-11-21 00:01:42 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
2014-10-11 00:46:38 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/record"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testEventRecorder struct {
|
|
|
|
OnEvent func(e *api.Event) (*api.Event, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateEvent records the event for testing.
|
2014-10-23 20:55:48 +00:00
|
|
|
func (t *testEventRecorder) Create(e *api.Event) (*api.Event, error) {
|
2014-10-11 00:46:38 +00:00
|
|
|
if t.OnEvent != nil {
|
|
|
|
return t.OnEvent(e)
|
|
|
|
}
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testEventRecorder) clearOnEvent() {
|
|
|
|
t.OnEvent = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventf(t *testing.T) {
|
2014-11-04 00:06:36 +00:00
|
|
|
testPod := &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
SelfLink: "/api/v1beta1/pods/foo",
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: "baz",
|
|
|
|
UID: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testRef, err := api.GetPartialReference(testPod, "desiredState.manifest.containers[2]")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-10-11 00:46:38 +00:00
|
|
|
table := []struct {
|
2014-11-04 00:06:36 +00:00
|
|
|
obj runtime.Object
|
|
|
|
status, reason string
|
|
|
|
messageFmt string
|
|
|
|
elements []interface{}
|
|
|
|
expect *api.Event
|
|
|
|
expectLog string
|
2014-10-11 00:46:38 +00:00
|
|
|
}{
|
|
|
|
{
|
2014-11-04 00:06:36 +00:00
|
|
|
obj: testRef,
|
2014-12-12 21:27:25 +00:00
|
|
|
status: "Running",
|
|
|
|
reason: "Started",
|
2014-11-04 00:06:36 +00:00
|
|
|
messageFmt: "some verbose message: %v",
|
|
|
|
elements: []interface{}{1},
|
|
|
|
expect: &api.Event{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2014-10-30 00:27:11 +00:00
|
|
|
Name: "foo",
|
|
|
|
Namespace: "baz",
|
2014-10-11 00:46:38 +00:00
|
|
|
},
|
2014-11-04 00:06:36 +00:00
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
Kind: "Pod",
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: "baz",
|
|
|
|
UID: "bar",
|
|
|
|
APIVersion: "v1beta1",
|
|
|
|
FieldPath: "desiredState.manifest.containers[2]",
|
|
|
|
},
|
2014-12-12 21:27:25 +00:00
|
|
|
Condition: "Running",
|
|
|
|
Reason: "Started",
|
|
|
|
Message: "some verbose message: 1",
|
|
|
|
Source: "eventTest",
|
2014-10-11 00:46:38 +00:00
|
|
|
},
|
2014-12-12 21:27:25 +00:00
|
|
|
expectLog: `Event(api.ObjectReference{Kind:"Pod", Namespace:"baz", Name:"foo", UID:"bar", APIVersion:"v1beta1", ResourceVersion:"", FieldPath:"desiredState.manifest.containers[2]"}): status: 'Running', reason: 'Started' some verbose message: 1`,
|
2014-11-04 00:06:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
obj: testPod,
|
2014-12-12 21:27:25 +00:00
|
|
|
status: "Running",
|
|
|
|
reason: "Started",
|
2014-10-11 00:46:38 +00:00
|
|
|
messageFmt: "some verbose message: %v",
|
|
|
|
elements: []interface{}{1},
|
|
|
|
expect: &api.Event{
|
2014-10-30 00:27:11 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: "baz",
|
|
|
|
},
|
2014-10-11 00:46:38 +00:00
|
|
|
InvolvedObject: api.ObjectReference{
|
|
|
|
Kind: "Pod",
|
|
|
|
Name: "foo",
|
2014-10-30 00:27:11 +00:00
|
|
|
Namespace: "baz",
|
2014-10-23 02:59:15 +00:00
|
|
|
UID: "bar",
|
2014-10-11 00:46:38 +00:00
|
|
|
APIVersion: "v1beta1",
|
|
|
|
},
|
2014-12-12 21:27:25 +00:00
|
|
|
Condition: "Running",
|
|
|
|
Reason: "Started",
|
|
|
|
Message: "some verbose message: 1",
|
|
|
|
Source: "eventTest",
|
2014-10-11 00:46:38 +00:00
|
|
|
},
|
2014-12-12 21:27:25 +00:00
|
|
|
expectLog: `Event(api.ObjectReference{Kind:"Pod", Namespace:"baz", Name:"foo", UID:"bar", APIVersion:"v1beta1", ResourceVersion:"", FieldPath:""}): status: 'Running', reason: 'Started' some verbose message: 1`,
|
2014-10-11 00:46:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range table {
|
|
|
|
called := make(chan struct{})
|
|
|
|
testEvents := testEventRecorder{
|
2014-10-22 05:28:12 +00:00
|
|
|
OnEvent: func(event *api.Event) (*api.Event, error) {
|
|
|
|
a := *event
|
|
|
|
// Just check that the timestamp was set.
|
|
|
|
if a.Timestamp.IsZero() {
|
|
|
|
t.Errorf("timestamp wasn't set")
|
|
|
|
}
|
|
|
|
a.Timestamp = item.expect.Timestamp
|
2014-10-30 00:27:11 +00:00
|
|
|
// Check that name has the right prefix.
|
|
|
|
if n, en := a.Name, item.expect.Name; !strings.HasPrefix(n, en) {
|
|
|
|
t.Errorf("Name '%v' does not contain prefix '%v'", n, en)
|
|
|
|
}
|
|
|
|
a.Name = item.expect.Name
|
2014-10-22 05:28:12 +00:00
|
|
|
if e, a := item.expect, &a; !reflect.DeepEqual(e, a) {
|
2014-10-11 00:46:38 +00:00
|
|
|
t.Errorf("diff: %s", util.ObjectDiff(e, a))
|
|
|
|
}
|
|
|
|
called <- struct{}{}
|
2014-10-22 05:28:12 +00:00
|
|
|
return event, nil
|
2014-10-11 00:46:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
recorder := record.StartRecording(&testEvents, "eventTest")
|
|
|
|
logger := record.StartLogging(t.Logf) // Prove that it is useful
|
|
|
|
logger2 := record.StartLogging(func(formatter string, args ...interface{}) {
|
|
|
|
if e, a := item.expectLog, fmt.Sprintf(formatter, args...); e != a {
|
|
|
|
t.Errorf("Expected '%v', got '%v'", e, a)
|
|
|
|
}
|
|
|
|
called <- struct{}{}
|
|
|
|
})
|
|
|
|
|
2014-11-04 00:06:36 +00:00
|
|
|
record.Eventf(item.obj, item.status, item.reason, item.messageFmt, item.elements...)
|
2014-10-11 00:46:38 +00:00
|
|
|
|
|
|
|
<-called
|
|
|
|
<-called
|
|
|
|
recorder.Stop()
|
|
|
|
logger.Stop()
|
|
|
|
logger2.Stop()
|
|
|
|
}
|
|
|
|
}
|
2014-11-21 00:01:42 +00:00
|
|
|
|
|
|
|
func TestWriteEventError(t *testing.T) {
|
|
|
|
ref := &api.ObjectReference{
|
|
|
|
Kind: "Pod",
|
|
|
|
Name: "foo",
|
|
|
|
Namespace: "baz",
|
|
|
|
UID: "bar",
|
|
|
|
APIVersion: "v1beta1",
|
|
|
|
}
|
|
|
|
type entry struct {
|
|
|
|
timesToSendError int
|
|
|
|
attemptsMade int
|
|
|
|
attemptsWanted int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
table := map[string]*entry{
|
|
|
|
"giveUp1": {
|
|
|
|
timesToSendError: 1000,
|
|
|
|
attemptsWanted: 1,
|
|
|
|
err: &client.RequestConstructionError{},
|
|
|
|
},
|
|
|
|
"giveUp2": {
|
|
|
|
timesToSendError: 1000,
|
|
|
|
attemptsWanted: 1,
|
|
|
|
err: &errors.StatusError{},
|
|
|
|
},
|
|
|
|
"retry1": {
|
|
|
|
timesToSendError: 1000,
|
|
|
|
attemptsWanted: 3,
|
|
|
|
err: &errors.UnexpectedObjectError{},
|
|
|
|
},
|
|
|
|
"retry2": {
|
|
|
|
timesToSendError: 1000,
|
|
|
|
attemptsWanted: 3,
|
|
|
|
err: fmt.Errorf("A weird error"),
|
|
|
|
},
|
|
|
|
"succeedEventually": {
|
|
|
|
timesToSendError: 2,
|
|
|
|
attemptsWanted: 2,
|
|
|
|
err: fmt.Errorf("A weird error"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
done := make(chan struct{})
|
|
|
|
|
|
|
|
defer record.StartRecording(
|
|
|
|
&testEventRecorder{
|
|
|
|
OnEvent: func(event *api.Event) (*api.Event, error) {
|
|
|
|
if event.Message == "finished" {
|
|
|
|
close(done)
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
item, ok := table[event.Message]
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Unexpected event: %#v", event)
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
item.attemptsMade++
|
|
|
|
if item.attemptsMade < item.timesToSendError {
|
|
|
|
return nil, item.err
|
|
|
|
}
|
|
|
|
return event, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"eventTest",
|
|
|
|
).Stop()
|
|
|
|
|
|
|
|
for caseName := range table {
|
|
|
|
record.Event(ref, "Status", "Reason", caseName)
|
|
|
|
}
|
|
|
|
record.Event(ref, "Status", "Reason", "finished")
|
|
|
|
<-done
|
|
|
|
|
|
|
|
for caseName, item := range table {
|
|
|
|
if e, a := item.attemptsWanted, item.attemptsMade; e != a {
|
|
|
|
t.Errorf("case %v: wanted %v, got %v attempts", caseName, e, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|