diff --git a/internal/main.go b/internal/main.go index af58aaf..9565820 100644 --- a/internal/main.go +++ b/internal/main.go @@ -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") { diff --git a/internal/main_test.go b/internal/main_test.go new file mode 100644 index 0000000..69d5083 --- /dev/null +++ b/internal/main_test.go @@ -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) + } + } +}