From b4dce5b78143bbc84661c644c6c61c93fbb4a91b Mon Sep 17 00:00:00 2001 From: Riya Singhal Date: Fri, 13 Oct 2023 17:31:24 +0530 Subject: [PATCH] cephfs: add unit test for ParseBlocklistEntries Signed-off-by: Riya Singhal --- .../csi-addons/networkfence/fencing_test.go | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/internal/csi-addons/networkfence/fencing_test.go b/internal/csi-addons/networkfence/fencing_test.go index c0994714fae5..b9a6db4b8734 100644 --- a/internal/csi-addons/networkfence/fencing_test.go +++ b/internal/csi-addons/networkfence/fencing_test.go @@ -169,3 +169,65 @@ func TestFetchIPfromEntry(t *testing.T) { }) } } + +func TestParseBlocklistEntries(t *testing.T) { + t.Parallel() + + tests := []struct { + output string + expected []string + expectedSize int + expectedErr bool + }{ + { + //nolint:lll // sample output cannot be split into multiple lines. + output: "listed 2 entries\n127.0.0.1:0/3710147553 2018-03-19 11:32:24.716146\n192.168.0.1:0/1234567890 2022-08-23 14:12:34.567890\n", + expected: []string{ + "127.0.0.1", + "192.168.0.1", + }, + expectedSize: 2, + expectedErr: false, + }, + { + output: "listed 0 entries\n", + expected: []string{}, + expectedSize: 0, + expectedErr: false, + }, + { + output: "listed 1 entries\n192.168.0.1:0/1234567890 2022-08-23 14:12:34.567890\n", + expected: []string{"192.168.0.1"}, + expectedSize: 1, + expectedErr: false, + }, + { + output: "", + expected: []string{}, + expectedSize: 0, + expectedErr: false, + }, + } + + for _, tt := range tests { + ts := tt + t.Run(ts.output, func(t *testing.T) { + t.Parallel() + entries, actualErr := parseBlocklistEntries(ts.output) + + if len(entries) != ts.expectedSize { + t.Errorf("expected length %d but got %d", ts.expectedSize, len(entries)) + } + + if (actualErr != nil) != ts.expectedErr { + t.Errorf("expected error %v but got %v", ts.expectedErr, actualErr) + } + + for i, entry := range entries { + if entry != ts.expected[i] { + t.Errorf("expected entry %s but got %s", ts.expected[i], entry) + } + } + }) + } +}