From 1eac96915eb725bed785fd6170126c97dd0dab4a Mon Sep 17 00:00:00 2001 From: Steve Wagner Date: Mon, 18 Dec 2023 16:55:58 -0800 Subject: [PATCH] Better test coverage (#132) * 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 * - address PR feedback --- .gitignore | 2 ++ internal/communication/roundtripper_test.go | 3 ++ internal/core/event_test.go | 31 +++++++++++++++++++++ internal/core/upstream_server_test.go | 16 +++++++++++ internal/probation/server_test.go | 20 +++++++++++++ 5 files changed, 72 insertions(+) create mode 100644 internal/core/event_test.go create mode 100644 internal/core/upstream_server_test.go diff --git a/.gitignore b/.gitignore index 2228b05..cb5c33a 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ Thumbs.db cover* tmp/ docs/tls/DESIGN.md +:q +qqq \ No newline at end of file diff --git a/internal/communication/roundtripper_test.go b/internal/communication/roundtripper_test.go index 4185b6d..f46fb71 100644 --- a/internal/communication/roundtripper_test.go +++ b/internal/communication/roundtripper_test.go @@ -58,6 +58,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) diff --git a/internal/core/event_test.go b/internal/core/event_test.go new file mode 100644 index 0000000..b3b8926 --- /dev/null +++ b/internal/core/event_test.go @@ -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) + } +} diff --git a/internal/core/upstream_server_test.go b/internal/core/upstream_server_test.go new file mode 100644 index 0000000..7b0eed5 --- /dev/null +++ b/internal/core/upstream_server_test.go @@ -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) + } +} diff --git a/internal/probation/server_test.go b/internal/probation/server_test.go index 5888def..f594bff 100644 --- a/internal/probation/server_test.go +++ b/internal/probation/server_test.go @@ -7,6 +7,8 @@ package probation import ( "github.com/nginxinc/kubernetes-nginx-ingress/test/mocks" + "github.com/sirupsen/logrus" + "net/http" "testing" ) @@ -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() + + defer server.Stop() + + 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("received a response from the probe server: %v", response) +}