79 lines
2.4 KiB
Go
79 lines
2.4 KiB
Go
package libraryfreeze
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestDockerControllerUsesScopedIDsAndGracefulSignal(t *testing.T) {
|
|
root, err := os.MkdirTemp(os.TempDir(), "kgf-")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(root)
|
|
socket := filepath.Join(root, "engine.sock")
|
|
listener, err := net.Listen("unix", socket)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id := strings.Repeat("a", 64)
|
|
running := true
|
|
var writes []string
|
|
server := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case r.Method == "GET" && strings.HasSuffix(r.URL.Path, "/containers/json"):
|
|
if !strings.Contains(r.URL.Query().Get("filters"), "test-source") {
|
|
t.Error("missing scope filter")
|
|
}
|
|
_ = json.NewEncoder(w).Encode([]map[string]string{{"Id": id}})
|
|
case r.Method == "GET" && strings.HasSuffix(r.URL.Path, "/json"):
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"Id": id, "Config": map[string]any{"Labels": map[string]string{sourceLabel: "test-source", environmentLabel: "test", roleLabel: "writer"}}, "State": map[string]any{"Running": running, "Status": map[bool]string{true: "running", false: "exited"}[running], "Health": map[string]string{"Status": "healthy"}}})
|
|
case r.Method == "POST" && strings.HasSuffix(r.URL.Path, "/kill"):
|
|
if r.URL.Query().Get("signal") != "SIGTERM" {
|
|
t.Error("unsafe stop signal")
|
|
}
|
|
writes = append(writes, "stop")
|
|
running = false
|
|
w.WriteHeader(204)
|
|
case r.Method == "POST" && strings.HasSuffix(r.URL.Path, "/start"):
|
|
writes = append(writes, "start")
|
|
running = true
|
|
w.WriteHeader(204)
|
|
default:
|
|
w.WriteHeader(404)
|
|
}
|
|
})}
|
|
go server.Serve(listener)
|
|
defer server.Close()
|
|
control, err := NewDockerController(socket, "test-source", time.Second)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer control.Close()
|
|
journalRoot := filepath.Join(root, "journal")
|
|
if err = os.Mkdir(journalRoot, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
engine, err := Open(journalRoot, "test-source", control)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer engine.Close()
|
|
result, err := engine.Run(context.Background(), "fixture-job", func(context.Context) (string, error) {
|
|
if running {
|
|
t.Fatal("writer was not stopped")
|
|
}
|
|
return strings.Repeat("b", 64), nil
|
|
})
|
|
if err != nil || result.State != "succeeded" || strings.Join(writes, ",") != "stop,start" {
|
|
t.Fatalf("result=%+v err=%v writes=%v", result, err, writes)
|
|
}
|
|
}
|