| 1 | package appupdate |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/sylvinus/agent-vm/internal/cage" |
| 11 | "github.com/sylvinus/agent-vm/internal/diagnostics" |
| 12 | ) |
| 13 | |
| 14 | func TestUpdateCheckWaitsForProxyButDownloadReportsFailure(t *testing.T) { |
| 15 | for _, operation := range []string{"check", "download"} { |
| 16 | t.Run(operation, func(t *testing.T) { |
| 17 | profile := t.TempDir() |
| 18 | config := filepath.Join(profile, "config.json") |
| 19 | data, _ := json.Marshal(cage.DefaultConfig()) |
| 20 | if err := os.WriteFile(config, data, 0600); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | var output bytes.Buffer |
| 24 | code := Run(config, []string{operation}, &output) |
| 25 | var state State |
| 26 | if err := json.Unmarshal(output.Bytes(), &state); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if operation == "check" { |
| 30 | if code != 0 || state.Phase != "waiting" || !state.Retryable { |
| 31 | t.Fatalf("expected deferred check: code=%d %+v", code, state) |
| 32 | } |
| 33 | info, err := diagnostics.New(profile).Info() |
| 34 | if err != nil || info.Bytes != 0 { |
| 35 | t.Fatalf("ordinary wait logged as failure: %+v %v", info, err) |
| 36 | } |
| 37 | } else if code == 0 || state.Phase != "failed" { |
| 38 | t.Fatalf("requested download must report unavailable proxy: code=%d %+v", code, state) |
| 39 | } |
| 40 | }) |
| 41 | } |
| 42 | } |