|
| 1 | +// nolint:dupl |
| 2 | +package utils |
| 3 | + |
| 4 | +import "testing" |
| 5 | + |
| 6 | +// TestGetCIDRs tests the GetCIDRs function. |
| 7 | +func TestGetCIDRs(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + name string |
| 10 | + obj map[string]interface{} |
| 11 | + expected string |
| 12 | + }{ |
| 13 | + { |
| 14 | + name: "nil input", |
| 15 | + obj: nil, |
| 16 | + expected: "", |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "empty map", |
| 20 | + obj: map[string]interface{}{}, |
| 21 | + expected: "", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "missing spec", |
| 25 | + obj: map[string]interface{}{ |
| 26 | + "foo": "bar", |
| 27 | + }, |
| 28 | + expected: "", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "spec is not a map", |
| 32 | + obj: map[string]interface{}{ |
| 33 | + "spec": "not-a-map", |
| 34 | + }, |
| 35 | + expected: "", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "spec without cidr", |
| 39 | + obj: map[string]interface{}{ |
| 40 | + "spec": map[string]interface{}{ |
| 41 | + "foo": "bar", |
| 42 | + }, |
| 43 | + }, |
| 44 | + expected: "", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "spec with cidr", |
| 48 | + obj: map[string]interface{}{ |
| 49 | + "spec": map[string]interface{}{ |
| 50 | + "cidr": "192.168.1.0/24", |
| 51 | + }, |
| 52 | + }, |
| 53 | + expected: "192.168.1.0/24", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "spec with cidr as empty string", |
| 57 | + obj: map[string]interface{}{ |
| 58 | + "spec": map[string]interface{}{ |
| 59 | + "cidr": "", |
| 60 | + }, |
| 61 | + }, |
| 62 | + expected: "", |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "cidr is not a string", |
| 66 | + obj: map[string]interface{}{ |
| 67 | + "spec": map[string]interface{}{ |
| 68 | + "cidr": 12345, |
| 69 | + }, |
| 70 | + }, |
| 71 | + expected: "", |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for _, tt := range tests { |
| 76 | + t.Run(tt.name, func(t *testing.T) { |
| 77 | + result := GetCIDRs(tt.obj) |
| 78 | + if result != tt.expected { |
| 79 | + t.Errorf("expected %q, got %q", tt.expected, result) |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
0 commit comments