github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/agent/pprof/pprof_test.go (about) 1 package pprof 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/hashicorp/nomad/ci" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestProfile(t *testing.T) { 12 ci.Parallel(t) 13 14 cases := []struct { 15 desc string 16 profile string 17 debug int 18 gc int 19 expectedHeaders map[string]string 20 expectedErr error 21 }{ 22 { 23 desc: "profile that exists", 24 profile: "goroutine", 25 expectedHeaders: map[string]string{ 26 "X-Content-Type-Options": "nosniff", 27 "Content-Type": "application/octet-stream", 28 "Content-Disposition": `attachment; filename="goroutine"`, 29 }, 30 }, 31 { 32 desc: "profile that does not exist", 33 profile: "nonexistent", 34 expectedErr: NewErrProfileNotFound("nonexistent"), 35 expectedHeaders: nil, 36 }, 37 { 38 desc: "profile with debug enabled", 39 profile: "allocs", 40 debug: 1, 41 expectedHeaders: map[string]string{ 42 "X-Content-Type-Options": "nosniff", 43 "Content-Type": "text/plain; charset=utf-8", 44 }, 45 }, 46 } 47 48 for _, tc := range cases { 49 t.Run(tc.desc, func(t *testing.T) { 50 resp, headers, err := Profile(tc.profile, tc.debug, tc.gc) 51 require.Equal(t, tc.expectedHeaders, headers) 52 53 if tc.expectedErr != nil { 54 require.Nil(t, resp) 55 require.Equal(t, err, tc.expectedErr) 56 } else { 57 require.NotNil(t, resp) 58 } 59 }) 60 } 61 } 62 63 func TestCPUProfile(t *testing.T) { 64 ci.Parallel(t) 65 66 cases := []struct { 67 desc string 68 expectedHeaders map[string]string 69 }{ 70 { 71 desc: "successful cpu profile", 72 expectedHeaders: map[string]string{ 73 "X-Content-Type-Options": "nosniff", 74 "Content-Type": "application/octet-stream", 75 "Content-Disposition": `attachment; filename="profile"`, 76 }, 77 }, 78 } 79 80 for _, tc := range cases { 81 t.Run(tc.desc, func(t *testing.T) { 82 resp, headers, err := CPUProfile(context.Background(), 0) 83 require.NoError(t, err) 84 require.Equal(t, tc.expectedHeaders, headers) 85 86 require.NotNil(t, resp) 87 }) 88 } 89 } 90 91 func TestTrace(t *testing.T) { 92 ci.Parallel(t) 93 94 cases := []struct { 95 desc string 96 expectedHeaders map[string]string 97 }{ 98 { 99 desc: "successful trace profile", 100 expectedHeaders: map[string]string{ 101 "X-Content-Type-Options": "nosniff", 102 "Content-Type": "application/octet-stream", 103 "Content-Disposition": `attachment; filename="trace"`, 104 }, 105 }, 106 } 107 108 for _, tc := range cases { 109 t.Run(tc.desc, func(t *testing.T) { 110 resp, headers, err := Trace(context.Background(), 0) 111 require.NoError(t, err) 112 require.Equal(t, tc.expectedHeaders, headers) 113 114 require.NotNil(t, resp) 115 }) 116 } 117 } 118 119 func TestCmdline(t *testing.T) { 120 ci.Parallel(t) 121 122 cases := []struct { 123 desc string 124 expectedHeaders map[string]string 125 }{ 126 { 127 desc: "successful cmdline request", 128 expectedHeaders: map[string]string{ 129 "X-Content-Type-Options": "nosniff", 130 "Content-Type": "text/plain; charset=utf-8", 131 }, 132 }, 133 } 134 135 for _, tc := range cases { 136 t.Run(tc.desc, func(t *testing.T) { 137 resp, headers, err := Cmdline() 138 require.NoError(t, err) 139 require.Equal(t, tc.expectedHeaders, headers) 140 141 require.NotNil(t, resp) 142 }) 143 } 144 }