| 1 | package hostwrite |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | type rejectSelector struct{ calls *int } |
| 10 | |
| 11 | func (s rejectSelector) Select(context.Context, string, string) (Work, error) { |
| 12 | *s.calls++ |
| 13 | return nil, ErrUnavailable |
| 14 | } |
| 15 | |
| 16 | func TestFlagAndDisabledParity(t *testing.T) { |
| 17 | for _, tc := range []struct { |
| 18 | value string |
| 19 | want bool |
| 20 | }{{"", false}, {" \n", false}, {"0", true}, {"1", true}} { |
| 21 | calls := 0 |
| 22 | got := Enabled(func(key string) string { |
| 23 | calls++ |
| 24 | if key != "CARLOS_HOST_CLAIM" { |
| 25 | t.Fatal(key) |
| 26 | } |
| 27 | return tc.value |
| 28 | }) |
| 29 | if got != tc.want || calls != 1 { |
| 30 | t.Fatal(tc, got, calls) |
| 31 | } |
| 32 | } |
| 33 | calls, writes := 0, 0 |
| 34 | work, err := (Config{Selector: rejectSelector{&calls}}).Select(context.Background(), "host", "config") |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | sentinel := errors.New("legacy error") |
| 39 | err = work.With(context.Background(), Identity{}, func() error { writes++; return sentinel }) |
| 40 | if err != sentinel || calls != 0 || writes != 1 { |
| 41 | t.Fatal(err, calls, writes) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestEnabledMissingProviderRefuses(t *testing.T) { |
| 46 | work, err := (Config{Enabled: true}).Select(context.Background(), "host", "config") |
| 47 | if work != nil || !errors.Is(err, ErrUnavailable) { |
| 48 | t.Fatal(work, err) |
| 49 | } |
| 50 | } |
| 51 | |