Skip to content

Commit

Permalink
Add/Update tests relevant for THC Events
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianSawicki committed May 11, 2023
1 parent 78980cc commit 5002833
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 21 deletions.
45 changes: 24 additions & 21 deletions pkg/controller/translator/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,30 +1211,30 @@ func TestSetEnableTHC(t *testing.T) {
sp: &utils.ServicePort{NEGEnabled: true},
svc: newService(map[string]string{thcLabel: thcValue}),
enableTHC: true,
want: &utils.ServicePort{NEGEnabled: true, THCEnabled: true},
want: &utils.ServicePort{NEGEnabled: true, THCEnabled: true, THCEvents: utils.THCEvents{THCConfigured: true}},
},
{
name: "annotation flag disabled",
sp: &utils.ServicePort{NEGEnabled: true, THCEnabled: true},
svc: newService(map[string]string{thcLabel: thcValue}),
enableTHC: false,
want: &utils.ServicePort{NEGEnabled: true, THCEnabled: false},
want: &utils.ServicePort{NEGEnabled: true, THCEnabled: false, THCEvents: utils.THCEvents{THCAnnotationWithoutFlag: true}},
},
{
name: "annotation NEG disabled",
sp: &utils.ServicePort{THCEnabled: true},
svc: newService(map[string]string{thcLabel: thcValue}),
enableTHC: true,
want: &utils.ServicePort{THCEnabled: false},
wantEvent: true,
eventPrefix: "Warning THCAnnotationWithoutNEG THC annotation present, but NEG is disabled. Will not enable Transparent Health Checks.",
name: "annotation NEG disabled",
sp: &utils.ServicePort{THCEnabled: true},
svc: newService(map[string]string{thcLabel: thcValue}),
enableTHC: true,
want: &utils.ServicePort{THCEnabled: false, THCEvents: utils.THCEvents{THCAnnotationWithoutNEG: true}},
},
{
name: "invalid annotation flag disabled",
sp: &utils.ServicePort{NEGEnabled: true, THCEnabled: true},
svc: newService(map[string]string{thcLabel: "random text"}),
enableTHC: false,
want: &utils.ServicePort{NEGEnabled: true, THCEnabled: false},
name: "invalid annotation flag disabled",
sp: &utils.ServicePort{NEGEnabled: true, THCEnabled: true},
svc: newService(map[string]string{thcLabel: "random text"}),
enableTHC: false,
want: &utils.ServicePort{NEGEnabled: true, THCEnabled: false},
wantEvent: true,
eventPrefix: "Warning THCAnnotationParsingFailed Parsing THC annotation failed",
},
{
name: "invalid annotation",
Expand All @@ -1250,7 +1250,7 @@ func TestSetEnableTHC(t *testing.T) {
sp: &utils.ServicePort{BackendConfig: &backendconfig.BackendConfig{}, NEGEnabled: true, THCEnabled: false},
svc: newService(map[string]string{thcLabel: thcValue}),
enableTHC: true,
want: &utils.ServicePort{BackendConfig: &backendconfig.BackendConfig{}, NEGEnabled: true, THCEnabled: true},
want: &utils.ServicePort{BackendConfig: &backendconfig.BackendConfig{}, NEGEnabled: true, THCEnabled: true, THCEvents: utils.THCEvents{THCConfigured: true}},
},
}
BC := &backendconfig.BackendConfig{}
Expand All @@ -1260,16 +1260,13 @@ func TestSetEnableTHC(t *testing.T) {
sp: &utils.ServicePort{BackendConfig: BC, NEGEnabled: true, THCEnabled: true},
svc: newService(map[string]string{thcLabel: thcValue}),
enableTHC: true,
want: &utils.ServicePort{BackendConfig: BC, NEGEnabled: true, THCEnabled: false},
want: &utils.ServicePort{BackendConfig: BC, NEGEnabled: true, THCEnabled: false, THCEvents: utils.THCEvents{BackendConfigOverridesTHC: true}},
})

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
translator := fakeTranslator()
bufferSize := 0
if tc.wantEvent {
bufferSize = 1
}
bufferSize := 1
fakeSingletonRecorderGetter := healthchecks.NewFakeSingletonRecorderGetter(bufferSize)
translator.recorderGetter = fakeSingletonRecorderGetter
translator.enableTHC = tc.enableTHC
Expand All @@ -1279,8 +1276,8 @@ func TestSetEnableTHC(t *testing.T) {
if !reflect.DeepEqual(&sp, tc.want) {
t.Errorf("setEnableTHC, %s\ngot %s,\nwant %s", tc.name, pretty.Sprint(&sp), pretty.Sprint(tc.want))
}
fakeRecorder := fakeSingletonRecorderGetter.FakeRecorder()
if tc.wantEvent {
fakeRecorder := fakeSingletonRecorderGetter.FakeRecorder()
select {
case output := <-fakeRecorder.Events:
if !strings.HasPrefix(output, tc.eventPrefix) {
Expand All @@ -1289,6 +1286,12 @@ func TestSetEnableTHC(t *testing.T) {
case <-time.After(10 * time.Second):
t.Fatalf("Timeout when expecting Event.")
}
} else {
select {
case output := <-fakeRecorder.Events:
t.Fatalf("Unexpected event emitted: %s.", output)
case <-time.After(100 * time.Millisecond):
}
}
})
}
Expand Down
62 changes: 62 additions & 0 deletions pkg/healthchecks/healthchecks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"google.golang.org/api/compute/v1"
api_v1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
networkingV1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/cloud-provider-gcp/providers/gce"
Expand Down Expand Up @@ -535,6 +536,67 @@ func getSingletonHealthcheck(t *testing.T, c *gce.Cloud) *compute.HealthCheck {
return utils.DeepCopyComputeHealthCheck(computeHCs[0]) // Make a copy to avoid reading an overwritten version later.
}

func TestNotifyAboutTHC(t *testing.T) {
t.Parallel()

testClusterValues := gce.DefaultTestClusterValues()
fakeGCE := gce.NewFakeGCECloud(testClusterValues)

fakeSingletonRecorderGetter := NewFakeSingletonRecorderGetter(10)
healthChecks := NewHealthChecker(fakeGCE, "/", defaultBackendSvc, fakeSingletonRecorderGetter, NewFakeServiceGetter(), false)

hc := translator.DefaultHealthCheck(3000, annotations.ProtocolHTTP)
hc.Service = &v1.Service{}

type tc = struct {
wantTexts []string
events utils.THCEvents
}

testCases := []tc{
{
wantTexts: []string{"Normal THCConfigured Transparent Health Check successfully configured."},
events: utils.THCEvents{THCConfigured: true},
},
{
wantTexts: []string{"Warning BackendConfigOverridesTHC Both THC and BackendConfig annotations present and the BackendConfig has spec.healthCheck. The THC annotation will be ignored."},
events: utils.THCEvents{BackendConfigOverridesTHC: true},
},
{
wantTexts: []string{"Warning THCAnnotationWithoutFlag THC annotation present, but the Transparent Health Checks feature is not enabled."},
events: utils.THCEvents{THCAnnotationWithoutFlag: true},
},
{
wantTexts: []string{"Warning THCAnnotationWithoutFlag THC annotation present, but the Transparent Health Checks feature is not enabled.", "Warning THCAnnotationWithoutFlag THC annotation is present for at least one Service, but the Transparent Health Checks feature is not enabled."},
events: utils.THCEvents{THCAnnotationWithoutFlag: true, Ingress: &networkingV1.Ingress{}},
},
{
wantTexts: []string{"Warning THCAnnotationWithoutNEG THC annotation present, but NEG is disabled. Will not enable Transparent Health Checks."},
events: utils.THCEvents{THCAnnotationWithoutNEG: true},
},
}

fakeRecorder := fakeSingletonRecorderGetter.FakeRecorder()
for _, tc := range testCases {
healthChecks.notifyAboutTHC(hc, tc.events)
for _, wantText := range tc.wantTexts {
select {
case output := <-fakeRecorder.Events:
if output != wantText {
t.Fatalf("Incorrect event emitted on healthcheck update: %s.", output)
}
case <-time.After(10 * time.Second):
t.Fatalf("Timeout when expecting Event.")
}
}
select {
case output := <-fakeRecorder.Events:
t.Fatalf("Unexpected event: %s", output)
case <-time.After(100 * time.Millisecond):
}
}
}

// Test changing the value of the flag EnableUpdateCustomHealthCheckDescription from false to true.
func TestRolloutUpdateCustomHCDescription(t *testing.T) {
// No parallel() because we modify the value of the flags:
Expand Down

0 comments on commit 5002833

Please sign in to comment.