From a473c5b38afaad3a17e7f0ea566cb74a454ca270 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 27 Dec 2022 16:14:50 +0100 Subject: [PATCH] opts: rename logOptsValidator, fix unhandled errors in tests This validator was not specific to "log options", so renaming it to make this clearer; also updating values used in the tests to make it clear they're not "actual" valid values, just for testing, and while updating, also fixed some unhandled errors in tests. Signed-off-by: Sebastiaan van Stijn --- opts/opts_test.go | 69 +++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/opts/opts_test.go b/opts/opts_test.go index 2b1c443417..f8b6267165 100644 --- a/opts/opts_test.go +++ b/opts/opts_test.go @@ -32,24 +32,30 @@ func TestValidateIPAddress(t *testing.T) { func TestMapOpts(t *testing.T) { tmpMap := make(map[string]string) - o := NewMapOpts(tmpMap, logOptsValidator) - o.Set("max-size=1") - if o.String() != "map[max-size:1]" { - t.Errorf("%s != [map[max-size:1]", o.String()) + o := NewMapOpts(tmpMap, sampleValidator) + err := o.Set("valid-option=1") + if err != nil { + t.Error(err) + } + if o.String() != "map[valid-option:1]" { + t.Errorf("%s != [map[valid-option:1]", o.String()) } - o.Set("max-file=2") + err = o.Set("valid-option2=2") + if err != nil { + t.Error(err) + } 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["valid-option"] != "1" { + t.Errorf("valid-option = %s != 1", tmpMap["valid-option"]) + } + if tmpMap["valid-option2"] != "2" { + t.Errorf("valid-option2 = %s != 2", tmpMap["valid-option2"]) } - if tmpMap["max-size"] != "1" { - t.Errorf("max-size = %s != 1", tmpMap["max-size"]) - } if o.Set("dummy-val=3") == nil { t.Error("validator is not being called") } @@ -57,15 +63,24 @@ func TestMapOpts(t *testing.T) { func TestListOptsWithoutValidator(t *testing.T) { o := NewListOpts(nil) - o.Set("foo") + err := o.Set("foo") + if err != nil { + t.Error(err) + } if o.String() != "[foo]" { t.Errorf("%s != [foo]", o.String()) } - o.Set("bar") + err = o.Set("bar") + if err != nil { + t.Error(err) + } if o.Len() != 2 { t.Errorf("%d != 2", o.Len()) } - o.Set("bar") + err = o.Set("bar") + if err != nil { + t.Error(err) + } if o.Len() != 3 { t.Errorf("%d != 3", o.Len()) } @@ -90,27 +105,35 @@ func TestListOptsWithoutValidator(t *testing.T) { } func TestListOptsWithValidator(t *testing.T) { - // Re-using logOptsvalidator (used by MapOpts) - o := NewListOpts(logOptsValidator) - o.Set("foo") + o := NewListOpts(sampleValidator) + err := o.Set("foo") + if err == nil { + t.Error(err) + } if o.String() != "" { t.Errorf(`%s != ""`, o.String()) } - o.Set("foo=bar") + err = o.Set("foo=bar") + if err == nil { + t.Error(err) + } if o.String() != "" { t.Errorf(`%s != ""`, o.String()) } - o.Set("max-file=2") + err = o.Set("valid-option2=2") + if err != nil { + t.Error(err) + } 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("valid-option2=2") { + t.Error(`o.Get("valid-option2=2") == false`) } if o.Get("baz") { - t.Error("o.Get(\"baz\") == true") + t.Error(`o.Get("baz") == true`) } - o.Delete("max-file=2") + o.Delete("valid-option2=2") if o.String() != "" { t.Errorf(`%s != ""`, o.String()) } @@ -277,7 +300,7 @@ func TestValidateLabel(t *testing.T) { } } -func logOptsValidator(val string) (string, error) { +func sampleValidator(val string) (string, error) { allowedKeys := map[string]string{"max-size": "1", "max-file": "2"} vals := strings.Split(val, "=") if allowedKeys[vals[0]] != "" {