github.com/xuyutom/docker@v1.6.0/pkg/networkfs/resolvconf/resolvconf_test.go (about) 1 package resolvconf 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "testing" 8 ) 9 10 func TestGet(t *testing.T) { 11 resolvConfUtils, err := Get() 12 if err != nil { 13 t.Fatal(err) 14 } 15 resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf") 16 if err != nil { 17 t.Fatal(err) 18 } 19 if string(resolvConfUtils) != string(resolvConfSystem) { 20 t.Fatalf("/etc/resolv.conf and GetResolvConf have different content.") 21 } 22 } 23 24 func TestGetNameservers(t *testing.T) { 25 for resolv, result := range map[string][]string{` 26 nameserver 1.2.3.4 27 nameserver 40.3.200.10 28 search example.com`: {"1.2.3.4", "40.3.200.10"}, 29 `search example.com`: {}, 30 `nameserver 1.2.3.4 31 search example.com 32 nameserver 4.30.20.100`: {"1.2.3.4", "4.30.20.100"}, 33 ``: {}, 34 ` nameserver 1.2.3.4 `: {"1.2.3.4"}, 35 `search example.com 36 nameserver 1.2.3.4 37 #nameserver 4.3.2.1`: {"1.2.3.4"}, 38 `search example.com 39 nameserver 1.2.3.4 # not 4.3.2.1`: {"1.2.3.4"}, 40 } { 41 test := GetNameservers([]byte(resolv)) 42 if !strSlicesEqual(test, result) { 43 t.Fatalf("Wrong nameserver string {%s} should be %v. Input: %s", test, result, resolv) 44 } 45 } 46 } 47 48 func TestGetNameserversAsCIDR(t *testing.T) { 49 for resolv, result := range map[string][]string{` 50 nameserver 1.2.3.4 51 nameserver 40.3.200.10 52 search example.com`: {"1.2.3.4/32", "40.3.200.10/32"}, 53 `search example.com`: {}, 54 `nameserver 1.2.3.4 55 search example.com 56 nameserver 4.30.20.100`: {"1.2.3.4/32", "4.30.20.100/32"}, 57 ``: {}, 58 ` nameserver 1.2.3.4 `: {"1.2.3.4/32"}, 59 `search example.com 60 nameserver 1.2.3.4 61 #nameserver 4.3.2.1`: {"1.2.3.4/32"}, 62 `search example.com 63 nameserver 1.2.3.4 # not 4.3.2.1`: {"1.2.3.4/32"}, 64 } { 65 test := GetNameserversAsCIDR([]byte(resolv)) 66 if !strSlicesEqual(test, result) { 67 t.Fatalf("Wrong nameserver string {%s} should be %v. Input: %s", test, result, resolv) 68 } 69 } 70 } 71 72 func TestGetSearchDomains(t *testing.T) { 73 for resolv, result := range map[string][]string{ 74 `search example.com`: {"example.com"}, 75 `search example.com # ignored`: {"example.com"}, 76 ` search example.com `: {"example.com"}, 77 ` search example.com # ignored`: {"example.com"}, 78 `search foo.example.com example.com`: {"foo.example.com", "example.com"}, 79 ` search foo.example.com example.com `: {"foo.example.com", "example.com"}, 80 ` search foo.example.com example.com # ignored`: {"foo.example.com", "example.com"}, 81 ``: {}, 82 `# ignored`: {}, 83 `nameserver 1.2.3.4 84 search foo.example.com example.com`: {"foo.example.com", "example.com"}, 85 `nameserver 1.2.3.4 86 search dup1.example.com dup2.example.com 87 search foo.example.com example.com`: {"foo.example.com", "example.com"}, 88 `nameserver 1.2.3.4 89 search foo.example.com example.com 90 nameserver 4.30.20.100`: {"foo.example.com", "example.com"}, 91 } { 92 test := GetSearchDomains([]byte(resolv)) 93 if !strSlicesEqual(test, result) { 94 t.Fatalf("Wrong search domain string {%s} should be %v. Input: %s", test, result, resolv) 95 } 96 } 97 } 98 99 func strSlicesEqual(a, b []string) bool { 100 if len(a) != len(b) { 101 return false 102 } 103 104 for i, v := range a { 105 if v != b[i] { 106 return false 107 } 108 } 109 110 return true 111 } 112 113 func TestBuild(t *testing.T) { 114 file, err := ioutil.TempFile("", "") 115 if err != nil { 116 t.Fatal(err) 117 } 118 defer os.Remove(file.Name()) 119 120 err = Build(file.Name(), []string{"ns1", "ns2", "ns3"}, []string{"search1"}) 121 if err != nil { 122 t.Fatal(err) 123 } 124 125 content, err := ioutil.ReadFile(file.Name()) 126 if err != nil { 127 t.Fatal(err) 128 } 129 130 if expected := "nameserver ns1\nnameserver ns2\nnameserver ns3\nsearch search1\n"; !bytes.Contains(content, []byte(expected)) { 131 t.Fatalf("Expected to find '%s' got '%s'", expected, content) 132 } 133 } 134 135 func TestBuildWithZeroLengthDomainSearch(t *testing.T) { 136 file, err := ioutil.TempFile("", "") 137 if err != nil { 138 t.Fatal(err) 139 } 140 defer os.Remove(file.Name()) 141 142 err = Build(file.Name(), []string{"ns1", "ns2", "ns3"}, []string{"."}) 143 if err != nil { 144 t.Fatal(err) 145 } 146 147 content, err := ioutil.ReadFile(file.Name()) 148 if err != nil { 149 t.Fatal(err) 150 } 151 152 if expected := "nameserver ns1\nnameserver ns2\nnameserver ns3\n"; !bytes.Contains(content, []byte(expected)) { 153 t.Fatalf("Expected to find '%s' got '%s'", expected, content) 154 } 155 if notExpected := "search ."; bytes.Contains(content, []byte(notExpected)) { 156 t.Fatalf("Expected to not find '%s' got '%s'", notExpected, content) 157 } 158 } 159 160 func TestFilterResolvDns(t *testing.T) { 161 ns0 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\n" 162 163 if result, _ := FilterResolvDns([]byte(ns0), false); result != nil { 164 if ns0 != string(result) { 165 t.Fatalf("Failed No Localhost: expected \n<%s> got \n<%s>", ns0, string(result)) 166 } 167 } 168 169 ns1 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\nnameserver 127.0.0.1\n" 170 if result, _ := FilterResolvDns([]byte(ns1), false); result != nil { 171 if ns0 != string(result) { 172 t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result)) 173 } 174 } 175 176 ns1 = "nameserver 10.16.60.14\nnameserver 127.0.0.1\nnameserver 10.16.60.21\n" 177 if result, _ := FilterResolvDns([]byte(ns1), false); result != nil { 178 if ns0 != string(result) { 179 t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result)) 180 } 181 } 182 183 ns1 = "nameserver 127.0.1.1\nnameserver 10.16.60.14\nnameserver 10.16.60.21\n" 184 if result, _ := FilterResolvDns([]byte(ns1), false); result != nil { 185 if ns0 != string(result) { 186 t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result)) 187 } 188 } 189 190 ns1 = "nameserver ::1\nnameserver 10.16.60.14\nnameserver 127.0.2.1\nnameserver 10.16.60.21\n" 191 if result, _ := FilterResolvDns([]byte(ns1), false); result != nil { 192 if ns0 != string(result) { 193 t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result)) 194 } 195 } 196 197 ns1 = "nameserver 10.16.60.14\nnameserver ::1\nnameserver 10.16.60.21\nnameserver ::1" 198 if result, _ := FilterResolvDns([]byte(ns1), false); result != nil { 199 if ns0 != string(result) { 200 t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result)) 201 } 202 } 203 204 // with IPv6 disabled (false param), the IPv6 nameserver should be removed 205 ns1 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\nnameserver ::1" 206 if result, _ := FilterResolvDns([]byte(ns1), false); result != nil { 207 if ns0 != string(result) { 208 t.Fatalf("Failed Localhost+IPv6 off: expected \n<%s> got \n<%s>", ns0, string(result)) 209 } 210 } 211 212 // with IPv6 enabled, the IPv6 nameserver should be preserved 213 ns0 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\n" 214 ns1 = "nameserver 10.16.60.14\nnameserver 2002:dead:beef::1\nnameserver 10.16.60.21\nnameserver ::1" 215 if result, _ := FilterResolvDns([]byte(ns1), true); result != nil { 216 if ns0 != string(result) { 217 t.Fatalf("Failed Localhost+IPv6 on: expected \n<%s> got \n<%s>", ns0, string(result)) 218 } 219 } 220 221 // with IPv6 enabled, and no non-localhost servers, Google defaults (both IPv4+IPv6) should be added 222 ns0 = "\nnameserver 8.8.8.8\nnameserver 8.8.4.4\nnameserver 2001:4860:4860::8888\nnameserver 2001:4860:4860::8844" 223 ns1 = "nameserver 127.0.0.1\nnameserver ::1\nnameserver 127.0.2.1" 224 if result, _ := FilterResolvDns([]byte(ns1), true); result != nil { 225 if ns0 != string(result) { 226 t.Fatalf("Failed no Localhost+IPv6 enabled: expected \n<%s> got \n<%s>", ns0, string(result)) 227 } 228 } 229 230 // with IPv6 disabled, and no non-localhost servers, Google defaults (only IPv4) should be added 231 ns0 = "\nnameserver 8.8.8.8\nnameserver 8.8.4.4" 232 ns1 = "nameserver 127.0.0.1\nnameserver ::1\nnameserver 127.0.2.1" 233 if result, _ := FilterResolvDns([]byte(ns1), false); result != nil { 234 if ns0 != string(result) { 235 t.Fatalf("Failed no Localhost+IPv6 enabled: expected \n<%s> got \n<%s>", ns0, string(result)) 236 } 237 } 238 }