github.com/hernad/nomad@v1.6.112/drivers/shared/hostnames/mount_unix_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  //go:build !windows
     5  // +build !windows
     6  
     7  package hostnames
     8  
     9  import (
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/hernad/nomad/plugins/drivers"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestGenerateEtcHostsMount(t *testing.T) {
    19  
    20  	testCases := []struct {
    21  		name        string
    22  		spec        *drivers.NetworkIsolationSpec
    23  		extraHosts  []string
    24  		expected    []string
    25  		expectedErr string
    26  	}{
    27  		{
    28  			name: "no-spec",
    29  		},
    30  		{
    31  			name: "no-hosts-config",
    32  			spec: &drivers.NetworkIsolationSpec{Mode: drivers.NetIsolationModeGroup},
    33  		},
    34  		{
    35  			name: "base-case",
    36  			spec: &drivers.NetworkIsolationSpec{
    37  				Mode: drivers.NetIsolationModeGroup,
    38  				HostsConfig: &drivers.HostsConfig{
    39  					Address:  "192.168.1.1",
    40  					Hostname: "xyzzy",
    41  				},
    42  			},
    43  			expected: []string{
    44  				"192.168.1.1 xyzzy",
    45  			},
    46  		},
    47  		{
    48  			name: "with-valid-extra-hosts",
    49  			spec: &drivers.NetworkIsolationSpec{
    50  				Mode: drivers.NetIsolationModeGroup,
    51  				HostsConfig: &drivers.HostsConfig{
    52  					Address:  "192.168.1.1",
    53  					Hostname: "xyzzy",
    54  				},
    55  			},
    56  			extraHosts: []string{
    57  				"apple:192.168.1.2",
    58  				"banana:2001:0db8:85a3:0000:0000:8a2e:0370:7334",
    59  			},
    60  			expected: []string{
    61  				"192.168.1.1 xyzzy",
    62  				"192.168.1.2 apple",
    63  				"2001:0db8:85a3:0000:0000:8a2e:0370:7334 banana",
    64  			},
    65  		},
    66  		{
    67  			name: "invalid-extra-hosts-syntax",
    68  			spec: &drivers.NetworkIsolationSpec{
    69  				Mode: drivers.NetIsolationModeGroup,
    70  				HostsConfig: &drivers.HostsConfig{
    71  					Address:  "192.168.1.1",
    72  					Hostname: "xyzzy",
    73  				},
    74  			},
    75  			extraHosts:  []string{"apple192.168.1.2"},
    76  			expectedErr: "invalid hosts entry \"apple192.168.1.2\"",
    77  		},
    78  		{
    79  			name: "invalid-extra-hosts-bad-ip",
    80  			spec: &drivers.NetworkIsolationSpec{
    81  				Mode: drivers.NetIsolationModeGroup,
    82  				HostsConfig: &drivers.HostsConfig{
    83  					Address:  "192.168.1.1",
    84  					Hostname: "xyzzy",
    85  				},
    86  			},
    87  			extraHosts:  []string{"apple:192.168.1.256"},
    88  			expectedErr: "invalid IP address \"apple:192.168.1.256\"",
    89  		},
    90  	}
    91  	for _, tc := range testCases {
    92  		tc := tc
    93  		t.Run(tc.name, func(t *testing.T) {
    94  			require := require.New(t)
    95  
    96  			taskDir := t.TempDir()
    97  			dest := filepath.Join(taskDir, "hosts")
    98  
    99  			got, err := GenerateEtcHostsMount(taskDir, tc.spec, tc.extraHosts)
   100  
   101  			if tc.expectedErr != "" {
   102  				require.EqualError(err, tc.expectedErr)
   103  			} else {
   104  				require.NoError(err)
   105  			}
   106  			if len(tc.expected) == 0 {
   107  				require.Nil(got)
   108  			} else {
   109  				require.NotNil(got)
   110  				require.FileExists(dest)
   111  				tmpHosts, err := os.ReadFile(dest)
   112  				require.NoError(err)
   113  				for _, line := range tc.expected {
   114  					require.Contains(string(tmpHosts), line)
   115  				}
   116  			}
   117  		})
   118  	}
   119  }