diff --git a/internal/errors/discoverer.go b/internal/errors/discoverer.go index ead7be84b7..c792e037be 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 type conversion of discovery cache 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..de1ef37f86 --- /dev/null +++ b/internal/errors/discoverer_test.go @@ -0,0 +1,335 @@ +// +// 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" + +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-discoverer", + }, + want: want{ + want: New("discover pod vald-discoverer 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) + } + }) + } +}