This commit is contained in:
1 parent
df94fc6d8b
commit
a2a96f6a46
5 files changed
+434
-42
No files matched your search
@@ -0,0 +1,133 @@
|
||||
package carriernodeclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListFilesMergesAcrossInstances(t *testing.T) {
|
||||
first := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"files":[{"rel":"a.mp4"}]}`))
|
||||
}))
|
||||
defer first.Close()
|
||||
second := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"files":[{"rel":"b.mp4"}]}`))
|
||||
}))
|
||||
defer second.Close()
|
||||
|
||||
client := NewMulti("", func(ctx context.Context) []string { return []string{first.URL, second.URL} })
|
||||
|
||||
files, err := client.ListFiles(context.Background(), "", false, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListFiles error: %v", err)
|
||||
}
|
||||
if len(files) != 2 {
|
||||
t.Fatalf("ListFiles returned %d files, want 2: %+v", len(files), files)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListFilesSkipsUnreachableInstance(t *testing.T) {
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"files":[{"rel":"a.mp4"}]}`))
|
||||
}))
|
||||
defer up.Close()
|
||||
down := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}))
|
||||
down.Close() // closed immediately so requests to it fail outright
|
||||
|
||||
client := NewMulti("", func(ctx context.Context) []string { return []string{down.URL, up.URL} })
|
||||
|
||||
files, err := client.ListFiles(context.Background(), "", false, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListFiles error: %v", err)
|
||||
}
|
||||
if len(files) != 1 || files[0].Rel != "a.mp4" {
|
||||
t.Fatalf("ListFiles = %+v, want just the reachable instance's file", files)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListFilesErrorsWhenNoInstanceReachable(t *testing.T) {
|
||||
down := httptest.NewServer(nil)
|
||||
down.Close()
|
||||
|
||||
client := NewMulti("", func(ctx context.Context) []string { return []string{down.URL} })
|
||||
|
||||
if _, err := client.ListFiles(context.Background(), "", false, 0); err == nil {
|
||||
t.Fatal("ListFiles error = nil, want an error when every instance is unreachable")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeFallsBackToNextInstance(t *testing.T) {
|
||||
notFound := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer notFound.Close()
|
||||
found := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Length", "42")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer found.Close()
|
||||
|
||||
client := NewMulti("", func(ctx context.Context) []string { return []string{notFound.URL, found.URL} })
|
||||
|
||||
exists, size, _, err := client.Probe(context.Background(), "/media/a.mp4")
|
||||
if err != nil {
|
||||
t.Fatalf("Probe error: %v", err)
|
||||
}
|
||||
if !exists {
|
||||
t.Fatal("Probe exists = false, want true (second instance has it)")
|
||||
}
|
||||
if size != 42 {
|
||||
t.Fatalf("Probe size = %d, want 42", size)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeExistsFalseWhenNoInstanceHasFile(t *testing.T) {
|
||||
notFound := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer notFound.Close()
|
||||
|
||||
client := NewMulti("", func(ctx context.Context) []string { return []string{notFound.URL, notFound.URL} })
|
||||
|
||||
exists, _, _, err := client.Probe(context.Background(), "/media/missing.mp4")
|
||||
if err != nil {
|
||||
t.Fatalf("Probe error: %v", err)
|
||||
}
|
||||
if exists {
|
||||
t.Fatal("Probe exists = true, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyAlwaysTargetsPrimaryInstance(t *testing.T) {
|
||||
var primaryHit, secondaryHit bool
|
||||
primary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
primaryHit = true
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer primary.Close()
|
||||
secondary := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
secondaryHit = true
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer secondary.Close()
|
||||
|
||||
client := NewMulti("", func(ctx context.Context) []string { return []string{primary.URL, secondary.URL} })
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/thumbnails/x.jpg", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
client.Proxy(rec, req, "/thumbnails/x.jpg")
|
||||
|
||||
if !primaryHit {
|
||||
t.Fatal("Proxy did not hit the primary instance")
|
||||
}
|
||||
if secondaryHit {
|
||||
t.Fatal("Proxy hit the secondary instance, want primary-only")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user