From 08e909d2b73d294489eb4767714a8efa10d995ae Mon Sep 17 00:00:00 2001 From: hlts2 Date: Tue, 8 Dec 2020 12:01:07 +0900 Subject: [PATCH 1/9] feat: add discoverer error test and comment Signed-off-by: hlts2 --- internal/errors/discoverer.go | 5 + internal/errors/discoverer_test.go | 320 +++++++++++++++++++++++++++++ 2 files changed, 325 insertions(+) create mode 100644 internal/errors/discoverer_test.go diff --git a/internal/errors/discoverer.go b/internal/errors/discoverer.go index ead7be84b7..f525732a3b 100644 --- a/internal/errors/discoverer.go +++ b/internal/errors/discoverer.go @@ -18,21 +18,26 @@ package errors var ( + // ErrAddrCouldNotDiscover represents a function to generate an error that address couldn't discover. ErrAddrCouldNotDiscover = func(err error, record string) error { return Wrapf(err, "addr %s ip couldn't discover", record) } + // ErrNodeNotFound represents a function to generate an error of discover node not found. ErrNodeNotFound = func(node string) error { return Errorf("discover node %s not found", node) } + // ErrNamespaceNotFound represents a function to generate an error of discover namespace not found. ErrNamespaceNotFound = func(ns string) error { return Errorf("discover namespace %s not found", ns) } + // ErrPodNameNotFound represents a function to generate an error of discover pod not found. ErrPodNameNotFound = func(name string) error { return Errorf("discover pod %s not found", name) } + // ErrInvalidDiscoveryCache represents an error that cast of discovery cache type failed. ErrInvalidDiscoveryCache = New("cache type cast failed") ) diff --git a/internal/errors/discoverer_test.go b/internal/errors/discoverer_test.go new file mode 100644 index 0000000000..008c10f5d6 --- /dev/null +++ b/internal/errors/discoverer_test.go @@ -0,0 +1,320 @@ +package errors + +import "testing" + +func TestErrAddrCouldNotDiscover(t *testing.T) { + type args struct { + err error + record string + } + type want struct { + want error + } + type test struct { + name string + args args + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got error) error { + if !Is(got, w.want) { + return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + { + name: "returns wrapped discover error when err is server error and record is `127.0.0.1`", + args: args{ + err: New("server error"), + record: "127.0.0.1", + }, + want: want{ + want: New("addr 127.0.0.1 ip couldn't discover: server error"), + }, + }, + { + name: "returns wrapped discover error when err is server error and record is empty", + args: args{ + err: New("server error"), + record: "", + }, + want: want{ + want: New("addr ip couldn't discover: server error"), + }, + }, + { + name: "returns discover error when err is nil error and record is `127.0.0.1`", + args: args{ + err: nil, + record: "127.0.0.1", + }, + want: want{ + want: New("addr 127.0.0.1 ip couldn't discover"), + }, + }, + { + name: "returns discover error when err is nil error and record is empty", + args: args{ + err: nil, + record: "", + }, + want: want{ + want: New("addr ip couldn't discover"), + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := ErrAddrCouldNotDiscover(test.args.err, test.args.record) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestErrNodeNotFound(t *testing.T) { + type args struct { + node string + } + type want struct { + want error + } + type test struct { + name string + args args + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got error) error { + if !Is(got, w.want) { + return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + { + name: "returns discover node not found error when node is `127.0.0.1`", + args: args{ + node: "127.0.0.1", + }, + want: want{ + want: New("discover node 127.0.0.1 not found"), + }, + }, + { + name: "returns discover node not found error when node is empty", + args: args{ + node: "", + }, + want: want{ + want: New("discover node not found"), + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := ErrNodeNotFound(test.args.node) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestErrNamespaceNotFound(t *testing.T) { + type args struct { + ns string + } + type want struct { + want error + } + type test struct { + name string + args args + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got error) error { + if !Is(got, w.want) { + return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + { + name: "returns discover namespace not found error when ns is `vald`", + args: args{ + ns: "vald", + }, + want: want{ + want: New("discover namespace vald not found"), + }, + }, + { + name: "returns discover namespace not found error when ns is empty", + args: args{ + ns: "", + }, + want: want{ + want: New("discover namespace not found"), + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := ErrNamespaceNotFound(test.args.ns) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestErrPodNameNotFound(t *testing.T) { + type args struct { + name string + } + type want struct { + want error + } + type test struct { + name string + args args + want want + checkFunc func(want, error) error + beforeFunc func(args) + afterFunc func(args) + } + defaultCheckFunc := func(w want, got error) error { + if !Is(got, w.want) { + return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + { + name: "returns discover pod not found error when ns is `vald-discoverer`", + args: args{ + name: "vald-discovere", + }, + want: want{ + want: New("discover pod vald-discovere not found"), + }, + }, + { + name: "returns discover pod not found error when name is empty", + args: args{ + name: "", + }, + want: want{ + want: New("discover pod not found"), + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + if test.beforeFunc != nil { + test.beforeFunc(test.args) + } + if test.afterFunc != nil { + defer test.afterFunc(test.args) + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := ErrPodNameNotFound(test.args.name) + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} + +func TestErrInvalidDiscoveryCache(t *testing.T) { + type want struct { + want error + } + type test struct { + name string + want want + checkFunc func(want, error) error + beforeFunc func() + afterFunc func() + } + defaultCheckFunc := func(w want, got error) error { + if !Is(got, w.want) { + return Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) + } + return nil + } + tests := []test{ + { + name: "returns cache type cast error", + want: want{ + want: New("cache type cast failed"), + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(tt *testing.T) { + if test.beforeFunc != nil { + test.beforeFunc() + } + if test.afterFunc != nil { + defer test.afterFunc() + } + if test.checkFunc == nil { + test.checkFunc = defaultCheckFunc + } + + got := ErrInvalidDiscoveryCache + if err := test.checkFunc(test.want, got); err != nil { + tt.Errorf("error = %v", err) + } + }) + } +} From 2413981cea7427b430a98497fee07b4c3acca343 Mon Sep 17 00:00:00 2001 From: hlts2 Date: Wed, 16 Dec 2020 14:34:05 +0900 Subject: [PATCH 2/9] fix: apply suggestion Signed-off-by: hlts2 --- internal/errors/discoverer_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/errors/discoverer_test.go b/internal/errors/discoverer_test.go index 008c10f5d6..40f830625c 100644 --- a/internal/errors/discoverer_test.go +++ b/internal/errors/discoverer_test.go @@ -236,10 +236,10 @@ func TestErrPodNameNotFound(t *testing.T) { { name: "returns discover pod not found error when ns is `vald-discoverer`", args: args{ - name: "vald-discovere", + name: "vald-discoverer", }, want: want{ - want: New("discover pod vald-discovere not found"), + want: New("discover pod vald-discoverer not found"), }, }, { From 11d0d8770c7069324265c00e2f14cbe02485fbf0 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Thu, 17 Dec 2020 11:00:57 +0900 Subject: [PATCH 3/9] Update internal/errors/discoverer.go --- internal/errors/discoverer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/discoverer.go b/internal/errors/discoverer.go index f525732a3b..c792e037be 100644 --- a/internal/errors/discoverer.go +++ b/internal/errors/discoverer.go @@ -38,6 +38,6 @@ var ( return Errorf("discover pod %s not found", name) } - // ErrInvalidDiscoveryCache represents an error that cast of discovery cache type failed. + // ErrInvalidDiscoveryCache represents an error that type conversion of discovery cache failed. ErrInvalidDiscoveryCache = New("cache type cast failed") ) From 749a9c8a9130537632320206132afbaeb5f549ce Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Mon, 21 Dec 2020 10:06:45 +0900 Subject: [PATCH 4/9] Update internal/errors/discoverer_test.go Co-authored-by: Kiichiro YUKAWA --- internal/errors/discoverer_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/discoverer_test.go b/internal/errors/discoverer_test.go index 40f830625c..49b8ebcc40 100644 --- a/internal/errors/discoverer_test.go +++ b/internal/errors/discoverer_test.go @@ -26,7 +26,7 @@ func TestErrAddrCouldNotDiscover(t *testing.T) { } tests := []test{ { - name: "returns wrapped discover error when err is server error and record is `127.0.0.1`", + name: "returns wrapped discover error when err is server error and record is '127.0.0.1'", args: args{ err: New("server error"), record: "127.0.0.1", From 7a15aba54aac5ea3412ab6fe356105e287dd7f87 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Mon, 21 Dec 2020 10:07:03 +0900 Subject: [PATCH 5/9] Update internal/errors/discoverer_test.go Co-authored-by: Kiichiro YUKAWA --- internal/errors/discoverer_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/discoverer_test.go b/internal/errors/discoverer_test.go index 49b8ebcc40..d5687b0d7a 100644 --- a/internal/errors/discoverer_test.go +++ b/internal/errors/discoverer_test.go @@ -46,7 +46,7 @@ func TestErrAddrCouldNotDiscover(t *testing.T) { }, }, { - name: "returns discover error when err is nil error and record is `127.0.0.1`", + name: "returns discover error when err is nil error and record is '127.0.0.1'", args: args{ err: nil, record: "127.0.0.1", From b247c9bc83ae8f299cb24c109d03544ea0809506 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Mon, 21 Dec 2020 10:07:14 +0900 Subject: [PATCH 6/9] Update internal/errors/discoverer_test.go Co-authored-by: Kiichiro YUKAWA --- internal/errors/discoverer_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/discoverer_test.go b/internal/errors/discoverer_test.go index d5687b0d7a..5f0b7e27eb 100644 --- a/internal/errors/discoverer_test.go +++ b/internal/errors/discoverer_test.go @@ -172,7 +172,7 @@ func TestErrNamespaceNotFound(t *testing.T) { } tests := []test{ { - name: "returns discover namespace not found error when ns is `vald`", + name: "returns discover namespace not found error when ns is 'vald'", args: args{ ns: "vald", }, From 99eed8e343cf1715b8aa15ca64f570a6b846177c Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Mon, 21 Dec 2020 10:07:27 +0900 Subject: [PATCH 7/9] Update internal/errors/discoverer_test.go Co-authored-by: Kiichiro YUKAWA --- internal/errors/discoverer_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/discoverer_test.go b/internal/errors/discoverer_test.go index 5f0b7e27eb..fbf633d12c 100644 --- a/internal/errors/discoverer_test.go +++ b/internal/errors/discoverer_test.go @@ -110,7 +110,7 @@ func TestErrNodeNotFound(t *testing.T) { } tests := []test{ { - name: "returns discover node not found error when node is `127.0.0.1`", + name: "returns discover node not found error when node is '127.0.0.1'", args: args{ node: "127.0.0.1", }, From 90b11366a516f0c1296388b744f891705fbd8998 Mon Sep 17 00:00:00 2001 From: Hiroto Funakoshi Date: Mon, 21 Dec 2020 10:07:42 +0900 Subject: [PATCH 8/9] Update internal/errors/discoverer_test.go Co-authored-by: Kiichiro YUKAWA --- internal/errors/discoverer_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/errors/discoverer_test.go b/internal/errors/discoverer_test.go index fbf633d12c..eb24572bb6 100644 --- a/internal/errors/discoverer_test.go +++ b/internal/errors/discoverer_test.go @@ -234,7 +234,7 @@ func TestErrPodNameNotFound(t *testing.T) { } tests := []test{ { - name: "returns discover pod not found error when ns is `vald-discoverer`", + name: "returns discover pod not found error when ns is 'vald-discoverer'", args: args{ name: "vald-discoverer", }, From 000659bd38ffefc2814f09432cd7e488efc7d730 Mon Sep 17 00:00:00 2001 From: vdaas-ci Date: Mon, 21 Dec 2020 05:41:50 +0000 Subject: [PATCH 9/9] :robot: Update license headers / Format go codes and yaml files Signed-off-by: vdaas-ci --- internal/errors/discoverer_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/internal/errors/discoverer_test.go b/internal/errors/discoverer_test.go index eb24572bb6..de1ef37f86 100644 --- a/internal/errors/discoverer_test.go +++ b/internal/errors/discoverer_test.go @@ -1,3 +1,18 @@ +// +// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt ) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// package errors import "testing"