Skip to content

Commit

Permalink
fake: Add new fake server for testing
Browse files Browse the repository at this point in the history
Move the fake server implementation into a separate package so it can be
consumed by other projects, for example kube-upgrade.

Signed-off-by: Heathcliff <[email protected]>
  • Loading branch information
heathcliff26 committed Dec 17, 2024
1 parent f98ba86 commit 4926e61
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 31 deletions.
80 changes: 80 additions & 0 deletions pkg/fake/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package fake

import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/heathcliff26/fleetlock/pkg/api"
"github.com/stretchr/testify/assert"
)

type FakeServer struct {
server *httptest.Server
assert *assert.Assertions

Path string
StatusCode int
Group string
ID string
}

func NewFakeServer(t *testing.T, statusCode int, path string) *FakeServer {
s := &FakeServer{
assert: assert.New(t),
Path: path,
StatusCode: statusCode,
}

s.server = httptest.NewServer(http.HandlerFunc(s.handleRequest))

return s
}

func (s *FakeServer) handleRequest(rw http.ResponseWriter, req *http.Request) {
s.assert.Contains([]string{"/v1/pre-reboot", "/v1/steady-state"}, req.URL.String(), "Should request a valid url")
if s.Path != "" {
s.assert.Equal(s.Path, req.URL.String(), "Should use the specified URL")
}

s.assert.Equal(http.MethodPost, req.Method, "Should be POST request")
s.assert.Equal("true", strings.ToLower(req.Header.Get("fleet-lock-protocol")), "fleet-lock-protocol header should be set")

params, err := api.ParseRequest(req.Body)
s.assert.NoError(err, "Request should have the correct format")

if s.Group != "" {
s.assert.Equal(s.Group, params.Client.Group, "Should have expected group")
} else {
s.assert.NotEmpty(params.Client.Group, "Should have group set")
}
if s.ID != "" {
s.assert.Equal(s.ID, params.Client.ID, "Should have expected id")
} else {
s.assert.NotEmpty(params.Client.ID, "Should have id set")
}

rw.WriteHeader(s.StatusCode)
b, err := json.MarshalIndent(api.FleetLockResponse{
Kind: "ok",
Value: "Success",
}, "", " ")
if !s.assert.NoError(err, "Error in fake server: failed to prepare response") {
return
}

_, err = rw.Write(b)
s.assert.NoError(err, "Error in fake server: failed to send response")
}

func (s *FakeServer) URL() string {
return s.server.URL
}

func (s *FakeServer) Close() {
if s != nil && s.server != nil {
s.server.Close()
}
}
41 changes: 10 additions & 31 deletions pkg/server/client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package client

import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/heathcliff26/fleetlock/pkg/api"
"github.com/heathcliff26/fleetlock/pkg/fake"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -131,35 +128,17 @@ func TestGetAndSet(t *testing.T) {
})
}

func NewFakeServer(t *testing.T, statusCode int, path string) (*FleetlockClient, *httptest.Server) {
assert := assert.New(t)
func NewFakeServer(t *testing.T, statusCode int, path string) (*FleetlockClient, *fake.FakeServer) {
testGroup, testID := "testGroup", "testID"

srv := fake.NewFakeServer(t, statusCode, path)
srv.Group = testGroup
srv.ID = testID

srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
assert.Equal(path, req.URL.String(), "Request use the correct request URL")
assert.Equal(http.MethodPost, req.Method, "Should be POST request")
assert.Equal("true", strings.ToLower(req.Header.Get("fleet-lock-protocol")), "fleet-lock-protocol header should be set")

params, err := api.ParseRequest(req.Body)
assert.NoError(err, "Request should have the correct format")
assert.Equal("testGroup", params.Client.Group, "Should have Group set")
assert.Equal("testID", params.Client.ID, "Should have ID set")

rw.WriteHeader(statusCode)
b, err := json.MarshalIndent(api.FleetLockResponse{
Kind: "ok",
Value: "Success",
}, "", " ")
if !assert.NoError(err, "Error in fake server: failed to prepare response") {
return
}

_, err = rw.Write(b)
assert.NoError(err, "Error in fake server: failed to send response")
}))
c := &FleetlockClient{
url: srv.URL,
group: "testGroup",
appID: "testID",
url: srv.URL(),
group: testGroup,
appID: testID,
}
return c, srv
}

0 comments on commit 4926e61

Please sign in to comment.