| 22 | ) |
| 23 | |
| 24 | func TestAvoidDuplicateAutomation(t *testing.T) { |
| 25 | tests := []struct { |
| 26 | name string |
| 27 | automateNames []string |
| 28 | expectedToManage bool |
| 29 | }{ |
| 30 | { |
| 31 | name: "do not manage if wildcard is automated", |
| 32 | automateNames: []string{"*.example.com"}, |
| 33 | expectedToManage: false, |
| 34 | }, |
| 35 | { |
| 36 | name: "manage if no automation configured", |
| 37 | automateNames: []string{}, |
| 38 | expectedToManage: true, |
| 39 | }, |
| 40 | { |
| 41 | name: "manage if explicitly requested even when wildcard automated", |
| 42 | automateNames: []string{"*.example.com", "sub.example.com"}, |
| 43 | expectedToManage: true, |
| 44 | }, |
| 45 | } |
| 46 | |
| 47 | for _, tc := range tests { |
| 48 | t.Run(tc.name, func(t *testing.T) { |
| 49 | automateJSON, err := json.Marshal(tc.automateNames) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | |
| 54 | tlsApp := &TLS{ |
| 55 | Automation: &AutomationConfig{ |
| 56 | Policies: []*AutomationPolicy{ |
| 57 | { |
| 58 | IssuersRaw: []json.RawMessage{ |
| 59 | []byte(`{"module": "internal"}`), |
| 60 | }, |
| 61 | }, |
| 62 | }, |
| 63 | }, |
| 64 | CertificatesRaw: map[string]json.RawMessage{ |
| 65 | "automate": automateJSON, |
| 66 | }, |
| 67 | } |
| 68 | |
| 69 | var cfg caddy.Config |
| 70 | ctx, err := caddy.ProvisionContext(&cfg) |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | |
| 75 | if err := tlsApp.Provision(ctx); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | |
| 79 | // simulate a case wherein the HTTP app starts first and |
| 80 | // tells the TLS app about the following auto-HTTPS domains |
| 81 | httpDomains := map[string]struct{}{"sub.example.com": {}} |