Skip to content

Commit

Permalink
fix: Add more tests and run them
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Apr 11, 2023
1 parent 071aa42 commit 60591f5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 37 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./

Expand Down
15 changes: 15 additions & 0 deletions server/annotations.go
Original file line number Diff line number Diff line change
@@ -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]
}
23 changes: 23 additions & 0 deletions server/annotations_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
13 changes: 0 additions & 13 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"os/signal"
"path/filepath"
"regexp"
"syscall"
"time"

Expand Down Expand Up @@ -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
Expand Down
24 changes: 0 additions & 24 deletions server/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions server/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}
}

0 comments on commit 60591f5

Please sign in to comment.