123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- /*
- Copyright 2017 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 util
- import (
- "bytes"
- "reflect"
- "sort"
- "testing"
- corev1 "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/apimachinery/pkg/runtime/schema"
- "k8s.io/apimachinery/pkg/runtime/serializer"
- kubeadmapiv1beta2 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta2"
- "k8s.io/kubernetes/cmd/kubeadm/app/constants"
- )
- var files = map[string][]byte{
- "foo": []byte(`
- kind: Foo
- apiVersion: foo.k8s.io/v1
- fooField: foo
- `),
- "bar": []byte(`
- apiVersion: bar.k8s.io/v2
- barField: bar
- kind: Bar
- `),
- "baz": []byte(`
- apiVersion: baz.k8s.io/v1
- kind: Baz
- baz:
- foo: bar
- `),
- "nokind": []byte(`
- apiVersion: baz.k8s.io/v1
- foo: foo
- bar: bar
- `),
- "noapiversion": []byte(`
- kind: Bar
- foo: foo
- bar: bar
- `),
- }
- func TestMarshalUnmarshalYaml(t *testing.T) {
- pod := &corev1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- Name: "someName",
- Namespace: "testNamespace",
- Labels: map[string]string{
- "test": "yes",
- },
- },
- Spec: corev1.PodSpec{
- RestartPolicy: corev1.RestartPolicyAlways,
- },
- }
- bytes, err := MarshalToYaml(pod, corev1.SchemeGroupVersion)
- if err != nil {
- t.Fatalf("unexpected error marshalling: %v", err)
- }
- t.Logf("\n%s", bytes)
- obj2, err := UnmarshalFromYaml(bytes, corev1.SchemeGroupVersion)
- if err != nil {
- t.Fatalf("unexpected error marshalling: %v", err)
- }
- pod2, ok := obj2.(*corev1.Pod)
- if !ok {
- t.Fatal("did not get a Pod")
- }
- if pod2.Name != pod.Name {
- t.Errorf("expected %q, got %q", pod.Name, pod2.Name)
- }
- if pod2.Namespace != pod.Namespace {
- t.Errorf("expected %q, got %q", pod.Namespace, pod2.Namespace)
- }
- if !reflect.DeepEqual(pod2.Labels, pod.Labels) {
- t.Errorf("expected %v, got %v", pod.Labels, pod2.Labels)
- }
- if pod2.Spec.RestartPolicy != pod.Spec.RestartPolicy {
- t.Errorf("expected %q, got %q", pod.Spec.RestartPolicy, pod2.Spec.RestartPolicy)
- }
- }
- func TestMarshalUnmarshalToYamlForCodecs(t *testing.T) {
- cfg := &kubeadmapiv1beta2.InitConfiguration{
- TypeMeta: metav1.TypeMeta{
- Kind: constants.InitConfigurationKind,
- APIVersion: kubeadmapiv1beta2.SchemeGroupVersion.String(),
- },
- NodeRegistration: kubeadmapiv1beta2.NodeRegistrationOptions{
- Name: "testNode",
- CRISocket: "/var/run/cri.sock",
- },
- BootstrapTokens: []kubeadmapiv1beta2.BootstrapToken{
- {
- Token: &kubeadmapiv1beta2.BootstrapTokenString{ID: "abcdef", Secret: "abcdef0123456789"},
- },
- },
- // NOTE: Using MarshalToYamlForCodecs and UnmarshalFromYamlForCodecs for ClusterConfiguration fields here won't work
- // by design. This is because we have a `json:"-"` annotation in order to avoid struct duplication. See the comment
- // at the kubeadmapiv1beta2.InitConfiguration definition.
- }
- kubeadmapiv1beta2.SetDefaults_InitConfiguration(cfg)
- scheme := runtime.NewScheme()
- if err := kubeadmapiv1beta2.AddToScheme(scheme); err != nil {
- t.Fatal(err)
- }
- codecs := serializer.NewCodecFactory(scheme)
- bytes, err := MarshalToYamlForCodecs(cfg, kubeadmapiv1beta2.SchemeGroupVersion, codecs)
- if err != nil {
- t.Fatalf("unexpected error marshalling InitConfiguration: %v", err)
- }
- t.Logf("\n%s", bytes)
- obj, err := UnmarshalFromYamlForCodecs(bytes, kubeadmapiv1beta2.SchemeGroupVersion, codecs)
- if err != nil {
- t.Fatalf("unexpected error unmarshalling InitConfiguration: %v", err)
- }
- cfg2, ok := obj.(*kubeadmapiv1beta2.InitConfiguration)
- if !ok || cfg2 == nil {
- t.Fatal("did not get InitConfiguration back")
- }
- if !reflect.DeepEqual(*cfg, *cfg2) {
- t.Errorf("expected %v, got %v", *cfg, *cfg2)
- }
- }
- func TestSplitYAMLDocuments(t *testing.T) {
- var tests = []struct {
- name string
- fileContents []byte
- gvkmap map[schema.GroupVersionKind][]byte
- expectedErr bool
- }{
- {
- name: "FooOnly",
- fileContents: files["foo"],
- gvkmap: map[schema.GroupVersionKind][]byte{
- {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"}: files["foo"],
- },
- },
- {
- name: "FooBar",
- fileContents: bytes.Join([][]byte{files["foo"], files["bar"]}, []byte(constants.YAMLDocumentSeparator)),
- gvkmap: map[schema.GroupVersionKind][]byte{
- {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"}: files["foo"],
- {Group: "bar.k8s.io", Version: "v2", Kind: "Bar"}: files["bar"],
- },
- },
- {
- name: "FooTwiceInvalid",
- fileContents: bytes.Join([][]byte{files["foo"], files["bar"], files["foo"]}, []byte(constants.YAMLDocumentSeparator)),
- expectedErr: true,
- },
- {
- name: "InvalidBaz",
- fileContents: bytes.Join([][]byte{files["foo"], files["baz"]}, []byte(constants.YAMLDocumentSeparator)),
- expectedErr: true,
- },
- {
- name: "InvalidNoKind",
- fileContents: files["nokind"],
- expectedErr: true,
- },
- {
- name: "InvalidNoAPIVersion",
- fileContents: files["noapiversion"],
- expectedErr: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t2 *testing.T) {
- gvkmap, err := SplitYAMLDocuments(rt.fileContents)
- if (err != nil) != rt.expectedErr {
- t2.Errorf("expected error: %t, actual: %t", rt.expectedErr, err != nil)
- }
- if !reflect.DeepEqual(gvkmap, rt.gvkmap) {
- t2.Errorf("expected gvkmap: %s\n\tactual: %s\n", rt.gvkmap, gvkmap)
- }
- })
- }
- }
- func TestGroupVersionKindsFromBytes(t *testing.T) {
- var tests = []struct {
- name string
- fileContents []byte
- gvks []string
- expectedErr bool
- }{
- {
- name: "FooOnly",
- fileContents: files["foo"],
- gvks: []string{
- "foo.k8s.io/v1, Kind=Foo",
- },
- },
- {
- name: "FooBar",
- fileContents: bytes.Join([][]byte{files["foo"], files["bar"]}, []byte(constants.YAMLDocumentSeparator)),
- gvks: []string{
- "foo.k8s.io/v1, Kind=Foo",
- "bar.k8s.io/v2, Kind=Bar",
- },
- },
- {
- name: "FooTwiceInvalid",
- fileContents: bytes.Join([][]byte{files["foo"], files["bar"], files["foo"]}, []byte(constants.YAMLDocumentSeparator)),
- gvks: []string{},
- expectedErr: true,
- },
- {
- name: "InvalidBaz",
- fileContents: bytes.Join([][]byte{files["foo"], files["baz"]}, []byte(constants.YAMLDocumentSeparator)),
- gvks: []string{},
- expectedErr: true,
- },
- {
- name: "InvalidNoKind",
- fileContents: files["nokind"],
- gvks: []string{},
- expectedErr: true,
- },
- {
- name: "InvalidNoAPIVersion",
- fileContents: files["noapiversion"],
- gvks: []string{},
- expectedErr: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t2 *testing.T) {
- gvks, err := GroupVersionKindsFromBytes(rt.fileContents)
- if (err != nil) != rt.expectedErr {
- t2.Errorf("expected error: %t, actual: %t", rt.expectedErr, err != nil)
- }
- strgvks := []string{}
- for _, gvk := range gvks {
- strgvks = append(strgvks, gvk.String())
- }
- sort.Strings(strgvks)
- sort.Strings(rt.gvks)
- if !reflect.DeepEqual(strgvks, rt.gvks) {
- t2.Errorf("expected gvks: %s\n\tactual: %s\n", rt.gvks, strgvks)
- }
- })
- }
- }
- func TestGroupVersionKindsHasKind(t *testing.T) {
- var tests = []struct {
- name string
- gvks []schema.GroupVersionKind
- kind string
- expected bool
- }{
- {
- name: "FooOnly",
- gvks: []schema.GroupVersionKind{
- {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"},
- },
- kind: "Foo",
- expected: true,
- },
- {
- name: "FooBar",
- gvks: []schema.GroupVersionKind{
- {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"},
- {Group: "bar.k8s.io", Version: "v2", Kind: "Bar"},
- },
- kind: "Bar",
- expected: true,
- },
- {
- name: "FooBazNoBaz",
- gvks: []schema.GroupVersionKind{
- {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"},
- {Group: "bar.k8s.io", Version: "v2", Kind: "Bar"},
- },
- kind: "Baz",
- expected: false,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t2 *testing.T) {
- actual := GroupVersionKindsHasKind(rt.gvks, rt.kind)
- if rt.expected != actual {
- t2.Errorf("expected gvks has kind: %t\n\tactual: %t\n", rt.expected, actual)
- }
- })
- }
- }
- func TestGroupVersionKindsHasInitConfiguration(t *testing.T) {
- var tests = []struct {
- name string
- gvks []schema.GroupVersionKind
- kind string
- expected bool
- }{
- {
- name: "NoInitConfiguration",
- gvks: []schema.GroupVersionKind{
- {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"},
- },
- expected: false,
- },
- {
- name: "InitConfigurationFound",
- gvks: []schema.GroupVersionKind{
- {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"},
- {Group: "bar.k8s.io", Version: "v2", Kind: "InitConfiguration"},
- },
- expected: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t2 *testing.T) {
- actual := GroupVersionKindsHasInitConfiguration(rt.gvks...)
- if rt.expected != actual {
- t2.Errorf("expected gvks has InitConfiguration: %t\n\tactual: %t\n", rt.expected, actual)
- }
- })
- }
- }
- func TestGroupVersionKindsHasJoinConfiguration(t *testing.T) {
- var tests = []struct {
- name string
- gvks []schema.GroupVersionKind
- kind string
- expected bool
- }{
- {
- name: "NoJoinConfiguration",
- gvks: []schema.GroupVersionKind{
- {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"},
- },
- expected: false,
- },
- {
- name: "JoinConfigurationFound",
- gvks: []schema.GroupVersionKind{
- {Group: "foo.k8s.io", Version: "v1", Kind: "Foo"},
- {Group: "bar.k8s.io", Version: "v2", Kind: "JoinConfiguration"},
- },
- expected: true,
- },
- }
- for _, rt := range tests {
- t.Run(rt.name, func(t2 *testing.T) {
- actual := GroupVersionKindsHasJoinConfiguration(rt.gvks...)
- if rt.expected != actual {
- t2.Errorf("expected gvks has JoinConfiguration: %t\n\tactual: %t\n", rt.expected, actual)
- }
- })
- }
- }
|