carlos / platform Public

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

Plain git — no account needed to clone.

Download

Download this file

1package appconfig
2
3import (
4 "bytes"
5 "context"
6 "errors"
7 "os"
8 "testing"
9
10 "github.com/carlosframework/platform/internal/release"
11)
12
13// Review Focus 1: an app that has never had config gets none from setting
14// the defaults, so no box sees a config change.
15func TestSwapPolicyDefaultsWriteNothing(t *testing.T) {
16 ctx := context.Background()
17 s := release.NewDirStore(t.TempDir())
18 got, err := SetSwapPolicy(ctx, s, "bab", "shop", SwapOverlap, 1)
19 if err != nil {
20 t.Fatal(err)
21 }
22 if got != (SwapPolicy{Swap: SwapOverlap, Concurrency: 1}) {
23 t.Fatalf("policy = %+v, want overlap/1", got)
24 }
25 if _, _, err := s.Get(ctx, Key("bab", "shop")); !errors.Is(err, os.ErrNotExist) {
26 t.Fatalf("setting the defaults created config.json: %v", err)
27 }
28 if _, _, err := s.Get(ctx, GenKey("bab", "shop")); !errors.Is(err, os.ErrNotExist) {
29 t.Fatalf("setting the defaults touched the config generation: %v", err)
30 }
31}
32
33func TestSwapPolicyHoldKeepsTheRestOfTheBundle(t *testing.T) {
34 ctx := context.Background()
35 s := release.NewDirStore(t.TempDir())
36 if err := SetEnv(ctx, s, "bab", "shop", map[string]string{"A": "1"}); err != nil {
37 t.Fatal(err)
38 }
39 if err := SetExecDelivery(ctx, s, "bab", "shop", ExecDeliveryFile); err != nil {
40 t.Fatal(err)
41 }
42 got, err := SetSwapPolicy(ctx, s, "bab", "shop", SwapHold, 3)
43 if err != nil || got != (SwapPolicy{Swap: SwapHold, Concurrency: 3}) {
44 t.Fatalf("policy = %+v, %v; want hold/3", got, err)
45 }
46 m, err := Get(ctx, s, "bab", "shop")
47 if err != nil {
48 t.Fatal(err)
49 }
50 if m.Swap != SwapHold || m.SwapConcurrency != 3 || m.Vars["A"].Value != "1" || m.ExecDelivery != ExecDeliveryFile {
51 t.Fatalf("manifest after set = %+v", m)
52 }
53 if _, _, err := s.Get(ctx, GenKey("bab", "shop")); err != nil {
54 t.Fatalf("a real change must touch the config generation: %v", err)
55 }
56 if _, err := SetSwapPolicy(ctx, s, "bab", "shop", SwapOverlap, 1); err != nil {
57 t.Fatal(err)
58 }
59 raw, _, err := s.Get(ctx, Key("bab", "shop"))
60 if err != nil {
61 t.Fatal(err)
62 }
63 if bytes.Contains(raw, []byte(`"swap`)) {
64 t.Fatalf("defaults still stored: %s", raw)
65 }
66}
67
68func TestSwapPolicyLeavesTheUnnamedHalf(t *testing.T) {
69 ctx := context.Background()
70 s := release.NewDirStore(t.TempDir())
71 if _, err := SetSwapPolicy(ctx, s, "bab", "shop", SwapHold, 0); err != nil {
72 t.Fatal(err)
73 }
74 got, err := SetSwapPolicy(ctx, s, "bab", "shop", "", 2)
75 if err != nil || got != (SwapPolicy{Swap: SwapHold, Concurrency: 2}) {
76 t.Fatalf("policy = %+v, %v; want hold kept and 2 set", got, err)
77 }
78}
79
80func TestSwapPolicyRefusesBadValues(t *testing.T) {
81 ctx := context.Background()
82 s := release.NewDirStore(t.TempDir())
83 if _, err := SetSwapPolicy(ctx, s, "bab", "shop", "fast", 0); err == nil {
84 t.Error("swap fast was accepted")
85 }
86 if _, err := SetSwapPolicy(ctx, s, "bab", "shop", "", -1); err == nil {
87 t.Error("concurrency -1 was accepted")
88 }
89 if _, err := SetSwapPolicy(ctx, s, "bab", "Shop!", SwapHold, 0); err == nil {
90 t.Error("an invalid app name was accepted")
91 }
92 if _, _, err := s.Get(ctx, Key("bab", "shop")); !errors.Is(err, os.ErrNotExist) {
93 t.Fatalf("a refused call wrote config.json: %v", err)
94 }
95}
96
97func TestPolicyOfFailsTowardHold(t *testing.T) {
98 if got := PolicyOf(nil); got != (SwapPolicy{Swap: SwapOverlap, Concurrency: 1}) {
99 t.Errorf("PolicyOf(nil) = %+v", got)
100 }
101 if got := PolicyOf(&Manifest{Swap: "fast"}); got.Swap != SwapHold {
102 t.Errorf("an unknown stored mode reads as %q, want hold", got.Swap)
103 }
104 if got := PolicyOf(&Manifest{SwapConcurrency: -2}); got.Concurrency != 1 {
105 t.Errorf("a negative stored concurrency reads as %d, want 1", got.Concurrency)
106 }
107}
108