From 18cfe931d1c8c3a4acc5fdc5ea654d143d7f1ade Mon Sep 17 00:00:00 2001 From: Riya Singhal Date: Tue, 26 Sep 2023 01:11:04 +0530 Subject: [PATCH] cephfs: adding unit test for fetchIP for client eviction Signed-off-by: Riya Singhal --- .../csi-addons/networkfence/fencing_test.go | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/internal/csi-addons/networkfence/fencing_test.go b/internal/csi-addons/networkfence/fencing_test.go index 6803c994c54d..2dc7cfde2060 100644 --- a/internal/csi-addons/networkfence/fencing_test.go +++ b/internal/csi-addons/networkfence/fencing_test.go @@ -51,3 +51,48 @@ func TestGetIPRange(t *testing.T) { }) } } + +func TestFetchIP(t *testing.T) { + t.Parallel() + + tests := []struct { + clientInfo string + expectedIP string + expectedErr bool + }{ + { + clientInfo: "client.4305 172.21.9.34:0/422650892", + expectedIP: "172.21.9.34", + expectedErr: false, + }, + { + clientInfo: "invalid client info", + expectedIP: "", + expectedErr: true, + }, + { + clientInfo: "", + expectedIP: "", + expectedErr: true, + }, + } + + for _, tt := range tests { + tt := tt + + t.Run(tt.clientInfo, func(t *testing.T) { + t.Parallel() + + client := activeClient{Inst: tt.clientInfo} + ip, actualErr := client.fetchIP() + + if (actualErr != nil) != tt.expectedErr { + t.Errorf("expected error %v but got %v", tt.expectedErr, actualErr) + } + + if ip != tt.expectedIP { + t.Errorf("expected IP %s but got %s", tt.expectedIP, ip) + } + }) + } +}