|
| 1 | +package plan |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestParse(t *testing.T) { |
| 11 | + t.Run("valid plan json", func(t *testing.T) { |
| 12 | + raw := `{"files":[{"path":"/tmp/test","content":"aGVsbG8gd29ybGQ="}],"instructions":[{"name":"setup","command":"/bin/sh"}]}` |
| 13 | + |
| 14 | + p, err := Parse([]byte(raw)) |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("unexpected error: %v", err) |
| 17 | + } |
| 18 | + |
| 19 | + if len(p.Files) != 1 || p.Files[0].Path != "/tmp/test" { |
| 20 | + t.Errorf("unexpected files: %+v", p.Files) |
| 21 | + } |
| 22 | + |
| 23 | + if len(p.OneTimeInstructions) != 1 || p.OneTimeInstructions[0].Name != "setup" { |
| 24 | + t.Errorf("unexpected instructions: %+v", p.OneTimeInstructions) |
| 25 | + } |
| 26 | + }) |
| 27 | + |
| 28 | + t.Run("invalid plan json", func(t *testing.T) { |
| 29 | + _, err := Parse([]byte(`{not valid json`)) |
| 30 | + if err == nil { |
| 31 | + t.Fatal("expected an error") |
| 32 | + } |
| 33 | + |
| 34 | + if !strings.Contains(err.Error(), "failed to parse plan") { |
| 35 | + t.Errorf("unexpected error message: %v", err) |
| 36 | + } |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +func TestPlanEncodeParse(t *testing.T) { |
| 41 | + original := Plan{ |
| 42 | + Files: []File{ |
| 43 | + { |
| 44 | + Content: "aGVsbG8gd29ybGQ=", |
| 45 | + UID: -1, |
| 46 | + GID: -1, |
| 47 | + Path: "/etc/myapp/config.yaml", |
| 48 | + Permissions: "0644", |
| 49 | + }, |
| 50 | + { |
| 51 | + Path: "/etc/myapp/", |
| 52 | + Directory: true, |
| 53 | + }, |
| 54 | + }, |
| 55 | + OneTimeInstructions: []OneTimeInstruction{ |
| 56 | + { |
| 57 | + CommonInstruction: CommonInstruction{ |
| 58 | + Name: "install", |
| 59 | + Command: "/bin/sh", |
| 60 | + Args: []string{"-c", "echo hello"}, |
| 61 | + }, |
| 62 | + SaveOutput: true, |
| 63 | + }, |
| 64 | + }, |
| 65 | + PeriodicInstructions: []PeriodicInstruction{ |
| 66 | + { |
| 67 | + CommonInstruction: CommonInstruction{ |
| 68 | + Name: "healthcheck", |
| 69 | + Command: "/bin/sh", |
| 70 | + Args: []string{"-c", "echo ok"}, |
| 71 | + }, |
| 72 | + PeriodSeconds: 60, |
| 73 | + SaveStderrOutput: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + data, err := json.Marshal(original) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("marshal: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + var restored Plan |
| 84 | + restored, err = Parse([]byte(data)) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("parse: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + if !reflect.DeepEqual(original, restored) { |
| 90 | + t.Errorf("plan changed after encode/parse\nwant: %+v\n got: %+v", original, restored) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestJSONKeys(t *testing.T) { |
| 95 | + tests := []struct { |
| 96 | + name string |
| 97 | + input string |
| 98 | + validate func(t *testing.T, p Plan) |
| 99 | + }{ |
| 100 | + { |
| 101 | + name: "files", |
| 102 | + input: `{"files":[{"path":"/tmp/test","content":"aGVsbG8gd29ybGQ="}]}`, |
| 103 | + validate: func(t *testing.T, p Plan) { |
| 104 | + if len(p.Files) != 1 || p.Files[0].Path != "/tmp/test" { |
| 105 | + t.Errorf("got %+v", p.Files) |
| 106 | + } |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "instructions", |
| 111 | + input: `{"instructions":[{"name":"test","command":"/bin/sh"}]}`, |
| 112 | + validate: func(t *testing.T, p Plan) { |
| 113 | + if len(p.OneTimeInstructions) != 1 || p.OneTimeInstructions[0].Name != "test" { |
| 114 | + t.Errorf("got %+v", p.OneTimeInstructions) |
| 115 | + } |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "periodicInstructions", |
| 120 | + input: `{"periodicInstructions":[{"name":"cron","periodSeconds":300}]}`, |
| 121 | + validate: func(t *testing.T, p Plan) { |
| 122 | + if len(p.PeriodicInstructions) != 1 || p.PeriodicInstructions[0].PeriodSeconds != 300 { |
| 123 | + t.Errorf("got %+v", p.PeriodicInstructions) |
| 124 | + } |
| 125 | + }, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "probes / httpGet", |
| 129 | + input: `{"probes":{"web":{"httpGet":{"url":"http://localhost/health","insecure":true}}}}`, |
| 130 | + validate: func(t *testing.T, p Plan) { |
| 131 | + probe, ok := p.Probes["web"] |
| 132 | + if !ok { |
| 133 | + t.Fatal("probes.web missing") |
| 134 | + } |
| 135 | + if probe.HTTPGetAction.URL != "http://localhost/health" || !probe.HTTPGetAction.Insecure { |
| 136 | + t.Errorf("got %+v", probe.HTTPGetAction) |
| 137 | + } |
| 138 | + }, |
| 139 | + }, |
| 140 | + } |
| 141 | + |
| 142 | + for _, tt := range tests { |
| 143 | + t.Run(tt.name, func(t *testing.T) { |
| 144 | + var p Plan |
| 145 | + |
| 146 | + p, err := Parse([]byte(tt.input)) |
| 147 | + if err != nil { |
| 148 | + t.Fatalf("parse: %v", err) |
| 149 | + } |
| 150 | + |
| 151 | + tt.validate(t, p) |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestChecksum(t *testing.T) { |
| 157 | + input := []byte(`{"files":[]}`) |
| 158 | + |
| 159 | + // verify idempotency by calling Checksum() twice with the same input |
| 160 | + output1, output2 := Checksum(input), Checksum(input) |
| 161 | + if output1 != output2 { |
| 162 | + t.Error("checksum result is not idempotent") |
| 163 | + } |
| 164 | +} |
0 commit comments