github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_dns_test.go (about)

     1  // Copyright 2016 The rkt 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  	"crypto/sha1"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/rkt/rkt/tests/testutils"
    26  )
    27  
    28  // TestDNS is checking how rkt fills /etc/resolv.conf
    29  func TestDNSParam(t *testing.T) {
    30  	imageFile := patchTestACI("rkt-inspect-exit.aci", "--exec=/inspect --print-msg=Hello --read-file")
    31  	defer os.Remove(imageFile)
    32  	ctx := testutils.NewRktRunCtx()
    33  	defer ctx.Cleanup()
    34  
    35  	for i, tt := range []struct {
    36  		paramDNS      string
    37  		expectedLine  string
    38  		expectedError bool
    39  	}{
    40  		{
    41  			paramDNS:      "",
    42  			expectedLine:  "Cannot read file",
    43  			expectedError: TestedFlavor.ExitStatusPreserved,
    44  		},
    45  		{
    46  			paramDNS:      "--dns=8.8.4.4",
    47  			expectedLine:  "nameserver 8.8.4.4",
    48  			expectedError: false,
    49  		},
    50  		{
    51  			paramDNS:      "--dns=8.8.8.8 --dns=8.8.4.4",
    52  			expectedLine:  "nameserver 8.8.8.8",
    53  			expectedError: false,
    54  		},
    55  		{
    56  			paramDNS:      "--dns=8.8.8.8 --dns=8.8.4.4 --dns-search=search.com --dns-opt=debug",
    57  			expectedLine:  "nameserver 8.8.4.4",
    58  			expectedError: false,
    59  		},
    60  		{
    61  			paramDNS:      "--dns-search=foo.com --dns-search=bar.com",
    62  			expectedLine:  "search foo.com bar.com",
    63  			expectedError: false,
    64  		},
    65  		{
    66  			paramDNS:      "--dns-opt=debug --dns-opt=use-vc --dns-opt=rotate",
    67  			expectedLine:  "options debug use-vc rotate",
    68  			expectedError: false,
    69  		},
    70  		{
    71  			paramDNS:      "--dns-opt=debug --dns-opt=use-vc --dns-opt=rotate --dns-domain=example.net",
    72  			expectedLine:  "domain example.net",
    73  			expectedError: false,
    74  		},
    75  	} {
    76  
    77  		rktCmd := fmt.Sprintf(`%s --insecure-options=image run --set-env=FILE=/etc/resolv.conf %s %s`,
    78  			ctx.Cmd(), tt.paramDNS, imageFile)
    79  		_ = i
    80  		// t.Logf("%d: %s\n", i, rktCmd)
    81  		runRktAndCheckOutput(t, rktCmd, tt.expectedLine, tt.expectedError)
    82  	}
    83  }
    84  
    85  // TestHostDNS checks that --dns=host reflects the host's /etc/resolv.conf
    86  func TestDNSHost(t *testing.T) {
    87  	dat, err := ioutil.ReadFile("/etc/resolv.conf")
    88  	if err != nil {
    89  		t.Fatal("Could not read host's resolv.conf", err)
    90  	}
    91  
    92  	sum := fmt.Sprintf("%x", sha1.Sum(dat))
    93  	t.Log("Expecting sum", sum)
    94  
    95  	ctx := testutils.NewRktRunCtx()
    96  	defer ctx.Cleanup()
    97  
    98  	appCmd := "--exec=/inspect -- --hash-file"
    99  	rktCmd := fmt.Sprintf("%s --insecure-options=image run --dns=host --set-env=FILE=/etc/resolv.conf %s %s",
   100  		ctx.Cmd(), getInspectImagePath(), appCmd)
   101  
   102  	child := spawnOrFail(t, rktCmd)
   103  	ctx.RegisterChild(child)
   104  	defer waitOrFail(t, child, 0)
   105  
   106  	expectedRegex := `sha1sum: ([0-9a-f]+)`
   107  	result, out, err := expectRegexTimeoutWithOutput(child, expectedRegex, 30*time.Second)
   108  	if err != nil {
   109  		t.Fatalf("Error: %v\nOutput: %v", err, out)
   110  	}
   111  
   112  	if result[1] != sum {
   113  		t.Fatalf("container's /etc/host has sha1sum %s expected %s", result[1], sum)
   114  	}
   115  }