fix(go1.15): require non-nil parent in context.WithValue

pull/5610/head
Pavel Zavora 2020-11-13 14:40:20 +01:00
parent a7983af69c
commit 7e0cba1890
7 changed files with 26 additions and 1 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {

View File

@ -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)
}

View File

@ -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)
}
})