github.com/psiphon-inc/goarista@v0.0.0-20160825065156-d002785f4c67/netns/address_test.go (about) 1 // Copyright (C) 2016 Arista Networks, Inc. 2 // Use of this source code is governed by the Apache License 2.0 3 // that can be found in the COPYING file. 4 5 package netns 6 7 import ( 8 "testing" 9 ) 10 11 func TestParseAddress(t *testing.T) { 12 tests := []struct { 13 desc string 14 arg string 15 vrf string 16 addr string 17 err bool 18 }{{ 19 "Parse address with VRF", 20 "vrf1/1.2.3.4:50", 21 "ns-vrf1", 22 "1.2.3.4:50", 23 false, 24 }, { 25 "Parse address without VRF", 26 "1.2.3.4:50", 27 "", 28 "1.2.3.4:50", 29 false, 30 }, { 31 "Parse malformed input", 32 "vrf1/1.2.3.4/24", 33 "", 34 "", 35 true, 36 }} 37 38 for _, tt := range tests { 39 vrf, addr, err := ParseAddress(tt.arg) 40 if tt.err { 41 if err == nil { 42 t.Fatalf("%s: expected error, but got success", tt.desc) 43 } 44 } else { 45 if err != nil { 46 t.Fatalf("%s: expected success, but got error %s", tt.desc, err) 47 } 48 if addr != tt.addr { 49 t.Fatalf("%s: expected addr %s, but got %s", tt.desc, tt.addr, addr) 50 } 51 if vrf != tt.vrf { 52 t.Fatalf("%s: expected VRF %s, but got %s", tt.desc, tt.vrf, vrf) 53 } 54 } 55 } 56 } 57 58 func TestVrfToNetNSTests(t *testing.T) { 59 tests := []struct { 60 desc string 61 vrf string 62 netNS string 63 }{{ 64 "Empty VRF name", 65 "", 66 "", 67 }, { 68 "Default VRF", 69 "default", 70 "default", 71 }, { 72 "Regular VRF name", 73 "cust1", 74 "ns-cust1", 75 }} 76 for _, tt := range tests { 77 netNS := VRFToNetNS(tt.vrf) 78 if netNS != tt.netNS { 79 t.Fatalf("%s: expected netNS %s, but got %s", tt.desc, tt.netNS, 80 netNS) 81 } 82 } 83 }