Skip to content

Commit

Permalink
Fail early when invalid port is configured
Browse files Browse the repository at this point in the history
  • Loading branch information
joyrex2001 committed Nov 23, 2023
1 parent 21c53f4 commit d5ed5f0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ func getKubedockURL() (string, error) {
}

port := strings.Split(viper.GetString("server.listen-addr")+":", ":")[1]
klog.Infof("api server started listening on %s", port)
if port == "" {
return "", fmt.Errorf("expected a port to be configured for listen-addr")
}

proto := "http"
if viper.GetBool("server.tls-enable") {
Expand Down
39 changes: 39 additions & 0 deletions internal/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package internal

import (
"strings"
"testing"

"github.com/joyrex2001/kubedock/internal/util/myip"
"github.com/spf13/viper"
)

func TestGetKubedockURL(t *testing.T) {
tests := []struct {
listen string
tls bool
res string
suc bool
}{
{":1234", false, "http://{{IP}}:1234", true},
{":1234", true, "https://{{IP}}:1234", true},
{"1234", false, "", false},
}

ip, _ := myip.Get()
for i, tst := range tests {
viper.Set("server.listen-addr", tst.listen)
viper.Set("server.tls-enable", tst.tls)
res, err := getKubedockURL()
if tst.suc && err != nil {
t.Errorf("failed test %d - unexpected error %s", i, err)
}
if !tst.suc && err == nil {
t.Errorf("failed test %d - expected error, but succeeded instead", i)
}
tst.res = strings.ReplaceAll(tst.res, "{{IP}}", ip)
if err == nil && res != tst.res {
t.Errorf("failed test %d - expected %s, but got %s", i, tst.res, res)
}
}
}

0 comments on commit d5ed5f0

Please sign in to comment.