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) + } + }) + } +}