Skip to content

Commit

Permalink
Use correct clients
Browse files Browse the repository at this point in the history
  • Loading branch information
jveski committed Nov 21, 2023
1 parent 1f16ea2 commit 361fc7e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
53 changes: 53 additions & 0 deletions internal/controllers/reconciliation/discoverycache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package reconciliation

import (
"context"
"testing"

openapi_v2 "github.com/google/gnostic-models/openapiv2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery/fake"
)

func TestDiscoveryCacheTypeMissingInitially(t *testing.T) {
client := &fakeDiscovery{}
d := &discoveryCache{client: client}

gvk := schema.GroupVersionKind{
Group: "test-group",
Version: "test-version",
Kind: "TestKind1",
}

t.Run("missing from spec", func(t *testing.T) {
s, err := d.Get(context.Background(), gvk)
require.EqualError(t, err, "resource was not found in openapi spec")
assert.Nil(t, s)
})

t.Run("added to spec after initial cache fill", func(t *testing.T) {
client.Schema = &openapi_v2.Document{} // TODO

s, err := d.Get(context.Background(), gvk)
require.NoError(t, err)
assert.NotNil(t, s)
})

t.Run("cache hit", func(t *testing.T) {
s, err := d.Get(context.Background(), gvk)
require.NoError(t, err)
assert.NotNil(t, s)
})
}

// the fake.FakeDiscovery doesn't allow fake OpenAPISchema return values.
type fakeDiscovery struct {
fake.FakeDiscovery
Schema *openapi_v2.Document
}

func (f *fakeDiscovery) OpenAPISchema() (*openapi_v2.Document, error) {
return f.Schema, nil
}
32 changes: 17 additions & 15 deletions internal/controllers/reconciliation/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
func TestControllerPodBasics(t *testing.T) {
ctx := testutil.NewContext(t)
mgr := testutil.NewManager(t)
cli := mgr.GetClient()
upstream := mgr.GetClient()
downstream := mgr.DownstreamClient

require.NoError(t, synthesis.NewRolloutController(mgr.Manager, time.Millisecond))
require.NoError(t, synthesis.NewStatusController(mgr.Manager))
Expand Down Expand Up @@ -85,39 +86,39 @@ func TestControllerPodBasics(t *testing.T) {
syn := &apiv1.Synthesizer{}
syn.Name = "test-syn"
syn.Spec.Image = "create"
require.NoError(t, cli.Create(ctx, syn))
require.NoError(t, upstream.Create(ctx, syn))

comp := &apiv1.Composition{}
comp.Name = "test-comp"
comp.Namespace = "default"
comp.Spec.Synthesizer.Name = syn.Name
require.NoError(t, cli.Create(ctx, comp))
require.NoError(t, upstream.Create(ctx, comp))

t.Run("creation", func(t *testing.T) {
testutil.Eventually(t, func() bool {
pod := &corev1.Pod{}
pod.Name = "test-pod"
pod.Namespace = "default"
return cli.Get(ctx, client.ObjectKeyFromObject(pod), pod) == nil
return downstream.Get(ctx, client.ObjectKeyFromObject(pod), pod) == nil
})
})

// we expect this to use strategic merge
t.Run("update", func(t *testing.T) {
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
if err := cli.Get(ctx, client.ObjectKeyFromObject(syn), syn); err != nil {
if err := downstream.Get(ctx, client.ObjectKeyFromObject(syn), syn); err != nil {
return err
}
syn.Spec.Image = "update"
return cli.Update(ctx, syn)
return downstream.Update(ctx, syn)
})
require.NoError(t, err)

testutil.Eventually(t, func() bool {
pod := &corev1.Pod{}
pod.Name = "test-pod"
pod.Namespace = "default"
require.NoError(t, cli.Get(ctx, client.ObjectKeyFromObject(pod), pod))
require.NoError(t, downstream.Get(ctx, client.ObjectKeyFromObject(pod), pod))
return pod.Spec.Containers[0].Image == "test-image-2"
})
})
Expand All @@ -126,7 +127,8 @@ func TestControllerPodBasics(t *testing.T) {
func TestControllerCRBasics(t *testing.T) {
ctx := testutil.NewContext(t)
mgr := testutil.NewManager(t)
cli := mgr.GetClient()
upstream := mgr.GetClient()
downstream := mgr.DownstreamClient

require.NoError(t, synthesis.NewRolloutController(mgr.Manager, time.Millisecond))
require.NoError(t, synthesis.NewStatusController(mgr.Manager))
Expand Down Expand Up @@ -179,45 +181,45 @@ func TestControllerCRBasics(t *testing.T) {
return []*apiv1.ResourceSlice{slice}
})

require.NoError(t, New(rm, mgr.RestConfig))
require.NoError(t, New(rm, mgr.DownstreamRestConfig))
mgr.Start(t)

syn := &apiv1.Synthesizer{}
syn.Name = "test-syn"
syn.Spec.Image = "create"
require.NoError(t, cli.Create(ctx, syn))
require.NoError(t, upstream.Create(ctx, syn))

comp := &apiv1.Composition{}
comp.Name = "test-comp"
comp.Namespace = "default"
comp.Spec.Synthesizer.Name = syn.Name
require.NoError(t, cli.Create(ctx, comp))
require.NoError(t, upstream.Create(ctx, comp))

t.Run("creation", func(t *testing.T) {
testutil.Eventually(t, func() bool {
cr := &testv1.TestResource{}
cr.Name = "test-resource"
cr.Namespace = "default"
return cli.Get(ctx, client.ObjectKeyFromObject(cr), cr) == nil
return downstream.Get(ctx, client.ObjectKeyFromObject(cr), cr) == nil
})
})

// we do not expect this to use strategic merge because CRs do not support it
t.Run("update", func(t *testing.T) {
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
if err := cli.Get(ctx, client.ObjectKeyFromObject(syn), syn); err != nil {
if err := downstream.Get(ctx, client.ObjectKeyFromObject(syn), syn); err != nil {
return err
}
syn.Spec.Image = "update"
return cli.Update(ctx, syn)
return downstream.Update(ctx, syn)
})
require.NoError(t, err)

testutil.Eventually(t, func() bool {
cr := &testv1.TestResource{}
cr.Name = "test-resource"
cr.Namespace = "default"
require.NoError(t, cli.Get(ctx, client.ObjectKeyFromObject(cr), cr))
require.NoError(t, downstream.Get(ctx, client.ObjectKeyFromObject(cr), cr))
return len(cr.Spec.Values) == 2 && cr.Spec.Values[0].Int == 234 && cr.Spec.Values[1].Int == 345
})
})
Expand Down
7 changes: 6 additions & 1 deletion internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func NewManager(t *testing.T) *Manager {
Manager: mgr,
RestConfig: cfg,
DownstreamRestConfig: cfg, // possible override below
DownstreamClient: mgr.GetClient(),
}

dir := os.Getenv("DOWNSTREAM_KUBEBUILDER_ASSETS")
Expand Down Expand Up @@ -136,6 +137,9 @@ func NewManager(t *testing.T) *Manager {
m.DownstreamRestConfig, err = downstreamEnv.Start()
require.NoError(t, err)

m.DownstreamClient, err = client.New(m.DownstreamRestConfig, client.Options{})
require.NoError(t, err)

// Log apiserver version
disc, err := discovery.NewDiscoveryClientForConfig(m.DownstreamRestConfig)
if err == nil {
Expand Down Expand Up @@ -165,7 +169,8 @@ func NewManager(t *testing.T) *Manager {
type Manager struct {
ctrl.Manager
RestConfig *rest.Config
DownstreamRestConfig *rest.Config // may or may not == RestConfig
DownstreamRestConfig *rest.Config // may or may not == RestConfig
DownstreamClient client.Client // may or may not == Manager.GetClient()
}

func (m *Manager) Start(t *testing.T) {
Expand Down

0 comments on commit 361fc7e

Please sign in to comment.