From 7b338c8f5873b4d64172c0fea6dd0ef788161130 Mon Sep 17 00:00:00 2001 From: Juan Jose Jaramillo Date: Tue, 28 May 2024 13:54:37 -0700 Subject: [PATCH] feat: Unit testing for package `v1alpha1` (#34) --- Makefile | 1 + .../v1alpha1/instrumentation_webhook_test.go | 69 +++++++++++++++++++ src/api/v1alpha1/webhook_suite_test.go | 4 +- 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/api/v1alpha1/instrumentation_webhook_test.go diff --git a/Makefile b/Makefile index 479b0ecb..f03f181a 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ TMP_DIR = $(shell pwd)/tmp # Go packages to test TEST_PACKAGES = ./src/internal/config \ + ./src/api/v1alpha1 \ ./src/instrumentation/ \ ./src/internal/version diff --git a/src/api/v1alpha1/instrumentation_webhook_test.go b/src/api/v1alpha1/instrumentation_webhook_test.go new file mode 100644 index 00000000..acd33f2f --- /dev/null +++ b/src/api/v1alpha1/instrumentation_webhook_test.go @@ -0,0 +1,69 @@ +package v1alpha1 + +import ( + "testing" + + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestInstrumentationDefaultingWebhook(t *testing.T) { + inst := &Instrumentation{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + AnnotationDefaultAutoInstrumentationJava: "java-img:1", + AnnotationDefaultAutoInstrumentationNodeJS: "nodejs-img:1", + AnnotationDefaultAutoInstrumentationPython: "python-img:1", + AnnotationDefaultAutoInstrumentationDotNet: "dotnet-img:1", + }, + }, + } + inst.Default() + assert.Equal(t, "java-img:1", inst.Spec.Java.Image) + assert.Equal(t, "nodejs-img:1", inst.Spec.NodeJS.Image) + assert.Equal(t, "python-img:1", inst.Spec.Python.Image) + assert.Equal(t, "dotnet-img:1", inst.Spec.DotNet.Image) +} + +func TestInstrumentationValidatingWebhook(t *testing.T) { + tests := []struct { + name string + err string + inst Instrumentation + }{ + { + name: "argument is a number", + inst: Instrumentation{ + Spec: InstrumentationSpec{ + Sampler: Sampler{ + Type: ParentBasedTraceIDRatio, + Argument: "0.99", + }, + }, + }, + }, + { + name: "argument is missing", + inst: Instrumentation{ + Spec: InstrumentationSpec{ + Sampler: Sampler{ + Type: ParentBasedTraceIDRatio, + }, + }, + }, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if test.err == "" { + assert.Nil(t, test.inst.ValidateCreate()) + assert.Nil(t, test.inst.ValidateUpdate(nil)) + } else { + err := test.inst.ValidateCreate() + assert.Contains(t, err.Error(), test.err) + err = test.inst.ValidateUpdate(nil) + assert.Contains(t, err.Error(), test.err) + } + }) + } +} \ No newline at end of file diff --git a/src/api/v1alpha1/webhook_suite_test.go b/src/api/v1alpha1/webhook_suite_test.go index ff2303a0..0a03b76a 100644 --- a/src/api/v1alpha1/webhook_suite_test.go +++ b/src/api/v1alpha1/webhook_suite_test.go @@ -61,10 +61,10 @@ var _ = BeforeSuite(func() { By("bootstrapping test environment") testEnv = &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, + CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "tests", "kustomize", "crd", "bases")}, ErrorIfCRDPathMissing: false, WebhookInstallOptions: envtest.WebhookInstallOptions{ - Paths: []string{filepath.Join("..", "..", "config", "webhook")}, + Paths: []string{filepath.Join("..", "..", "..", "tests", "kustomize", "webhook")}, }, }