Debian Codegraph @

plugins/ipam/host-local/dns_test.go

raw symbols SCIP
1 // Copyright 2016 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package main
16
17 import (
18 "os"
19
20 . "github.com/onsi/ginkgo/v2"
21 . "github.com/onsi/gomega"
22
23 "github.com/containernetworking/cni/pkg/types"
24 )
25
26 var _ = Describe("parsing resolv.conf", func() {
27 It("parses a simple resolv.conf file", func() {
28 contents := `
29 nameserver 192.0.2.0
30 nameserver 192.0.2.1
31 `
32 dns, err := parse(contents)
33 Expect(err).NotTo(HaveOccurred())
34 Expect(*dns).Should(Equal(types.DNS{Nameservers: []string{"192.0.2.0", "192.0.2.1"}}))
35 })
36 It("ignores comments", func() {
37 dns, err := parse(`
38 nameserver 192.0.2.0
39 ;nameserver 192.0.2.1
40 `)
41 Expect(err).NotTo(HaveOccurred())
42 Expect(*dns).Should(Equal(types.DNS{Nameservers: []string{"192.0.2.0"}}))
43 })
44 It("parses all fields", func() {
45 dns, err := parse(`
46 nameserver 192.0.2.0
47 nameserver 192.0.2.2
48 domain example.com
49 ;nameserver comment
50 #nameserver comment
51 search example.net example.org
52 search example.gov
53 options one two three
54 options four
55 `)
56 Expect(err).NotTo(HaveOccurred())
57 Expect(*dns).Should(Equal(types.DNS{
58 Nameservers: []string{"192.0.2.0", "192.0.2.2"},
59 Domain: "example.com",
60 Search: []string{"example.net", "example.org", "example.gov"},
61 Options: []string{"one", "two", "three", "four"},
62 }))
63 })
64 })
65
66 func parse(contents string) (*types.DNS, error) {
67 f, err := os.CreateTemp("", "host_local_resolv")
68 if err != nil {
69 return nil, err
70 }
71 defer f.Close()
72 defer os.Remove(f.Name())
73
74 if _, err := f.WriteString(contents); err != nil {
75 return nil, err
76 }
77
78 return parseResolvConf(f.Name())
79 }