我正在为我的kubernetis Controller 实现测试。
我正在使用NewSimpleClientset。
我想接收一个对象,以查看对象是否通过提示程序正确传递。

我的测试功能看起来像这样

func Test_TestListInformer(t *testing.T) {
    dpl := GenerateHelloWorldDeployment()
    clientSet := fake.NewSimpleClientset(&dpl)
    watchlist := cache.NewListWatchFromClient(clientSet.ExtensionsV1beta1().RESTClient(), "deployments", api_v1.NamespaceAll, fields.Everything())
    var expectedObj interface{}

    resyncPeriod := 30 * time.Minute

    _, eController := cache.NewInformer(
        watchlist,
        &v1beta1.Deployment{},
        resyncPeriod,
        cache.ResourceEventHandlerFuncs{
            AddFunc: func(obj interface{}) {
                expectedObj = obj
            },
            DeleteFunc: func(obj interface{}) {
                expectedObj = obj
            },
            UpdateFunc: func(oldObj interface{}, newObj interface{}) {
                expectedObj = oldObj
            },
        },
    )
    stopChan := make(chan struct{})
    go eController.Run(stopChan)
    time.Sleep(time.Second)
    close(stopChan)


}

它崩溃
E1220 17:06:06.304112   35412 runtime.go:66] Observed a panic: "invalid memory address or nil pointer dereference" (runtime error: invalid memory address or nil pointer dereference)
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/runtime/runtime.go:72
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/runtime/runtime.go:65
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/runtime/runtime.go:51
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/asm_amd64.s:509
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/panic.go:491
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/panic.go:63
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/signal_unix.go:367
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/rest/client.go:222
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/rest/client.go:247
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/tools/cache/listwatch.go:68
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/tools/cache/listwatch.go:101
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/tools/cache/reflector.go:249
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/tools/cache/reflector.go:204
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:133
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:134
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:88
/Users/myuser/dev/go/src/github.com//vendor/k8s.io/client-go/tools/cache/reflector.go:203
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/client-go/tools/cache/controller.go:122
/Users/myuser/dev/go/src/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:54
/Users/myuser/dev/go/srmyrepo/myprojc/github.com/myrepo/myproj/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:71
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/asm_amd64.s:2337

GenerateHelloWorldDeployment 返回的部署对象是好的。
我可以用clientSet.ExtensionsV1beta1().Deployments("").Get("hello-world-deployment",meta_v1.GetOptions{})检索它

为什么告密者崩溃?

最佳答案

弄清楚了。
k8s go lib很难掌握。许多对象和接口(interface)重复它们自己。

你必须用
来自“k8s.io/client-go/tools/cache/testing”的NewFakeControllerSource

dpl := GenerateFakeDeployment()

watchlist := fcache.NewFakeControllerSource()
watchlist.Add(&dpl)

var expectedObj interface{}

resyncPeriod := 30 * time.Minute

_, eController := cache.NewInformer(
    watchlist,
    &v1beta1.Deployment{},
    resyncPeriod,
    cache.ResourceEventHandlerFuncs{
        AddFunc: func(obj interface{}) {
            expectedObj = obj
        },
        DeleteFunc: func(obj interface{}) {
            expectedObj = obj
        },
        UpdateFunc: func(oldObj interface{}, newObj interface{}) {
            expectedObj = oldObj
        },
    },
)
stopChan := make(chan struct{})
go eController.Run(stopChan)

10-06 04:44