-
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.
- Loading branch information
Showing
4 changed files
with
149 additions
and
27 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
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) | ||
} |