| 1 | // Copyright 2019 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 testutilsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/" |
| 19 | "osscip-go gomod github.com/golang/go/src go1.24.2 os/" |
| 20 | "stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/" |
| 21 | |
| 22 | "github.com/containernetworking/cni/pkg/typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/" |
| 23 | ) |
| 24 | |
| 25 | // TmpResolvConf will create a temporary file and write the provided DNS settings to |
| 26 | // it in the resolv.conf format. It returns the path of the created temporary file or |
| 27 | // an error if any occurs while creating/writing the file. It is the caller's |
| 28 | // responsibility to remove the file. |
| 29 | func TmpResolvConfscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/testutils`/TmpResolvConf().(dnsConf typesscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/.DNSscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/DNS#) (string, error) { |
| 30 | f, err := osscip-go gomod github.com/golang/go/src go1.24.2 os/.CreateTempscip-go gomod github.com/golang/go/src go1.24.2 os/CreateTemp().("", "cni_test_resolv.conf") |
| 31 | if err != nil { |
| 32 | return "", fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to get temp file for CNI test resolv.conf: %v", err) |
| 33 | } |
| 34 | defer f.Closescip-go gomod github.com/golang/go/src go1.24.2 os/File#Close().() |
| 35 | |
| 36 | path := f.Namescip-go gomod github.com/golang/go/src go1.24.2 os/File#Name().() |
| 37 | defer func() { |
| 38 | if err != nil { |
| 39 | osscip-go gomod github.com/golang/go/src go1.24.2 os/.RemoveAllscip-go gomod github.com/golang/go/src go1.24.2 os/RemoveAll().(path) |
| 40 | } |
| 41 | }() |
| 42 | |
| 43 | // see "man 5 resolv.conf" for the format of resolv.conf |
| 44 | var resolvConfLines []string |
| 45 | for _, nameserver := range dnsConf.Nameserversscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/DNS#Nameservers. { |
| 46 | resolvConfLines = append(resolvConfLines, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("nameserver %s", nameserver)) |
| 47 | } |
| 48 | resolvConfLines = append(resolvConfLines, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("domain %s", dnsConf.Domainscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/DNS#Domain.)) |
| 49 | resolvConfLines = append(resolvConfLines, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("search %s", stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.2 strings/Join().(dnsConf.Searchscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/DNS#Search., " "))) |
| 50 | resolvConfLines = append(resolvConfLines, fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Sprintf().("options %s", stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.2 strings/Join().(dnsConf.Optionsscip-go gomod github.com/containernetworking/cni v1.3.0 `github.com/containernetworking/cni/pkg/types`/DNS#Options., " "))) |
| 51 | |
| 52 | resolvConf := stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.2 strings/Join().(resolvConfLines, "\n") |
| 53 | _, err = f.Writescip-go gomod github.com/golang/go/src go1.24.2 os/File#Write().([]byte(resolvConf)) |
| 54 | if err != nil { |
| 55 | return "", fmtscip-go gomod github.com/golang/go/src go1.24.2 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.2 fmt/Errorf().("failed to write temp resolv.conf for CNI test: %v", err) |
| 56 | } |
| 57 | |
| 58 | return path, err |
| 59 | } |