From c721913cbe8e12746784676fea70441186ec9f08 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Mon, 24 Feb 2025 12:25:32 +0000 Subject: [PATCH] Increase tests coverage. --- autocleanup_test.go | 22 ++++++++ metadata_cache_test.go | 119 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 autocleanup_test.go create mode 100644 metadata_cache_test.go diff --git a/autocleanup_test.go b/autocleanup_test.go new file mode 100644 index 0000000..e7266a3 --- /dev/null +++ b/autocleanup_test.go @@ -0,0 +1,22 @@ +package traefikoidc + +import ( + "sync/atomic" + "testing" + "time" +) + +func TestAutoCleanupRoutine(t *testing.T) { + var counter int32 + cleanupFunc := func() { + atomic.AddInt32(&counter, 1) + } + stop := make(chan struct{}) + go autoCleanupRoutine(50*time.Millisecond, stop, cleanupFunc) + time.Sleep(250 * time.Millisecond) + close(stop) + + if atomic.LoadInt32(&counter) < 3 { + t.Errorf("Expected cleanup to be called at least 3 times, got %d", counter) + } +} \ No newline at end of file diff --git a/metadata_cache_test.go b/metadata_cache_test.go new file mode 100644 index 0000000..610a1ce --- /dev/null +++ b/metadata_cache_test.go @@ -0,0 +1,119 @@ +package traefikoidc + +import ( + "fmt" + "net/http" + "testing" + "time" +) + +func TestIsCacheValid(t *testing.T) { + // Setup with a dummy ProviderMetadata. + pm := &ProviderMetadata{} + mc := &MetadataCache{ + metadata: pm, + expiresAt: time.Now().Add(1 * time.Hour), + } + if !mc.isCacheValid() { + t.Errorf("Expected cache to be valid") + } + mc.expiresAt = time.Now().Add(-1 * time.Hour) + if mc.isCacheValid() { + t.Errorf("Expected cache to be invalid") + } +} + +func TestCleanup(t *testing.T) { + pm := &ProviderMetadata{} + mc := &MetadataCache{ + metadata: pm, + expiresAt: time.Now().Add(-1 * time.Hour), + } + mc.Cleanup() + if mc.metadata != nil { + t.Errorf("Expected metadata to be nil after cleanup") + } +} + +func TestGetMetadata_Cached(t *testing.T) { + dummyData := &ProviderMetadata{} + // Construct MetadataCache manually to avoid interference from auto cleanup. + mc := &MetadataCache{ + metadata: dummyData, + expiresAt: time.Now().Add(1 * time.Hour), + stopCleanup: make(chan struct{}), + autoCleanupInterval: 5 * time.Minute, + } + // Use NewLogger to create a logger that writes errors only. + logger := NewLogger("error") + result, err := mc.GetMetadata("http://example.com", http.DefaultClient, logger) + if err != nil { + t.Errorf("Expected no error, got %v", err) + } + if result != dummyData { + t.Errorf("Expected cached metadata to be returned") + } +} + +func TestMetadataCacheAutoCleanup(t *testing.T) { + mc := &MetadataCache{ + autoCleanupInterval: 50 * time.Millisecond, + stopCleanup: make(chan struct{}), + } + // Start auto cleanup. + go mc.startAutoCleanup() + mc.mutex.Lock() + mc.metadata = &ProviderMetadata{} + mc.expiresAt = time.Now().Add(-50 * time.Millisecond) + mc.mutex.Unlock() + + // Wait enough time for the auto cleanup to run. + time.Sleep(200 * time.Millisecond) + mc.Close() + mc.mutex.RLock() + defer mc.mutex.RUnlock() + if mc.metadata != nil { + t.Errorf("Expected metadata to be cleared by auto cleanup") + } +} + +type errorRoundTripper struct { + err error +} + +func (e errorRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { + return nil, e.err +} + +func TestGetMetadata_FetchError(t *testing.T) { + // Create an HTTP client that always returns an error. + errorClient := &http.Client{ + Transport: errorRoundTripper{err: fmt.Errorf("fake fetch error")}, + } + + // Case 1: Cache is empty. + mc := &MetadataCache{ + stopCleanup: make(chan struct{}), + } + logger := NewLogger("error") + metadata, err := mc.GetMetadata("http://example.com", errorClient, logger) + if err == nil { + t.Errorf("Expected error, got nil") + } + if metadata != nil { + t.Errorf("Expected nil metadata, got %v", metadata) + } + + // Case 2: Cache has old metadata. + dummy := &ProviderMetadata{} + mc.metadata = dummy + mc.expiresAt = time.Now().Add(-1 * time.Minute) + logger2 := NewLogger("error") + metadata, err = mc.GetMetadata("http://example.com", errorClient, logger2) + if err != nil { + t.Errorf("Expected no error when cached metadata exists, got %v", err) + } + if metadata != dummy { + t.Errorf("Expected cached metadata to be returned") + } +} \ No newline at end of file