Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yottahmd committed May 2, 2022
1 parent 545983f commit ab85b5f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 14 deletions.
4 changes: 3 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ func startServer(cfg *admin.Config) error {
server.Shutdown()
})

return server.Serve()
err := server.Serve()
utils.LogIgnoreErr("running server", err)
return err
}
60 changes: 60 additions & 0 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"fmt"
"net"
"net/http"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/yohamta/dagu/internal/admin"
"github.com/yohamta/dagu/internal/settings"
"github.com/yohamta/dagu/internal/utils"
)

func Test_serverCommand(t *testing.T) {
app := makeApp()
dir := utils.MustTempDir("dagu_test_server")
os.Setenv("HOME", dir)

port := findPort(t)
os.Setenv(settings.CONFIG__ADMIN_PORT, port)
settings.InitTest(dir)

done := make(chan struct{})
go func() {
runAppTestOutput(app, appTest{
args: []string{"", "server"}, errored: false,
output: []string{"admin server is running "},
}, t)
close(done)
}()

time.Sleep(time.Millisecond * 100)

cfg := admin.DefaultConfig()
res, err := http.Post(
fmt.Sprintf("http://%s:%s/shutdown", cfg.Host, cfg.Port),
"application/json",
nil,
)
require.NoError(t, err)
require.Equal(t, http.StatusOK, res.StatusCode)

<-done
}

func findPort(t *testing.T) string {
t.Helper()
ln, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
port := ln.Addr().(*net.TCPAddr).Port
if err := ln.Close(); err != nil {
panic(err)
}
return fmt.Sprintf("%d", port)
}
3 changes: 2 additions & 1 deletion internal/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strconv"

"github.com/yohamta/dagu/internal/settings"
"github.com/yohamta/dagu/internal/utils"
)

Expand Down Expand Up @@ -43,7 +44,7 @@ func (c *Config) setup() {
c.Host = "127.0.0.1"
}
if c.Port == "" {
c.Port = "8000"
c.Port = settings.MustGet(settings.CONFIG__ADMIN_PORT)
}
if len(c.Env) == 0 {
env := utils.DefaultEnv()
Expand Down
2 changes: 1 addition & 1 deletion internal/admin/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (svr *server) Serve() (err error) {
log.Printf("admin server is running at \"http://%s\"\n", svr.addr)

err = svr.server.ListenAndServe()
if err != http.ErrServerClosed {
if err == http.ErrServerClosed {
err = nil
}
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions internal/admin/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func TestHttpServerStartShutdown(t *testing.T) {
os.RemoveAll(dir)

host := "127.0.0.1"
port := findPort()
port := findPort(t)
server := NewServer(&Config{
Host: host,
Port: port,
})

go func() {
err := server.Serve()
require.Equal(t, http.ErrServerClosed, err)
require.NoError(t, err)
}()

time.Sleep(time.Millisecond * 300)
Expand All @@ -47,7 +47,7 @@ func TestHttpServerShutdownWithAPI(t *testing.T) {
os.RemoveAll(dir)

host := "127.0.0.1"
port := findPort()
port := findPort(t)
server := NewServer(&Config{
Host: host,
Port: port,
Expand All @@ -56,7 +56,7 @@ func TestHttpServerShutdownWithAPI(t *testing.T) {

go func() {
err := server.Serve()
require.Equal(t, http.ErrServerClosed, err)
require.NoError(t, err)
}()

time.Sleep(time.Millisecond * 300)
Expand All @@ -81,7 +81,7 @@ func TestHttpServerBasicAuth(t *testing.T) {
os.RemoveAll(dir)

host := "127.0.0.1"
port := findPort()
port := findPort(t)
server := NewServer(&Config{
Host: host,
Port: port,
Expand All @@ -92,7 +92,7 @@ func TestHttpServerBasicAuth(t *testing.T) {

go func() {
err := server.Serve()
require.Equal(t, http.ErrServerClosed, err)
require.NoError(t, err)
}()
defer server.Shutdown()

Expand All @@ -118,7 +118,8 @@ func TestHttpServerBasicAuth(t *testing.T) {
require.Equal(t, "200 OK", res.Status)
}

func findPort() string {
func findPort(t *testing.T) string {
t.Helper()
ln, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
Expand Down
9 changes: 5 additions & 4 deletions internal/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ var ErrConfigNotFound = fmt.Errorf("config not found")
var cache map[string]string = nil

const (
CONFIG__DATA_DIR = "DAGU__DATA"
CONFIG__LOGS_DIR = "DAGU__LOGS"
CONFIG__DATA_DIR = "DAGU__DATA"
CONFIG__LOGS_DIR = "DAGU__LOGS"
CONFIG__ADMIN_PORT = "CONFIG__ADMIN_PORT"
)

func MustGet(name string) string {
Expand Down Expand Up @@ -43,9 +44,9 @@ func load() {
cache[CONFIG__DATA_DIR] = config(
CONFIG__DATA_DIR,
path.Join(dir, "/.dagu/data"))
cache[CONFIG__LOGS_DIR] = config(
CONFIG__LOGS_DIR,
cache[CONFIG__LOGS_DIR] = config(CONFIG__LOGS_DIR,
path.Join(dir, "/.dagu/logs"))
cache[CONFIG__ADMIN_PORT] = config(CONFIG__ADMIN_PORT, "8000")
}

func InitTest(dir string) {
Expand Down

0 comments on commit ab85b5f

Please sign in to comment.