(t *testing.T)
| 188 | } |
| 189 | |
| 190 | func TestReadSnapshotFile(t *testing.T) { |
| 191 | t.Run("Successful file read and unmarshal", func(t *testing.T) { |
| 192 | snapshotSpec := app.SnapshotSpec{ |
| 193 | Components: []app.SnapshotComponent{ |
| 194 | { |
| 195 | Name: "Named", |
| 196 | ContainerImage: "", |
| 197 | }, |
| 198 | { |
| 199 | Name: "Set name", |
| 200 | ContainerImage: "registry.io/repository/image:another", |
| 201 | }, |
| 202 | }, |
| 203 | } |
| 204 | fs := afero.NewMemMapFs() |
| 205 | spec := `{"components":[{"name": "Named", "containerImage":""},{"name": "Set name", "containerImage":"registry.io/repository/image:another"}]}` |
| 206 | |
| 207 | err := afero.WriteFile(fs, "/correct.json", []byte(spec), 0o644) |
| 208 | if err != nil { |
| 209 | t.Fatalf("Setup failure: could not write file: %v", err) |
| 210 | } |
| 211 | |
| 212 | content, err := afero.ReadFile(fs, "/correct.json") |
| 213 | assert.NoError(t, err) |
| 214 | got, err := readSnapshotSource(content) |
| 215 | assert.Equal(t, snapshotSpec, got) |
| 216 | assert.NoError(t, err) |
| 217 | }) |
| 218 | |
| 219 | t.Run("Invalid Spec", func(t *testing.T) { |
| 220 | fs := afero.NewMemMapFs() |
| 221 | spec := `bad spec` |
| 222 | specFile := "/badSpec.json" |
| 223 | |
| 224 | err := afero.WriteFile(fs, specFile, []byte(spec), 0o644) |
| 225 | if err != nil { |
| 226 | t.Fatalf("Setup failure: could not write file: %v", err) |
| 227 | } |
| 228 | |
| 229 | content, err := afero.ReadFile(fs, specFile) |
| 230 | assert.NoError(t, err) |
| 231 | _, err = readSnapshotSource(content) |
| 232 | wrapped := errors.New("error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type v1alpha1.SnapshotSpec") |
| 233 | expected := fmt.Errorf("unable to parse Snapshot specification from %s: %w", spec, wrapped) |
| 234 | assert.Error(t, err, expected) |
| 235 | }) |
| 236 | |
| 237 | t.Run("Snapshot with .spec wrapper (cluster record format)", func(t *testing.T) { |
| 238 | snapshotSpec := app.SnapshotSpec{ |
| 239 | Components: []app.SnapshotComponent{ |
| 240 | { |
| 241 | Name: "Named", |
| 242 | ContainerImage: "registry.io/repository/image:tag", |
| 243 | }, |
| 244 | }, |
| 245 | } |
| 246 | fs := afero.NewMemMapFs() |
| 247 | // Simulate a cluster record format with .spec wrapper |
nothing calls this directly
no test coverage detected