Skip to content

Commit

Permalink
Restrict the names and namespaces and perform delete operations too
Browse files Browse the repository at this point in the history
  • Loading branch information
wallrj committed Aug 1, 2017
1 parent fa6852f commit a1e2777
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
4 changes: 3 additions & 1 deletion staging/src/k8s.io/client-go/examples/informer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/golang/glog"

"k8s.io/api/core/v1"
"k8s.io/client-go/informers"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -62,9 +63,10 @@ func (c *PodLoggingController) podAdd(obj interface{}) {
}

func (c *PodLoggingController) podUpdate(old, new interface{}) {
newPod := new.(*v1.Pod)
key, err := cache.MetaNamespaceKeyFunc(new)
if err == nil {
glog.Infof("POD UPDATED. %q", key)
glog.Infof("POD UPDATED. %q, %s", key, newPod.Status.Phase)
} else {
glog.Error(err)
}
Expand Down
47 changes: 24 additions & 23 deletions staging/src/k8s.io/client-go/examples/informer/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"fmt"
"math/rand"
"reflect"
"testing"
Expand All @@ -25,6 +26,7 @@ import (

"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/testing/fuzzer"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
Expand All @@ -33,14 +35,25 @@ import (
kapitesting "k8s.io/kubernetes/pkg/api/testing"
)

func podForTest(r *rand.Rand) *v1.Pod {
func podForTest(rand *rand.Rand) *v1.Pod {
apiObjectFuzzer := fuzzer.FuzzerFor(
kapitesting.FuzzerFuncs,
r,
rand,
api.Codecs,
)
var p v1.Pod
apiObjectFuzzer.Fuzz(&p)

p.SetName(fmt.Sprintf("Pod%d", rand.Intn(10)))
p.SetNamespace(fmt.Sprintf("Namespace%d", rand.Intn(10)))
phases := []v1.PodPhase{
"creating",
"starting",
"running",
"stopping",
"deleting",
}
p.Status.Phase = phases[rand.Intn(len(phases))]
p.Spec.InitContainers = nil
p.Status.InitContainerStatuses = nil
return &p
Expand All @@ -64,37 +77,25 @@ func TestMain(t *testing.T) {
t.Error(err)
}

f := func(p *v1.Pod) bool {
// initialLineCount := glog.Stats.Info.Lines()
// for _, pod := range tc.initial {
// fakeWatch.Delete(pod)
// }
// <-time.After(1 * time.Second)
// actualLineCount := glog.Stats.Info.Lines() - initialLineCount
// if tc.expectedLineCount != actualLineCount {
// t.Errorf(
// "Line count mismatch. Expected %d. Got %d",
// tc.expectedLineCount,
// actualLineCount,
// )
// }
fakeWatch.Add(p)
// key, err := cache.MetaNamespaceKeyFunc(p)
// if err == nil {
// t.Log(key)
// } else {
// t.Error(err)
// }
f := func(p *v1.Pod, operation func(runtime.Object)) bool {
operation(p)
return true
}

operations := []func(runtime.Object){
fakeWatch.Add,
fakeWatch.Delete,
}

err = quick.Check(
f,
&quick.Config{
MaxCount: 1000,
Values: func(values []reflect.Value, r *rand.Rand) {
p := podForTest(r)
values[0] = reflect.ValueOf(p)
operation := operations[rand.Intn(len(operations))]
values[1] = reflect.ValueOf(operation)
},
},
)
Expand Down

0 comments on commit a1e2777

Please sign in to comment.