|
| 1 | +// Copyright 2021 OnMetal authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package kustomizeutils |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/onmetal/controller-utils/testdata" |
| 19 | + . "github.com/onsi/ginkgo" |
| 20 | + . "github.com/onsi/gomega" |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 24 | + "k8s.io/client-go/kubernetes/scheme" |
| 25 | + "sigs.k8s.io/kustomize/api/hasher" |
| 26 | + "sigs.k8s.io/kustomize/api/resmap" |
| 27 | + "sigs.k8s.io/kustomize/api/resource" |
| 28 | + "sigs.k8s.io/kustomize/api/types" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = Describe("Kustomizeutils", func() { |
| 32 | + Describe("BuildKustomization", func() { |
| 33 | + It("should build the kustomization", func() { |
| 34 | + resMap, err := BuildKustomization(types.Kustomization{ |
| 35 | + ConfigMapGenerator: []types.ConfigMapArgs{ |
| 36 | + { |
| 37 | + GeneratorArgs: types.GeneratorArgs{ |
| 38 | + Name: "my-config", |
| 39 | + KvPairSources: types.KvPairSources{ |
| 40 | + LiteralSources: []string{"foo=bar"}, |
| 41 | + }, |
| 42 | + Options: &types.GeneratorOptions{ |
| 43 | + DisableNameSuffixHash: true, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }) |
| 49 | + Expect(err).NotTo(HaveOccurred()) |
| 50 | + Expect(resMap.Size()).To(Equal(1)) |
| 51 | + resources := resMap.Resources() |
| 52 | + Expect(resources).To(HaveLen(1)) |
| 53 | + resource := resources[0] |
| 54 | + data, err := resource.AsYAML() |
| 55 | + Expect(err).NotTo(HaveOccurred()) |
| 56 | + Expect(string(data)).To(Equal(testdata.ConfigMapYAML)) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + Describe("BuildKustomizationIntoList", func() { |
| 61 | + It("should build the kustomization into a list", func() { |
| 62 | + list := &corev1.ConfigMapList{} |
| 63 | + Expect(BuildKustomizationIntoList(scheme.Codecs.UniversalDeserializer(), types.Kustomization{ |
| 64 | + ConfigMapGenerator: []types.ConfigMapArgs{ |
| 65 | + { |
| 66 | + GeneratorArgs: types.GeneratorArgs{ |
| 67 | + Name: "my-config", |
| 68 | + KvPairSources: types.KvPairSources{ |
| 69 | + LiteralSources: []string{"foo=bar"}, |
| 70 | + }, |
| 71 | + Options: &types.GeneratorOptions{ |
| 72 | + DisableNameSuffixHash: true, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, list)).To(Succeed()) |
| 78 | + Expect(list.Items).To(ConsistOf(corev1.ConfigMap{ |
| 79 | + TypeMeta: metav1.TypeMeta{ |
| 80 | + Kind: "ConfigMap", |
| 81 | + APIVersion: "v1", |
| 82 | + }, |
| 83 | + ObjectMeta: metav1.ObjectMeta{ |
| 84 | + Name: "my-config", |
| 85 | + }, |
| 86 | + Data: map[string]string{"foo": "bar"}, |
| 87 | + })) |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + Describe("DecodeResource", func() { |
| 92 | + It("should decode the resource into the object", func() { |
| 93 | + res, err := resource.NewFactory(&hasher.Hasher{}).FromBytes([]byte(testdata.ConfigMapYAML)) |
| 94 | + Expect(err).NotTo(HaveOccurred()) |
| 95 | + |
| 96 | + cm := &corev1.ConfigMap{} |
| 97 | + _, _, err = DecodeResource(scheme.Codecs.UniversalDeserializer(), res, nil, cm) |
| 98 | + Expect(err).NotTo(HaveOccurred()) |
| 99 | + Expect(cm).To(Equal(&corev1.ConfigMap{ |
| 100 | + TypeMeta: metav1.TypeMeta{ |
| 101 | + Kind: "ConfigMap", |
| 102 | + APIVersion: "v1", |
| 103 | + }, |
| 104 | + ObjectMeta: metav1.ObjectMeta{ |
| 105 | + Name: "my-config", |
| 106 | + }, |
| 107 | + Data: map[string]string{"foo": "bar"}, |
| 108 | + })) |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + Describe("DecodeResMapIntoList", func() { |
| 113 | + It("should decode the resmap into a list", func() { |
| 114 | + res, err := resource.NewFactory(&hasher.Hasher{}).FromBytes([]byte(testdata.ConfigMapYAML)) |
| 115 | + Expect(err).NotTo(HaveOccurred()) |
| 116 | + resMap := resmap.New() |
| 117 | + Expect(resMap.Append(res)) |
| 118 | + |
| 119 | + list := &corev1.ConfigMapList{} |
| 120 | + Expect(DecodeResMapIntoList(scheme.Codecs.UniversalDeserializer(), resMap, list)).To(Succeed()) |
| 121 | + Expect(list.Items).To(ConsistOf(corev1.ConfigMap{ |
| 122 | + TypeMeta: metav1.TypeMeta{ |
| 123 | + Kind: "ConfigMap", |
| 124 | + APIVersion: "v1", |
| 125 | + }, |
| 126 | + ObjectMeta: metav1.ObjectMeta{ |
| 127 | + Name: "my-config", |
| 128 | + }, |
| 129 | + Data: map[string]string{"foo": "bar"}, |
| 130 | + })) |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + Describe("DecodeResMapUnstructureds", func() { |
| 135 | + res, err := resource.NewFactory(&hasher.Hasher{}).FromBytes([]byte(testdata.ConfigMapYAML)) |
| 136 | + Expect(err).NotTo(HaveOccurred()) |
| 137 | + resMap := resmap.New() |
| 138 | + Expect(resMap.Append(res)) |
| 139 | + |
| 140 | + unstructureds, err := DecodeResMapUnstructureds(resMap) |
| 141 | + Expect(err).NotTo(HaveOccurred()) |
| 142 | + Expect(unstructureds).To(ConsistOf(unstructured.Unstructured{Object: map[string]interface{}{ |
| 143 | + "kind": "ConfigMap", |
| 144 | + "apiVersion": "v1", |
| 145 | + "metadata": map[string]interface{}{ |
| 146 | + "name": "my-config", |
| 147 | + }, |
| 148 | + "data": map[string]interface{}{ |
| 149 | + "foo": "bar", |
| 150 | + }, |
| 151 | + }})) |
| 152 | + }) |
| 153 | +}) |
0 commit comments