Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fieldmanager tests for stripFields #74207

Merged
merged 3 commits into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
Expand Down Expand Up @@ -34,3 +34,16 @@ filegroup(
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

go_test(
name = "go_default_test",
srcs = ["fieldmanager_test.go"],
embed = [":go_default_library"],
deps = [
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fieldmanager_test

import (
"errors"
"testing"
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager"
)

type fakeObjectConvertor struct{}

func (c *fakeObjectConvertor) Convert(in, out, context interface{}) error {
out = in
return nil
}

func (c *fakeObjectConvertor) ConvertToVersion(in runtime.Object, _ runtime.GroupVersioner) (runtime.Object, error) {
return in, nil
}

func (c *fakeObjectConvertor) ConvertFieldLabel(_ schema.GroupVersionKind, _, _ string) (string, string, error) {
return "", "", errors.New("not implemented")
}

type fakeObjectDefaulter struct{}

func (d *fakeObjectDefaulter) Default(in runtime.Object) {}

func NewTestFieldManager(t *testing.T) *fieldmanager.FieldManager {
apelisse marked this conversation as resolved.
Show resolved Hide resolved
gv := schema.GroupVersion{
Group: "apps",
Version: "v1",
}

return fieldmanager.NewCRDFieldManager(
&fakeObjectConvertor{},
&fakeObjectDefaulter{},
gv,
gv,
)
}

func TestFieldManagerCreation(t *testing.T) {
if NewTestFieldManager(t) == nil {
t.Fatal("failed to create FieldManager")
}
}

func TestApplyStripsFields(t *testing.T) {
f := NewTestFieldManager(t)

obj := &corev1.Pod{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if you keep this empty?

ObjectMeta: metav1.ObjectMeta{
Name: "a",
Namespace: "a",
CreationTimestamp: metav1.Time{Time: time.Time{}},
SelfLink: "a",
},
}

newObj, err := f.Apply(obj, []byte(`{
"apiVersion": "v1",
"kind": "Deployment",
"metadata": {
"name": "b",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That really shouldn't happen though (applying with a different name/namespace), it might not matter in this test but that's confusing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, it's my only way of testing if the fields get stripped on changes. Or is there another one?

"namespace": "b",
"creationTimestamp": "2016-05-19T09:59:00Z",
"selfLink": "b",
"uid": "b",
"resourceVersion": "b"
}
}`), false)
if err != nil {
t.Fatalf("failed to apply object: %v", err)
}

accessor, err := meta.Accessor(newObj)
if err != nil {
t.Fatalf("couldn't get accessor: %v", err)
}

if m := accessor.GetManagedFields(); len(m) != 0 {
apelisse marked this conversation as resolved.
Show resolved Hide resolved
t.Fatalf("fields did not get stripped on apply: %v", m)
}
}
func TestApplyDoesNotStripLabels(t *testing.T) {
f := NewTestFieldManager(t)

obj := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "a",
Namespace: "a",
CreationTimestamp: metav1.Time{Time: time.Time{}},
SelfLink: "a",
},
}

newObj, err := f.Apply(obj, []byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"labels": {
"a": "b"
},
}
}`), false)
if err != nil {
t.Fatalf("failed to apply object: %v", err)
}

accessor, err := meta.Accessor(newObj)
if err != nil {
t.Fatalf("couldn't get accessor: %v", err)
}

if m := accessor.GetManagedFields(); len(m) != 1 {
t.Fatalf("labels shouldn't get stripped on apply: %v", m)
}
}