diff --git a/charts/falco/tests/unit/chartInfo.go b/charts/falco/tests/unit/chartInfo.go new file mode 100644 index 000000000..11b4b3d9c --- /dev/null +++ b/charts/falco/tests/unit/chartInfo.go @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024 The Falco 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 unit + +import ( + "testing" + + "github.com/gruntwork-io/terratest/modules/helm" + "gopkg.in/yaml.v3" +) + +func chartInfo(t *testing.T, chartPath string) (map[string]interface{}, error) { + // Get chart info. + output, err := helm.RunHelmCommandAndGetOutputE(t, &helm.Options{}, "show", "chart", chartPath) + if err != nil { + return nil, err + } + chartInfo := map[string]interface{}{} + err = yaml.Unmarshal([]byte(output), &chartInfo) + return chartInfo, err +} diff --git a/charts/falco/tests/unit/serviceTemplate_test.go b/charts/falco/tests/unit/serviceTemplate_test.go index 1f76bcd2d..6568e0a4e 100644 --- a/charts/falco/tests/unit/serviceTemplate_test.go +++ b/charts/falco/tests/unit/serviceTemplate_test.go @@ -16,9 +16,8 @@ package unit import ( - "encoding/json" + "fmt" "path/filepath" - "reflect" "testing" "github.com/gruntwork-io/terratest/modules/helm" @@ -130,4 +129,32 @@ func (s *serviceTemplateTest) TestCustomLabelsValues() { s.Equal(expectedVal, value) } s.Equal("falco-label", labels["custom-label"]) +} + + +func (s *serviceTemplateTest) TestCustomAnnotationsValues() { + // Render the service and check that it has not been rendered. + options := &helm.Options{SetValues: map[string]string{"metrics.service.annotations" : {"custom-annotation": "falco-annotation"}}} + output, err := helm.RenderTemplateE(s.T(), &helm.Options{}, s.chartPath, s.releaseName, s.templates) + s.Error(err, "should error") + s.Equal("error while running command: exit status 1; Error: could not find template templates/service.yaml in chart", err.Error()) + + + cInfo, err := chartInfo(s.T(), s.chartPath) + s.NoError(err) + // Get app version. + appVersion, found := cInfo["appVersion"] + s.True(found, "should find app version in chart info") + appVersion = appVersion.(string) + // Get chart version. + chartVersion, found := cInfo["version"] + s.True(found, "should find chart version in chart info") + // Get chart name. + chartName, found := cInfo["name"] + s.True(found, "should find chart name in chart info") + chartName = chartName.(string) + var svc corev1.Service + helm.UnmarshalK8SYaml(s.T(), output, &svc) + annotations := svc.GetAnnotations() + s.Equal("falco-annotation", annotations["custom-annotation"]) } \ No newline at end of file