diff --git a/dependency/nomad_service.go b/dependency/nomad_service.go index 07e0c6d6b..e3e50f2cf 100644 --- a/dependency/nomad_service.go +++ b/dependency/nomad_service.go @@ -68,13 +68,16 @@ func NewNomadServiceQuery(s string) (*NomadServiceQuery, error) { }, nil } -func NewNomadServiceChooseQuery(count int, hash string, s string) (*NomadServiceQuery, error) { +// NewNomadServiceChooseQuery parses s using NewNomadServiceQuery, and then also +// configures the resulting query with the choose parameter set according to the +// count and key arguments. +func NewNomadServiceChooseQuery(count int, key, s string) (*NomadServiceQuery, error) { query, err := NewNomadServiceQuery(s) if err != nil { return nil, err } - choose := fmt.Sprintf("%d|%s", count, hash) + choose := fmt.Sprintf("%d|%s", count, key) query.choose = choose return query, nil diff --git a/dependency/nomad_service_test.go b/dependency/nomad_service_test.go index 7cb185b2f..b24a7692c 100644 --- a/dependency/nomad_service_test.go +++ b/dependency/nomad_service_test.go @@ -78,6 +78,26 @@ func TestNewNomadServiceQuery(t *testing.T) { }, false, }, + { + "choose", + "name:1|abc123", + &NomadServiceQuery{ + name: "name", + choose: "1|abc123", + }, + false, + }, + { + "tag_Name_region_choose", + "tag.name@us-east-1:1|abc123", + &NomadServiceQuery{ + region: "us-east-1", + name: "name", + tag: "tag", + choose: "1:abc123", + }, + false, + }, } for i, tc := range cases { @@ -234,3 +254,31 @@ func TestNomadServiceQuery_String(t *testing.T) { }) } } + +func TestNomadServiceQuery_String_3arg(t *testing.T) { + cases := []struct { + name string + i string + count int + key string + exp string + }{ + { + "choose", + "redis", + 3, + "abc123", + "nomad.service(redis:3|abc123)", + }, + } + + for i, tc := range cases { + t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) { + d, err := NewNomadServiceChooseQuery(3, "abc123", tc.i) + if err != nil { + t.Fatal(err) + } + require.Equal(t, tc.exp, d.String()) + }) + } +}