refactor: inject service client / podman in daemon

This commit is contained in:
Marc 2024-10-02 23:03:57 -04:00
parent 824ba8ba0b
commit 01df6c4dd5
Signed by: marc
GPG key ID: 048E042F22B5DC79
7 changed files with 84 additions and 10 deletions

View file

@ -10,7 +10,7 @@ func getDaemonCommand() *cobra.Command {
Use: "daemon",
Short: "Starts a daemon instance.",
RunE: func(cmd *cobra.Command, args []string) error {
d := daemon.NewDaemon("", 8000)
d := daemon.NewDaemon("", 8000, nil)
d.Start()
return nil

View file

@ -1,16 +1,17 @@
package daemon
import (
"context"
"net/http"
)
func GetApiRoutes() map[string]http.HandlerFunc {
return map[string]http.HandlerFunc{
func GetApiRoutes() map[string]HandlerFuncWithContext {
return map[string]HandlerFuncWithContext{
"/service": ServiceList,
}
}
func ServiceList(w http.ResponseWriter, r *http.Request) {
func ServiceList(w http.ResponseWriter, r *http.Request, c context.Context) {
var returnPayload []byte
var returnCode int

View file

@ -1,6 +1,7 @@
package daemon
import (
"context"
"net/http"
"net/http/httptest"
"testing"
@ -12,7 +13,7 @@ func TestServiceListUnsupportedMethods(t *testing.T) {
req := httptest.NewRequest(method, "/service", nil)
resp := httptest.NewRecorder()
ServiceList(resp, req)
ServiceList(resp, req, context.Background())
response := resp.Result()

View file

@ -1,17 +1,37 @@
package daemon
import (
"context"
"fmt"
"net/http"
service "spud/service"
)
type Daemon struct {
Host string
Port int
Host string
Port int
Services service.ServiceClient
Routes map[string]http.HandlerFunc
}
func NewDaemon(host string, port int) *Daemon {
return &Daemon{Host: host, Port: port}
type HandlerFuncWithContext = func(w http.ResponseWriter, r *http.Request, c context.Context)
func handleFuncWithContext(h HandlerFuncWithContext, c context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h(w, r, c)
}
}
func NewDaemon(host string, port int, serviceClient service.ServiceClient) *Daemon {
d := &Daemon{Host: host, Port: port}
if serviceClient == nil {
d.Services = &service.PodmanServiceClient{}
} else {
d.Services = serviceClient
}
return d
}
func (d Daemon) GetListenAddress() string {
@ -19,8 +39,9 @@ func (d Daemon) GetListenAddress() string {
}
func (d Daemon) Start() {
daemonContext := context.WithValue(context.Background(), "client", d.Services)
for route, handler := range GetApiRoutes() {
http.HandleFunc(route, handler)
http.HandleFunc(route, handleFuncWithContext(handler, daemonContext))
}
http.ListenAndServe(d.GetListenAddress(), nil)

31
daemon/daemon_test.go Normal file
View file

@ -0,0 +1,31 @@
package daemon
import (
"reflect"
service "spud/service"
service_definition "spud/service_definition"
"testing"
)
type DummyClient struct{}
func (c DummyClient) Create(d service_definition.ServiceDefinition) {}
func (c DummyClient) Stop(d string) {}
func TestDaemonDefaultsToPodmanClient(t *testing.T) {
d := NewDaemon("host", 0, nil)
clientType := reflect.TypeOf(d.Services).String()
if clientType != reflect.TypeOf(&service.PodmanServiceClient{}).String() {
t.Errorf("Expected podman client, got %s instead.", clientType)
}
}
func TestDaemonUsesInjectedClientIfProvided(t *testing.T) {
d := NewDaemon("host", 0, DummyClient{})
clientType := reflect.TypeOf(d.Services).String()
if clientType != reflect.TypeOf(DummyClient{}).String() {
t.Errorf("Expected dummy client, got %s instead.", clientType)
}
}

10
service/client.go Normal file
View file

@ -0,0 +1,10 @@
package service
import (
service_definition "spud/service_definition"
)
type ServiceClient interface {
Create(service_definition.ServiceDefinition)
Stop(string)
}

View file

@ -7,6 +7,16 @@ import (
service_definition "spud/service_definition"
)
type PodmanServiceClient struct{}
func (c PodmanServiceClient) Create(definition service_definition.ServiceDefinition) {
CreateService(definition)
}
func (c PodmanServiceClient) Stop(name string) {
StopService(name)
}
func CreateService(definition service_definition.ServiceDefinition) {
var err error