|
| 1 | +// Copyright (c) 2019-2022 Red Hat, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package kubernetes |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "os" |
| 19 | + "testing" |
| 20 | + |
| 21 | + dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" |
| 22 | + "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" |
| 23 | + "github.com/devfile/devworkspace-operator/pkg/provision/sync" |
| 24 | + "github.com/google/go-cmp/cmp" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + "k8s.io/apimachinery/pkg/runtime" |
| 28 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 29 | + clientgoscheme "k8s.io/client-go/kubernetes/scheme" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 31 | + "sigs.k8s.io/yaml" |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + scheme = runtime.NewScheme() |
| 36 | + api = sync.ClusterAPI{ |
| 37 | + Scheme: scheme, |
| 38 | + } |
| 39 | +) |
| 40 | + |
| 41 | +func init() { |
| 42 | + utilruntime.Must(clientgoscheme.AddToScheme(scheme)) |
| 43 | + utilruntime.Must(v1alpha1.AddToScheme(scheme)) |
| 44 | + utilruntime.Must(dw.AddToScheme(scheme)) |
| 45 | +} |
| 46 | + |
| 47 | +func TestDeserializeObject(t *testing.T) { |
| 48 | + InitializeDeserializer(scheme) |
| 49 | + defer func() { |
| 50 | + decoder = nil |
| 51 | + }() |
| 52 | + tests := []struct { |
| 53 | + name string |
| 54 | + filePath string |
| 55 | + expectedObjStub client.Object |
| 56 | + expectedErrRegexp string |
| 57 | + }{ |
| 58 | + { |
| 59 | + name: "Deserializes Pod", |
| 60 | + filePath: "testdata/k8s_objects/pod.yaml", |
| 61 | + expectedObjStub: &corev1.Pod{}, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "Deserializes Service", |
| 65 | + filePath: "testdata/k8s_objects/service.yaml", |
| 66 | + expectedObjStub: &corev1.Service{}, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "Deserializes ConfigMap", |
| 70 | + filePath: "testdata/k8s_objects/configmap.yaml", |
| 71 | + expectedObjStub: &corev1.ConfigMap{}, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "Kubernetes list", |
| 75 | + filePath: "testdata/k8s_objects/kubernetes-list.yaml", |
| 76 | + expectedErrRegexp: "objects of kind 'List' are unsupported", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "Random yaml that is not a Kubernetes object", |
| 80 | + filePath: "testdata/k8s_objects/non-k8s-object.yaml", |
| 81 | + expectedErrRegexp: "Object 'Kind' is missing", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "Object with unrecognized kind", |
| 85 | + filePath: "testdata/k8s_objects/unrecognized-kind.yaml", |
| 86 | + expectedErrRegexp: "no kind .* is registered for version .* in scheme", |
| 87 | + }, |
| 88 | + } |
| 89 | + for _, tt := range tests { |
| 90 | + t.Run(fmt.Sprintf("%s (%s)", tt.name, tt.filePath), func(t *testing.T) { |
| 91 | + jsonBytes := readBytesFromFile(t, tt.filePath) |
| 92 | + actualObj, err := deserializeToObject(jsonBytes, api) |
| 93 | + if tt.expectedErrRegexp != "" { |
| 94 | + if !assert.Error(t, err, "Expect error to be returned") { |
| 95 | + return |
| 96 | + } |
| 97 | + assert.Regexp(t, tt.expectedErrRegexp, err.Error(), "Expect error to match pattern") |
| 98 | + } else { |
| 99 | + if !assert.NoError(t, err, "Expect no error to be returned") { |
| 100 | + return |
| 101 | + } |
| 102 | + err := yaml.Unmarshal(jsonBytes, tt.expectedObjStub) |
| 103 | + assert.NoError(t, err) |
| 104 | + assert.True(t, cmp.Equal(tt.expectedObjStub, actualObj), cmp.Diff(tt.expectedObjStub, actualObj)) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestErrorIfDeserializerNotInitialized(t *testing.T) { |
| 111 | + _, err := deserializeToObject([]byte(""), api) |
| 112 | + assert.Error(t, err) |
| 113 | + assert.Equal(t, "kubernetes object deserializer is not initialized", err.Error()) |
| 114 | +} |
| 115 | + |
| 116 | +func readBytesFromFile(t *testing.T, filePath string) []byte { |
| 117 | + bytes, err := os.ReadFile(filePath) |
| 118 | + if err != nil { |
| 119 | + t.Fatal(err) |
| 120 | + } |
| 121 | + return bytes |
| 122 | +} |
0 commit comments