github.com/palantir/witchcraft-go-server/v2@v2.76.0/integration/diagnostics_test.go (about)

     1  // Copyright (c) 2023 Palantir Technologies. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package integration
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  	"net/http"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/palantir/pkg/httpserver"
    26  	"github.com/palantir/witchcraft-go-server/v2/config"
    27  	"github.com/palantir/witchcraft-go-server/v2/witchcraft"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  const (
    32  	testDiagnosticType = "go.goroutines.v1"
    33  	testSecret         = "secretForTest"
    34  	tmpDir             = "/tmp"
    35  	tmpFilePath        = "/tmp/tmpFile"
    36  )
    37  
    38  func TestServer_DiagnosticsSharedSecret(t *testing.T) {
    39  	tests := []struct {
    40  		name          string
    41  		secret        string
    42  		runtimeConfig config.Runtime
    43  		prepare       func()
    44  		cleanup       func()
    45  	}{
    46  		{
    47  			name:          "no secret specified in runtime config",
    48  			secret:        "any secret should work",
    49  			runtimeConfig: config.Runtime{},
    50  		},
    51  		{
    52  			name:   "debug-shared-secret is specified in runtime config",
    53  			secret: testSecret,
    54  			runtimeConfig: config.Runtime{
    55  				DiagnosticsConfig: config.DiagnosticsConfig{DebugSharedSecret: testSecret},
    56  			},
    57  		},
    58  		{
    59  			name:   "debug-shared-secret-file is specified in runtime config",
    60  			secret: testSecret,
    61  			runtimeConfig: config.Runtime{
    62  				DiagnosticsConfig: config.DiagnosticsConfig{DebugSharedSecretFile: tmpFilePath},
    63  			},
    64  			prepare: func() {
    65  				err := os.MkdirAll(tmpDir, 0755)
    66  				require.NoError(t, err)
    67  				err = os.WriteFile(tmpFilePath, []byte(testSecret), 0644)
    68  				require.NoError(t, err)
    69  			},
    70  			cleanup: func() {
    71  				err := os.Remove(tmpFilePath)
    72  				require.NoError(t, err)
    73  			},
    74  		},
    75  		{
    76  			name:   "both debug-shared-secret and debug-shared-secret-file is specified in runtime config",
    77  			secret: testSecret,
    78  			runtimeConfig: config.Runtime{
    79  				DiagnosticsConfig: config.DiagnosticsConfig{
    80  					DebugSharedSecret: testSecret, DebugSharedSecretFile: filepath.Join(tmpDir, "fileDoesntExist"),
    81  				},
    82  			},
    83  		},
    84  	}
    85  	for _, tt := range tests {
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			if tt.prepare != nil {
    88  				tt.prepare()
    89  				defer tt.cleanup()
    90  			}
    91  
    92  			port, err := httpserver.AvailablePort()
    93  			require.NoError(t, err)
    94  			server, serverErr, cleanup := createAndRunCustomTestServer(t, port, port, nil, io.Discard,
    95  				func(t *testing.T, initFn witchcraft.InitFunc, installCfg config.Install, logOutputBuffer io.Writer) *witchcraft.Server {
    96  					return createTestServer(t, initFn, installCfg, logOutputBuffer).
    97  						WithRuntimeConfig(tt.runtimeConfig)
    98  				})
    99  
   100  			defer func() {
   101  				require.NoError(t, server.Close())
   102  			}()
   103  			defer cleanup()
   104  			client := testServerClient()
   105  
   106  			request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://localhost:%d/%s/debug/diagnostic/%s", port, basePath, testDiagnosticType), nil)
   107  			require.NoError(t, err)
   108  			request.Header.Set("Authorization", "Bearer "+tt.secret)
   109  			resp, err := client.Do(request)
   110  			require.NoError(t, err)
   111  			require.Equal(t, http.StatusOK, resp.StatusCode)
   112  
   113  			select {
   114  			case err := <-serverErr:
   115  				require.NoError(t, err)
   116  			default:
   117  			}
   118  		})
   119  	}
   120  }