Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add component Flags to astro deployment logs #1633

Merged
merged 5 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions cloud/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
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
Expand Down Expand Up @@ -174,13 +174,27 @@
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")

Check warning on line 193 in cloud/deployment/deployment.go

View check run for this annotation

Codecov / codecov/patch

cloud/deployment/deployment.go#L193

Added line #L193 was not covered by tests
}

getDeploymentLogsParams := astrocore.GetDeploymentLogsParams{
Sources: []astrocore.GetDeploymentLogsParamsSources{
"scheduler",
"webserver",
"triggerer",
"worker",
},
Sources: componentSources,
MaxNumResults: &logCount,
Range: &timeRange,
Offset: &offset,
Expand Down
54 changes: 50 additions & 4 deletions cloud/deployment/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,14 +788,48 @@ 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
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(&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)
Expand Down Expand Up @@ -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)
Expand All @@ -844,18 +878,30 @@ 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)
mockCoreClient.AssertExpectations(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) {
Expand Down
10 changes: 9 additions & 1 deletion cmd/cloud/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ var (
forDuration string
removeOverride bool
forceOverride bool
logWebserver bool
logScheduler bool
logWorkers bool
logTriggerer bool

deploymentType = standard
deploymentVariableListExample = `
Expand Down Expand Up @@ -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().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
}

Expand Down Expand Up @@ -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
Expand Down
66 changes: 63 additions & 3 deletions cmd/cloud/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down Expand Up @@ -341,15 +375,41 @@ 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"}
_, err = execDeploymentCmd(cmdArgs...)
assert.NoError(t, err)

cmdArgs = []string{"logs", "test-id-1", "-i"}
_, err = execDeploymentCmd(cmdArgs...)
assert.NoError(t, err)
mockPlatformCoreClient.AssertExpectations(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", "--webserver", "--scheduler", "--workers", "--triggerer", "-w"}
_, err := execDeploymentCmd(cmdArgs...)
assert.NoError(t, err)

cmdArgs = []string{"logs", "test-id-1", "", "-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", "", "", "-i"}
cmdArgs = []string{"logs", "test-id-1", "--webserver", "--scheduler", "--workers", "--triggerer", "-i"}
_, err = execDeploymentCmd(cmdArgs...)
assert.NoError(t, err)
mockPlatformCoreClient.AssertExpectations(t)
Expand Down