From 7e0cba189039db276e4ff35257d22bedf52719fb Mon Sep 17 00:00:00 2001 From: Pavel Zavora Date: Fri, 13 Nov 2020 14:40:20 +0100 Subject: [PATCH] fix(go1.15): require non-nil parent in context.WithValue --- kv/organizations.go | 3 +++ organizations/dashboards_test.go | 3 +++ organizations/organizations_test.go | 3 +++ organizations/servers_test.go | 3 +++ organizations/sources_test.go | 3 +++ server/context.go | 3 +++ server/sources_test.go | 9 ++++++++- 7 files changed, 26 insertions(+), 1 deletion(-) diff --git a/kv/organizations.go b/kv/organizations.go index 476f5e5e9..85c331340 100644 --- a/kv/organizations.go +++ b/kv/organizations.go @@ -154,6 +154,9 @@ func (s *organizationsStore) Delete(ctx context.Context, o *chronograf.Organizat // Each of the associated organization stores expects organization to be // set on the context. + if ctx == nil { + ctx = context.Background() // context could be possible nil before go 1.15, see https://github.com/golang/go/issues/40737 + } ctx = context.WithValue(ctx, organizations.ContextKey, o.ID) sourcesStore := organizations.NewSourcesStore(s.client.SourcesStore(), o.ID) diff --git a/organizations/dashboards_test.go b/organizations/dashboards_test.go index ba2dcaedb..177acdfe0 100644 --- a/organizations/dashboards_test.go +++ b/organizations/dashboards_test.go @@ -78,6 +78,9 @@ func TestDashboards_All(t *testing.T) { } for _, tt := range tests { s := organizations.NewDashboardsStore(tt.fields.DashboardsStore, tt.args.organization) + if tt.args.ctx == nil { + tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737 + } tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization) gots, err := s.All(tt.args.ctx) if (err != nil) != tt.wantErr { diff --git a/organizations/organizations_test.go b/organizations/organizations_test.go index 99635c54a..b91d9328c 100644 --- a/organizations/organizations_test.go +++ b/organizations/organizations_test.go @@ -94,6 +94,9 @@ func TestOrganizations_All(t *testing.T) { } for _, tt := range tests { s := organizations.NewOrganizationsStore(tt.fields.OrganizationsStore, tt.args.organization) + if tt.args.ctx == nil { + tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737 + } tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization) gots, err := s.All(tt.args.ctx) if (err != nil) != tt.wantErr { diff --git a/organizations/servers_test.go b/organizations/servers_test.go index 981662ac5..24ec1cc3e 100644 --- a/organizations/servers_test.go +++ b/organizations/servers_test.go @@ -79,6 +79,9 @@ func TestServers_All(t *testing.T) { } for _, tt := range tests { s := organizations.NewServersStore(tt.fields.ServersStore, tt.args.organization) + if tt.args.ctx == nil { + tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737 + } tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization) gots, err := s.All(tt.args.ctx) if (err != nil) != tt.wantErr { diff --git a/organizations/sources_test.go b/organizations/sources_test.go index 8a57e24fb..c9ad2532d 100644 --- a/organizations/sources_test.go +++ b/organizations/sources_test.go @@ -79,6 +79,9 @@ func TestSources_All(t *testing.T) { } for _, tt := range tests { s := organizations.NewSourcesStore(tt.fields.SourcesStore, tt.args.organization) + if tt.args.ctx == nil { + tt.args.ctx = context.Background() // required since go 1.15, see https://github.com/golang/go/issues/40737 + } tt.args.ctx = context.WithValue(tt.args.ctx, organizations.ContextKey, tt.args.organization) gots, err := s.All(tt.args.ctx) if (err != nil) != tt.wantErr { diff --git a/server/context.go b/server/context.go index b3f4e0567..86b35bd81 100644 --- a/server/context.go +++ b/server/context.go @@ -26,5 +26,8 @@ func hasServerContext(ctx context.Context) bool { } func serverContext(ctx context.Context) context.Context { + if ctx == nil { + ctx = context.Background() // context could be possible nil before go 1.15, see https://github.com/golang/go/issues/40737 + } return context.WithValue(ctx, ServerContextKey, true) } diff --git a/server/sources_test.go b/server/sources_test.go index 961f13b7e..4997c39d3 100644 --- a/server/sources_test.go +++ b/server/sources_test.go @@ -8,6 +8,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "strings" "testing" "github.com/bouk/httprouter" @@ -135,7 +136,7 @@ func Test_ValidSourceRequest(t *testing.T) { }, }, wants: wants{ - err: fmt.Errorf("invalid source URI: parse im a bad url: invalid URI for request"), + err: fmt.Errorf("invalid source URI: parse"), // im a bad url: invalid URI for request }, }, } @@ -150,6 +151,12 @@ func Test_ValidSourceRequest(t *testing.T) { return } if err.Error() != tt.wants.err.Error() { + if err != nil && tt.wants.err != nil { + if strings.HasPrefix(err.Error(), tt.wants.err.Error()) { + // error messages vary between go versions, but they have the same prefixes + return + } + } t.Errorf("%q. ValidSourceRequest() = %q, want %q", tt.name, err, tt.wants.err) } })