carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "context"
5 "fmt"
6 "io"
7 "mime/multipart"
8 "net/http"
9 "net/http/httptest"
10 "os"
11 "path/filepath"
12 "strings"
13 "testing"
14)
15
16func TestValidSwapFlag(t *testing.T) {
17 for _, ok := range []string{"", "hold"} {
18 if err := validSwapFlag(ok); err != nil {
19 t.Errorf("validSwapFlag(%q) = %v", ok, err)
20 }
21 }
22 for _, bad := range []string{"overlap", "Hold", "fast"} {
23 if err := validSwapFlag(bad); err == nil || err.Error() != fmt.Sprintf(shipBadSwap, bad) {
24 t.Errorf("validSwapFlag(%q) = %v", bad, err)
25 }
26 }
27}
28
29func TestCheckSwapEcho(t *testing.T) {
30 if err := checkSwapEcho("", []byte(`{}`)); err != nil {
31 t.Errorf("no demand sent: %v", err)
32 }
33 if err := checkSwapEcho("hold", []byte(`{"swap":"hold"}`)); err != nil {
34 t.Errorf("echoed: %v", err)
35 }
36 if err := checkSwapEcho("hold", []byte(`{"result":"shipped"}`)); err == nil || err.Error() != shipSwapNoEcho {
37 t.Errorf("an old console's answer passed: %v", err)
38 }
39}
40
41func TestAPISessionShipSendsTheDemand(t *testing.T) {
42 var meta string
43 echo := `,"swap":"hold"`
44 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
45 mr, err := r.MultipartReader()
46 if err != nil {
47 t.Fatal(err)
48 }
49 var p *multipart.Part
50 if p, err = mr.NextPart(); err != nil {
51 t.Fatal(err)
52 }
53 b, _ := io.ReadAll(p)
54 meta = string(b)
55 w.Header().Set("Content-Type", "application/json")
56 w.WriteHeader(http.StatusCreated)
57 io.WriteString(w, `{"result":"shipped","version":"1.2.3"`+echo+`}`)
58 }))
59 defer ts.Close()
60 file := filepath.Join(t.TempDir(), "bin")
61 if err := os.WriteFile(file, []byte("x"), 0o600); err != nil {
62 t.Fatal(err)
63 }
64 ship := func(s *apiSession) error {
65 _, err := s.ship(context.Background(), apiAcct{Seg: "acct"}, "app", "1.2.3", "binary", "", "",
66 map[string]string{"linux-arm64": file}, nil, false)
67 return err
68 }
69 s := newTestSession(ts.URL)
70 s.shipSwap = "hold"
71 if err := ship(s); err != nil {
72 t.Fatalf("ship: %v", err)
73 }
74 if !strings.Contains(meta, `"swap":"hold"`) {
75 t.Fatalf("meta = %s, want the demand", meta)
76 }
77 echo = ""
78 if err := ship(s); err == nil || err.Error() != shipSwapNoEcho {
79 t.Fatalf("an answer without the echo: %v", err)
80 }
81 s.shipSwap = ""
82 if err := ship(s); err != nil {
83 t.Fatalf("a ship with no demand needs no echo: %v", err)
84 }
85 if strings.Contains(meta, "swap") {
86 t.Fatalf("a ship with no demand sent one: %s", meta)
87 }
88}
89