github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/acceptance/debug_remote_test.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package acceptance
    12  
    13  import (
    14  	"context"
    15  	gosql "database/sql"
    16  	"fmt"
    17  	"net/http"
    18  	"testing"
    19  
    20  	"github.com/cockroachdb/cockroach/pkg/acceptance/cluster"
    21  	"github.com/cockroachdb/cockroach/pkg/testutils"
    22  	"github.com/cockroachdb/cockroach/pkg/util/log"
    23  )
    24  
    25  func TestDebugRemote(t *testing.T) {
    26  	s := log.Scope(t)
    27  	defer s.Close(t)
    28  	// TODO(tschottdorf): hard to run this as RunLocal since we need to access
    29  	// the ui endpoint from a non-local address.
    30  	RunDocker(t, testDebugRemote)
    31  }
    32  
    33  func testDebugRemote(t *testing.T) {
    34  	cfg := cluster.TestConfig{
    35  		Name:     "TestDebugRemote",
    36  		Duration: *flagDuration,
    37  		Nodes:    []cluster.NodeConfig{{Stores: []cluster.StoreConfig{{}}}},
    38  	}
    39  	ctx := context.Background()
    40  	l := StartCluster(ctx, t, cfg).(*cluster.DockerCluster)
    41  	defer l.AssertAndStop(ctx, t)
    42  
    43  	db, err := gosql.Open("postgres", l.PGUrl(ctx, 0))
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	defer db.Close()
    48  
    49  	testCases := []struct {
    50  		remoteDebug string
    51  		status      int
    52  		expectedErr string
    53  	}{
    54  		{"any", http.StatusOK, ""},
    55  		{"ANY", http.StatusOK, ""},
    56  		{"local", http.StatusForbidden, ""},
    57  		{"off", http.StatusForbidden, ""},
    58  		{"unrecognized", http.StatusForbidden, "invalid mode: 'unrecognized'"},
    59  	}
    60  	for _, c := range testCases {
    61  		t.Run(c.remoteDebug, func(t *testing.T) {
    62  			setStmt := fmt.Sprintf("SET CLUSTER SETTING server.remote_debugging.mode = '%s'",
    63  				c.remoteDebug)
    64  			if _, err := db.Exec(setStmt); !testutils.IsError(err, c.expectedErr) {
    65  				t.Fatalf("expected \"%s\", but found %v", c.expectedErr, err)
    66  			}
    67  			for i, url := range []string{
    68  				"/debug/",
    69  				"/debug/pprof",
    70  				"/debug/requests",
    71  				"/debug/range?id=1",
    72  				"/debug/certificates",
    73  				"/debug/logspy?duration=1ns",
    74  			} {
    75  				t.Run(url, func(t *testing.T) {
    76  					resp, err := cluster.HTTPClient.Get(l.URL(ctx, 0) + url)
    77  					if err != nil {
    78  						t.Fatalf("%d: %v", i, err)
    79  					}
    80  					resp.Body.Close()
    81  
    82  					if c.status != resp.StatusCode {
    83  						t.Fatalf("%d: expected %d, but got %d", i, c.status, resp.StatusCode)
    84  					}
    85  				})
    86  			}
    87  		})
    88  	}
    89  }