Skip to content

Commit

Permalink
Remove WIP fake client usage
Browse files Browse the repository at this point in the history
  • Loading branch information
stippi2 committed Oct 16, 2024
1 parent 569abd8 commit 38c5b1e
Showing 1 changed file with 1 addition and 54 deletions.
55 changes: 1 addition & 54 deletions replicate/secret/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"bytes"
"context"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/clientcmd"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"sync"
"testing"
Expand All @@ -25,8 +23,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
k8stesting "k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"
)

Expand Down Expand Up @@ -65,55 +61,6 @@ func (pf *PlainFormatter) Format(entry *log.Entry) ([]byte, error) {
return b.Bytes(), nil
}

func incrementResourceVersion(obj metav1.Object) {
rv := obj.GetResourceVersion()
if rv == "" {
obj.SetResourceVersion("1")
log.Debugf("### initialized resource version of %s/%s: %s", obj.GetNamespace(), obj.GetName(), obj.GetResourceVersion())
return
}
if i, err := strconv.Atoi(rv); err == nil {
obj.SetResourceVersion(strconv.Itoa(i + 1))
log.Debugf("### incremented resource version of %s/%s: %s", obj.GetNamespace(), obj.GetName(), obj.GetResourceVersion())
} else {
log.Errorf("### error parsing resource version of %s/%s: %s", obj.GetNamespace(), obj.GetName(), rv)
}
}

// setupFakeClientSet creates a fake.Clientset with a reactor that increments the resource version
func setupFakeClientSet() *fake.Clientset {
fakeClientSet := fake.NewClientset()

fakeClientSet.PrependReactor("*", "*", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
switch a := action.(type) {
case k8stesting.CreateAction:
obj := a.GetObject().(metav1.Object)
incrementResourceVersion(obj)
case k8stesting.UpdateAction:
obj := a.GetObject().(metav1.Object)
incrementResourceVersion(obj)
case k8stesting.PatchAction:
patchImpl, ok := a.(k8stesting.PatchActionImpl)
if !ok {
return false, nil, nil
}
existingObj, err := fakeClientSet.Tracker().Get(patchImpl.Resource, patchImpl.GetNamespace(), patchImpl.Name)
if existingObj == nil {
log.Errorf("### existing object not found: %s/%s", patchImpl.GetNamespace(), patchImpl.Name)
return false, nil, nil
}
incrementResourceVersion(existingObj.(metav1.Object))
err = fakeClientSet.Tracker().Update(patchImpl.Resource, existingObj, patchImpl.GetNamespace())
if err != nil {
return false, nil, err
}
}
return false, nil, nil
})

return fakeClientSet
}

func setupRealClientSet(t *testing.T) *kubernetes.Clientset {
kubeconfig := os.Getenv("KUBECONFIG")
//is KUBECONFIG is not specified try to use the local KUBECONFIG or the in cluster config
Expand Down Expand Up @@ -1343,7 +1290,7 @@ func TestSecretReplicatorSyncByContent(t *testing.T) {
log.SetFormatter(&PlainFormatter{})

prefix := namespacePrefix()
client := setupFakeClientSet()
client := setupRealClientSet(t)
ctx := context.TODO()

repl := NewReplicator(client, 60*time.Second, false, true)
Expand Down

0 comments on commit 38c5b1e

Please sign in to comment.