Skip to content

Instantly share code, notes, and snippets.

@antoineco
Last active October 11, 2018 17:40
Show Gist options
  • Save antoineco/262c7dc7860ddd25ef5cd124d2a8ceb3 to your computer and use it in GitHub Desktop.
Save antoineco/262c7dc7860ddd25ef5cd124d2a8ceb3 to your computer and use it in GitHub Desktop.
Delve issue #1368
[prune]
unused-packages = true
go-tests = true
non-go = true
[[constraint]]
name = "k8s.io/client-go"
branch = "release-7.0"
[[constraint]]
name = "k8s.io/api"
branch = "release-1.10"
[[constraint]]
name = "k8s.io/apimachinery"
branch = "release-1.10"
package main
import (
"github.com/golang/glog"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
clientcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
//_ "k8s.io/client-go/kubernetes/fake"
)
// NewRecorder creates an event Recorder.
func NewRecorder(component string, client kubernetes.Interface) record.EventRecorder {
eventSink := &clientcorev1.EventSinkImpl{
client.CoreV1().Events(corev1.NamespaceAll),
}
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.V(3).Infof)
eventBroadcaster.StartRecordingToSink(eventSink)
return eventBroadcaster.NewRecorder(
scheme.Scheme,
corev1.EventSource{Component: component},
)
}
package main
import (
"context"
"fmt"
"testing"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
)
const defaultTestsTimeout = 2 * time.Second
func TestEvent(t *testing.T) {
const (
ns = "test"
name = "dummy"
)
obj := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
SelfLink: fmt.Sprintf("/api/v1/namespaces/%s/pods/%s", ns, name),
},
}
cli := fake.NewSimpleClientset()
r := NewRecorder("testing", cli)
ctx, cancel := context.WithTimeout(context.Background(), defaultTestsTimeout)
defer cancel()
w, err := cli.EventsV1beta1().Events(ns).Watch(metav1.ListOptions{})
if err != nil {
t.Fatalf("Error obtaining watch interface: %s", err)
}
defer w.Stop()
eventReceived := w.ResultChan()
r.Eventf(obj, corev1.EventTypeNormal, "SomeReason", "Dummy event")
select {
case <-ctx.Done():
t.Error("Timed out waiting for event")
case <-eventReceived:
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment