From 84d68c82e78cd0f9e26f58382cf050ca37f32969 Mon Sep 17 00:00:00 2001 From: Riya Singhal Date: Fri, 13 Oct 2023 17:07:35 +0530 Subject: [PATCH] cephfs: add unit test for fetchIPfromEntry Signed-off-by: Riya Singhal --- .../csi-addons/networkfence/fencing_test.go | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/internal/csi-addons/networkfence/fencing_test.go b/internal/csi-addons/networkfence/fencing_test.go index bbe82120d070..c0994714fae5 100644 --- a/internal/csi-addons/networkfence/fencing_test.go +++ b/internal/csi-addons/networkfence/fencing_test.go @@ -132,3 +132,40 @@ func TestFetchID(t *testing.T) { }) } } + +func TestFetchIPfromEntry(t *testing.T) { + t.Parallel() + + tests := []struct { + entry string + expectedIP string + expectedErr bool + }{ + { + entry: "127.0.0.1:0/3710147553 2018-03-19 11:32:24.716146", + expectedIP: "127.0.0.1", + expectedErr: false, + }, + { + entry: "", + expectedIP: "", + expectedErr: true, + }, + } + + for _, tt := range tests { + ts := tt + t.Run(ts.entry, func(t *testing.T) { + t.Parallel() + ip, actualErr := fetchIPfromEntry(ts.entry) + + if (actualErr != nil) != ts.expectedErr { + t.Errorf("expected error %v but got %v", ts.expectedErr, actualErr) + } + + if ip != ts.expectedIP { + t.Errorf("expected IP %s but got %s", ts.expectedIP, ip) + } + }) + } +}