Files
ngrok-operator/internal/annotations/annotations_test.go
T
Jonathan Stacks aa1781d348 Dependency updates (#785)
* chore: Update to go 1.26.1

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>

* chore: Run 'go fix ./...' for go 1.26.1

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>

* chore: Upgrade go modules

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>

* chore: Fix deprecations and linter warnings

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>

---------

Signed-off-by: Jonathan Stacks <jonstacks@users.noreply.github.com>
2026-03-23 16:50:43 +00:00

224 lines
5.4 KiB
Go

/*
Copyright 2017 The Kubernetes 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 annotations_test
import (
"testing"
"github.com/ngrok/ngrok-operator/internal/annotations"
"github.com/ngrok/ngrok-operator/internal/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
networking "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestExtractNgrokTrafficPolicyFromAnnotations(t *testing.T) {
tests := []struct {
name string
annotations map[string]string
expected string
expectedErr error
}{
{
name: "Valid traffic policy",
annotations: map[string]string{
annotations.TrafficPolicyAnnotation: "policy1",
},
expected: "policy1",
expectedErr: nil,
},
{
name: "No annotations",
annotations: nil,
expected: "",
expectedErr: errors.ErrMissingAnnotations,
},
{
name: "Multiple traffic policies (invalid)",
annotations: map[string]string{
annotations.TrafficPolicyAnnotation: "policy1,policy2",
},
expected: "",
expectedErr: errors.New("multiple traffic policies are not supported: [policy1 policy2]"),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
obj := &networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ingress",
Namespace: "default",
Annotations: tc.annotations,
},
}
policy, err := annotations.ExtractNgrokTrafficPolicyFromAnnotations(obj)
if tc.expectedErr != nil {
require.Error(t, err)
assert.Equal(t, tc.expectedErr, err)
assert.Empty(t, policy)
} else {
require.NoError(t, err)
assert.Equal(t, tc.expected, policy)
}
})
}
}
func TestExtractUseEndpointPooling(t *testing.T) {
tests := []struct {
name string
annotations map[string]string
expected *bool
expectedErr error
}{
{
name: "Pooling enabled",
annotations: map[string]string{
"k8s.ngrok.com/pooling-enabled": "true",
},
expected: new(true),
expectedErr: nil,
},
{
name: "Pooling disabled",
annotations: map[string]string{
"k8s.ngrok.com/pooling-enabled": "false",
},
expected: new(false),
expectedErr: nil,
},
{
name: "Invalid value",
annotations: map[string]string{
"k8s.ngrok.com/pooling-enabled": "foo",
},
expected: new(false),
expectedErr: nil,
},
{
name: "Annotation not present",
annotations: nil,
expected: nil,
expectedErr: nil,
},
{
name: "Empty annotation value returns error",
annotations: map[string]string{
"k8s.ngrok.com/pooling-enabled": "",
},
expected: nil,
expectedErr: errors.InvalidContent{Name: "the annotation k8s.ngrok.com/pooling-enabled does not contain a valid value ()"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
obj := &networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ingress",
Namespace: "default",
Annotations: tc.annotations,
},
}
useEndpoints, err := annotations.ExtractUseEndpointPooling(obj)
if tc.expectedErr != nil {
require.Error(t, err)
assert.Equal(t, tc.expectedErr, err)
} else {
require.NoError(t, err)
if tc.expected == nil {
assert.Nil(t, useEndpoints)
} else {
require.NotNil(t, useEndpoints)
assert.Equal(t, *tc.expected, *useEndpoints)
}
}
})
}
}
func TestExtractUseBindings(t *testing.T) {
tests := []struct {
name string
annotations map[string]string
expected []string
expectedErr error
}{
{
name: "Valid Bindings: Public",
annotations: map[string]string{
"k8s.ngrok.com/bindings": "public",
},
expected: []string{"public"},
expectedErr: nil,
},
{
name: "Invalid Binding Annotation",
annotations: map[string]string{
"k8s.ngrok.com/bindings": "foo",
},
expected: []string{"foo"},
expectedErr: nil,
},
{
name: "Invalid Binding Errs When n > 1",
annotations: map[string]string{
"k8s.ngrok.com/bindings": "public,internal",
},
expectedErr: errors.New("multiple bindings are not supported: [public internal]"),
},
{
name: "Missing Bindings Value",
annotations: map[string]string{
"k8s.ngrok.com/bindings": "",
},
expectedErr: errors.NewInvalidAnnotationContent("k8s.ngrok.com/bindings", ""),
},
{
name: "Annotation Not present",
annotations: nil,
expected: nil,
expectedErr: nil,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
obj := &networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ingress",
Namespace: "default",
Annotations: tc.annotations,
},
}
binding, err := annotations.ExtractUseBindings(obj)
if tc.expectedErr != nil {
require.Error(t, err)
assert.Equal(t, tc.expectedErr, err)
} else {
require.NoError(t, err)
assert.Equal(t, tc.expected, binding)
}
})
}
}