Something went wrong. Try again.
An orchestrator running microVMs on demand. Built for AI agent sandboxes, CI engines, and more.
Something went wrong. Try again.
3.0 kB · 112 lines
Go
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113package initd
import ( "bytes" "context" "encoding/binary" "io" "net" "net/http" "net/http/httptest" "strings" "testing")
// fakeInitd serves a forced-frame /exec response so the client can be// exercised without a guest. We bypass the vsock dialer by pointing the// client at an httptest server via DialContext.type fakeInitd struct { *httptest.Server}
func newFakeInitd(handler http.Handler) *fakeInitd { return &fakeInitd{httptest.NewServer(handler)}}
func (f *fakeInitd) httpClient() *http.Client { addr := strings.TrimPrefix(f.URL, "http://") return &http.Client{ Transport: &http.Transport{ DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { var d net.Dialer return d.DialContext(ctx, "tcp", addr) }, }, }}
// newClientWithHTTP builds a *Client whose http transport is wired to the// given httptest server, sidestepping the unix dial + CONNECT handshake.func newClientWithHTTP(httpc *http.Client) *Client { return &Client{ udsPath: "(test)", port: VsockPort, httpClient: httpc, }}
func writeFrame(w io.Writer, kind FrameKind, payload []byte) error { hdr := make([]byte, FrameHeaderSize) EncodeHeader(hdr, FrameHeader{Kind: kind, Length: uint32(len(payload))}) if _, err := w.Write(hdr); err != nil { return err } if len(payload) > 0 { if _, err := w.Write(payload); err != nil { return err } } return nil}
func TestExecParsesFrames(t *testing.T) { srv := newFakeInitd(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/exec" || r.Method != http.MethodPost { http.Error(w, "wrong route", http.StatusNotFound) return } w.Header().Set("Content-Type", "application/octet-stream") w.WriteHeader(http.StatusOK) _ = writeFrame(w, FrameStdout, []byte("hello\n")) _ = writeFrame(w, FrameStderr, []byte("oops\n")) exit := make([]byte, 4) binary.BigEndian.PutUint32(exit, uint32(int32(7))) _ = writeFrame(w, FrameExit, exit) })) defer srv.Close()
c := newClientWithHTTP(srv.httpClient())
var stdout, stderr bytes.Buffer res, err := c.Exec(context.Background(), ExecRequest{Command: []string{"true"}}, &stdout, &stderr) if err != nil { t.Fatalf("Exec: %v", err) } if res.ExitCode != 7 { t.Errorf("exit code = %d, want 7", res.ExitCode) } if got := stdout.String(); got != "hello\n" { t.Errorf("stdout = %q", got) } if got := stderr.String(); got != "oops\n" { t.Errorf("stderr = %q", got) }}
func TestExecCarriesError(t *testing.T) { srv := newFakeInitd(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/octet-stream") w.WriteHeader(http.StatusOK) _ = writeFrame(w, FrameError, []byte("exec failed: ENOENT")) })) defer srv.Close()
c := newClientWithHTTP(srv.httpClient()) res, err := c.Exec(context.Background(), ExecRequest{Command: []string{"missing"}}, nil, nil) if err != nil { t.Fatalf("Exec: %v", err) } if res.Error != "exec failed: ENOENT" { t.Errorf("Error = %q", res.Error) }}