Skip to content

Commit

Permalink
chore (test): add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-kerjean committed Jan 23, 2024
1 parent 7efbf4f commit 2877278
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 10 deletions.
5 changes: 3 additions & 2 deletions common/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

func TestErrors(t *testing.T) {
assert.Contains(t, ErrNotFound.Error(), "Page Not Found")
assert.Contains(t, ErrNotAuthorized.Error(), "Not Authorized")
assert.Equal(t, ErrNotFound.Error(), "Page Not Found")
assert.Equal(t, ErrNotAuthorized.Error(), "Not Authorized")
assert.Equal(t, ErrNotAvailable.Error(), "Not Available")
}
10 changes: 10 additions & 0 deletions common/info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package common

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetAddress(t *testing.T) {
assert.True(t, len(GetAddress()) > 0)
}
2 changes: 2 additions & 0 deletions common/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ func TestLogger(t *testing.T) {
Log.Info("hello %s", "world")
Log.Warning("hello %s", "world")
Log.Error("hello %s", "world")
Log.Debug("hello %s", "world")
Log.Stdout("hello %s", "world")
assert.NotNil(t, NewNilLogger())
}
16 changes: 16 additions & 0 deletions common/random_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package common

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestRandomGenerator(t *testing.T) {
b := ""
for i := 1; i < 250; i++ {
size := (i % 20) + 1
b = RandomString(size)
assert.True(t, len(b) > 0)
assert.Equal(t, size, len(b))
}
}
15 changes: 15 additions & 0 deletions common/ssl/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ssl

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSSLGeneration(t *testing.T) {
cert, pool, err := GenerateSelfSigned()

assert.NoError(t, err)
assert.NotNil(t, cert)
assert.NotNil(t, pool)
}
16 changes: 16 additions & 0 deletions ctrl/static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,19 @@ func TestHandleStatic(t *testing.T) {
assert.Equal(t, test.ExpectedCode, res.Code)
}
}

func TestEndpointHealthCheck(t *testing.T) {
w := httptest.NewRecorder()
HealthCheck(w, httptest.NewRequest("GET", "/healthz", nil))

assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "OK", w.Body.String())
}

func TestEndpointFavicon(t *testing.T) {
w := httptest.NewRecorder()
ServeFavicon(w, httptest.NewRequest("GET", "/favicon.ico", nil))

assert.Equal(t, http.StatusOK, w.Code)
assert.True(t, len(w.Body.String()) > 10)
}
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"strconv"
)

var port int = 3456
var (
port int = 3456
srv *http.Server
)

func init() {
if pStr := os.Getenv("PORT"); pStr != "" {
Expand Down Expand Up @@ -52,7 +55,7 @@ func main() {
Log.Error("ssl.GenerateSelfSigned %s", err.Error())
return
}
srv := &http.Server{
srv = &http.Server{
Addr: fmt.Sprintf(
":%d",
port,
Expand Down
28 changes: 28 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"crypto/tls"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestApplicationBootOK(t *testing.T) {
// given
go main()
time.Sleep(100 * time.Millisecond)

// when
resp, err := (&http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}).Get("https://127.0.0.1:3456/healthz")
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

// then
srv.Close()
}
11 changes: 9 additions & 2 deletions webfleet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"golang.org/x/crypto/acme/autocert"
)

var srv *http.Server

func main() {
addr := ":8123"
router := mux.NewRouter()
Expand All @@ -32,7 +34,7 @@ func main() {
HostPolicy: autocert.HostWhitelist(host, host+addr),
Cache: autocert.DirCache("certs"),
}
srv := &http.Server{
srv = &http.Server{
Addr: ":https",
Handler: router,
TLSConfig: &tls.Config{
Expand All @@ -47,6 +49,11 @@ func main() {
}
return
}

srv = &http.Server{
Addr: addr,
Handler: router,
}
Log.Info("Listening on %s", addr)
http.ListenAndServe(addr, router)
srv.ListenAndServe()
}
28 changes: 28 additions & 0 deletions webfleet/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"crypto/tls"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestApplicationBootOK(t *testing.T) {
// given
go main()
time.Sleep(100 * time.Millisecond)

// when
resp, err := (&http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}).Get("http://127.0.0.1:8123/favicon.ico")
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

// then
srv.Close()
}
3 changes: 3 additions & 0 deletions webfleet/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var (
)

func init() {
if strings.HasSuffix(os.Args[0], ".test") {
return
}
AUTH_DRIVER = strings.ToLower(os.Getenv("AUTH_DRIVER"))
switch AUTH_DRIVER {
case "yolo":
Expand Down
2 changes: 1 addition & 1 deletion webfleet/model/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func GetMachineInfo() []byte {
}(),
Kernel: func() string {
if isMac() {
return ""
return strings.TrimPrefix(execCmd("sysctl", "kern.version"), "kern.version: ")
} else if isAndroid() {
return ""
}
Expand Down
6 changes: 3 additions & 3 deletions webfleet/model/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestGetMachineInfo(t *testing.T) {
assert.NotEqual(t, "", data["os"])
assert.NotEqual(t, "", data["kernel"])
assert.NotEqual(t, "", data["arch"])
// assert.NotEqual(t, data["publicIP"], "")
// assert.Equal(t, data["privateIP"], "")
// Log.Info("DATA %+v", data)
assert.NotEqual(t, "", data["device"])
assert.NotEqual(t, "", data["machineID"])
assert.Equal(t, true, data["isOnline"])
}
33 changes: 33 additions & 0 deletions webfleet/model/server_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package model

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestServerState(t *testing.T) {
// given
s := ServerManager{}

// when
a, err := s.List()
assert.NoError(t, err)
// then
assert.Equal(t, len(a), 0)

// when
err = s.Add("tenant", map[string]interface{}{"device": "test"})
assert.NoError(t, err)
a, err = s.List()
assert.NoError(t, err)
// then
assert.Equal(t, len(a), 1)

// when
s.Remove("tenant")
a, err = s.List()
assert.NoError(t, err)
// then
assert.Equal(t, len(a), 0)
}
22 changes: 22 additions & 0 deletions webfleet/view/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package view

import (
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
)

func TestErrorPageWithError(t *testing.T) {
w := httptest.NewRecorder()
ErrorPage(w, errors.New("__error__"), http.StatusTeapot)
assert.Equal(t, http.StatusTeapot, w.Code)
}

func TestErrorPageWithoutError(t *testing.T) {
w := httptest.NewRecorder()
ErrorPage(w, nil, http.StatusTeapot)
assert.Equal(t, http.StatusTeapot, w.Code)
}

0 comments on commit 2877278

Please sign in to comment.