open-nomad/command/agent/pprof/pprof_test.go
Drew Bailey 45210ed901
Rename profile package to pprof
Address pr feedback, rename profile package to pprof to more accurately
describe its purpose. Adds gc param for heap lookup profiles.
2020-01-09 15:15:10 -05:00

136 lines
3 KiB
Go

package pprof
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestProfile(t *testing.T) {
cases := []struct {
desc string
profile string
debug int
gc int
expectedHeaders map[string]string
expectedErr error
}{
{
desc: "profile that exists",
profile: "goroutine",
expectedHeaders: map[string]string{
"X-Content-Type-Options": "nosniff",
"Content-Type": "application/octet-stream",
"Content-Disposition": `attachment; filename="goroutine"`,
},
},
{
desc: "profile that does not exist",
profile: "nonexistent",
expectedErr: NewErrProfileNotFound("nonexistent"),
expectedHeaders: nil,
},
{
desc: "profile with debug enabled",
profile: "allocs",
debug: 1,
expectedHeaders: map[string]string{
"X-Content-Type-Options": "nosniff",
"Content-Type": "text/plain; charset=utf-8",
},
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
resp, headers, err := Profile(tc.profile, tc.debug, tc.gc)
require.Equal(t, tc.expectedHeaders, headers)
if tc.expectedErr != nil {
require.Nil(t, resp)
require.Equal(t, err, tc.expectedErr)
} else {
require.NotNil(t, resp)
}
})
}
}
func TestCPUProfile(t *testing.T) {
cases := []struct {
desc string
expectedHeaders map[string]string
}{
{
desc: "successful cpu profile",
expectedHeaders: map[string]string{
"X-Content-Type-Options": "nosniff",
"Content-Type": "application/octet-stream",
"Content-Disposition": `attachment; filename="profile"`,
},
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
resp, headers, err := CPUProfile(context.Background(), 0)
require.NoError(t, err)
require.Equal(t, tc.expectedHeaders, headers)
require.NotNil(t, resp)
})
}
}
func TestTrace(t *testing.T) {
cases := []struct {
desc string
expectedHeaders map[string]string
}{
{
desc: "successful trace profile",
expectedHeaders: map[string]string{
"X-Content-Type-Options": "nosniff",
"Content-Type": "application/octet-stream",
"Content-Disposition": `attachment; filename="trace"`,
},
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
resp, headers, err := Trace(context.Background(), 0)
require.NoError(t, err)
require.Equal(t, tc.expectedHeaders, headers)
require.NotNil(t, resp)
})
}
}
func TestCmdline(t *testing.T) {
cases := []struct {
desc string
expectedHeaders map[string]string
}{
{
desc: "successful cmdline request",
expectedHeaders: map[string]string{
"X-Content-Type-Options": "nosniff",
"Content-Type": "text/plain; charset=utf-8",
},
},
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
resp, headers, err := Cmdline()
require.NoError(t, err)
require.Equal(t, tc.expectedHeaders, headers)
require.NotNil(t, resp)
})
}
}