Skip to content

Commit

Permalink
feat: Unit testing for package v1alpha1 (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjjaramillo authored May 28, 2024
1 parent 80b665b commit 7b338c8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
69 changes: 69 additions & 0 deletions src/api/v1alpha1/instrumentation_webhook_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
4 changes: 2 additions & 2 deletions src/api/v1alpha1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")},
},
}

Expand Down

0 comments on commit 7b338c8

Please sign in to comment.