Skip to content

Commit 89ae07c

Browse files
committed
repro program
1 parent 2a553d6 commit 89ae07c

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/onsi/gomega v1.27.10
1515
github.com/prometheus/client_golang v1.16.0
1616
github.com/prometheus/client_model v0.4.0
17+
github.com/stretchr/testify v1.8.2
1718
go.uber.org/goleak v1.2.1
1819
go.uber.org/zap v1.25.0
1920
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
@@ -67,6 +68,7 @@ require (
6768
github.com/modern-go/reflect2 v1.0.2 // indirect
6869
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
6970
github.com/pkg/errors v0.9.1 // indirect
71+
github.com/pmezard/go-difflib v1.0.0 // indirect
7072
github.com/prometheus/common v0.44.0 // indirect
7173
github.com/prometheus/procfs v0.10.1 // indirect
7274
github.com/spf13/cobra v1.7.0 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
277277
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
278278
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
279279
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
280+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
280281
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 h1:6fotK7otjonDflCTK0BCfls4SPy3NcCVb5dqqmbRknE=
281282
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8=
282283
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

t/retval_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package t
2+
3+
import (
4+
"context"
5+
"github.com/go-logr/logr"
6+
gmg "github.com/onsi/gomega"
7+
"github.com/stretchr/testify/assert"
8+
"time"
9+
10+
"k8s.io/apimachinery/pkg/api/errors"
11+
"k8s.io/apimachinery/pkg/api/meta"
12+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
13+
"k8s.io/client-go/rest"
14+
"k8s.io/klog/v2/ktesting"
15+
ctrl "sigs.k8s.io/controller-runtime"
16+
"sigs.k8s.io/controller-runtime/pkg/client"
17+
"sigs.k8s.io/controller-runtime/pkg/envtest"
18+
"sigs.k8s.io/controller-runtime/pkg/manager"
19+
20+
"testing"
21+
)
22+
23+
func TestRetValue(t *testing.T) {
24+
ctx, cancel := context.WithCancel(context.Background())
25+
defer cancel()
26+
27+
restCfg, tearDownFn := setupEnvtest(t)
28+
defer tearDownFn(t)
29+
30+
var log = ctrl.Log.WithName("ret-error-demo")
31+
kl := ktesting.NewLogger(t, ktesting.NewConfig())
32+
ctrl.SetLogger(kl)
33+
34+
manager, err := ctrl.NewManager(restCfg, ctrl.Options{})
35+
assert.NoError(t, err)
36+
37+
go func() {
38+
assert.NoError(t, manager.Start(ctx))
39+
}()
40+
41+
keyValid := client.ObjectKey{Name: "a-name", Namespace: "default"}
42+
keyBadNS := client.ObjectKey{Name: "a-name", Namespace: "in/valid"}
43+
44+
report(ctx, manager, keyValid, makeUnstructured("ReplicaSet", "apps/v1"), log)
45+
report(ctx, manager, keyValid, makeUnstructured("Badger", "apps/v1"), log)
46+
report(ctx, manager, keyBadNS, makeUnstructured("Gherkin", "inexistent.group.com/v1"), log)
47+
}
48+
49+
func makeUnstructured(kind string, apiVersion string) *unstructured.Unstructured {
50+
rs := &unstructured.Unstructured{}
51+
rs.SetKind(kind)
52+
rs.SetAPIVersion(apiVersion)
53+
return rs
54+
}
55+
56+
func report(ctx context.Context, manager manager.Manager, key client.ObjectKey, obj *unstructured.Unstructured, log logr.Logger) {
57+
clientCtx, clientCancel := context.WithTimeout(ctx, time.Second)
58+
defer clientCancel()
59+
err := manager.GetClient().Get(clientCtx, key, obj)
60+
if err != nil && errors.IsNotFound(err) {
61+
log.Info("Kind is present but object was not found", "kind", obj.GetObjectKind().GroupVersionKind().Kind)
62+
} else if err != nil && meta.IsNoMatchError(err) {
63+
log.Info("Kind does not exist", "kind", obj.GetObjectKind().GroupVersionKind().Kind)
64+
} else if err != nil {
65+
log.Error(err, "Unexpected error")
66+
}
67+
}
68+
69+
func setupEnvtest(t *testing.T) (*rest.Config, func(t *testing.T)) {
70+
t.Log("Setup envtest")
71+
72+
g := gmg.NewWithT(t)
73+
testEnv := &envtest.Environment{
74+
CRDDirectoryPaths: []string{"testdata"},
75+
}
76+
77+
cfg, err := testEnv.Start()
78+
g.Expect(err).NotTo(gmg.HaveOccurred())
79+
g.Expect(cfg).NotTo(gmg.BeNil())
80+
81+
teardownFunc := func(t *testing.T) {
82+
t.Log("Stop envtest")
83+
g.Expect(testEnv.Stop()).To(gmg.Succeed())
84+
}
85+
86+
return cfg, teardownFunc
87+
}

0 commit comments

Comments
 (0)