Skip to content

Commit

Permalink
add some tests, fix some lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ndhanushkodi committed May 27, 2022
1 parent 832e633 commit 70f5a76
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
11 changes: 5 additions & 6 deletions control-plane/connect-inject/endpoints_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package connectinject
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"regexp"
Expand Down Expand Up @@ -943,22 +942,22 @@ 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:
if strings.TrimSpace(pieces[1]) == "svc" {
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 {
Expand All @@ -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)
}

}
Expand Down
55 changes: 55 additions & 0 deletions control-plane/connect-inject/endpoints_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 70f5a76

Please sign in to comment.