From 483e78615dbc69cfaf2071bd75ef84e93ae57768 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Wed, 1 Nov 2023 13:26:45 -0400 Subject: [PATCH] template: fix test assertion to be compatible between CE/ENT (#18957) The template hook emits an error when the task has a Consul block that requires WI but there's no WI. The exact error message we get depends on whether we're running in CE or ENT. Update the test assertion so that we can tolerate this difference without building ENT-specific test files. --- .../taskrunner/template_hook_test.go | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/client/allocrunner/taskrunner/template_hook_test.go b/client/allocrunner/taskrunner/template_hook_test.go index 18798b9e6..e52c62498 100644 --- a/client/allocrunner/taskrunner/template_hook_test.go +++ b/client/allocrunner/taskrunner/template_hook_test.go @@ -58,45 +58,39 @@ func Test_templateHook_Prestart_ConsulWI(t *testing.T) { tests := []struct { name string req *interfaces.TaskPrestartRequest - wantErr bool wantErrMsg string consulToken string }{ { - "task with no Consul WI", - &interfaces.TaskPrestartRequest{ + name: "task with no Consul WI", + req: &interfaces.TaskPrestartRequest{ Task: &structs.Task{}, TaskDir: &allocdir.TaskDir{Dir: "foo"}, }, - false, - "", - "", }, { - "task with Consul WI but no corresponding identity", - &interfaces.TaskPrestartRequest{ + name: "task with Consul WI but no corresponding identity", + req: &interfaces.TaskPrestartRequest{ Task: &structs.Task{ Name: "foo", Consul: &structs.Consul{Cluster: "bar"}, }, TaskDir: &allocdir.TaskDir{Dir: "foo"}, }, - true, - "consul tokens for cluster default and identity consul_bar requested by task foo not found", - "", + // note: the exact message will vary between CE and ENT because they + // have different helpers for Consul cluster name lookup + wantErrMsg: "not found", }, { - "task with Consul WI", - &interfaces.TaskPrestartRequest{ + name: "task with Consul WI", + req: &interfaces.TaskPrestartRequest{ Task: &structs.Task{ Name: "foo", Consul: &structs.Consul{Cluster: "default"}, }, TaskDir: &allocdir.TaskDir{Dir: "foo"}, }, - false, - "", - hr.GetConsulTokens()[structs.ConsulDefaultCluster]["consul_default"], + consulToken: hr.GetConsulTokens()[structs.ConsulDefaultCluster]["consul_default"], }, } for _, tt := range tests { @@ -109,9 +103,9 @@ func Test_templateHook_Prestart_ConsulWI(t *testing.T) { } err := h.Prestart(context.Background(), tt.req, nil) - if tt.wantErr { + if tt.wantErrMsg != "" { must.NotNil(t, err) - must.Eq(t, tt.wantErrMsg, err.Error()) + must.ErrorContains(t, err, tt.wantErrMsg) } else { must.Nil(t, err) }