1
0
Fork 0

More HTTP-level testing of Load

master
Philip O'Toole 2 years ago
parent 27587634c6
commit b978e3857d

@ -1,6 +1,7 @@
package http package http
import ( import (
"bytes"
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -8,6 +9,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -687,6 +689,75 @@ func Test_BackupFlagsNoLeaderOK(t *testing.T) {
} }
} }
func Test_LoadOK(t *testing.T) {
m := &MockStore{}
c := &mockClusterService{}
s := New("127.0.0.1:0", m, c, nil)
if err := s.Start(); err != nil {
t.Fatalf("failed to start service")
}
defer s.Close()
client := &http.Client{}
host := fmt.Sprintf("http://%s", s.Addr().String())
resp, err := client.Post(host+"/db/load", "application/octet-stream", strings.NewReader("SELECT"))
if err != nil {
t.Fatalf("failed to make load request")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Fatalf("failed to get expected StatusOK for load, got %d", resp.StatusCode)
}
}
func Test_LoadFlagsNoLeader(t *testing.T) {
m := &MockStore{
leaderAddr: "foo:1234",
}
c := &mockClusterService{
apiAddr: "http://1.2.3.4:999",
}
s := New("127.0.0.1:0", m, c, nil)
if err := s.Start(); err != nil {
t.Fatalf("failed to start service")
}
defer s.Close()
testData, err := os.ReadFile("testdata/load.db")
if err != nil {
t.Fatalf("failed to load test SQLite data")
}
m.loadFn = func(br *command.LoadRequest) error {
return store.ErrNotLeader
}
clusterLoadCalled := false
c.loadFn = func(lr *command.LoadRequest, nodeAddr string, timeout time.Duration) error {
clusterLoadCalled = true
if bytes.Compare(lr.Data, testData) != 0 {
t.Fatalf("wrong data passed to cluster load")
}
return nil
}
client := &http.Client{}
host := fmt.Sprintf("http://%s", s.Addr().String())
resp, err := client.Post(host+"/db/load", "application/octet-stream", bytes.NewReader(testData))
if err != nil {
t.Fatalf("failed to make load request")
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("failed to get expected StatusOK for load, got %d", resp.StatusCode)
}
if !clusterLoadCalled {
t.Fatalf("cluster load was not called")
}
}
func Test_RegisterStatus(t *testing.T) { func Test_RegisterStatus(t *testing.T) {
var stats *mockStatusReporter var stats *mockStatusReporter
m := &MockStore{} m := &MockStore{}
@ -1046,6 +1117,7 @@ type MockStore struct {
executeFn func(er *command.ExecuteRequest) ([]*command.ExecuteResult, error) executeFn func(er *command.ExecuteRequest) ([]*command.ExecuteResult, error)
queryFn func(qr *command.QueryRequest) ([]*command.QueryRows, error) queryFn func(qr *command.QueryRequest) ([]*command.QueryRows, error)
backupFn func(br *command.BackupRequest, dst io.Writer) error backupFn func(br *command.BackupRequest, dst io.Writer) error
loadFn func(lr *command.LoadRequest) error
leaderAddr string leaderAddr string
} }
@ -1063,10 +1135,6 @@ func (m *MockStore) Query(qr *command.QueryRequest) ([]*command.QueryRows, error
return nil, nil return nil, nil
} }
func (m *MockStore) Load(lr *command.LoadRequest) error {
return nil
}
func (m *MockStore) Join(id, addr string, voter bool) error { func (m *MockStore) Join(id, addr string, voter bool) error {
return nil return nil
} }
@ -1098,6 +1166,13 @@ func (m *MockStore) Backup(br *command.BackupRequest, w io.Writer) error {
return m.backupFn(br, w) return m.backupFn(br, w)
} }
func (m *MockStore) Load(lr *command.LoadRequest) error {
if m.loadFn != nil {
return m.loadFn(lr)
}
return nil
}
type mockClusterService struct { type mockClusterService struct {
apiAddr string apiAddr string
executeFn func(er *command.ExecuteRequest, addr string, t time.Duration) ([]*command.ExecuteResult, error) executeFn func(er *command.ExecuteRequest, addr string, t time.Duration) ([]*command.ExecuteResult, error)

Binary file not shown.
Loading…
Cancel
Save