Skip to content

Instantly share code, notes, and snippets.

@kwiesmueller
Last active May 9, 2019 14:33
Show Gist options
  • Save kwiesmueller/923e98898bd8d477bd1975f54d43b423 to your computer and use it in GitHub Desktop.
Save kwiesmueller/923e98898bd8d477bd1975f54d43b423 to your computer and use it in GitHub Desktop.
put at staging/src/k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager/intfloat_test.go
package fieldmanager
import (
"errors"
"testing"
gojson "encoding/json"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/json"
"sigs.k8s.io/structured-merge-diff/value"
"sigs.k8s.io/yaml"
)
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 {
gv := schema.GroupVersion{
Group: "apps",
Version: "v1",
}
return NewCRDFieldManager(
&fakeObjectConvertor{},
&fakeObjectDefaulter{},
gv,
gv,
)
}
func TestIntFloatBug(t *testing.T) {
f := NewTestFieldManager(t)
y := []byte(`{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "test-svc"
},
"spec": {
"ports": [{
"port": 8080,
"protocol": "UDP"
}]
}
}`)
t.Run("yaml", func(t *testing.T) {
t.Run("object", func(t *testing.T) {
u := &unstructured.Unstructured{Object: map[string]interface{}{}}
if err := yaml.Unmarshal(y, &u.Object); err != nil {
t.Fatal(err)
}
//t.Error(spew.Sprintf("%#v", u))
ty, err := f.typeConverter.ObjectToTyped(u)
if err != nil {
t.Fatal(err)
}
t.Error(loop(t, *ty.AsValue()))
})
t.Run("direct", func(t *testing.T) {
u := &unstructured.Unstructured{Object: map[string]interface{}{}}
if err := yaml.Unmarshal(y, &u); err != nil {
t.Fatal(err)
}
//t.Error(spew.Sprintf("%#v", u))
ty, err := f.typeConverter.ObjectToTyped(u)
if err != nil {
t.Fatal(err)
}
t.Error(loop(t, *ty.AsValue()))
})
})
t.Run("json", func(t *testing.T) {
t.Run("object", func(t *testing.T) {
u := &unstructured.Unstructured{Object: map[string]interface{}{}}
if err := json.Unmarshal(y, &u.Object); err != nil {
t.Fatal(err)
}
//t.Error(spew.Sprintf("%#v", u))
ty, err := f.typeConverter.ObjectToTyped(u)
if err != nil {
t.Fatal(err)
}
t.Error(loop(t, *ty.AsValue()))
})
t.Run("direct", func(t *testing.T) {
u := &unstructured.Unstructured{Object: map[string]interface{}{}}
if err := json.Unmarshal(y, &u); err != nil {
t.Fatal(err)
}
//t.Error(spew.Sprintf("%#v", u))
ty, err := f.typeConverter.ObjectToTyped(u)
if err != nil {
t.Fatal(err)
}
t.Error(loop(t, *ty.AsValue()))
})
})
t.Run("gojson", func(t *testing.T) {
t.Run("object", func(t *testing.T) {
u := &unstructured.Unstructured{Object: map[string]interface{}{}}
if err := gojson.Unmarshal(y, &u.Object); err != nil {
t.Fatal(err)
}
//t.Error(spew.Sprintf("%#v", u))
ty, err := f.typeConverter.ObjectToTyped(u)
if err != nil {
t.Fatal(err)
}
t.Error(loop(t, *ty.AsValue()))
})
t.Run("direct", func(t *testing.T) {
u := &unstructured.Unstructured{Object: map[string]interface{}{}}
if err := json.Unmarshal(y, &u); err != nil {
t.Fatal(err)
}
//t.Error(spew.Sprintf("%#v", u))
ty, err := f.typeConverter.ObjectToTyped(u)
if err != nil {
t.Fatal(err)
}
t.Error(loop(t, *ty.AsValue()))
})
})
}
func loop(t *testing.T, val value.Value) value.Value {
if val.MapValue != nil {
for _, field := range val.MapValue.Items {
ret := loop(t, field.Value)
if ret != null {
return ret
}
}
} else if val.ListValue != nil {
for _, val := range val.ListValue.Items {
ret := loop(t, val)
if ret != null {
return ret
}
}
} else {
if val.IntValue != nil {
t.Errorf("Int: %#v - %v\n", val.IntValue, *val.IntValue)
return val
}
if val.FloatValue != nil {
t.Errorf("Float: %#v - %v\n", val.FloatValue, *val.FloatValue)
return val
}
}
return null
}
var null = value.Value{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment