| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo" |
| 5 | "amadan.net/oficina/equipo/internal/equipo/library" |
| 6 | "errors" |
| 7 | "gorm.io/gorm" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestPlaybookTemplatesVersionAcceptance(t *testing.T) { |
| 12 | f := newFixture(t) |
| 13 | templates := library.PlaybookCatalog() |
| 14 | copyValue := "Version one" |
| 15 | deps := library.Dependencies{ |
| 16 | Authorize: func(_ *gorm.DB, _ equipo.Actor) (library.Access, error) { |
| 17 | return library.Access{TeamID: f.TeamID, Owner: true}, nil |
| 18 | }, |
| 19 | Audit: func(_ *gorm.DB, _ equipo.Actor, _, _ string) error { return nil }, Copy: func(string) string { return copyValue }, PlaybookCatalog: func() []library.Template { return templates }, |
| 20 | Invalid: equipo.ErrInvalid, Forbidden: equipo.ErrForbidden, Conflict: equipo.ErrConflict, NotFound: equipo.ErrNotFound, |
| 21 | } |
| 22 | must(t, f.DB.G.Transaction(func(tx *gorm.DB) error { |
| 23 | s := library.New(tx, deps) |
| 24 | row, err := s.InstallPlaybookTemplate(owner, templates[0].Ref, equipo.Mutation{RequestID: "install"}) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | templates = append(templates, templates[0]) |
| 29 | templates[len(templates)-1].Ref.Version = 2 |
| 30 | copyValue = "Version two" |
| 31 | current, err := s.Playbook(owner, row.ID) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | if current.Name != row.Name { |
| 36 | t.Fatal("catalog changed copy") |
| 37 | } |
| 38 | diff, err := s.PlaybookTemplateDiff(owner, row.ID, templates[len(templates)-1].Ref) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | copyValue = "Changed after preview" |
| 43 | if _, err = s.ApplyPlaybookTemplate(owner, row.ID, diff, []string{"name"}, equipo.Mutation{RequestID: "changed", Revision: 1}); !errors.Is(err, equipo.ErrConflict) { |
| 44 | t.Fatal("changed offered update accepted", err) |
| 45 | } |
| 46 | copyValue = "Version two" |
| 47 | updated, err := s.ApplyPlaybookTemplate(owner, row.ID, diff, []string{"name"}, equipo.Mutation{RequestID: "apply", Revision: 1}) |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | current, err = s.Playbook(owner, row.ID) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | if current.Name != "Version two" || current.Template.Version != 2 || current.Steps[0].Instructions != row.Steps[0].Instructions { |
| 56 | t.Fatal("update changed unselected fields or lost version", current) |
| 57 | } |
| 58 | copyValue = "Later release" |
| 59 | retry, err := s.ApplyPlaybookTemplate(owner, row.ID, diff, []string{"name"}, equipo.Mutation{RequestID: "apply", Revision: 1}) |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | if retry.Revision != updated.Revision || retry.Name != updated.Name { |
| 64 | t.Fatal("retry changed accepted update") |
| 65 | } |
| 66 | return nil |
| 67 | })) |
| 68 | } |
| 69 | |