Skip to content

Commit 910c3d3

Browse files
torcolvinbbrks
andauthored
CBG-4712 /_sgcollect_info endpoint to work with non default port (#7601)
Co-authored-by: Ben Brooks <ben.brooks@couchbase.com>
1 parent 6b0d541 commit 910c3d3

12 files changed

+862
-423
lines changed

base/main_test_bucket_pool_config.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"strconv"
1616
"testing"
1717
"time"
18+
19+
"github.com/stretchr/testify/require"
1820
)
1921

2022
// Bucket names start with a fixed prefix and end with a sequential bucket number and a creation timestamp for uniqueness
@@ -213,3 +215,27 @@ func TestClusterPassword() string {
213215
}
214216
return password
215217
}
218+
219+
// TestRunSGCollectIntegrationTests runs the tests only if a specific environment variable is set. These should always run under jenkins/github actions.
220+
func TestRunSGCollectIntegrationTests(t *testing.T) {
221+
env := "SG_TEST_SGCOLLECT_INTEGRATION"
222+
val, ok := os.LookupEnv(env)
223+
if !ok {
224+
ciEnvVars := []string{
225+
"CI", // convention by github actions
226+
"JENKINS_URL", // from jenkins
227+
}
228+
for _, ciEnv := range ciEnvVars {
229+
if os.Getenv(ciEnv) != "" {
230+
return
231+
}
232+
}
233+
t.Skip("Skipping sgcollect integration tests - set " + env + "=true to run")
234+
}
235+
236+
runTests, err := strconv.ParseBool(val)
237+
require.NoError(t, err, "Couldn't parse %s=%s as bool", env, val)
238+
if !runTests {
239+
t.Skip("Skipping sgcollect integration tests - set " + env + "=true to run")
240+
}
241+
}

rest/admin_api.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ func (h *handler) handleSetLogging() error {
16371637

16381638
func (h *handler) handleSGCollectStatus() error {
16391639
status := "stopped"
1640-
if sgcollectInstance.IsRunning() {
1640+
if h.server.SGCollect.IsRunning() {
16411641
status = "running"
16421642
}
16431643

@@ -1647,7 +1647,7 @@ func (h *handler) handleSGCollectStatus() error {
16471647
}
16481648

16491649
func (h *handler) handleSGCollectCancel() error {
1650-
err := sgcollectInstance.Stop()
1650+
err := h.server.SGCollect.Stop()
16511651
if err != nil {
16521652
return base.HTTPErrorf(http.StatusBadRequest, "Error stopping sgcollect_info: %v", err)
16531653
}
@@ -1664,7 +1664,7 @@ func (h *handler) handleSGCollect() error {
16641664
return err
16651665
}
16661666

1667-
var params sgCollectOptions
1667+
var params SGCollectOptions
16681668
if err = base.JSONUnmarshal(body, &params); err != nil {
16691669
return base.HTTPErrorf(http.StatusBadRequest, "Unable to parse request body: %v", err)
16701670
}
@@ -1676,11 +1676,24 @@ func (h *handler) handleSGCollect() error {
16761676
// Populate username and password used by sgcollect_info script for talking to Sync Gateway.
16771677
params.syncGatewayUsername, params.syncGatewayPassword = h.getBasicAuth()
16781678

1679-
zipFilename := sgcollectFilename()
1679+
addr, err := h.server.getServerAddr(adminServer)
1680+
if err != nil {
1681+
return base.HTTPErrorf(http.StatusInternalServerError, "Error getting admin server address: %v", err)
1682+
}
1683+
if h.server.Config.API.HTTPS.TLSCertPath != "" {
1684+
addr = "https://" + addr
1685+
} else {
1686+
addr = "http://" + addr
1687+
}
1688+
params.adminURL = addr
1689+
1690+
zipFilename := SGCollectFilename()
16801691

16811692
logFilePath := h.server.Config.Logging.LogFilePath
16821693

1683-
if err := sgcollectInstance.Start(logFilePath, h.serialNumber, zipFilename, params); err != nil {
1694+
ctx := base.CorrelationIDLogCtx(context.WithoutCancel(h.ctx()), fmt.Sprintf("SGCollect-%03d", h.serialNumber))
1695+
1696+
if err := h.server.SGCollect.Start(ctx, logFilePath, zipFilename, params); err != nil {
16841697
return base.HTTPErrorf(http.StatusInternalServerError, "Error running sgcollect_info: %v", err)
16851698
}
16861699

rest/audit_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func TestAuditLoggingFields(t *testing.T) {
239239
auditableAction: func(t testing.TB) {
240240
headers := map[string]string{
241241
requestInfoHeaderName: `{"extra":"field"}`,
242-
"Authorization": getBasicAuthHeader(requestUser, RestTesterDefaultUserPassword),
242+
"Authorization": GetBasicAuthHeader(t, requestUser, RestTesterDefaultUserPassword),
243243
}
244244
RequireStatus(t, rt.SendRequestWithHeaders(http.MethodGet, "/db/", "", headers), http.StatusOK)
245245
},
@@ -442,7 +442,7 @@ func TestAuditLoggingFields(t *testing.T) {
442442
name: "metrics request authenticated",
443443
auditableAction: func(t testing.TB) {
444444
headers := map[string]string{
445-
"Authorization": getBasicAuthHeader(base.TestClusterUsername(), base.TestClusterPassword()),
445+
"Authorization": GetBasicAuthHeader(t, base.TestClusterUsername(), base.TestClusterPassword()),
446446
}
447447
RequireStatus(t, rt.SendMetricsRequestWithHeaders(http.MethodGet, "/_metrics", "", headers), http.StatusOK)
448448
},
@@ -473,7 +473,7 @@ func TestAuditLoggingFields(t *testing.T) {
473473
t.Skip("Skipping subtest that requires admin auth to be enabled")
474474
}
475475
headers := map[string]string{
476-
"Authorization": getBasicAuthHeader("notauser", base.TestClusterPassword()),
476+
"Authorization": GetBasicAuthHeader(t, "notauser", base.TestClusterPassword()),
477477
}
478478
RequireStatus(t, rt.SendMetricsRequestWithHeaders(http.MethodGet, "/_metrics", "", headers), http.StatusUnauthorized)
479479
},
@@ -724,7 +724,7 @@ func TestEffectiveUserID(t *testing.T) {
724724
)
725725
reqHeaders := map[string]string{
726726
"user_header": fmt.Sprintf(`{"%s": "%s", "%s":"%s"}`, headerDomain, cnfDomain, headerUser, cnfUser),
727-
"Authorization": getBasicAuthHeader(realUser, RestTesterDefaultUserPassword),
727+
"Authorization": GetBasicAuthHeader(t, realUser, RestTesterDefaultUserPassword),
728728
}
729729

730730
rt := NewRestTester(t, &RestTesterConfig{

rest/server_context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ type ServerContext struct {
8686
DatabaseInitManager *DatabaseInitManager // Manages database initialization (index creation and readiness) independent of database stop/start/reload, when using persistent config
8787
ActiveReplicationsCounter
8888
invalidDatabaseConfigTracking invalidDatabaseConfigs
89+
// handle sgcollect processes for a given Server
90+
SGCollect *sgCollect
8991
}
9092

9193
type ActiveReplicationsCounter struct {
@@ -163,6 +165,7 @@ func NewServerContext(ctx context.Context, config *StartupConfig, persistentConf
163165
BootstrapContext: &bootstrapContext{sgVersion: *base.ProductVersion},
164166
hasStarted: make(chan struct{}),
165167
_httpServers: map[serverType]*serverInfo{},
168+
SGCollect: newSGCollect(ctx),
166169
}
167170
sc.invalidDatabaseConfigTracking = invalidDatabaseConfigs{
168171
dbNames: map[string]*invalidConfigInfo{},

0 commit comments

Comments
 (0)