fix: Add more tests and run them
This commit is contained in:
parent
071aa420ff
commit
60591f5f8b
6 changed files with 58 additions and 37 deletions
|
@ -10,6 +10,9 @@ RUN go mod download
|
||||||
# Copy the go source
|
# Copy the go source
|
||||||
COPY ./server/*.go ./
|
COPY ./server/*.go ./
|
||||||
|
|
||||||
|
# Test
|
||||||
|
RUN go test -v ./...
|
||||||
|
|
||||||
# Build
|
# Build
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -o well-known ./
|
RUN CGO_ENABLED=0 GOOS=linux go build -a -o well-known ./
|
||||||
|
|
||||||
|
|
15
server/annotations.go
Normal file
15
server/annotations.go
Normal 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]
|
||||||
|
}
|
23
server/annotations_test.go
Normal file
23
server/annotations_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -194,18 +193,6 @@ func discoverData(clientset *kubernetes.Clientset, ns string) (wkRegistry, error
|
||||||
return reg, nil
|
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 {
|
func updateConfigMap(ctx context.Context, client kubernetes.Interface, reg wkRegistry) error {
|
||||||
cm := &v1.ConfigMap{Data: reg.encode()}
|
cm := &v1.ConfigMap{Data: reg.encode()}
|
||||||
cm.Namespace = namespace
|
cm.Namespace = namespace
|
||||||
|
|
|
@ -36,30 +36,6 @@ func (reg wkRegistry) encode() map[string]string {
|
||||||
return d
|
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{} {
|
func mergeStructs(x1, x2 interface{}) interface{} {
|
||||||
if reflect.TypeOf(x1) != reflect.TypeOf(x2) {
|
if reflect.TypeOf(x1) != reflect.TypeOf(x2) {
|
||||||
return x1
|
return x1
|
||||||
|
|
|
@ -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())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue