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

     1  // Copyright 2015 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  // +build !fly
    16  
    17  package main
    18  
    19  import (
    20  	"crypto/sha1"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"strings"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/coreos/gexpect"
    30  	"github.com/rkt/rkt/tests/testutils"
    31  )
    32  
    33  // stage1/prepare-app will populate /etc/hosts with a 127.0.0.1 entry when absent,
    34  // or leave it alone when present.  These tests verify that behavior.
    35  
    36  /*
    37  	The stage0 can create an etc hosts
    38  
    39  	prepare-app will overwrite the app's /etc/hosts if the stage0 created one
    40  	Otherwise, it will create a fallback /etc/hosts if the stage2 does not have one
    41  */
    42  func TestEtcHosts(t *testing.T) {
    43  	ctx := testutils.NewRktRunCtx()
    44  	defer ctx.Cleanup()
    45  
    46  	tmpdir := mustTempDir("rkt-tests.")
    47  	defer os.RemoveAll(tmpdir)
    48  	tmpetc := mustTempDir("rkt-tests-etc.")
    49  	defer os.RemoveAll(tmpetc)
    50  
    51  	tmpfile := filepath.Join(tmpetc, "hosts")
    52  	if err := ioutil.WriteFile(tmpfile, []byte(`<<<preexisting etc>>>`), 0600); err != nil {
    53  		t.Fatalf("Cannot create etc/hosts file: %v", err)
    54  	}
    55  
    56  	dat, err := ioutil.ReadFile("/etc/hosts")
    57  	if err != nil {
    58  		t.Fatal("Could not read host's /etc/hosts", err)
    59  	}
    60  
    61  	sum := fmt.Sprintf("%x", sha1.Sum(dat))
    62  
    63  	tests := []struct {
    64  		rktArgs     string
    65  		inspectArgs string
    66  		expectRegEx string
    67  	}{
    68  		{
    69  			fmt.Sprintf("--volume hosts,kind=host,source=%s", tmpfile),
    70  			"--mount volume=hosts,target=/etc/hosts --exec=/inspect -- -file-name=/etc/hosts -read-file",
    71  			"<<<preexisting etc>>>",
    72  		},
    73  		{ // Test that with no /etc/hosts, the fallback is created with container hostname
    74  			"",
    75  			"--exec=/inspect -- -file-name=/etc/hosts -read-file",
    76  			"127.0.0.1 localhost localhost.domain localhost4 localhost4.localdomain4 rkt-[a-z0-9-]{36}",
    77  		},
    78  		{ // Test that with --hosts-entry=host, the host's is copied
    79  			"--hosts-entry=host",
    80  			"--exec=/inspect -- -file-name=/etc/hosts -hash-file",
    81  			sum,
    82  		},
    83  		{ // Test that multiple '--hosts-entry=host' entries are fine
    84  			"--hosts-entry=host --hosts-entry=host",
    85  			"--exec=/inspect -- -file-name=/etc/hosts -hash-file",
    86  			sum,
    87  		},
    88  		{ // test that we create our own
    89  			"--hosts-entry=128.66.0.99=host1",
    90  			"--exec=/inspect -- -file-name=/etc/hosts -read-file",
    91  			"128.66.0.99 host1",
    92  		},
    93  	}
    94  
    95  	for i, tt := range tests {
    96  		cmd := fmt.Sprintf(
    97  			"%s run --insecure-options=image %s %s %s",
    98  			ctx.Cmd(),
    99  			tt.rktArgs,
   100  			getInspectImagePath(),
   101  			tt.inspectArgs)
   102  
   103  		t.Logf("Running test #%v: %v", i, cmd)
   104  
   105  		child, err := gexpect.Spawn(cmd)
   106  		if err != nil {
   107  			t.Fatalf("Cannot exec rkt #%v: %v", i, err)
   108  		}
   109  
   110  		_, out, err := expectRegexTimeoutWithOutput(child, tt.expectRegEx, 30*time.Second)
   111  		if err != nil {
   112  			t.Fatalf("Test %d %v output: %v", i, err, out)
   113  		}
   114  
   115  		waitOrFail(t, child, 0)
   116  	}
   117  }
   118  
   119  // stage1/prepare-app will bind mount /proc/sys/kernel/hostname on /etc/hostname,
   120  // see https://github.com/rkt/rkt/issues/2657
   121  var etcHostnameTests = []struct {
   122  	aciBuildArgs   []string
   123  	runArgs        []string
   124  	expectedOutput string
   125  	expectErr      bool
   126  }{
   127  	{
   128  		[]string{"--exec=/inspect -file-name /etc/hostname -stat-file"},
   129  		nil,
   130  		`/etc/hostname: mode: -rw-r--r--`,
   131  		false,
   132  	},
   133  	{
   134  		[]string{"--exec=/inspect -file-name /etc/hostname -read-file"},
   135  		[]string{"--hostname custom_hostname_setting"},
   136  		`<<<custom_hostname_setting`,
   137  		false,
   138  	},
   139  	{
   140  		[]string{"--exec=/inspect -file-name /etc/hostname -read-file"},
   141  		nil,
   142  		`<<<rkt-`,
   143  		false,
   144  	},
   145  }
   146  
   147  func TestPrepareAppCheckEtcHostname(t *testing.T) {
   148  	ctx := testutils.NewRktRunCtx()
   149  	defer ctx.Cleanup()
   150  
   151  	for i, ti := range etcHostnameTests {
   152  
   153  		aciFileName := patchTestACI(fmt.Sprintf("rkt-inspect-hostname-%d.aci", i), ti.aciBuildArgs...)
   154  		defer os.Remove(aciFileName)
   155  
   156  		rktCmd := fmt.Sprintf("%s --insecure-options=image run %s %s", ctx.Cmd(), aciFileName, strings.Join(ti.runArgs, " "))
   157  		runRktAndCheckOutput(t, rktCmd, ti.expectedOutput, ti.expectErr)
   158  	}
   159  }