-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: deprecate AuthToken for non-OG pyroscope cloud (#125)
* refactor: deprecate AuthToken for non-OG pyroscope cloud * chore: rewrite log message * test: add test for uploadProfile
- Loading branch information
Showing
7 changed files
with
189 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,22 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU= | ||
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= | ||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package testutil | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
type TestLogger struct { | ||
sync.Mutex | ||
lines []string | ||
} | ||
|
||
func NewTestLogger() *TestLogger { | ||
return &TestLogger{lines: make([]string, 0)} | ||
} | ||
|
||
func (t *TestLogger) Debugf(format string, args ...interface{}) { t.put(format, args...) } | ||
func (t *TestLogger) Infof(format string, args ...interface{}) { t.put(format, args...) } | ||
func (t *TestLogger) Errorf(format string, args ...interface{}) { t.put(format, args...) } | ||
|
||
func (t *TestLogger) put(format string, args ...interface{}) { | ||
t.Lock() | ||
t.lines = append(t.lines, fmt.Sprintf(format, args...)) | ||
t.Unlock() | ||
} | ||
|
||
// Lines returns a copy of the logged lines | ||
func (t *TestLogger) Lines() []string { | ||
t.Lock() | ||
defer t.Unlock() | ||
return append([]string(nil), t.lines...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package remote | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/grafana/pyroscope-go/internal/testutil" | ||
"github.com/grafana/pyroscope-go/upstream" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestUploadProfile(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cfg Config | ||
serverAddress string | ||
expectedAuthHeader string | ||
expectWarning bool | ||
}{ | ||
{ | ||
name: "OG Pyroscope Cloud with AuthToken", | ||
cfg: Config{ | ||
AuthToken: "test-token", | ||
Address: "https://example.pyroscope.cloud", | ||
}, | ||
expectedAuthHeader: "Bearer test-token", | ||
expectWarning: false, | ||
}, | ||
{ | ||
name: "Non-OG Server with BasicAuth", | ||
cfg: Config{ | ||
BasicAuthUser: "user", | ||
BasicAuthPassword: "pass", | ||
Address: "https://example.com", | ||
}, | ||
expectedAuthHeader: "Basic dXNlcjpwYXNz", // Base64 encoded "user:pass" | ||
expectWarning: false, | ||
}, | ||
{ | ||
name: "Non-OG Server with AuthToken (Deprecated)", | ||
cfg: Config{ | ||
AuthToken: "deprecated-token", | ||
Address: "https://example.com", | ||
}, | ||
expectedAuthHeader: "", | ||
expectWarning: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
logger := testutil.NewTestLogger() | ||
mockClient := new(MockHTTPClient) | ||
|
||
mockClient.On("Do", mock.Anything).Return(&http.Response{ | ||
StatusCode: 200, | ||
Body: io.NopCloser(bytes.NewBufferString("OK")), | ||
}, nil) | ||
|
||
r := &Remote{ | ||
cfg: tt.cfg, | ||
client: mockClient, | ||
logger: logger, | ||
} | ||
|
||
err := r.uploadProfile(&upstream.UploadJob{ | ||
Name: "test-profile", | ||
StartTime: time.Now(), | ||
EndTime: time.Now().Add(time.Minute), | ||
SpyName: "test-spy", | ||
SampleRate: 100, | ||
Units: "samples", | ||
}) | ||
assert.NoError(t, err) | ||
|
||
if tt.expectWarning { | ||
assert.Contains(t, logger.Lines(), authTokenDeprecationWarning) | ||
} else { | ||
assert.NotContains(t, logger.Lines(), authTokenDeprecationWarning) | ||
} | ||
|
||
mockClient.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool { | ||
return req.Header.Get("Authorization") == tt.expectedAuthHeader | ||
})) | ||
|
||
mockClient.AssertExpectations(t) | ||
}) | ||
} | ||
} | ||
|
||
type MockHTTPClient struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) { | ||
args := m.Called(req) | ||
return args.Get(0).(*http.Response), args.Error(1) | ||
} |