Skip to content

Commit 07d1368

Browse files
committed
Better test coverage across most modules
- improve RoundTripper coverage - exclude coverage files from source control - Test coverage for UpstreamServer - Test coverage for probation server module - Test coverage for Event module - ignore test coverage files
1 parent 8ef68dd commit 07d1368

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ Thumbs.db
1818
########
1919
*.log
2020
/misc-dev/
21+
22+
cover.html
23+
cover.out
24+
tmp/
25+
26+
cover.out
27+
cover.html
28+
tmp/

internal/communication/roundtripper_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func TestRoundTripperRoundTrip(t *testing.T) {
5151
t.Fatalf(`Unexpected error: %v`, err)
5252
}
5353

54+
request.Header.Set("Content-Type", "application/json")
55+
request.Header.Set("x-nginx-loadbalancer-kubernetes", "nlk")
56+
5457
response, err := roundTripper.RoundTrip(request)
5558
if err != nil {
5659
t.Fatalf(`Unexpected error: %v`, err)

internal/core/event_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package core
2+
3+
import (
4+
v1 "k8s.io/api/core/v1"
5+
"testing"
6+
)
7+
8+
func TestNewEvent(t *testing.T) {
9+
expectedType := Created
10+
expectedService := &v1.Service{}
11+
expectedPreviousService := &v1.Service{}
12+
expectedNodeIps := []string{"127.0.0.1"}
13+
14+
event := NewEvent(expectedType, expectedService, expectedPreviousService, expectedNodeIps)
15+
16+
if event.Type != expectedType {
17+
t.Errorf("expected Created, got %v", event.Type)
18+
}
19+
20+
if event.Service != expectedService {
21+
t.Errorf("expected service, got %#v", event.Service)
22+
}
23+
24+
if event.PreviousService != expectedPreviousService {
25+
t.Errorf("expected previous service, got %#v", event.PreviousService)
26+
}
27+
28+
if event.NodeIps[0] != expectedNodeIps[0] {
29+
t.Errorf("expected node ips, got %#v", event.NodeIps)
30+
}
31+
}

internal/core/upstream_server_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2023 F5 Inc. All rights reserved.
3+
* Use of this source code is governed by the Apache License that can be found in the LICENSE file.
4+
*/
5+
6+
package core
7+
8+
import "testing"
9+
10+
func TestNewUpstreamServer(t *testing.T) {
11+
host := "localhost"
12+
us := NewUpstreamServer(host)
13+
if us.Host != host {
14+
t.Errorf("NewUpstreamServer(%s) = %s; want %s", host, us.Host, host)
15+
}
16+
}

internal/probation/server_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package probation
77

88
import (
99
"github.com/nginxinc/kubernetes-nginx-ingress/test/mocks"
10+
"github.com/sirupsen/logrus"
11+
"net/http"
1012
"testing"
1113
)
1214

@@ -51,3 +53,21 @@ func TestHealthServer_HandleFailCheck(t *testing.T) {
5153
t.Errorf("Expected 'Service Not Available', got %v", body)
5254
}
5355
}
56+
57+
func TestHealthServer_Start(t *testing.T) {
58+
server := NewHealthServer()
59+
server.Start()
60+
61+
response, err := http.Get("http://localhost:51031/livez")
62+
if err != nil {
63+
t.Error(err)
64+
}
65+
66+
if response.StatusCode != http.StatusOK {
67+
t.Errorf("Expected status code %v, got %v", http.StatusAccepted, response.StatusCode)
68+
}
69+
70+
logrus.Infof("recevied a response from the probe server: %v", response)
71+
72+
server.Stop()
73+
}

0 commit comments

Comments
 (0)