github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/taskenv/network_test.go (about) 1 package taskenv 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/nomad/ci" 7 "github.com/hashicorp/nomad/nomad/structs" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func Test_InterpolateNetworks(t *testing.T) { 12 ci.Parallel(t) 13 14 testCases := []struct { 15 inputTaskEnv *TaskEnv 16 inputNetworks structs.Networks 17 expectedOutputNetworks structs.Networks 18 name string 19 }{ 20 { 21 inputTaskEnv: testEnv, 22 inputNetworks: structs.Networks{ 23 {Hostname: "my-little-pony"}, 24 }, 25 expectedOutputNetworks: structs.Networks{ 26 {Hostname: "my-little-pony"}, 27 }, 28 name: "non-interpolated hostname", 29 }, 30 { 31 inputTaskEnv: testEnv, 32 inputNetworks: structs.Networks{ 33 {Hostname: "${foo}-cache-${baz}"}, 34 }, 35 expectedOutputNetworks: structs.Networks{ 36 {Hostname: "bar-cache-blah"}, 37 }, 38 name: "interpolated hostname", 39 }, 40 { 41 inputTaskEnv: testEnv, 42 inputNetworks: structs.Networks{ 43 { 44 DNS: &structs.DNSConfig{ 45 Servers: []string{"127.0.0.1"}, 46 Options: []string{"some-opt"}, 47 Searches: []string{"example.com"}, 48 }, 49 }, 50 }, 51 expectedOutputNetworks: structs.Networks{ 52 { 53 DNS: &structs.DNSConfig{ 54 Servers: []string{"127.0.0.1"}, 55 Options: []string{"some-opt"}, 56 Searches: []string{"example.com"}, 57 }, 58 }, 59 }, 60 name: "non-interpolated dns servers", 61 }, 62 { 63 inputTaskEnv: testEnv, 64 inputNetworks: structs.Networks{ 65 { 66 DNS: &structs.DNSConfig{ 67 Servers: []string{"${foo}"}, 68 Options: []string{"${foo}-opt"}, 69 Searches: []string{"${foo}.example.com"}, 70 }, 71 }, 72 }, 73 expectedOutputNetworks: structs.Networks{ 74 { 75 DNS: &structs.DNSConfig{ 76 Servers: []string{"bar"}, 77 Options: []string{"bar-opt"}, 78 Searches: []string{"bar.example.com"}, 79 }, 80 }, 81 }, 82 name: "interpolated dns servers", 83 }, 84 } 85 86 for _, tc := range testCases { 87 t.Run(tc.name, func(t *testing.T) { 88 actualOutput := InterpolateNetworks(tc.inputTaskEnv, tc.inputNetworks) 89 assert.Equal(t, tc.expectedOutputNetworks, actualOutput, tc.name) 90 }) 91 } 92 }