|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestOffsetIP(t *testing.T) { |
| 9 | + cases := []struct { |
| 10 | + input string |
| 11 | + offset int |
| 12 | + expected string |
| 13 | + }{ |
| 14 | + {"10.3.0.0/24", 1, "10.3.0.1"}, |
| 15 | + {"10.3.0.0/24", 10, "10.3.0.10"}, |
| 16 | + {"10.3.0.0/24", 15, "10.3.0.15"}, |
| 17 | + {"10.3.0.0/16", 1, "10.3.0.1"}, |
| 18 | + {"10.3.0.0/16", 10, "10.3.0.10"}, |
| 19 | + {"10.3.0.0/16", 15, "10.3.0.15"}, |
| 20 | + {"10.33.1.200/16", 1, "10.33.0.1"}, |
| 21 | + {"10.33.1.200/16", 10, "10.33.0.10"}, |
| 22 | + {"10.33.1.200/16", 15, "10.33.0.15"}, |
| 23 | + {"192.168.1.0/24", 15, "192.168.1.15"}, |
| 24 | + } |
| 25 | + |
| 26 | + for _, c := range cases { |
| 27 | + _, cidr, err := net.ParseCIDR(c.input) |
| 28 | + if err != nil { |
| 29 | + t.Errorf("unexpected CIDR parse error: %v", err) |
| 30 | + } |
| 31 | + ip, err := offsetServiceIP(cidr, c.offset) |
| 32 | + if ip.String() != c.expected { |
| 33 | + t.Errorf("expected %s, got %s", c.expected, ip.String()) |
| 34 | + } |
| 35 | + } |
| 36 | +} |
0 commit comments