|
| 1 | +// Copyright 2019 FairwindsOps Inc |
| 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 validator |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + conf "github.com/fairwindsops/polaris/pkg/config" |
| 21 | + "github.com/fairwindsops/polaris/pkg/kube" |
| 22 | + "github.com/fairwindsops/polaris/test" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 26 | +) |
| 27 | + |
| 28 | +func TestGetTemplateInputReturnsPolarisSubKeys(t *testing.T) { |
| 29 | + pod := test.MockPod() // Includes a container, required by GetPodSpec |
| 30 | + pod.Spec.NodeName = "testNodeName" |
| 31 | + pod.ObjectMeta.Name = "testpod" |
| 32 | + genRes, err := kube.NewGenericResourceFromPod(pod, pod) |
| 33 | + require.NoError(t, err, "creating new generic resource from a pod") |
| 34 | + schemaTest := schemaTestCase{ |
| 35 | + Target: conf.TargetPodSpec, // ends up being set in the case of target: PodTemplate |
| 36 | + Resource: genRes, |
| 37 | + } |
| 38 | + |
| 39 | + templateInput, err := getTemplateInput(schemaTest) |
| 40 | + require.NoError(t, err, "getting template input from a generic resource") |
| 41 | + require.NotNil(t, templateInput) |
| 42 | + nodeName, ok, err := unstructured.NestedString(templateInput, "Polaris", "PodSpec", "nodeName") |
| 43 | + require.NoError(t, err, "getting Polaris.PodSpec.nodeName from template input") |
| 44 | + require.True(t, ok, "getting Polaris.PodSpec.nodeName from template input") |
| 45 | + require.Equal(t, "testNodeName", nodeName, "the nodeName from template output") |
| 46 | + podName, ok, err := unstructured.NestedString(templateInput, "Polaris", "PodTemplate", "metadata", "name") |
| 47 | + require.NoError(t, err, "getting Polaris.PodTemplate.metadata.name from template input") |
| 48 | + require.True(t, ok, "getting Polaris.PodTemplate.metadata.name from template input") |
| 49 | + require.Equal(t, "testpod", podName, "the pod from template input") |
| 50 | +} |
0 commit comments