github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/client/taskenv/services_test.go (about)

     1  package taskenv
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/nomad/structs"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  // TestInterpolateServices asserts that all service
    11  // and check fields are properly interpolated.
    12  func TestInterpolateServices(t *testing.T) {
    13  	t.Parallel()
    14  	services := []*structs.Service{
    15  		{
    16  			Name:      "${name}",
    17  			PortLabel: "${portlabel}",
    18  			Tags:      []string{"${tags}"},
    19  			Checks: []*structs.ServiceCheck{
    20  				{
    21  					Name:          "${checkname}",
    22  					Type:          "${checktype}",
    23  					Command:       "${checkcmd}",
    24  					Args:          []string{"${checkarg}"},
    25  					Path:          "${checkstr}",
    26  					Protocol:      "${checkproto}",
    27  					PortLabel:     "${checklabel}",
    28  					InitialStatus: "${checkstatus}",
    29  					Method:        "${checkmethod}",
    30  					Header: map[string][]string{
    31  						"${checkheaderk}": {"${checkheaderv}"},
    32  					},
    33  				},
    34  			},
    35  		},
    36  	}
    37  
    38  	env := &TaskEnv{
    39  		EnvMap: map[string]string{
    40  			"name":         "name",
    41  			"portlabel":    "portlabel",
    42  			"tags":         "tags",
    43  			"checkname":    "checkname",
    44  			"checktype":    "checktype",
    45  			"checkcmd":     "checkcmd",
    46  			"checkarg":     "checkarg",
    47  			"checkstr":     "checkstr",
    48  			"checkpath":    "checkpath",
    49  			"checkproto":   "checkproto",
    50  			"checklabel":   "checklabel",
    51  			"checkstatus":  "checkstatus",
    52  			"checkmethod":  "checkmethod",
    53  			"checkheaderk": "checkheaderk",
    54  			"checkheaderv": "checkheaderv",
    55  		},
    56  	}
    57  
    58  	interpolated := InterpolateServices(env, services)
    59  
    60  	exp := []*structs.Service{
    61  		{
    62  			Name:      "name",
    63  			PortLabel: "portlabel",
    64  			Tags:      []string{"tags"},
    65  			Checks: []*structs.ServiceCheck{
    66  				{
    67  					Name:          "checkname",
    68  					Type:          "checktype",
    69  					Command:       "checkcmd",
    70  					Args:          []string{"checkarg"},
    71  					Path:          "checkstr",
    72  					Protocol:      "checkproto",
    73  					PortLabel:     "checklabel",
    74  					InitialStatus: "checkstatus",
    75  					Method:        "checkmethod",
    76  					Header: map[string][]string{
    77  						"checkheaderk": {"checkheaderv"},
    78  					},
    79  				},
    80  			},
    81  		},
    82  	}
    83  
    84  	require.Equal(t, exp, interpolated)
    85  }