| 1 |
package atomtest |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"codeberg.org/miekg/dns/cmd/atomdns/atom" |
| 8 |
) |
| 9 |
|
| 10 |
// New returns a server suitable for testing. Use cancel to shutdown the server |
| 11 |
// Use [server.Addr] to get the listening addresses. NewTest starts 2 servers, one on UDP and another on TCP. |
| 12 |
// The config will be prefixed with, unless global is set in which case that string is used as the global |
| 13 |
// block. |
| 14 |
// |
| 15 |
// { |
| 16 |
// dns { |
| 17 |
// addr [::]:0 |
| 18 |
// limits { |
| 19 |
// run 1 |
| 20 |
// } |
| 21 |
// } |
| 22 |
// } |
| 23 |
func New(config string, global ...string) (server *atom.Server, cancel func(), err error) { |
| 24 |
testconfig := ` |
| 25 |
{ |
| 26 |
dns { |
| 27 |
addr [::]:0 |
| 28 |
limits { |
| 29 |
run 1 |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
` |
| 34 |
if len(global) > 0 { |
| 35 |
testconfig = global[0] |
| 36 |
} |
| 37 |
|
| 38 |
config = testconfig + config |
| 39 |
s, err := atom.New("<test>", strings.NewReader(config)) |
| 40 |
if err != nil { |
| 41 |
return nil, nil, err |
| 42 |
} |
| 43 |
if err := s.Start(); err != nil { |
| 44 |
return nil, nil, err |
| 45 |
} |
| 46 |
return s, func() { s.Shutdown(context.TODO()) }, nil |
| 47 |
} |