2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-12-19 16:41:55 +00:00
|
|
|
package pprof
|
2019-12-11 19:58:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2019-12-11 19:58:41 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProfile(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2019-12-11 19:58:41 +00:00
|
|
|
cases := []struct {
|
|
|
|
desc string
|
|
|
|
profile string
|
|
|
|
debug int
|
2019-12-19 16:41:55 +00:00
|
|
|
gc int
|
2019-12-11 19:58:41 +00:00
|
|
|
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) {
|
2019-12-19 16:41:55 +00:00
|
|
|
resp, headers, err := Profile(tc.profile, tc.debug, tc.gc)
|
2019-12-11 19:58:41 +00:00
|
|
|
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) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2019-12-11 19:58:41 +00:00
|
|
|
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) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2019-12-11 19:58:41 +00:00
|
|
|
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) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2019-12-11 19:58:41 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|