diff --git a/charts/falco/tests/unit/serviceTemplate_test.go b/charts/falco/tests/unit/serviceTemplate_test.go new file mode 100644 index 00000000..1252216d --- /dev/null +++ b/charts/falco/tests/unit/serviceTemplate_test.go @@ -0,0 +1,74 @@ +// 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 ( + "encoding/json" + "path/filepath" + "reflect" + "testing" + + "github.com/gruntwork-io/terratest/modules/helm" + corev1 "k8s.io/api/core/v1" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +type serviceTemplateTest struct { + suite.Suite + chartPath string + releaseName string + namespace string + templates []string +} + +func TestServiceTemplate(t *testing.T) { + t.Parallel() + + chartFullPath, err := filepath.Abs(chartPath) + require.NoError(t, err) + + suite.Run(t, &serviceTemplateTest{ + Suite: suite.Suite{}, + chartPath: chartFullPath, + releaseName: "falco-test", + namespace: "falco-namespace-test", + templates: []string{"templates/service.yaml"}, + }) +} + +func (s *serviceTemplateTest) TestCreationDefaultValues() { + // Render the service and check that it has not been rendered. + _, 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()) +} + + +func (s *serviceTemplateTest) TestDefaultLabelsValues() { + // Render the service and check that it has not been rendered. + 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()) + + var svc corev1.Service + helm.UnmarshalK8SYaml(s.T(), output, &svc) + s.Equal("falco", svc.Metadata.Labels["app.kubernetes.io/name"]) + s.Equal("Helm", svc.Metadata.Labels["app.kubernetes.io/managed-by"]) + s.Equal("0.38.2", svc.Metadata.Labels["app.kubernetes.io/version"]) + s.Equal("falco-4.8.1", svc.Metadata.Labels["helm.sh/chart"]) + s.Equal("falco-metrics", svc.Metadata.Labels["type"]) +}