| 1 | package teamcontract |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "math" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 10 | ) |
| 11 | |
| 12 | func finitePolicy() team.Policy { |
| 13 | return team.DefaultPolicy(1_000_000_000, team.Rates{Version: 1, InputMicrosPer: 1, OutputMicrosPer: 1}) |
| 14 | } |
| 15 | |
| 16 | func TestPolicyConsumesExactlyOneJSONValue(t *testing.T) { |
| 17 | wire, err := json.Marshal(finitePolicy()) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | if _, err := team.ParsePolicy(append(append([]byte(nil), wire...), []byte(" \n\t")...)); err != nil { |
| 22 | t.Fatalf("valid policy with whitespace: %v", err) |
| 23 | } |
| 24 | for _, suffix := range []string{"}", "]", " {}", " null", " invalid"} { |
| 25 | t.Run(suffix, func(t *testing.T) { |
| 26 | input := append(append([]byte(nil), wire...), []byte(suffix)...) |
| 27 | if _, err := team.ParsePolicy(input); err == nil { |
| 28 | t.Fatalf("accepted bytes after policy object: %q", suffix) |
| 29 | } |
| 30 | }) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestPolicyCannotOverflowDuration(t *testing.T) { |
| 35 | seconds := int64(math.MaxInt64)/int64(time.Second) + 1 |
| 36 | if int64(int(seconds)) != seconds { |
| 37 | t.Skip("duration-overflow value does not fit this platform's int") |
| 38 | } |
| 39 | for name, set := range map[string]func(*team.Policy){ |
| 40 | "lease": func(p *team.Policy) { p.LeaseSeconds = int(seconds) }, |
| 41 | "job": func(p *team.Policy) { p.JobSeconds = int(seconds) }, |
| 42 | "attempt": func(p *team.Policy) { p.AttemptSeconds = int(seconds) }, |
| 43 | } { |
| 44 | t.Run(name, func(t *testing.T) { |
| 45 | p := finitePolicy() |
| 46 | set(&p) |
| 47 | if err := p.Validate(); err == nil { |
| 48 | t.Fatal("accepted seconds that overflow time.Duration") |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | } |
| 53 | |