From 16a439fe1eb5e4a9e3864edac6aaf1f6149a4d94 Mon Sep 17 00:00:00 2001 From: Parthvi Vala Date: Thu, 8 Dec 2022 21:46:44 +0530 Subject: [PATCH] Fix test failures Signed-off-by: Parthvi Vala --- pkg/devfile/generator/generators.go | 5 +- pkg/devfile/generator/generators_test.go | 63 ++++++++++++++++++++++-- pkg/devfile/generator/utils.go | 1 + 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/pkg/devfile/generator/generators.go b/pkg/devfile/generator/generators.go index 8278a5da..628187d2 100644 --- a/pkg/devfile/generator/generators.go +++ b/pkg/devfile/generator/generators.go @@ -185,13 +185,12 @@ func GetDeployment(devfileObj parser.DevfileObj, deployParams DeploymentParams) Containers: deployParams.Containers, Volumes: deployParams.Volumes, } + globalAttributes, err := devfileObj.Data.GetAttributes() if err != nil { return nil, err } - components, err := devfileObj.Data.GetDevfileContainerComponents(common.DevfileOptions{ - FilterByName: "", - }) + components, err := devfileObj.Data.GetDevfileContainerComponents(common.DevfileOptions{}) if err != nil { return nil, err } diff --git a/pkg/devfile/generator/generators_test.go b/pkg/devfile/generator/generators_test.go index fac1c503..59f761e8 100644 --- a/pkg/devfile/generator/generators_test.go +++ b/pkg/devfile/generator/generators_test.go @@ -17,14 +17,16 @@ package generator import ( "fmt" + apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + "reflect" + "strings" + "testing" + "github.com/stretchr/testify/assert" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/utils/pointer" - "reflect" - "strings" - "testing" v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" "github.com/devfile/api/v2/pkg/attributes" @@ -1077,6 +1079,7 @@ func TestGetDeployment(t *testing.T) { containerComponents []v1.Component deploymentParams DeploymentParams expected appsv1.Deployment + attributes attributes.Attributes }{ { // Currently dedicatedPod can only filter out annotations @@ -1172,6 +1175,55 @@ func TestGetDeployment(t *testing.T) { }, }, }, + { + name: "pod should have pod-overrides attribute", + containerComponents: []v1.Component{ + testingutil.GenerateDummyContainerComponent("container1", nil, []v1.Endpoint{ + { + Name: "http-8080", + TargetPort: 8080, + }, + }, nil, v1.Annotation{ + Deployment: map[string]string{ + "key1": "value1", + }, + }, nil), + testingutil.GenerateDummyContainerComponent("container2", nil, nil, nil, v1.Annotation{ + Deployment: map[string]string{ + "key2": "value2", + }, + }, nil), + }, + attributes: attributes.Attributes{ + PodOverridesAttribute: apiext.JSON{Raw: []byte("{\"spec\": {\"serviceAccountName\": \"new-service-account\"}}")}, + }, + deploymentParams: DeploymentParams{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "preserved-key": "preserved-value", + }, + }, + Containers: containers, + }, + expected: appsv1.Deployment{ + ObjectMeta: objectMeta, + Spec: appsv1.DeploymentSpec{ + Strategy: appsv1.DeploymentStrategy{ + Type: appsv1.RecreateDeploymentStrategyType, + }, + Selector: &metav1.LabelSelector{ + MatchLabels: nil, + }, + Template: corev1.PodTemplateSpec{ + ObjectMeta: objectMeta, + Spec: corev1.PodSpec{ + Containers: containers, + ServiceAccountName: "new-service-account", + }, + }, + }, + }, + }, } for _, tt := range tests { @@ -1186,8 +1238,9 @@ func TestGetDeployment(t *testing.T) { }, } // set up the mock data - mockGetComponents := mockDevfileData.EXPECT().GetComponents(options) - mockGetComponents.Return(tt.containerComponents, nil).AnyTimes() + mockDevfileData.EXPECT().GetAttributes().Return(tt.attributes, nil).AnyTimes() + mockDevfileData.EXPECT().GetDevfileContainerComponents(common.DevfileOptions{}).Return(tt.containerComponents, nil).AnyTimes() + mockDevfileData.EXPECT().GetComponents(options).Return(tt.containerComponents, nil).AnyTimes() devObj := parser.DevfileObj{ Data: mockDevfileData, diff --git a/pkg/devfile/generator/utils.go b/pkg/devfile/generator/utils.go index e1f3db1a..d75db90f 100644 --- a/pkg/devfile/generator/utils.go +++ b/pkg/devfile/generator/utils.go @@ -256,6 +256,7 @@ func getPodTemplateSpec(globalAttributes attributes.Attributes, components []v1. if err != nil { return nil, err } + patchedPodTemplateSpec.ObjectMeta = podTemplateSpecParams.ObjectMeta podTemplateSpec = patchedPodTemplateSpec }