Skip to content

Commit

Permalink
Restructure MatchesConsul test to test against mismatched type
Browse files Browse the repository at this point in the history
- This ensures we dont have to necessarily write failing tests every
  time we add a field
  • Loading branch information
Ashwin Venkatesh committed Sep 29, 2020
1 parent 638d806 commit 63b035c
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 292 deletions.
33 changes: 26 additions & 7 deletions api/v1alpha1/proxydefaults_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,27 @@ import (
// Test MatchesConsul for cases that should return true.
func TestProxyDefaults_MatchesConsul(t *testing.T) {
cases := map[string]struct {
Ours ProxyDefaults
Theirs *capi.ProxyConfigEntry
Ours ProxyDefaults
Theirs capi.ConfigEntry
Matches bool
}{
"empty fields": {
"empty fields matches": {
Ours: ProxyDefaults{
ObjectMeta: metav1.ObjectMeta{
Name: common.Global,
},
Spec: ProxyDefaultsSpec{},
},
Theirs: &capi.ProxyConfigEntry{
Name: common.Global,
Kind: capi.ProxyDefaults,
Name: common.Global,
Kind: capi.ProxyDefaults,
Namespace: "default",
CreateIndex: 1,
ModifyIndex: 2,
},
Matches: true,
},
"all fields set": {
"all fields set matches": {
Ours: ProxyDefaults{
ObjectMeta: metav1.ObjectMeta{
Name: common.Global,
Expand Down Expand Up @@ -86,11 +91,25 @@ func TestProxyDefaults_MatchesConsul(t *testing.T) {
},
},
},
Matches: true,
},
"mismatched types does not match": {
Ours: ProxyDefaults{
ObjectMeta: metav1.ObjectMeta{
Name: common.Global,
},
Spec: ProxyDefaultsSpec{},
},
Theirs: &capi.ServiceConfigEntry{
Name: common.Global,
Kind: capi.ProxyDefaults,
},
Matches: false,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
require.True(t, c.Ours.MatchesConsul(c.Theirs))
require.Equal(t, c.Ours.MatchesConsul(c.Theirs), c.Matches)
})
}
}
Expand Down
218 changes: 16 additions & 202 deletions api/v1alpha1/servicedefaults_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestToConsul(t *testing.T) {
func TestServiceDefaults_ToConsul(t *testing.T) {
cases := map[string]struct {
input *ServiceDefaults
expected *capi.ServiceConfigEntry
Expand Down Expand Up @@ -286,12 +286,13 @@ func TestToConsul(t *testing.T) {
}
}

func TestMatchesConsul_Matches(t *testing.T) {
func TestServiceDefaults_MatchesConsul(t *testing.T) {
cases := map[string]struct {
internal *ServiceDefaults
consul *capi.ServiceConfigEntry
consul capi.ConfigEntry
matches bool
}{
"empty fields": {
"empty fields matches": {
&ServiceDefaults{
ObjectMeta: metav1.ObjectMeta{
Name: "my-test-service",
Expand All @@ -305,8 +306,9 @@ func TestMatchesConsul_Matches(t *testing.T) {
CreateIndex: 1,
ModifyIndex: 2,
},
true,
},
"all fields populated": {
"all fields populated matches": {
&ServiceDefaults{
ObjectMeta: metav1.ObjectMeta{
Name: "my-test-service",
Expand Down Expand Up @@ -360,217 +362,29 @@ func TestMatchesConsul_Matches(t *testing.T) {
},
ExternalSNI: "sni-value",
},
true,
},
}

for name, testCase := range cases {
t.Run(name, func(t *testing.T) {
require.True(t, testCase.internal.MatchesConsul(testCase.consul))
})
}
}

func TestMatchesConsul_NotMatch(t *testing.T) {
cases := map[string]struct {
internal *ServiceDefaults
consul *capi.ServiceConfigEntry
}{
"name:mismatched": {
"mismatched types does not match": {
&ServiceDefaults{
ObjectMeta: metav1.ObjectMeta{
Name: "my-test-service",
},
Spec: ServiceDefaultsSpec{},
},
&capi.ServiceConfigEntry{
Kind: capi.ServiceDefaults,
Name: "differently-named-service",
},
},
"protocol:mismatched": {
&ServiceDefaults{
Spec: ServiceDefaultsSpec{
Protocol: "http",
},
},
&capi.ServiceConfigEntry{
Kind: capi.ServiceDefaults,
Protocol: "https",
},
},
"gatewayConfig:mismatched": {
&ServiceDefaults{
Spec: ServiceDefaultsSpec{
MeshGateway: MeshGatewayConfig{
Mode: "remote",
},
},
},
&capi.ServiceConfigEntry{
Kind: capi.ServiceDefaults,
MeshGateway: capi.MeshGatewayConfig{
Mode: capi.MeshGatewayModeLocal,
},
},
},
"externalSNI:mismatched": {
&ServiceDefaults{
Spec: ServiceDefaultsSpec{
ExternalSNI: "test-external-sni",
},
},
&capi.ServiceConfigEntry{
&capi.ProxyConfigEntry{
Kind: capi.ServiceDefaults,
ExternalSNI: "different-external-sni",
},
},
"expose.checks:mismatched": {
&ServiceDefaults{
Spec: ServiceDefaultsSpec{
Expose: ExposeConfig{
Checks: true,
},
},
},
&capi.ServiceConfigEntry{
Kind: capi.ServiceDefaults,
Expose: capi.ExposeConfig{
Checks: false,
},
},
},
"expose.paths.listenerPort:mismatched": {
&ServiceDefaults{
Spec: ServiceDefaultsSpec{
Expose: ExposeConfig{
Paths: []ExposePath{
{
ListenerPort: 80,
},
},
},
},
},
&capi.ServiceConfigEntry{
Kind: capi.ServiceDefaults,
Expose: capi.ExposeConfig{
Paths: []capi.ExposePath{
{
ListenerPort: 81,
},
},
},
},
},
"expose.paths.path:mismatched": {
&ServiceDefaults{
Spec: ServiceDefaultsSpec{
Expose: ExposeConfig{
Paths: []ExposePath{
{
Path: "/test/path",
},
},
},
},
},
&capi.ServiceConfigEntry{
Kind: capi.ServiceDefaults,
Expose: capi.ExposeConfig{
Paths: []capi.ExposePath{
{
Path: "/differnt/path",
},
},
},
},
},
"expose.paths.localPathPort:mismatched": {
&ServiceDefaults{
Spec: ServiceDefaultsSpec{
Expose: ExposeConfig{
Paths: []ExposePath{
{
LocalPathPort: 42,
},
},
},
},
},
&capi.ServiceConfigEntry{
Kind: capi.ServiceDefaults,
Expose: capi.ExposeConfig{
Paths: []capi.ExposePath{
{
LocalPathPort: 21,
},
},
},
},
},
"expose.paths.protocol:mismatched": {
&ServiceDefaults{
Spec: ServiceDefaultsSpec{
Expose: ExposeConfig{
Paths: []ExposePath{
{
Protocol: "tcp",
},
},
},
},
},
&capi.ServiceConfigEntry{
Kind: capi.ServiceDefaults,
Expose: capi.ExposeConfig{
Paths: []capi.ExposePath{
{
Protocol: "https",
},
},
},
},
},
"expose.paths:mismatched when path lengths are different": {
&ServiceDefaults{
Spec: ServiceDefaultsSpec{
Expose: ExposeConfig{
Paths: []ExposePath{
{
ListenerPort: 8080,
Path: "/second/test/path",
LocalPathPort: 11,
Protocol: "https",
},
{
ListenerPort: 80,
Path: "/test/path",
LocalPathPort: 42,
Protocol: "tcp",
},
},
},
},
},
&capi.ServiceConfigEntry{
Kind: capi.ServiceDefaults,
Expose: capi.ExposeConfig{
Paths: []capi.ExposePath{
{
ListenerPort: 8080,
Path: "/second/test/path",
LocalPathPort: 11,
Protocol: "https",
},
},
},
Name: "my-test-service",
Namespace: "namespace",
CreateIndex: 1,
ModifyIndex: 2,
},
false,
},
}

for name, testCase := range cases {
t.Run(name, func(t *testing.T) {
require.False(t, testCase.internal.MatchesConsul(testCase.consul))
require.Equal(t, testCase.internal.MatchesConsul(testCase.consul), testCase.matches)
})
}
}
Expand Down
36 changes: 29 additions & 7 deletions api/v1alpha1/serviceresolver_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@ import (

func TestServiceResolver_MatchesConsul(t *testing.T) {
cases := map[string]struct {
Ours ServiceResolver
Theirs *capi.ServiceResolverConfigEntry
Ours ServiceResolver
Theirs capi.ConfigEntry
Matches bool
}{
"empty fields": {
"empty fields matches": {
Ours: ServiceResolver{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
},
Spec: ServiceResolverSpec{},
},
Theirs: &capi.ServiceResolverConfigEntry{
Name: "name",
Kind: capi.ServiceResolver,
Name: "name",
Kind: capi.ServiceResolver,
Namespace: "foobar",
CreateIndex: 1,
ModifyIndex: 2,
},
Matches: true,
},
"all fields set": {
"all fields set matches": {
Ours: ServiceResolver{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Expand Down Expand Up @@ -149,11 +154,28 @@ func TestServiceResolver_MatchesConsul(t *testing.T) {
},
},
},
Matches: true,
},
"different types does not match": {
Ours: ServiceResolver{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
},
Spec: ServiceResolverSpec{},
},
Theirs: &capi.ProxyConfigEntry{
Name: "name",
Kind: capi.ServiceResolver,
Namespace: "foobar",
CreateIndex: 1,
ModifyIndex: 2,
},
Matches: false,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
require.True(t, c.Ours.MatchesConsul(c.Theirs))
require.Equal(t, c.Ours.MatchesConsul(c.Theirs), c.Matches)
})
}
}
Expand Down
Loading

0 comments on commit 63b035c

Please sign in to comment.