From 64ee1b0bc5196527bf4b3f6fc92def947bccc307 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Mon, 21 Mar 2022 22:34:18 -0500 Subject: [PATCH] chore: Add tests for runtime WithLogger opt --- runtime/docker/opts_test.go | 45 ++++++++++++++++++++++++++++++++ runtime/kubernetes/opts_test.go | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/runtime/docker/opts_test.go b/runtime/docker/opts_test.go index 456df3b4..4d1a8c35 100644 --- a/runtime/docker/opts_test.go +++ b/runtime/docker/opts_test.go @@ -5,6 +5,7 @@ package docker import ( + "github.com/sirupsen/logrus" "reflect" "testing" ) @@ -72,3 +73,47 @@ func TestDocker_ClientOpt_WithHostVolumes(t *testing.T) { } } } + +func TestDocker_ClientOpt_WithLogger(t *testing.T) { + // setup tests + tests := []struct { + failure bool + logger *logrus.Entry + }{ + { + failure: false, + logger: &logrus.Entry{}, + }, + { + failure: false, + logger: nil, + }, + } + + // run tests + for _, test := range tests { + _service, err := New( + WithLogger(test.logger), + ) + + if test.failure { + if err == nil { + t.Errorf("WithLogger should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("WithLogger returned err: %v", err) + } + + if test.logger == nil && _service.Logger == nil { + t.Errorf("_engine.Logger should not be nil even if nil is passed to WithLogger") + } + + if test.logger != nil && !reflect.DeepEqual(_service.Logger, test.logger) { + t.Errorf("WithLogger set %v, want %v", _service.Logger, test.logger) + } + } +} diff --git a/runtime/kubernetes/opts_test.go b/runtime/kubernetes/opts_test.go index aaff223d..f1131cee 100644 --- a/runtime/kubernetes/opts_test.go +++ b/runtime/kubernetes/opts_test.go @@ -5,6 +5,7 @@ package kubernetes import ( + "github.com/sirupsen/logrus" "reflect" "testing" ) @@ -161,3 +162,48 @@ func TestKubernetes_ClientOpt_WithPrivilegedImages(t *testing.T) { } } } + +func TestKubernetes_ClientOpt_WithLogger(t *testing.T) { + // setup tests + tests := []struct { + failure bool + logger *logrus.Entry + }{ + { + failure: false, + logger: &logrus.Entry{}, + }, + { + failure: false, + logger: nil, + }, + } + + // run tests + for _, test := range tests { + _engine, err := New( + WithConfigFile("testdata/config"), + WithLogger(test.logger), + ) + + if test.failure { + if err == nil { + t.Errorf("WithLogger should have returned err") + } + + continue + } + + if err != nil { + t.Errorf("WithLogger returned err: %v", err) + } + + if test.logger == nil && _engine.Logger == nil { + t.Errorf("_engine.Logger should not be nil even if nil is passed to WithLogger") + } + + if test.logger != nil && !reflect.DeepEqual(_engine.Logger, test.logger) { + t.Errorf("WithLogger set %v, want %v", _engine.Logger, test.logger) + } + } +}