From 873257821536941560497a16e51370044fce1661 Mon Sep 17 00:00:00 2001 From: Isaac Chung Date: Wed, 24 Apr 2024 14:13:35 -0700 Subject: [PATCH 1/3] added component Flags to astro deployment logs --- cloud/deployment/deployment.go | 28 ++++++++++---- cloud/deployment/deployment_test.go | 54 ++++++++++++++++++++++++-- cmd/cloud/deployment.go | 10 ++++- cmd/cloud/deployment_test.go | 60 +++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 12 deletions(-) diff --git a/cloud/deployment/deployment.go b/cloud/deployment/deployment.go index 2ba0021c6..5ebc92baf 100644 --- a/cloud/deployment/deployment.go +++ b/cloud/deployment/deployment.go @@ -141,7 +141,7 @@ func List(ws string, fromAllWorkspaces bool, platformCoreClient astroplatformcor return nil } -func Logs(deploymentID, ws, deploymentName, keyword string, warnLogs, errorLogs, infoLogs bool, logCount int, platformCoreClient astroplatformcore.CoreClient, coreClient astrocore.CoreClient) error { +func Logs(deploymentID, ws, deploymentName, keyword string, logWebserver, logScheduler, logTriggerer, logWorkers, warnLogs, errorLogs, infoLogs bool, logCount int, platformCoreClient astroplatformcore.CoreClient, coreClient astrocore.CoreClient) error { var logLevel string var i int // log level @@ -174,13 +174,27 @@ func Logs(deploymentID, ws, deploymentName, keyword string, warnLogs, errorLogs, deploymentID = deployment.Id timeRange := 86400 offset := 0 + + // get log source + var componentSources []astrocore.GetDeploymentLogsParamsSources + if logWebserver { + componentSources = append(componentSources, "webserver") + } + if logScheduler { + componentSources = append(componentSources, "scheduler") + } + if logTriggerer { + componentSources = append(componentSources, "triggerer") + } + if logWorkers { + componentSources = append(componentSources, "worker") + } + if len(componentSources) == 0 { + componentSources = append(componentSources, "webserver", "scheduler", "triggerer", "worker") + } + getDeploymentLogsParams := astrocore.GetDeploymentLogsParams{ - Sources: []astrocore.GetDeploymentLogsParamsSources{ - "scheduler", - "webserver", - "triggerer", - "worker", - }, + Sources: componentSources, MaxNumResults: &logCount, Range: &timeRange, Offset: &offset, diff --git a/cloud/deployment/deployment_test.go b/cloud/deployment/deployment_test.go index 464dff0ee..228f9e708 100644 --- a/cloud/deployment/deployment_test.go +++ b/cloud/deployment/deployment_test.go @@ -788,6 +788,40 @@ func TestLogs(t *testing.T) { StatusCode: 200, }, } + mockGetDeploymentLogsMultipleComponentsResponse := astrocore.GetDeploymentLogsResponse{ + JSON200: &astrocore.DeploymentLog{ + Limit: 4, + MaxNumResults: 10, + Offset: 0, + ResultCount: 1, + Results: []astrocore.DeploymentLogEntry{ + { + Raw: "test log line", + Timestamp: 1, + Source: astrocore.DeploymentLogEntrySourceWebserver, + }, + { + Raw: "test log line 2", + Timestamp: 2, + Source: astrocore.DeploymentLogEntrySourceTriggerer, + }, + { + Raw: "test log line 3", + Timestamp: 2, + Source: astrocore.DeploymentLogEntrySourceScheduler, + }, + { + Raw: "test log line 4", + Timestamp: 2, + Source: astrocore.DeploymentLogEntrySourceWorker, + }, + }, + SearchId: "search-id", + }, + HTTPResponse: &http.Response{ + StatusCode: 200, + }, + } t.Run("success", func(t *testing.T) { // Mock ListDeployments @@ -795,7 +829,7 @@ func TestLogs(t *testing.T) { mockPlatformCoreClient.On("GetDeploymentWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&deploymentResponse, nil).Once() mockCoreClient.On("GetDeploymentLogsWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&mockGetDeploymentLogsResponse, nil).Once() - err := Logs(deploymentID, ws, "", "", true, false, false, logCount, mockPlatformCoreClient, mockCoreClient) + err := Logs(deploymentID, ws, "", "", true, true, true, true, true, false, false, logCount, mockPlatformCoreClient, mockCoreClient) assert.NoError(t, err) mockPlatformCoreClient.AssertExpectations(t) @@ -827,7 +861,7 @@ func TestLogs(t *testing.T) { defer func() { os.Stdin = stdin }() os.Stdin = r - err = Logs("", ws, "", "keyword", false, false, false, 1, mockPlatformCoreClient, mockCoreClient) + err = Logs("", ws, "", "keyword", true, true, true, true, false, false, false, 1, mockPlatformCoreClient, mockCoreClient) assert.NoError(t, err) mockPlatformCoreClient.AssertExpectations(t) @@ -844,7 +878,7 @@ func TestLogs(t *testing.T) { // Mock GetDeploymentLogsWithResponse to return an error mockCoreClient.On("GetDeploymentLogsWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&mockGetDeploymentLogsResponse, errMock).Once() - err := Logs(deploymentID, ws, "", "", false, false, false, logCount, mockPlatformCoreClient, mockCoreClient) + err := Logs(deploymentID, ws, "", "", true, true, true, true, false, false, false, logCount, mockPlatformCoreClient, mockCoreClient) assert.ErrorIs(t, err, errMock) mockPlatformCoreClient.AssertExpectations(t) @@ -852,10 +886,22 @@ func TestLogs(t *testing.T) { }) t.Run("query for more than one log level error", func(t *testing.T) { - err := Logs(deploymentID, ws, "", "", true, true, true, logCount, mockPlatformCoreClient, mockCoreClient) + err := Logs(deploymentID, ws, "", "", true, true, true, true, true, true, true, logCount, mockPlatformCoreClient, mockCoreClient) assert.Error(t, err) assert.Equal(t, err.Error(), "cannot query for more than one log level and/or keyword at a time") }) + t.Run("multiple components", func(t *testing.T) { + // Mock ListDeployments + mockPlatformCoreClient.On("ListDeploymentsWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&mockListDeploymentsResponse, nil).Once() + mockPlatformCoreClient.On("GetDeploymentWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&deploymentResponse, nil).Once() + mockCoreClient.On("GetDeploymentLogsWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&mockGetDeploymentLogsMultipleComponentsResponse, nil).Once() + + err := Logs(deploymentID, ws, "", "", true, true, true, true, true, false, false, logCount, mockPlatformCoreClient, mockCoreClient) + assert.NoError(t, err) + + mockPlatformCoreClient.AssertExpectations(t) + mockCoreClient.AssertExpectations(t) + }) } func TestCreate(t *testing.T) { diff --git a/cmd/cloud/deployment.go b/cmd/cloud/deployment.go index bc89cc608..8b576c010 100644 --- a/cmd/cloud/deployment.go +++ b/cmd/cloud/deployment.go @@ -72,6 +72,10 @@ var ( forDuration string removeOverride bool forceOverride bool + logWebserver bool + logScheduler bool + logWorkers bool + logTriggerer bool deploymentType = standard deploymentVariableListExample = ` @@ -359,6 +363,10 @@ func newDeploymentLogsCmd() *cobra.Command { cmd.Flags().StringVarP(&logsKeyword, "keyword", "k", "", "Show logs that contain this exact keyword or phrase.") cmd.Flags().IntVarP(&logCount, "log-count", "c", logCount, "Number of logs to show") cmd.Flags().StringVarP(&deploymentName, "deployment-name", "n", "", "Name of the deployment to show logs of") + cmd.Flags().BoolVarP(&logWebserver, "webserver", "r", false, "Show logs from the webserver") + cmd.Flags().BoolVarP(&logScheduler, "scheduler", "s", false, "Show logs from the scheduler") + cmd.Flags().BoolVarP(&logWorkers, "workers", "o", false, "Show logs from the workers") + cmd.Flags().BoolVarP(&logTriggerer, "triggerer", "t", false, "Show logs from the triggerer") return cmd } @@ -613,7 +621,7 @@ func deploymentLogs(cmd *cobra.Command, args []string) error { return errors.Wrap(err, "failed to find a valid Workspace") } - return deployment.Logs(deploymentID, ws, deploymentName, logsKeyword, warnLogs, errorLogs, infoLogs, logCount, platformCoreClient, astroCoreClient) + return deployment.Logs(deploymentID, ws, deploymentName, logsKeyword, logWebserver, logScheduler, logTriggerer, logWorkers, warnLogs, errorLogs, infoLogs, logCount, platformCoreClient, astroCoreClient) } func deploymentCreate(cmd *cobra.Command, _ []string, out io.Writer) error { //nolint:gocognit,gocyclo diff --git a/cmd/cloud/deployment_test.go b/cmd/cloud/deployment_test.go index 66a3d0261..2061b2a37 100644 --- a/cmd/cloud/deployment_test.go +++ b/cmd/cloud/deployment_test.go @@ -177,6 +177,40 @@ var ( StatusCode: 200, }, } + mockGetDeploymentLogsMultipleComponentsResponse = astrocore.GetDeploymentLogsResponse{ + JSON200: &astrocore.DeploymentLog{ + Limit: logCount, + MaxNumResults: 10, + Offset: 0, + ResultCount: 1, + Results: []astrocore.DeploymentLogEntry{ + { + Raw: "test log line", + Timestamp: 1, + Source: astrocore.DeploymentLogEntrySourceWebserver, + }, + { + Raw: "test log line 2", + Timestamp: 2, + Source: astrocore.DeploymentLogEntrySourceTriggerer, + }, + { + Raw: "test log line 3", + Timestamp: 2, + Source: astrocore.DeploymentLogEntrySourceScheduler, + }, + { + Raw: "test log line 4", + Timestamp: 2, + Source: astrocore.DeploymentLogEntrySourceWorker, + }, + }, + SearchId: "search-id", + }, + HTTPResponse: &http.Response{ + StatusCode: 200, + }, + } GetDeploymentOptionsResponseAlphaOK = astrocore.GetDeploymentOptionsResponse{ JSON200: &astrocore.DeploymentOptions{ DefaultValues: astrocore.DefaultValueOptions{}, @@ -356,6 +390,32 @@ func TestDeploymentLogs(t *testing.T) { mockCoreClient.AssertExpectations(t) } +func TestDeploymentLogsMultipleComponents(t *testing.T) { + testUtil.InitTestConfig(testUtil.LocalPlatform) + + mockPlatformCoreClient := new(astroplatformcore_mocks.ClientWithResponsesInterface) + mockCoreClient := new(astrocore_mocks.ClientWithResponsesInterface) + mockPlatformCoreClient.On("ListDeploymentsWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&mockListDeploymentsResponse, nil).Times(3) + mockPlatformCoreClient.On("GetDeploymentWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&deploymentResponse, nil).Times(3) + mockCoreClient.On("GetDeploymentLogsWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&mockGetDeploymentLogsMultipleComponentsResponse, nil).Times(3) + platformCoreClient = mockPlatformCoreClient + astroCoreClient = mockCoreClient + + cmdArgs := []string{"logs", "test-id-1", "-r", "-s", "-o", "-t", "-w", "", ""} + _, err := execDeploymentCmd(cmdArgs...) + assert.NoError(t, err) + + cmdArgs = []string{"logs", "test-id-1", "-r", "-s", "-o", "-t", "", "-e", ""} + _, err = execDeploymentCmd(cmdArgs...) + assert.NoError(t, err) + + cmdArgs = []string{"logs", "test-id-1", "-r", "-s", "-o", "-t", "", "", "-i"} + _, err = execDeploymentCmd(cmdArgs...) + assert.NoError(t, err) + mockPlatformCoreClient.AssertExpectations(t) + mockCoreClient.AssertExpectations(t) +} + func TestDeploymentCreate(t *testing.T) { testUtil.InitTestConfig(testUtil.LocalPlatform) From 4a70710b26f8c358b4406ec6edfd9a5e861c9595 Mon Sep 17 00:00:00 2001 From: Isaac Chung Date: Wed, 24 Apr 2024 16:02:08 -0700 Subject: [PATCH 2/3] addressed comments --- cmd/cloud/deployment.go | 8 ++++---- cmd/cloud/deployment_test.go | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/cloud/deployment.go b/cmd/cloud/deployment.go index 8b576c010..4d7e593bc 100644 --- a/cmd/cloud/deployment.go +++ b/cmd/cloud/deployment.go @@ -363,10 +363,10 @@ func newDeploymentLogsCmd() *cobra.Command { cmd.Flags().StringVarP(&logsKeyword, "keyword", "k", "", "Show logs that contain this exact keyword or phrase.") cmd.Flags().IntVarP(&logCount, "log-count", "c", logCount, "Number of logs to show") cmd.Flags().StringVarP(&deploymentName, "deployment-name", "n", "", "Name of the deployment to show logs of") - cmd.Flags().BoolVarP(&logWebserver, "webserver", "r", false, "Show logs from the webserver") - cmd.Flags().BoolVarP(&logScheduler, "scheduler", "s", false, "Show logs from the scheduler") - cmd.Flags().BoolVarP(&logWorkers, "workers", "o", false, "Show logs from the workers") - cmd.Flags().BoolVarP(&logTriggerer, "triggerer", "t", false, "Show logs from the triggerer") + cmd.Flags().BoolVar(&logWebserver, "webserver", false, "Show logs from the webserver") + cmd.Flags().BoolVar(&logScheduler, "scheduler", false, "Show logs from the scheduler") + cmd.Flags().BoolVar(&logWorkers, "workers", false, "Show logs from the workers") + cmd.Flags().BoolVar(&logTriggerer, "triggerer", false, "Show logs from the triggerer") return cmd } diff --git a/cmd/cloud/deployment_test.go b/cmd/cloud/deployment_test.go index 2061b2a37..4aea0a88f 100644 --- a/cmd/cloud/deployment_test.go +++ b/cmd/cloud/deployment_test.go @@ -401,15 +401,15 @@ func TestDeploymentLogsMultipleComponents(t *testing.T) { platformCoreClient = mockPlatformCoreClient astroCoreClient = mockCoreClient - cmdArgs := []string{"logs", "test-id-1", "-r", "-s", "-o", "-t", "-w", "", ""} + cmdArgs := []string{"logs", "test-id-1", "--webserver", "--scheduler", "--workers", "--triggerer", "-w", "", ""} _, err := execDeploymentCmd(cmdArgs...) assert.NoError(t, err) - cmdArgs = []string{"logs", "test-id-1", "-r", "-s", "-o", "-t", "", "-e", ""} + cmdArgs = []string{"logs", "test-id-1", "--webserver", "--scheduler", "--workers", "--triggerer", "", "-e", ""} _, err = execDeploymentCmd(cmdArgs...) assert.NoError(t, err) - cmdArgs = []string{"logs", "test-id-1", "-r", "-s", "-o", "-t", "", "", "-i"} + cmdArgs = []string{"logs", "test-id-1", "--webserver", "--scheduler", "--workers", "--triggerer", "", "", "-i"} _, err = execDeploymentCmd(cmdArgs...) assert.NoError(t, err) mockPlatformCoreClient.AssertExpectations(t) From d15ea3335c55955dacca5b90b1aa0753a357897e Mon Sep 17 00:00:00 2001 From: Isaac Chung Date: Fri, 26 Apr 2024 15:53:21 -0700 Subject: [PATCH 3/3] fixed nits --- cmd/cloud/deployment_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/cloud/deployment_test.go b/cmd/cloud/deployment_test.go index 4aea0a88f..0ece34a07 100644 --- a/cmd/cloud/deployment_test.go +++ b/cmd/cloud/deployment_test.go @@ -375,15 +375,15 @@ func TestDeploymentLogs(t *testing.T) { platformCoreClient = mockPlatformCoreClient astroCoreClient = mockCoreClient - cmdArgs := []string{"logs", "test-id-1", "-w", "", ""} + cmdArgs := []string{"logs", "test-id-1", "-w"} _, err := execDeploymentCmd(cmdArgs...) assert.NoError(t, err) - cmdArgs = []string{"logs", "test-id-1", "", "-e", ""} + cmdArgs = []string{"logs", "test-id-1", "-e"} _, err = execDeploymentCmd(cmdArgs...) assert.NoError(t, err) - cmdArgs = []string{"logs", "test-id-1", "", "", "-i"} + cmdArgs = []string{"logs", "test-id-1", "-i"} _, err = execDeploymentCmd(cmdArgs...) assert.NoError(t, err) mockPlatformCoreClient.AssertExpectations(t) @@ -401,15 +401,15 @@ func TestDeploymentLogsMultipleComponents(t *testing.T) { platformCoreClient = mockPlatformCoreClient astroCoreClient = mockCoreClient - cmdArgs := []string{"logs", "test-id-1", "--webserver", "--scheduler", "--workers", "--triggerer", "-w", "", ""} + cmdArgs := []string{"logs", "test-id-1", "--webserver", "--scheduler", "--workers", "--triggerer", "-w"} _, err := execDeploymentCmd(cmdArgs...) assert.NoError(t, err) - cmdArgs = []string{"logs", "test-id-1", "--webserver", "--scheduler", "--workers", "--triggerer", "", "-e", ""} + cmdArgs = []string{"logs", "test-id-1", "--webserver", "--scheduler", "--workers", "--triggerer", "-e"} _, err = execDeploymentCmd(cmdArgs...) assert.NoError(t, err) - cmdArgs = []string{"logs", "test-id-1", "--webserver", "--scheduler", "--workers", "--triggerer", "", "", "-i"} + cmdArgs = []string{"logs", "test-id-1", "--webserver", "--scheduler", "--workers", "--triggerer", "-i"} _, err = execDeploymentCmd(cmdArgs...) assert.NoError(t, err) mockPlatformCoreClient.AssertExpectations(t)