Generate code

Kubernetes-commit: 7772769d19a82a26aa91181e0804ff2ccbdd843c
This commit is contained in:
Joe Betz
2024-06-24 15:58:35 -04:00
committed by Kubernetes Publisher
parent 2c866525dd
commit c4145a9c20
133 changed files with 1210 additions and 1080 deletions

View File

@@ -21,6 +21,7 @@ package fake
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
applyconfigurations "k8s.io/client-go/applyconfigurations"
"k8s.io/client-go/discovery"
fakediscovery "k8s.io/client-go/discovery/fake"
clientset "k8s.io/client-go/kubernetes"
@@ -133,8 +134,12 @@ import (
// NewSimpleClientset returns a clientset that will respond with the provided objects.
// It's backed by a very simple object tracker that processes creates, updates and deletions as-is,
// without applying any validations and/or defaults. It shouldn't be considered a replacement
// without applying any field management, validations and/or defaults. It shouldn't be considered a replacement
// for a real clientset and is mostly useful in simple unit tests.
//
// DEPRECATED: NewClientset replaces this with support for field management, which significantly improves
// server side apply testing. NewClientset is only available when apply configurations are generated (e.g.
// via --with-applyconfig).
func NewSimpleClientset(objects ...runtime.Object) *Clientset {
o := testing.NewObjectTracker(scheme, codecs.UniversalDecoder())
for _, obj := range objects {
@@ -176,6 +181,38 @@ func (c *Clientset) Tracker() testing.ObjectTracker {
return c.tracker
}
// NewClientset returns a clientset that will respond with the provided objects.
// It's backed by a very simple object tracker that processes creates, updates and deletions as-is,
// without applying any validations and/or defaults. It shouldn't be considered a replacement
// for a real clientset and is mostly useful in simple unit tests.
func NewClientset(objects ...runtime.Object) *Clientset {
o := testing.NewFieldManagedObjectTracker(
scheme,
codecs.UniversalDecoder(),
applyconfigurations.NewTypeConverter(scheme),
)
for _, obj := range objects {
if err := o.Add(obj); err != nil {
panic(err)
}
}
cs := &Clientset{tracker: o}
cs.discovery = &fakediscovery.FakeDiscovery{Fake: &cs.Fake}
cs.AddReactor("*", "*", testing.ObjectReaction(o))
cs.AddWatchReactor("*", func(action testing.Action) (handled bool, ret watch.Interface, err error) {
gvr := action.GetResource()
ns := action.GetNamespace()
watch, err := o.Watch(gvr, ns)
if err != nil {
return false, nil, err
}
return true, watch, nil
})
return cs
}
var (
_ clientset.Interface = &Clientset{}
_ testing.FakeClient = &Clientset{}

View File

@@ -18,10 +18,13 @@ package fake
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
v1 "k8s.io/api/core/v1"
policy "k8s.io/api/policy/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
v1ac "k8s.io/client-go/applyconfigurations/core/v1"
)
func TestNewSimpleClientset(t *testing.T) {
@@ -56,3 +59,86 @@ func TestNewSimpleClientset(t *testing.T) {
t.Logf("TestNewSimpleClientset() res = %v", pods)
}
}
func TestManagedFieldClientset(t *testing.T) {
client := NewClientset()
name := "pod-1"
namespace := "default"
cm, err := client.CoreV1().ConfigMaps("default").Create(context.Background(),
&v1.ConfigMap{
ObjectMeta: meta_v1.ObjectMeta{Name: name, Namespace: namespace},
Data: map[string]string{"k0": "v0"},
}, meta_v1.CreateOptions{FieldManager: "test-manager-0"})
if err != nil {
t.Errorf("Failed to create pod: %v", err)
}
assert.Equal(t, map[string]string{"k0": "v0"}, cm.Data)
// Apply with test-manager-1
// Expect data to be shared with initial create
cm, err = client.CoreV1().ConfigMaps("default").Apply(context.Background(),
v1ac.ConfigMap(name, namespace).WithData(map[string]string{"k1": "v1"}),
meta_v1.ApplyOptions{FieldManager: "test-manager-1"})
if err != nil {
t.Errorf("Failed to create pod: %v", err)
}
assert.Equal(t, map[string]string{"k0": "v0", "k1": "v1"}, cm.Data)
// Apply conflicting with test-manager-2, expect apply to fail
_, err = client.CoreV1().ConfigMaps("default").Apply(context.Background(),
v1ac.ConfigMap(name, namespace).WithData(map[string]string{"k1": "xyz"}),
meta_v1.ApplyOptions{FieldManager: "test-manager-2"})
if assert.Error(t, err) {
assert.Equal(t, "Apply failed with 1 conflict: conflict with \"test-manager-1\": .data.k1", err.Error())
}
// Apply with test-manager-2
// Expect data to be shared with initial create and test-manager-1
cm, err = client.CoreV1().ConfigMaps("default").Apply(context.Background(),
v1ac.ConfigMap(name, namespace).WithData(map[string]string{"k2": "v2"}),
meta_v1.ApplyOptions{FieldManager: "test-manager-2"})
if err != nil {
t.Errorf("Failed to create pod: %v", err)
}
assert.Equal(t, map[string]string{"k0": "v0", "k1": "v1", "k2": "v2"}, cm.Data)
// Apply with test-manager-1
// Expect owned data to be updated
cm, err = client.CoreV1().ConfigMaps("default").Apply(context.Background(),
v1ac.ConfigMap(name, namespace).WithData(map[string]string{"k1": "v101"}),
meta_v1.ApplyOptions{FieldManager: "test-manager-1"})
if err != nil {
t.Errorf("Failed to create pod: %v", err)
}
assert.Equal(t, map[string]string{"k0": "v0", "k1": "v101", "k2": "v2"}, cm.Data)
// Force apply with test-manager-2
// Expect data owned by test-manager-1 to be updated, expect data already owned but not in apply configuration to be removed
cm, err = client.CoreV1().ConfigMaps("default").Apply(context.Background(),
v1ac.ConfigMap(name, namespace).WithData(map[string]string{"k1": "v202"}),
meta_v1.ApplyOptions{FieldManager: "test-manager-2", Force: true})
if err != nil {
t.Errorf("Failed to create pod: %v", err)
}
assert.Equal(t, map[string]string{"k0": "v0", "k1": "v202"}, cm.Data)
// Update with test-manager-1 to perform a force update of the entire resource
cm, err = client.CoreV1().ConfigMaps("default").Update(context.Background(),
&v1.ConfigMap{
TypeMeta: meta_v1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: meta_v1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Data: map[string]string{
"k99": "v99",
},
}, meta_v1.UpdateOptions{FieldManager: "test-manager-0"})
if err != nil {
t.Errorf("Failed to update pod: %v", err)
}
assert.Equal(t, map[string]string{"k99": "v99"}, cm.Data)
}