Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing improvements #339

Merged
merged 1 commit into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pkg/backendconfig/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ func TestBackendConfigName(t *testing.T) {
}

for _, tc := range testCases {
result := BackendConfigName(tc.backendConfigs, tc.svcPort)
if result != tc.expected {
t.Errorf("%s: expected %s but got %s", tc.desc, tc.expected, result)
}
t.Run(tc.desc, func(t *testing.T) {
result := BackendConfigName(tc.backendConfigs, tc.svcPort)
if result != tc.expected {
t.Errorf("%s: expected %s but got %s", tc.desc, tc.expected, result)
}
})
}
}
152 changes: 29 additions & 123 deletions pkg/backends/features/cdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package features

import (
"reflect"
"testing"

backendconfigv1beta1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1beta1"
Expand All @@ -27,14 +26,14 @@ import (

func TestEnsureCDN(t *testing.T) {
testCases := []struct {
desc string
sp utils.ServicePort
be *composite.BackendService
expected bool
desc string
sp utils.ServicePort
be *composite.BackendService
updateExpected bool
}{
{
"settings are identical, no update needed",
utils.ServicePort{
desc: "settings are identical, no update needed",
sp: utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
Cdn: &backendconfigv1beta1.CDNConfig{
Expand All @@ -46,161 +45,68 @@ func TestEnsureCDN(t *testing.T) {
},
},
},
&composite.BackendService{
be: &composite.BackendService{
EnableCDN: true,
CdnPolicy: &composite.BackendServiceCdnPolicy{
CacheKeyPolicy: &composite.CacheKeyPolicy{
IncludeHost: true,
},
},
},
false,
updateExpected: false,
},
{
"cache settings are different, update needed",
utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
Cdn: &backendconfigv1beta1.CDNConfig{
Enabled: true,
CachePolicy: &backendconfigv1beta1.CacheKeyPolicy{
QueryStringWhitelist: []string{"foo"},
},
},
},
},
},
&composite.BackendService{
EnableCDN: true,
},
true,
},
{
"enabled setting is different, update needed",
utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
Cdn: &backendconfigv1beta1.CDNConfig{
Enabled: true,
},
},
},
},
&composite.BackendService{
EnableCDN: false,
},
true,
},
}

for _, testCase := range testCases {
result := EnsureCDN(testCase.sp, testCase.be)
if result != testCase.expected {
t.Errorf("%v: expected %v but got %v", testCase.desc, testCase.expected, result)
}
}
}

func TestApplyCDNSettings(t *testing.T) {
testCases := []struct {
desc string
sp utils.ServicePort
be *composite.BackendService
expected composite.BackendService
}{
{
"apply settings on empty BackendService",
utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
Cdn: &backendconfigv1beta1.CDNConfig{
Enabled: true,
CachePolicy: &backendconfigv1beta1.CacheKeyPolicy{
IncludeHost: true,
},
},
},
},
},
&composite.BackendService{},
composite.BackendService{
EnableCDN: true,
CdnPolicy: &composite.BackendServiceCdnPolicy{
CacheKeyPolicy: &composite.CacheKeyPolicy{
IncludeHost: true,
},
},
},
},
{
"overwrite some fields on existing settings",
utils.ServicePort{
desc: "cache settings are different, update needed",
sp: utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
Cdn: &backendconfigv1beta1.CDNConfig{
Enabled: true,
CachePolicy: &backendconfigv1beta1.CacheKeyPolicy{
IncludeHost: true,
IncludeQueryString: false,
IncludeProtocol: false,
IncludeQueryString: true,
},
},
},
},
},
&composite.BackendService{
EnableCDN: false,
CdnPolicy: &composite.BackendServiceCdnPolicy{
CacheKeyPolicy: &composite.CacheKeyPolicy{
IncludeHost: false,
IncludeProtocol: true,
IncludeQueryString: true,
},
},
},
composite.BackendService{
be: &composite.BackendService{
EnableCDN: true,
CdnPolicy: &composite.BackendServiceCdnPolicy{
CacheKeyPolicy: &composite.CacheKeyPolicy{
IncludeHost: true,
IncludeProtocol: false,
IncludeHost: false,
IncludeQueryString: true,
IncludeProtocol: true,
},
},
},
updateExpected: true,
},
{
"no feature settings in spec",
utils.ServicePort{
desc: "enabled setting is different, update needed",
sp: utils.ServicePort{
BackendConfig: &backendconfigv1beta1.BackendConfig{
Spec: backendconfigv1beta1.BackendConfigSpec{
Cdn: nil,
},
},
},
&composite.BackendService{
EnableCDN: true,
CdnPolicy: &composite.BackendServiceCdnPolicy{
CacheKeyPolicy: &composite.CacheKeyPolicy{
IncludeHost: true,
Cdn: &backendconfigv1beta1.CDNConfig{
Enabled: true,
},
},
},
},
composite.BackendService{
be: &composite.BackendService{
EnableCDN: false,
CdnPolicy: &composite.BackendServiceCdnPolicy{
CacheKeyPolicy: &composite.CacheKeyPolicy{
IncludeHost: false,
},
},
},
updateExpected: true,
},
}

for _, testCase := range testCases {
applyCDNSettings(testCase.sp, testCase.be)
if !reflect.DeepEqual(testCase.expected, *testCase.be) {
t.Errorf("%v: expected %+v but got %+v", testCase.desc, testCase.expected, *testCase.be)
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
result := EnsureCDN(tc.sp, tc.be)
if result != tc.updateExpected {
t.Errorf("%v: expected %v but got %v", tc.desc, tc.updateExpected, result)
}
})
}
}
Loading