From 4776fa136112e714747391b70a296dff2e913831 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 27 May 2016 11:13:34 +0200 Subject: [PATCH] add parsers tests Signed-off-by: Martin --- acme/acme.go | 9 ++++--- acme/acme_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++ configuration.go | 7 +----- 3 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 acme/acme_test.go diff --git a/acme/acme.go b/acme/acme.go index 081e4d8e4..99e340ae4 100644 --- a/acme/acme.go +++ b/acme/acme.go @@ -163,7 +163,7 @@ func (dc *DomainsCertificate) needRenew() bool { // ACME allows to connect to lets encrypt and retrieve certs type ACME struct { Email string `description:"Email address used for registration"` - Domains []Domain `description:"SANs (alternative domains) to each main domain"` + Domains []Domain `description:"SANs (alternative domains) to each main domain using format: --acme.domains='main.com,san1.com,san2.com' --acme.domains='main.net,san1.net,san2.net'"` StorageFile string `description:"File used for certificates storage."` OnDemand bool `description:"Enable on demand certificate. This will request a certificate from Let's Encrypt during the first TLS handshake for a hostname that does not yet have a certificate."` CAServer string `description:"CA server to use."` @@ -181,12 +181,15 @@ func (ds *Domains) Set(str string) error { } // get function slice := strings.FieldsFunc(str, fargs) - if len(slice) >= 2 { + if len(slice) < 1 { return fmt.Errorf("Parse error ACME.Domain. Imposible to parse %s", str) } d := Domain{ Main: slice[0], - SANs: slice[1:], + SANs: []string{}, + } + if len(slice) > 1 { + d.SANs = slice[1:] } *ds = append(*ds, d) return nil diff --git a/acme/acme_test.go b/acme/acme_test.go new file mode 100644 index 000000000..302c5866e --- /dev/null +++ b/acme/acme_test.go @@ -0,0 +1,61 @@ +package acme + +import ( + "reflect" + "testing" +) + +func TestDomainsSet(t *testing.T) { + checkMap := map[string]Domains{ + "": {}, + "foo.com": {Domain{Main: "foo.com", SANs: []string{}}}, + "foo.com,bar.net": {Domain{Main: "foo.com", SANs: []string{"bar.net"}}}, + "foo.com,bar1.net,bar2.net,bar3.net": {Domain{Main: "foo.com", SANs: []string{"bar1.net", "bar2.net", "bar3.net"}}}, + } + for in, check := range checkMap { + ds := Domains{} + ds.Set(in) + if !reflect.DeepEqual(check, ds) { + t.Errorf("Expected %+v\nGo %+v", check, ds) + } + } +} + +func TestDomainsSetAppend(t *testing.T) { + inSlice := []string{ + "", + "foo1.com", + "foo2.com,bar.net", + "foo3.com,bar1.net,bar2.net,bar3.net", + } + checkSlice := []Domains{ + {}, + { + Domain{ + Main: "foo1.com", + SANs: []string{}}}, + { + Domain{ + Main: "foo1.com", + SANs: []string{}}, + Domain{ + Main: "foo2.com", + SANs: []string{"bar.net"}}}, + { + Domain{ + Main: "foo1.com", + SANs: []string{}}, + Domain{ + Main: "foo2.com", + SANs: []string{"bar.net"}}, + Domain{Main: "foo3.com", + SANs: []string{"bar1.net", "bar2.net", "bar3.net"}}}, + } + ds := Domains{} + for i, in := range inSlice { + ds.Set(in) + if !reflect.DeepEqual(checkSlice[i], ds) { + t.Errorf("Expected %s %+v\nGo %+v", in, checkSlice[i], ds) + } + } +} diff --git a/configuration.go b/configuration.go index 35a7234de..72703e177 100644 --- a/configuration.go +++ b/configuration.go @@ -49,8 +49,7 @@ type DefaultEntryPoints []string // String is the method to format the flag's value, part of the flag.Value interface. // The String method's output will be used in diagnostics. func (dep *DefaultEntryPoints) String() string { - //TODO : The string returned should be formatted in such way that the func Set below could parse it. - return fmt.Sprintf("%#v", dep) + return strings.Join(*dep, ",") } // Set is the method to set the flag value, part of the flag.Value interface. @@ -86,9 +85,6 @@ type EntryPoints map[string]*EntryPoint // String is the method to format the flag's value, part of the flag.Value interface. // The String method's output will be used in diagnostics. func (ep *EntryPoints) String() string { - //TODO : The string returned should be formatted in such way that the func Set below could parse it. - //Like this --entryPoints='Name:http Address::8000 Redirect.EntryPoint:https' - //But the Set func parses entrypoint one by one only return fmt.Sprintf("%+v", *ep) } @@ -176,7 +172,6 @@ type Certificates []Certificate // The String method's output will be used in diagnostics. func (certs *Certificates) String() string { if len(*certs) == 0 { - //TODO : return "" } return (*certs)[0].CertFile + "," + (*certs)[0].KeyFile