Skip to content

Commit

Permalink
Better test coverage across most modules
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ciroque committed Dec 18, 2023
1 parent 8ef68dd commit 07d1368
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ Thumbs.db
########
*.log
/misc-dev/

cover.html
cover.out
tmp/

cover.out
cover.html
tmp/
3 changes: 3 additions & 0 deletions internal/communication/roundtripper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func TestRoundTripperRoundTrip(t *testing.T) {
t.Fatalf(`Unexpected error: %v`, err)
}

request.Header.Set("Content-Type", "application/json")
request.Header.Set("x-nginx-loadbalancer-kubernetes", "nlk")

response, err := roundTripper.RoundTrip(request)
if err != nil {
t.Fatalf(`Unexpected error: %v`, err)
Expand Down
31 changes: 31 additions & 0 deletions internal/core/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core

import (
v1 "k8s.io/api/core/v1"
"testing"
)

func TestNewEvent(t *testing.T) {
expectedType := Created
expectedService := &v1.Service{}
expectedPreviousService := &v1.Service{}
expectedNodeIps := []string{"127.0.0.1"}

event := NewEvent(expectedType, expectedService, expectedPreviousService, expectedNodeIps)

if event.Type != expectedType {
t.Errorf("expected Created, got %v", event.Type)
}

if event.Service != expectedService {
t.Errorf("expected service, got %#v", event.Service)
}

if event.PreviousService != expectedPreviousService {
t.Errorf("expected previous service, got %#v", event.PreviousService)
}

if event.NodeIps[0] != expectedNodeIps[0] {
t.Errorf("expected node ips, got %#v", event.NodeIps)
}
}
16 changes: 16 additions & 0 deletions internal/core/upstream_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) 2023 F5 Inc. All rights reserved.
* Use of this source code is governed by the Apache License that can be found in the LICENSE file.
*/

package core

import "testing"

func TestNewUpstreamServer(t *testing.T) {
host := "localhost"
us := NewUpstreamServer(host)
if us.Host != host {
t.Errorf("NewUpstreamServer(%s) = %s; want %s", host, us.Host, host)
}
}
20 changes: 20 additions & 0 deletions internal/probation/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package probation

import (
"github.com/nginxinc/kubernetes-nginx-ingress/test/mocks"
"github.com/sirupsen/logrus"
"net/http"
"testing"
)

Expand Down Expand Up @@ -51,3 +53,21 @@ func TestHealthServer_HandleFailCheck(t *testing.T) {
t.Errorf("Expected 'Service Not Available', got %v", body)
}
}

func TestHealthServer_Start(t *testing.T) {
server := NewHealthServer()
server.Start()

response, err := http.Get("http://localhost:51031/livez")
if err != nil {
t.Error(err)
}

if response.StatusCode != http.StatusOK {
t.Errorf("Expected status code %v, got %v", http.StatusAccepted, response.StatusCode)
}

logrus.Infof("recevied a response from the probe server: %v", response)

server.Stop()
}

0 comments on commit 07d1368

Please sign in to comment.