mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-05 17:10:27 +00:00
Generate code
Kubernetes-commit: 7772769d19a82a26aa91181e0804ff2ccbdd843c
This commit is contained in:
committed by
Kubernetes Publisher
parent
2c866525dd
commit
c4145a9c20
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user