github.com/smithx10/nomad@v0.9.1-rc1/client/allocrunner/taskrunner/service_hook_test.go (about)

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