2014-02-11 21:46:55 -05:00
|
|
|
package opts
|
2013-12-02 17:33:33 -05:00
|
|
|
|
|
|
|
import (
|
2015-05-04 17:39:48 -04:00
|
|
|
"fmt"
|
2015-02-09 09:59:05 -05:00
|
|
|
"strings"
|
2013-12-02 17:33:33 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-07-09 17:47:55 -04:00
|
|
|
func TestValidateIPAddress(t *testing.T) {
|
|
|
|
if ret, err := ValidateIPAddress(`1.2.3.4`); err != nil || ret == "" {
|
|
|
|
t.Fatalf("ValidateIPAddress(`1.2.3.4`) got %s %s", ret, err)
|
2013-12-02 17:33:33 -05:00
|
|
|
}
|
|
|
|
|
2014-07-09 17:47:55 -04:00
|
|
|
if ret, err := ValidateIPAddress(`127.0.0.1`); err != nil || ret == "" {
|
|
|
|
t.Fatalf("ValidateIPAddress(`127.0.0.1`) got %s %s", ret, err)
|
2013-12-02 17:33:33 -05:00
|
|
|
}
|
|
|
|
|
2014-07-09 17:47:55 -04:00
|
|
|
if ret, err := ValidateIPAddress(`::1`); err != nil || ret == "" {
|
|
|
|
t.Fatalf("ValidateIPAddress(`::1`) got %s %s", ret, err)
|
2013-12-02 17:33:33 -05:00
|
|
|
}
|
|
|
|
|
2014-07-09 17:47:55 -04:00
|
|
|
if ret, err := ValidateIPAddress(`127`); err == nil || ret != "" {
|
|
|
|
t.Fatalf("ValidateIPAddress(`127`) got %s %s", ret, err)
|
2014-06-13 08:02:12 -04:00
|
|
|
}
|
|
|
|
|
2014-07-09 17:47:55 -04:00
|
|
|
if ret, err := ValidateIPAddress(`random invalid string`); err == nil || ret != "" {
|
|
|
|
t.Fatalf("ValidateIPAddress(`random invalid string`) got %s %s", ret, err)
|
2013-12-02 17:33:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-02-07 11:48:14 -05:00
|
|
|
|
2015-05-04 17:39:48 -04:00
|
|
|
func TestMapOpts(t *testing.T) {
|
|
|
|
tmpMap := make(map[string]string)
|
2015-05-05 00:18:28 -04:00
|
|
|
o := NewMapOpts(tmpMap, logOptsValidator)
|
2015-05-04 17:39:48 -04:00
|
|
|
o.Set("max-size=1")
|
|
|
|
if o.String() != "map[max-size:1]" {
|
|
|
|
t.Errorf("%s != [map[max-size:1]", o.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
o.Set("max-file=2")
|
|
|
|
if len(tmpMap) != 2 {
|
|
|
|
t.Errorf("map length %d != 2", len(tmpMap))
|
|
|
|
}
|
|
|
|
|
|
|
|
if tmpMap["max-file"] != "2" {
|
|
|
|
t.Errorf("max-file = %s != 2", tmpMap["max-file"])
|
|
|
|
}
|
|
|
|
|
|
|
|
if tmpMap["max-size"] != "1" {
|
|
|
|
t.Errorf("max-size = %s != 1", tmpMap["max-size"])
|
|
|
|
}
|
|
|
|
if o.Set("dummy-val=3") == nil {
|
2016-11-14 04:01:17 -05:00
|
|
|
t.Error("validator is not being called")
|
2015-05-04 17:39:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
func TestListOptsWithoutValidator(t *testing.T) {
|
2014-08-09 21:13:44 -04:00
|
|
|
o := NewListOpts(nil)
|
|
|
|
o.Set("foo")
|
2014-10-06 21:54:52 -04:00
|
|
|
if o.String() != "[foo]" {
|
|
|
|
t.Errorf("%s != [foo]", o.String())
|
|
|
|
}
|
|
|
|
o.Set("bar")
|
|
|
|
if o.Len() != 2 {
|
|
|
|
t.Errorf("%d != 2", o.Len())
|
|
|
|
}
|
2015-07-12 04:33:30 -04:00
|
|
|
o.Set("bar")
|
|
|
|
if o.Len() != 3 {
|
|
|
|
t.Errorf("%d != 3", o.Len())
|
|
|
|
}
|
2014-10-06 21:54:52 -04:00
|
|
|
if !o.Get("bar") {
|
|
|
|
t.Error("o.Get(\"bar\") == false")
|
|
|
|
}
|
|
|
|
if o.Get("baz") {
|
|
|
|
t.Error("o.Get(\"baz\") == true")
|
|
|
|
}
|
|
|
|
o.Delete("foo")
|
2015-07-12 04:33:30 -04:00
|
|
|
if o.String() != "[bar bar]" {
|
|
|
|
t.Errorf("%s != [bar bar]", o.String())
|
|
|
|
}
|
|
|
|
listOpts := o.GetAll()
|
|
|
|
if len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
|
|
|
|
t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
|
2014-10-06 21:54:52 -04:00
|
|
|
}
|
2015-07-12 04:33:30 -04:00
|
|
|
mapListOpts := o.GetMap()
|
|
|
|
if len(mapListOpts) != 1 {
|
|
|
|
t.Errorf("Expected [map[bar:{}]], got [%v]", mapListOpts)
|
|
|
|
}
|
|
|
|
|
2014-08-09 21:13:44 -04:00
|
|
|
}
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
func TestListOptsWithValidator(t *testing.T) {
|
|
|
|
// Re-using logOptsvalidator (used by MapOpts)
|
|
|
|
o := NewListOpts(logOptsValidator)
|
|
|
|
o.Set("foo")
|
2017-03-30 21:35:04 -04:00
|
|
|
if o.String() != "" {
|
|
|
|
t.Errorf(`%s != ""`, o.String())
|
2015-07-12 04:33:30 -04:00
|
|
|
}
|
|
|
|
o.Set("foo=bar")
|
2017-03-30 21:35:04 -04:00
|
|
|
if o.String() != "" {
|
|
|
|
t.Errorf(`%s != ""`, o.String())
|
2015-07-12 04:33:30 -04:00
|
|
|
}
|
|
|
|
o.Set("max-file=2")
|
|
|
|
if o.Len() != 1 {
|
|
|
|
t.Errorf("%d != 1", o.Len())
|
|
|
|
}
|
|
|
|
if !o.Get("max-file=2") {
|
|
|
|
t.Error("o.Get(\"max-file=2\") == false")
|
|
|
|
}
|
|
|
|
if o.Get("baz") {
|
|
|
|
t.Error("o.Get(\"baz\") == true")
|
|
|
|
}
|
|
|
|
o.Delete("max-file=2")
|
2017-03-30 21:35:04 -04:00
|
|
|
if o.String() != "" {
|
|
|
|
t.Errorf(`%s != ""`, o.String())
|
2015-07-12 04:33:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateDNSSearch(t *testing.T) {
|
2014-02-07 11:48:14 -05:00
|
|
|
valid := []string{
|
2014-06-26 07:03:23 -04:00
|
|
|
`.`,
|
2014-02-07 11:48:14 -05:00
|
|
|
`a`,
|
|
|
|
`a.`,
|
|
|
|
`1.foo`,
|
|
|
|
`17.foo`,
|
|
|
|
`foo.bar`,
|
|
|
|
`foo.bar.baz`,
|
|
|
|
`foo.bar.`,
|
|
|
|
`foo.bar.baz`,
|
|
|
|
`foo1.bar2`,
|
|
|
|
`foo1.bar2.baz`,
|
|
|
|
`1foo.2bar.`,
|
|
|
|
`1foo.2bar.baz`,
|
|
|
|
`foo-1.bar-2`,
|
|
|
|
`foo-1.bar-2.baz`,
|
|
|
|
`foo-1.bar-2.`,
|
|
|
|
`foo-1.bar-2.baz`,
|
|
|
|
`1-foo.2-bar`,
|
|
|
|
`1-foo.2-bar.baz`,
|
|
|
|
`1-foo.2-bar.`,
|
|
|
|
`1-foo.2-bar.baz`,
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid := []string{
|
|
|
|
``,
|
2014-06-26 07:03:23 -04:00
|
|
|
` `,
|
|
|
|
` `,
|
2014-02-07 11:48:14 -05:00
|
|
|
`17`,
|
|
|
|
`17.`,
|
|
|
|
`.17`,
|
|
|
|
`17-.`,
|
|
|
|
`17-.foo`,
|
|
|
|
`.foo`,
|
|
|
|
`foo-.bar`,
|
|
|
|
`-foo.bar`,
|
|
|
|
`foo.bar-`,
|
|
|
|
`foo.bar-.baz`,
|
|
|
|
`foo.-bar`,
|
|
|
|
`foo.-bar.baz`,
|
2014-11-25 06:45:20 -05:00
|
|
|
`foo.bar.baz.this.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbethis.should.fail.on.long.name.beause.it.is.longer.thanisshouldbe`,
|
2014-02-07 11:48:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, domain := range valid {
|
2015-07-12 04:33:30 -04:00
|
|
|
if ret, err := ValidateDNSSearch(domain); err != nil || ret == "" {
|
|
|
|
t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
|
2014-02-07 11:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, domain := range invalid {
|
2015-07-12 04:33:30 -04:00
|
|
|
if ret, err := ValidateDNSSearch(domain); err == nil || ret != "" {
|
|
|
|
t.Fatalf("ValidateDNSSearch(`"+domain+"`) got %s %s", ret, err)
|
2014-02-07 11:48:14 -05:00
|
|
|
}
|
2015-07-12 04:33:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateLabel(t *testing.T) {
|
|
|
|
if _, err := ValidateLabel("label"); err == nil || err.Error() != "bad attribute format: label" {
|
|
|
|
t.Fatalf("Expected an error [bad attribute format: label], go %v", err)
|
|
|
|
}
|
|
|
|
if actual, err := ValidateLabel("key1=value1"); err != nil || actual != "key1=value1" {
|
|
|
|
t.Fatalf("Expected [key1=value1], got [%v,%v]", actual, err)
|
|
|
|
}
|
|
|
|
// Validate it's working with more than one =
|
|
|
|
if actual, err := ValidateLabel("key1=value1=value2"); err != nil {
|
|
|
|
t.Fatalf("Expected [key1=value1=value2], got [%v,%v]", actual, err)
|
|
|
|
}
|
|
|
|
// Validate it's working with one more
|
|
|
|
if actual, err := ValidateLabel("key1=value1=value2=value3"); err != nil {
|
|
|
|
t.Fatalf("Expected [key1=value1=value2=value2], got [%v,%v]", actual, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:39:48 -04:00
|
|
|
func logOptsValidator(val string) (string, error) {
|
|
|
|
allowedKeys := map[string]string{"max-size": "1", "max-file": "2"}
|
|
|
|
vals := strings.Split(val, "=")
|
|
|
|
if allowedKeys[vals[0]] != "" {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("invalid key %s", vals[0])
|
|
|
|
}
|
2015-12-10 18:35:10 -05:00
|
|
|
|
|
|
|
func TestNamedListOpts(t *testing.T) {
|
|
|
|
var v []string
|
|
|
|
o := NewNamedListOptsRef("foo-name", &v, nil)
|
|
|
|
|
|
|
|
o.Set("foo")
|
|
|
|
if o.String() != "[foo]" {
|
|
|
|
t.Errorf("%s != [foo]", o.String())
|
|
|
|
}
|
|
|
|
if o.Name() != "foo-name" {
|
|
|
|
t.Errorf("%s != foo-name", o.Name())
|
|
|
|
}
|
|
|
|
if len(v) != 1 {
|
|
|
|
t.Errorf("expected foo to be in the values, got %v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNamedMapOpts(t *testing.T) {
|
|
|
|
tmpMap := make(map[string]string)
|
|
|
|
o := NewNamedMapOpts("max-name", tmpMap, nil)
|
|
|
|
|
|
|
|
o.Set("max-size=1")
|
|
|
|
if o.String() != "map[max-size:1]" {
|
|
|
|
t.Errorf("%s != [map[max-size:1]", o.String())
|
|
|
|
}
|
|
|
|
if o.Name() != "max-name" {
|
|
|
|
t.Errorf("%s != max-name", o.Name())
|
|
|
|
}
|
|
|
|
if _, exist := tmpMap["max-size"]; !exist {
|
|
|
|
t.Errorf("expected map-size to be in the values, got %v", tmpMap)
|
|
|
|
}
|
|
|
|
}
|
2016-12-23 14:09:12 -05:00
|
|
|
|
|
|
|
func TestParseLink(t *testing.T) {
|
|
|
|
name, alias, err := ParseLink("name:alias")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected not to error out on a valid name:alias format but got: %v", err)
|
|
|
|
}
|
|
|
|
if name != "name" {
|
|
|
|
t.Fatalf("Link name should have been name, got %s instead", name)
|
|
|
|
}
|
|
|
|
if alias != "alias" {
|
|
|
|
t.Fatalf("Link alias should have been alias, got %s instead", alias)
|
|
|
|
}
|
|
|
|
// short format definition
|
|
|
|
name, alias, err = ParseLink("name")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected not to error out on a valid name only format but got: %v", err)
|
|
|
|
}
|
|
|
|
if name != "name" {
|
|
|
|
t.Fatalf("Link name should have been name, got %s instead", name)
|
|
|
|
}
|
|
|
|
if alias != "name" {
|
|
|
|
t.Fatalf("Link alias should have been name, got %s instead", alias)
|
|
|
|
}
|
|
|
|
// empty string link definition is not allowed
|
|
|
|
if _, _, err := ParseLink(""); err == nil || !strings.Contains(err.Error(), "empty string specified for links") {
|
|
|
|
t.Fatalf("Expected error 'empty string specified for links' but got: %v", err)
|
|
|
|
}
|
|
|
|
// more than two colons are not allowed
|
|
|
|
if _, _, err := ParseLink("link:alias:wrong"); err == nil || !strings.Contains(err.Error(), "bad format for links: link:alias:wrong") {
|
|
|
|
t.Fatalf("Expected error 'bad format for links: link:alias:wrong' but got: %v", err)
|
|
|
|
}
|
|
|
|
}
|