fix: Add more tests and run them

This commit is contained in:
Dries De Peuter 2023-04-12 00:59:13 +02:00
parent 071aa420ff
commit 60591f5f8b
No known key found for this signature in database
6 changed files with 58 additions and 37 deletions

View file

@ -10,6 +10,9 @@ RUN go mod download
# Copy the go source
COPY ./server/*.go ./
# Test
RUN go test -v ./...
# Build
RUN CGO_ENABLED=0 GOOS=linux go build -a -o well-known ./

15
server/annotations.go Normal file
View file

@ -0,0 +1,15 @@
package main
import "regexp"
func resolveName(name string) string {
r := regexp.MustCompile(`^well-known.stenic.io/(.+)$`)
if !r.MatchString(name) {
return ""
}
m := r.FindStringSubmatch(name)
if len(m) != 2 {
return ""
}
return m[1]
}

View file

@ -0,0 +1,23 @@
package main
import "testing"
func Test_resolveName(t *testing.T) {
tests := []struct {
name string
annotation string
want string
}{
{"simple", "well-known.stenic.io/annotation", "annotation"},
{"slash", "well-known.stenic.io/annotation/dfs", "annotation/dfs"},
{"empty", "well-known.stenic.io/", ""},
{"wrong", "bad", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := resolveName(tt.annotation); got != tt.want {
t.Errorf("resolveName() = %v, want %v", got, tt.want)
}
})
}
}

View file

@ -8,7 +8,6 @@ import (
"os"
"os/signal"
"path/filepath"
"regexp"
"syscall"
"time"
@ -194,18 +193,6 @@ func discoverData(clientset *kubernetes.Clientset, ns string) (wkRegistry, error
return reg, nil
}
func resolveName(name string) string {
r := regexp.MustCompile(`^well-known.stenic.io/(.+)$`)
if !r.MatchString(name) {
return ""
}
m := r.FindStringSubmatch(name)
if len(m) != 2 {
return ""
}
return m[1]
}
func updateConfigMap(ctx context.Context, client kubernetes.Interface, reg wkRegistry) error {
cm := &v1.ConfigMap{Data: reg.encode()}
cm.Namespace = namespace

View file

@ -36,30 +36,6 @@ func (reg wkRegistry) encode() map[string]string {
return d
}
func mergeMapsRecursive(x1, x2 interface{}) interface{} {
switch x1 := x1.(type) {
case map[string]interface{}:
x2, ok := x2.(map[string]interface{})
if !ok {
return x1
}
for k, v2 := range x2 {
if v1, ok := x1[k]; ok {
x1[k] = mergeMapsRecursive(v1, v2)
} else {
x1[k] = v2
}
}
case nil:
// merge(nil, map[string]interface{...}) -> map[string]interface{...}
x2, ok := x2.(map[string]interface{})
if ok {
return x2
}
}
return x1
}
func mergeStructs(x1, x2 interface{}) interface{} {
if reflect.TypeOf(x1) != reflect.TypeOf(x2) {
return x1

View file

@ -27,3 +27,20 @@ func Test_wkData_append(t *testing.T) {
})
}
}
func Test_wkRegistry_encode(t *testing.T) {
tests := []struct {
name string
reg wkRegistry
want map[string]string
}{
{"simple", wkRegistry{"a": wkData{"b": 1}}, map[string]string{"a.json": "{\n \"b\": 1\n}"}},
{"double", wkRegistry{"a": wkData{"b": 1}, "b": wkData{"c": 2}}, map[string]string{"a.json": "{\n \"b\": 1\n}", "b.json": "{\n \"c\": 2\n}"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, tt.reg.encode(), "expected: %v, got: %v", tt.want, tt.reg.encode())
})
}
}