| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package scheme |
| 18 |
|
| 19 |
import ( |
| 20 |
"net/url" |
| 21 |
"reflect" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/google/go-cmp/cmp" |
| 25 |
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" |
| 26 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 |
) |
| 28 |
|
| 29 |
func TestListOptions(t *testing.T) { |
| 30 |
// verify round trip conversion |
| 31 |
ten := int64(10) |
| 32 |
in := &metav1.ListOptions{ |
| 33 |
LabelSelector: "a=1", |
| 34 |
FieldSelector: "b=1", |
| 35 |
ResourceVersion: "10", |
| 36 |
TimeoutSeconds: &ten, |
| 37 |
Watch: true, |
| 38 |
} |
| 39 |
out := &metainternalversion.ListOptions{} |
| 40 |
if err := Scheme.Convert(in, out, nil); err != nil { |
| 41 |
t.Fatal(err) |
| 42 |
} |
| 43 |
actual := &metav1.ListOptions{} |
| 44 |
if err := Scheme.Convert(out, actual, nil); err != nil { |
| 45 |
t.Fatal(err) |
| 46 |
} |
| 47 |
if !reflect.DeepEqual(in, actual) { |
| 48 |
t.Errorf("unexpected: %s", cmp.Diff(in, actual)) |
| 49 |
} |
| 50 |
|
| 51 |
// verify failing conversion |
| 52 |
for i, failingObject := range []*metav1.ListOptions{ |
| 53 |
{LabelSelector: "a!!!"}, |
| 54 |
{FieldSelector: "a!!!"}, |
| 55 |
} { |
| 56 |
out = &metainternalversion.ListOptions{} |
| 57 |
if err := Scheme.Convert(failingObject, out, nil); err == nil { |
| 58 |
t.Errorf("%d: unexpected conversion: %#v", i, out) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// verify kind registration |
| 63 |
if gvks, unversioned, err := Scheme.ObjectKinds(in); err != nil || unversioned || gvks[0] != metav1.SchemeGroupVersion.WithKind("ListOptions") { |
| 64 |
t.Errorf("unexpected: %v %v %v", gvks[0], unversioned, err) |
| 65 |
} |
| 66 |
if gvks, unversioned, err := Scheme.ObjectKinds(out); err != nil || unversioned || gvks[0] != metainternalversion.SchemeGroupVersion.WithKind("ListOptions") { |
| 67 |
t.Errorf("unexpected: %v %v %v", gvks[0], unversioned, err) |
| 68 |
} |
| 69 |
|
| 70 |
actual = &metav1.ListOptions{} |
| 71 |
if err := ParameterCodec.DecodeParameters(url.Values{"watch": []string{"1"}}, metav1.SchemeGroupVersion, actual); err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
if !actual.Watch { |
| 75 |
t.Errorf("unexpected watch decode: %#v", actual) |
| 76 |
} |
| 77 |
|
| 78 |
// check ParameterCodec |
| 79 |
query, err := ParameterCodec.EncodeParameters(in, metav1.SchemeGroupVersion) |
| 80 |
if err != nil { |
| 81 |
t.Fatal(err) |
| 82 |
} |
| 83 |
actual = &metav1.ListOptions{} |
| 84 |
if err := ParameterCodec.DecodeParameters(query, metav1.SchemeGroupVersion, actual); err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
if !reflect.DeepEqual(in, actual) { |
| 88 |
t.Errorf("unexpected: %s", cmp.Diff(in, actual)) |
| 89 |
} |
| 90 |
} |