Skip to content

Commit

Permalink
repro program
Browse files Browse the repository at this point in the history
  • Loading branch information
porridge committed Sep 11, 2023
1 parent 2a553d6 commit 89ae07c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
87 changes: 87 additions & 0 deletions t/retval_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 89ae07c

Please sign in to comment.