From 89ae07ce10bad2f4d1c73f03df025c59438208b2 Mon Sep 17 00:00:00 2001 From: Marcin Owsiany Date: Mon, 11 Sep 2023 12:23:21 +0200 Subject: [PATCH] repro program --- go.mod | 2 ++ go.sum | 1 + t/retval_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 t/retval_test.go diff --git a/go.mod b/go.mod index 4bf765f737..c656f9438e 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/onsi/gomega v1.27.10 github.com/prometheus/client_golang v1.16.0 github.com/prometheus/client_model v0.4.0 + github.com/stretchr/testify v1.8.2 go.uber.org/goleak v1.2.1 go.uber.org/zap v1.25.0 golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e @@ -67,6 +68,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/common v0.44.0 // indirect github.com/prometheus/procfs v0.10.1 // indirect github.com/spf13/cobra v1.7.0 // indirect diff --git a/go.sum b/go.sum index 363330dbba..78bafc5f86 100644 --- a/go.sum +++ b/go.sum @@ -277,6 +277,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 h1:6fotK7otjonDflCTK0BCfls4SPy3NcCVb5dqqmbRknE= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/t/retval_test.go b/t/retval_test.go new file mode 100644 index 0000000000..ae603860b8 --- /dev/null +++ b/t/retval_test.go @@ -0,0 +1,87 @@ +package t + +import ( + "context" + "github.com/go-logr/logr" + gmg "github.com/onsi/gomega" + "github.com/stretchr/testify/assert" + "time" + + "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/client-go/rest" + "k8s.io/klog/v2/ktesting" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/envtest" + "sigs.k8s.io/controller-runtime/pkg/manager" + + "testing" +) + +func TestRetValue(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + restCfg, tearDownFn := setupEnvtest(t) + defer tearDownFn(t) + + var log = ctrl.Log.WithName("ret-error-demo") + kl := ktesting.NewLogger(t, ktesting.NewConfig()) + ctrl.SetLogger(kl) + + manager, err := ctrl.NewManager(restCfg, ctrl.Options{}) + assert.NoError(t, err) + + go func() { + assert.NoError(t, manager.Start(ctx)) + }() + + keyValid := client.ObjectKey{Name: "a-name", Namespace: "default"} + keyBadNS := client.ObjectKey{Name: "a-name", Namespace: "in/valid"} + + report(ctx, manager, keyValid, makeUnstructured("ReplicaSet", "apps/v1"), log) + report(ctx, manager, keyValid, makeUnstructured("Badger", "apps/v1"), log) + report(ctx, manager, keyBadNS, makeUnstructured("Gherkin", "inexistent.group.com/v1"), log) +} + +func makeUnstructured(kind string, apiVersion string) *unstructured.Unstructured { + rs := &unstructured.Unstructured{} + rs.SetKind(kind) + rs.SetAPIVersion(apiVersion) + return rs +} + +func report(ctx context.Context, manager manager.Manager, key client.ObjectKey, obj *unstructured.Unstructured, log logr.Logger) { + clientCtx, clientCancel := context.WithTimeout(ctx, time.Second) + defer clientCancel() + err := manager.GetClient().Get(clientCtx, key, obj) + if err != nil && errors.IsNotFound(err) { + log.Info("Kind is present but object was not found", "kind", obj.GetObjectKind().GroupVersionKind().Kind) + } else if err != nil && meta.IsNoMatchError(err) { + log.Info("Kind does not exist", "kind", obj.GetObjectKind().GroupVersionKind().Kind) + } else if err != nil { + log.Error(err, "Unexpected error") + } +} + +func setupEnvtest(t *testing.T) (*rest.Config, func(t *testing.T)) { + t.Log("Setup envtest") + + g := gmg.NewWithT(t) + testEnv := &envtest.Environment{ + CRDDirectoryPaths: []string{"testdata"}, + } + + cfg, err := testEnv.Start() + g.Expect(err).NotTo(gmg.HaveOccurred()) + g.Expect(cfg).NotTo(gmg.BeNil()) + + teardownFunc := func(t *testing.T) { + t.Log("Stop envtest") + g.Expect(testEnv.Stop()).To(gmg.Succeed()) + } + + return cfg, teardownFunc +}