-
Notifications
You must be signed in to change notification settings - Fork 92
Adding several minor changes to improve testability before migrating the existing k8s audit parser to the new audit parser #375
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
Merged
kyasbal
merged 3 commits into
GoogleCloudPlatform:epic/issue-373
from
kyasbal:epic/issue-373-1-prepare-implementing-new-parser
Nov 26, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package inspectiontaskbase | ||
|
|
||
| import ( | ||
| "context" | ||
| "slices" | ||
|
|
||
| inspectionmetadata "github.com/GoogleCloudPlatform/khi/pkg/core/inspection/metadata" | ||
| coretask "github.com/GoogleCloudPlatform/khi/pkg/core/task" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/core/task/taskid" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/model/log" | ||
| inspectioncore_contract "github.com/GoogleCloudPlatform/khi/pkg/task/inspection/inspectioncore/contract" | ||
| ) | ||
|
|
||
| func NewLogSorterByTimeTask(taskID taskid.TaskImplementationID[[]*log.Log], logSource taskid.TaskReference[[]*log.Log]) coretask.Task[[]*log.Log] { | ||
| return NewProgressReportableInspectionTask(taskID, []taskid.UntypedTaskReference{logSource}, func(ctx context.Context, taskMode inspectioncore_contract.InspectionTaskModeType, progress *inspectionmetadata.TaskProgressMetadata) ([]*log.Log, error) { | ||
| if taskMode != inspectioncore_contract.TaskModeRun { | ||
| return []*log.Log{}, nil | ||
| } | ||
| progress.MarkIndeterminate() | ||
| logs := coretask.GetTaskResult(ctx, logSource) | ||
| logs = slices.Clone(logs) | ||
| slices.SortFunc(logs, func(a, b *log.Log) int { | ||
| aFieldSet := log.MustGetFieldSet(a, &log.CommonFieldSet{}) | ||
| bFieldSet := log.MustGetFieldSet(b, &log.CommonFieldSet{}) | ||
| return aFieldSet.Timestamp.Compare(bFieldSet.Timestamp) | ||
| }) | ||
| return logs, nil | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package inspectiontaskbase | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| inspectiontest "github.com/GoogleCloudPlatform/khi/pkg/core/inspection/test" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/core/task/taskid" | ||
| tasktest "github.com/GoogleCloudPlatform/khi/pkg/core/task/test" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/model/log" | ||
| inspectioncore_contract "github.com/GoogleCloudPlatform/khi/pkg/task/inspection/inspectioncore/contract" | ||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| func TestLogSorterByTimeTask(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| mode inspectioncore_contract.InspectionTaskModeType | ||
| logs []*log.CommonFieldSet | ||
| want []*log.CommonFieldSet | ||
| }{ | ||
| { | ||
| name: "should return an empty list for empty log input on task run mode", | ||
| mode: inspectioncore_contract.TaskModeRun, | ||
| logs: []*log.CommonFieldSet{}, | ||
| want: make([]*log.CommonFieldSet, 0), | ||
| }, | ||
| { | ||
| name: "should return an empty list for empty log input on task dry run mode", | ||
| mode: inspectioncore_contract.TaskModeDryRun, | ||
| logs: []*log.CommonFieldSet{ | ||
| { | ||
| DisplayID: "foo", | ||
| Timestamp: time.Date(2025, 11, 21, 13, 16, 34, 0, time.UTC), | ||
| }, | ||
| { | ||
| DisplayID: "bar", | ||
| Timestamp: time.Date(2025, 11, 21, 13, 16, 33, 0, time.UTC), | ||
| }, | ||
| { | ||
| DisplayID: "qux", | ||
| Timestamp: time.Date(2025, 11, 21, 13, 16, 32, 0, time.UTC), | ||
| }, | ||
| }, | ||
| want: make([]*log.CommonFieldSet, 0), | ||
| }, | ||
| { | ||
| name: "should return sorted logs on task run mode", | ||
| mode: inspectioncore_contract.TaskModeRun, | ||
| logs: []*log.CommonFieldSet{ | ||
| { | ||
| DisplayID: "foo", | ||
| Timestamp: time.Date(2025, 11, 21, 13, 16, 34, 0, time.UTC), | ||
| }, | ||
| { | ||
| DisplayID: "bar", | ||
| Timestamp: time.Date(2025, 11, 21, 13, 16, 33, 0, time.UTC), | ||
| }, | ||
| { | ||
| DisplayID: "qux", | ||
| Timestamp: time.Date(2025, 11, 21, 13, 16, 32, 0, time.UTC), | ||
| }, | ||
| }, | ||
| want: []*log.CommonFieldSet{ | ||
| { | ||
| DisplayID: "qux", | ||
| Timestamp: time.Date(2025, 11, 21, 13, 16, 32, 0, time.UTC), | ||
| }, | ||
| { | ||
| DisplayID: "bar", | ||
| Timestamp: time.Date(2025, 11, 21, 13, 16, 33, 0, time.UTC), | ||
| }, | ||
| { | ||
| DisplayID: "foo", | ||
| Timestamp: time.Date(2025, 11, 21, 13, 16, 34, 0, time.UTC), | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| logs := []*log.Log{} | ||
| for _, commonFieldSet := range tc.logs { | ||
| l := log.NewLogWithFieldSetsForTest(commonFieldSet) | ||
| logs = append(logs, l) | ||
| } | ||
|
|
||
| testSourceTaskID := taskid.NewDefaultImplementationID[[]*log.Log]("source") | ||
| testTaskID := taskid.NewDefaultImplementationID[[]*log.Log]("dest") | ||
| task := NewLogSorterByTimeTask(testTaskID, testSourceTaskID.Ref()) | ||
|
|
||
| ctx := inspectiontest.WithDefaultTestInspectionTaskContext(context.Background()) | ||
| sortedLogs, _, err := inspectiontest.RunInspectionTask(ctx, task, tc.mode, map[string]any{}, tasktest.NewTaskDependencyValuePair(testSourceTaskID.Ref(), logs)) | ||
| if err != nil { | ||
| t.Fatalf("RunInspectionTask returned an unexpected error: %v", err) | ||
| } | ||
|
|
||
| got := []*log.CommonFieldSet{} | ||
| for _, l := range sortedLogs { | ||
| fieldSet := log.MustGetFieldSet(l, &log.CommonFieldSet{}) | ||
| got = append(got, fieldSet) | ||
| } | ||
|
|
||
| if diff := cmp.Diff(tc.want, got); diff != "" { | ||
| t.Errorf("log sort mismatch (-want +got):\n%s", diff) | ||
| } | ||
|
|
||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package inspectiontaskbasetest | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| inspectiontest "github.com/GoogleCloudPlatform/khi/pkg/core/inspection/test" | ||
| coretask "github.com/GoogleCloudPlatform/khi/pkg/core/task" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/core/task/taskid" | ||
| tasktest "github.com/GoogleCloudPlatform/khi/pkg/core/task/test" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/model/log" | ||
| inspectioncore_contract "github.com/GoogleCloudPlatform/khi/pkg/task/inspection/inspectioncore/contract" | ||
| ) | ||
|
|
||
| // FilterTaskTestCase is a test case for testing a filter task. | ||
| // It contains the log fields to be filtered and the expected result. | ||
| type FilterTaskTestCase struct { | ||
| Description string | ||
| LogFields []log.FieldSet | ||
| WantIncluded bool | ||
| } | ||
|
|
||
| // AssertFilterTask asserts that the given filter task behaves as expected for the given test cases. | ||
| // It runs the task with the given log fields and checks if the log is included or excluded from the result. | ||
| func AssertFilterTask(t *testing.T, task coretask.Task[[]*log.Log], sourceRef taskid.TaskReference[[]*log.Log], testCases []FilterTaskTestCase) { | ||
| t.Helper() | ||
| for _, tc := range testCases { | ||
| t.Run(tc.Description, func(t *testing.T) { | ||
| l := log.NewLogWithFieldSetsForTest(tc.LogFields...) | ||
| ctx := inspectiontest.WithDefaultTestInspectionTaskContext(t.Context()) | ||
|
|
||
| result, _, err := inspectiontest.RunInspectionTask(ctx, task, inspectioncore_contract.TaskModeRun, map[string]any{}, tasktest.NewTaskDependencyValuePair(sourceRef, []*log.Log{l})) | ||
| if err != nil { | ||
| t.Fatalf("RunInspectionTask failed: %v", err) | ||
| } | ||
| if tc.WantIncluded { | ||
| if len(result) == 0 { | ||
| t.Errorf("given log was unexpectedly filtered out. want=1, got=0") | ||
| } | ||
| } else { | ||
| if len(result) != 0 { | ||
| t.Errorf("given log was unexpectedly included. want=0, got=1") | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package inspectiontaskbasetest | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| inspectiontaskbase "github.com/GoogleCloudPlatform/khi/pkg/core/inspection/taskbase" | ||
| inspectiontest "github.com/GoogleCloudPlatform/khi/pkg/core/inspection/test" | ||
| coretask "github.com/GoogleCloudPlatform/khi/pkg/core/task" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/core/task/taskid" | ||
| tasktest "github.com/GoogleCloudPlatform/khi/pkg/core/task/test" | ||
| "github.com/GoogleCloudPlatform/khi/pkg/model/log" | ||
| inspectioncore_contract "github.com/GoogleCloudPlatform/khi/pkg/task/inspection/inspectioncore/contract" | ||
| ) | ||
|
|
||
| // GrouperTaskTestCase is a test case for log grouper task. | ||
| type GrouperTaskTestCase struct { | ||
| Description string | ||
| LogFields []log.FieldSet | ||
| WantGroup string | ||
| } | ||
|
|
||
| // AssertGrouperTask asserts that the given grouper task behaves as expected for the given test cases. | ||
| func AssertGrouperTask(t *testing.T, task coretask.Task[inspectiontaskbase.LogGroupMap], sourceRef taskid.TaskReference[[]*log.Log], testCases []GrouperTaskTestCase) { | ||
| t.Helper() | ||
| for _, tc := range testCases { | ||
| t.Run(tc.Description, func(t *testing.T) { | ||
| l := log.NewLogWithFieldSetsForTest(tc.LogFields...) | ||
| ctx := inspectiontest.WithDefaultTestInspectionTaskContext(t.Context()) | ||
|
|
||
| result, _, err := inspectiontest.RunInspectionTask(ctx, task, inspectioncore_contract.TaskModeRun, map[string]any{}, tasktest.NewTaskDependencyValuePair(sourceRef, []*log.Log{l})) | ||
| if err != nil { | ||
| t.Fatalf("RunInspectionTask failed: %v", err) | ||
| } | ||
| if len(result) != 1 { | ||
| t.Fatalf("unexpected element count found in result: want=1, got=%d", len(result)) | ||
| } | ||
| for _, group := range result { | ||
| if group.Group != tc.WantGroup { | ||
| t.Fatalf("unexpected group found in result: want=%s, got=%s", tc.WantGroup, group.Group) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.