github.com/manicqin/nomad@v0.9.5/command/agent/pprof/pprof_test.go (about)

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