go.ligato.io/vpp-agent/v3@v3.5.0/tests/e2e/e2etest/dns.go (about) 1 // Copyright (c) 2020 Pantheon.tech 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 e2etest 16 17 import ( 18 "fmt" 19 20 docker "github.com/fsouza/go-dockerclient" 21 "github.com/go-errors/errors" 22 ) 23 24 const ( 25 LigatoDNSHostNameSuffix = "test.ligato.io" 26 dnsImage = "coredns/coredns:1.8.0" 27 dnsStopTimeout = 1 // seconds 28 ) 29 30 // DNSServer is represents running DNS server 31 type DNSServer struct { 32 ComponentRuntime 33 ctx *TestCtx 34 } 35 36 // NewDNSServer creates and starts new DNS server container 37 func NewDNSServer(ctx *TestCtx, optMods ...DNSOptModifier) (*DNSServer, error) { 38 // compute options 39 opts := DefaultDNSOpt(ctx) 40 for _, mod := range optMods { 41 mod(opts) 42 } 43 44 // create struct for DNS server 45 dnsServer := &DNSServer{ 46 ComponentRuntime: opts.Runtime, 47 ctx: ctx, 48 } 49 50 // get runtime specific options and start DNS server in runtime environment 51 startOpts, err := opts.RuntimeStartOptions(ctx, opts) 52 if err != nil { 53 return nil, errors.Errorf("can't get DNSServer start option for runtime due to: %v", err) 54 } 55 err = dnsServer.Start(startOpts) 56 if err != nil { 57 return nil, errors.Errorf("can't start DNS server due to: %v", err) 58 } 59 60 return dnsServer, nil 61 } 62 63 func (dns *DNSServer) Stop(options ...interface{}) error { 64 if err := dns.ComponentRuntime.Stop(options); err != nil { 65 // not additionally cleaning up after attempting to stop test topology component because 66 // it would lock access to further inspection of this component (i.e. why it won't stop) 67 return err 68 } 69 // cleanup 70 dns.ctx.DNSServer = nil 71 return nil 72 } 73 74 // DNSServerStartOptionsForContainerRuntime translates DNSOpt to options for ComponentRuntime.Start(option) 75 // method implemented by ContainerRuntime 76 func DNSServerStartOptionsForContainerRuntime(ctx *TestCtx, options interface{}) (interface{}, error) { 77 opts, ok := options.(*DNSOpt) 78 if !ok { 79 return nil, errors.Errorf("expected DNSOpt but got %+v", options) 80 } 81 82 // create configuration files on shared-files docker volume 83 hostFileContent := fmt.Sprintf(` 84 # Hosts file for Domain: %s 85 # Place entries below in standard hosts file format: ipaddress hostname fqdn 86 %s 87 `, opts.DomainNameSuffix, opts.HostsConfig) 88 hostsFilepath := CreateFileOnSharedVolume(ctx, "staticHosts", hostFileContent) 89 corefileContent := fmt.Sprintf(`%s { 90 log 91 errors 92 hosts %s %s 93 } 94 . { 95 # everything else will be resolved by external public DNS server 96 log 97 errors 98 forward . 8.8.8.8:53 99 } 100 `, opts.DomainNameSuffix, hostsFilepath, opts.DomainNameSuffix) 101 coreFilepath := CreateFileOnSharedVolume(ctx, "Corefile", corefileContent) 102 103 // construct container options 104 containerOptions := &docker.CreateContainerOptions{ 105 Name: "e2e-test-dns", 106 Config: &docker.Config{ 107 Image: dnsImage, 108 Cmd: []string{"-conf", coreFilepath}, // CMD adds only additional parameters to command in ENTRYPOINT 109 }, 110 HostConfig: &docker.HostConfig{ 111 Binds: []string{ 112 shareVolumeName + ":" + ctx.ShareDir, // needed for coredns configuration 113 }, 114 }, 115 } 116 117 return &ContainerStartOptions{ 118 ContainerOptions: containerOptions, 119 Pull: true, 120 }, nil 121 }