From 60591f5f8b1f1d6aa2a9727796fd212ded0a8fba Mon Sep 17 00:00:00 2001 From: Dries De Peuter Date: Wed, 12 Apr 2023 00:59:13 +0200 Subject: [PATCH] fix: Add more tests and run them --- Dockerfile | 3 +++ server/annotations.go | 15 +++++++++++++++ server/annotations_test.go | 23 +++++++++++++++++++++++ server/main.go | 13 ------------- server/model.go | 24 ------------------------ server/model_test.go | 17 +++++++++++++++++ 6 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 server/annotations.go create mode 100644 server/annotations_test.go diff --git a/Dockerfile b/Dockerfile index c82a581..d202cc3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 ./ diff --git a/server/annotations.go b/server/annotations.go new file mode 100644 index 0000000..060ff35 --- /dev/null +++ b/server/annotations.go @@ -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] +} diff --git a/server/annotations_test.go b/server/annotations_test.go new file mode 100644 index 0000000..b36ccad --- /dev/null +++ b/server/annotations_test.go @@ -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) + } + }) + } +} diff --git a/server/main.go b/server/main.go index f0c2eb8..6652e79 100644 --- a/server/main.go +++ b/server/main.go @@ -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 diff --git a/server/model.go b/server/model.go index 1f6764e..bc9d50f 100644 --- a/server/model.go +++ b/server/model.go @@ -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 diff --git a/server/model_test.go b/server/model_test.go index e32edf9..6bc5e5b 100644 --- a/server/model_test.go +++ b/server/model_test.go @@ -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()) + }) + } +}