From 0dbc5dcaa084bd270bddbd2e357c98baa802915f Mon Sep 17 00:00:00 2001 From: Nitya Dhanushkodi Date: Fri, 27 May 2022 00:07:03 -0700 Subject: [PATCH] add some tests, fix some lint --- .../connect-inject/endpoints_controller.go | 11 ++-- .../endpoints_controller_test.go | 55 +++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/control-plane/connect-inject/endpoints_controller.go b/control-plane/connect-inject/endpoints_controller.go index 5ec883f140..a8aee156bf 100644 --- a/control-plane/connect-inject/endpoints_controller.go +++ b/control-plane/connect-inject/endpoints_controller.go @@ -3,7 +3,6 @@ package connectinject import ( "context" "encoding/json" - "errors" "fmt" "net" "regexp" @@ -943,14 +942,14 @@ func (r *EndpointsController) processAnnotatedUpstream(pod corev1.Pod, rawUpstre case "dc": datacenter = strings.TrimSpace(pieces[4]) default: - return api.Upstream{}, errors.New(fmt.Sprintf("upstream structured incorrectly: %s", rawUpstream)) + return api.Upstream{}, fmt.Errorf("upstream structured incorrectly: %s", rawUpstream) } fallthrough case 4: if strings.TrimSpace(pieces[3]) == "ns" { namespace = strings.TrimSpace(pieces[2]) } else { - return api.Upstream{}, errors.New(fmt.Sprintf("upstream structured incorrectly: %s", rawUpstream)) + return api.Upstream{}, fmt.Errorf("upstream structured incorrectly: %s", rawUpstream) } fallthrough case 2: @@ -958,7 +957,7 @@ func (r *EndpointsController) processAnnotatedUpstream(pod corev1.Pod, rawUpstre serviceName = strings.TrimSpace(pieces[0]) } default: - return api.Upstream{}, errors.New(fmt.Sprintf("upstream structured incorrectly: %s", rawUpstream)) + return api.Upstream{}, fmt.Errorf("upstream structured incorrectly: %s", rawUpstream) } } else { @@ -971,13 +970,13 @@ func (r *EndpointsController) processAnnotatedUpstream(pod corev1.Pod, rawUpstre case "dc": datacenter = strings.TrimSpace(pieces[2]) default: - return api.Upstream{}, errors.New(fmt.Sprintf("upstream structured incorrectly: %s", rawUpstream)) + return api.Upstream{}, fmt.Errorf("upstream structured incorrectly: %s", rawUpstream) } fallthrough case 2: serviceName = strings.TrimSpace(pieces[0]) default: - return api.Upstream{}, errors.New(fmt.Sprintf("upstream structured incorrectly: %s", rawUpstream)) + return api.Upstream{}, fmt.Errorf("upstream structured incorrectly: %s", rawUpstream) } } diff --git a/control-plane/connect-inject/endpoints_controller_test.go b/control-plane/connect-inject/endpoints_controller_test.go index 35e7c0d43f..aaac91547c 100644 --- a/control-plane/connect-inject/endpoints_controller_test.go +++ b/control-plane/connect-inject/endpoints_controller_test.go @@ -360,6 +360,61 @@ func TestProcessUpstreams(t *testing.T) { consulNamespacesEnabled: true, consulPartitionsEnabled: true, }, + { + name: "annotated upstream error: invalid partition/dc/peer", + pod: func() *corev1.Pod { + pod1 := createPod("pod1", "1.2.3.4", true, true) + pod1.Annotations[annotationUpstreams] = "upstream1.svc.ns1.ns.part1.err:1234" + return pod1 + }, + expErr: "upstream structured incorrectly: upstream1.svc.ns1.ns.part1.err:1234", + consulNamespacesEnabled: true, + consulPartitionsEnabled: false, + }, + { + name: "annotated upstream error: invalid namespace", + pod: func() *corev1.Pod { + pod1 := createPod("pod1", "1.2.3.4", true, true) + pod1.Annotations[annotationUpstreams] = "upstream1.svc.ns1.err:1234" + return pod1 + }, + expErr: "upstream structured incorrectly: upstream1.svc.ns1.err:1234", + consulNamespacesEnabled: true, + consulPartitionsEnabled: false, + }, + { + name: "annotated upstream error: invalid number of pieces in the address", + pod: func() *corev1.Pod { + pod1 := createPod("pod1", "1.2.3.4", true, true) + pod1.Annotations[annotationUpstreams] = "upstream1.svc.err:1234" + return pod1 + }, + expErr: "upstream structured incorrectly: upstream1.svc.err:1234", + consulNamespacesEnabled: true, + consulPartitionsEnabled: false, + }, + { + name: "annotated upstream error: invalid peer", + pod: func() *corev1.Pod { + pod1 := createPod("pod1", "1.2.3.4", true, true) + pod1.Annotations[annotationUpstreams] = "upstream1.svc.peer1.err:1234" + return pod1 + }, + expErr: "upstream structured incorrectly: upstream1.svc.peer1.err:1234", + consulNamespacesEnabled: false, + consulPartitionsEnabled: false, + }, + { + name: "annotated upstream error: invalid number of pieces in the address without namespaces and partitions", + pod: func() *corev1.Pod { + pod1 := createPod("pod1", "1.2.3.4", true, true) + pod1.Annotations[annotationUpstreams] = "upstream1.svc.err:1234" + return pod1 + }, + expErr: "upstream structured incorrectly: upstream1.svc.err:1234", + consulNamespacesEnabled: false, + consulPartitionsEnabled: false, + }, { name: "upstream with datacenter without ProxyDefaults", pod: func() *corev1.Pod {