From d925937d5308a7f10b66e378c51b3f80284fd17d Mon Sep 17 00:00:00 2001 From: Riya Singhal Date: Tue, 26 Sep 2023 02:18:21 +0530 Subject: [PATCH] cephfs: adding unit test for fetchID Signed-off-by: Riya Singhal --- internal/csi-addons/networkfence/fencing.go | 1 - .../csi-addons/networkfence/fencing_test.go | 38 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/internal/csi-addons/networkfence/fencing.go b/internal/csi-addons/networkfence/fencing.go index 205f85ffc..eeda40683 100644 --- a/internal/csi-addons/networkfence/fencing.go +++ b/internal/csi-addons/networkfence/fencing.go @@ -224,7 +224,6 @@ func (ac *activeClient) fetchID() (int, error) { } return 0, fmt.Errorf("failed to extract client ID, incorrect format: %s", clientInfo) - } // AddClientEviction blocks access for all the IPs in the CIDR block diff --git a/internal/csi-addons/networkfence/fencing_test.go b/internal/csi-addons/networkfence/fencing_test.go index 31623f083..bbe82120d 100644 --- a/internal/csi-addons/networkfence/fencing_test.go +++ b/internal/csi-addons/networkfence/fencing_test.go @@ -94,3 +94,41 @@ func TestFetchIP(t *testing.T) { }) } } + +func TestFetchID(t *testing.T) { + t.Parallel() + + tests := []struct { + clientInfo string + expectedID int + expectedErr bool + }{ + { + clientInfo: "client.4305 172.21.9.34:0/422650892", + expectedID: 4305, + expectedErr: false, + }, + { + clientInfo: "", + expectedID: 0, + expectedErr: true, + }, + } + + for _, tt := range tests { + ts := tt + t.Run(ts.clientInfo, func(t *testing.T) { + t.Parallel() + ac := &activeClient{Inst: ts.clientInfo} + actualID, actualErr := ac.fetchID() + + if (actualErr != nil) != ts.expectedErr { + t.Errorf("expected error %v but got %v", ts.expectedErr, actualErr) + } + + if actualID != ts.expectedID { + t.Errorf("expected ID %d but got %d", ts.expectedID, actualID) + } + }) + } +}