Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore(coderd): add MockAuditor.Contains test helper#10421

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
johnstcn merged 2 commits intomainfromcj/flake/test-workspace-dormant
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes fromall 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
77 changes: 77 additions & 0 deletionscoderd/audit/audit.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,10 @@ package audit
import (
"context"
"sync"
"testing"

"github.com/google/uuid"
"golang.org/x/exp/slices"

"github.com/coder/coder/v2/coderd/database"
)
Expand DownExpand Up@@ -68,3 +72,76 @@ func (a *MockAuditor) Export(_ context.Context, alog database.AuditLog) error {
func (*MockAuditor) diff(any, any) Map {
return Map{}
}

// Contains returns true if, for each non-zero-valued field in expected,
// there exists a corresponding audit log in the mock auditor that matches
// the expected values. Returns false otherwise.
func (a *MockAuditor) Contains(t testing.TB, expected database.AuditLog) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Might be useful to be able to require multiple log entries and thus, also that the order is correct? Just a thought, feel free to disregard if it's premature.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

We could assert relative ordering perhaps, but asserting absolute ordering would likely run into the same kinds of issues we saw in#10396.

a.mutex.Lock()
defer a.mutex.Unlock()
for idx, al := range a.auditLogs {
if expected.ID != uuid.Nil && al.ID != expected.ID {
t.Logf("audit log %d: expected ID %s, got %s", idx+1, expected.ID, al.ID)
continue
}
if !expected.Time.IsZero() && expected.Time != al.Time {
t.Logf("audit log %d: expected Time %s, got %s", idx+1, expected.Time, al.Time)
continue
}
if expected.UserID != uuid.Nil && al.UserID != expected.UserID {
t.Logf("audit log %d: expected UserID %s, got %s", idx+1, expected.UserID, al.UserID)
continue
}
if expected.OrganizationID != uuid.Nil && al.UserID != expected.UserID {
t.Logf("audit log %d: expected OrganizationID %s, got %s", idx+1, expected.OrganizationID, al.OrganizationID)
continue
}
if expected.Ip.Valid && al.Ip.IPNet.String() != expected.Ip.IPNet.String() {
t.Logf("audit log %d: expected Ip %s, got %s", idx+1, expected.Ip.IPNet, al.Ip.IPNet)
continue
}
if expected.UserAgent.Valid && al.UserAgent.String != expected.UserAgent.String {
t.Logf("audit log %d: expected UserAgent %s, got %s", idx+1, expected.UserAgent.String, al.UserAgent.String)
continue
}
if expected.ResourceType != "" && expected.ResourceType != al.ResourceType {
t.Logf("audit log %d: expected ResourceType %s, got %s", idx+1, expected.ResourceType, al.ResourceType)
continue
}
if expected.ResourceID != uuid.Nil && expected.ResourceID != al.ResourceID {
t.Logf("audit log %d: expected ResourceID %s, got %s", idx+1, expected.ResourceID, al.ResourceID)
continue
}
if expected.ResourceTarget != "" && expected.ResourceTarget != al.ResourceTarget {
t.Logf("audit log %d: expected ResourceTarget %s, got %s", idx+1, expected.ResourceTarget, al.ResourceTarget)
continue
}
if expected.Action != "" && expected.Action != al.Action {
t.Logf("audit log %d: expected Action %s, got %s", idx+1, expected.Action, al.Action)
continue
}
if len(expected.Diff) > 0 && slices.Compare(expected.Diff, al.Diff) != 0 {
t.Logf("audit log %d: expected Diff %s, got %s", idx+1, string(expected.Diff), string(al.Diff))
continue
}
if expected.StatusCode != 0 && expected.StatusCode != al.StatusCode {
t.Logf("audit log %d: expected StatusCode %d, got %d", idx+1, expected.StatusCode, al.StatusCode)
continue
}
if len(expected.AdditionalFields) > 0 && slices.Compare(expected.AdditionalFields, al.AdditionalFields) != 0 {
t.Logf("audit log %d: expected AdditionalFields %s, got %s", idx+1, string(expected.AdditionalFields), string(al.AdditionalFields))
continue
}
if expected.RequestID != uuid.Nil && expected.RequestID != al.RequestID {
t.Logf("audit log %d: expected RequestID %s, got %s", idx+1, expected.RequestID, al.RequestID)
continue
}
if expected.ResourceIcon != "" && expected.ResourceIcon != al.ResourceIcon {
t.Logf("audit log %d: expected ResourceIcon %s, got %s", idx+1, expected.ResourceIcon, al.ResourceIcon)
continue
}
return true
}

return false
}
40 changes: 15 additions & 25 deletionscoderd/workspaces_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -511,7 +511,11 @@ func TestPostWorkspacesByOrganization(t *testing.T) {
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
verifyAuditWorkspaceCreated(t, auditor, workspace.Name)
assert.True(t, auditor.Contains(t, database.AuditLog{
ResourceType: database.ResourceTypeWorkspace,
Action: database.AuditActionCreate,
ResourceTarget: workspace.Name,
}))
})

t.Run("CreateFromVersionWithAuditLogs", func(t *testing.T) {
Expand All@@ -535,7 +539,11 @@ func TestPostWorkspacesByOrganization(t *testing.T) {

require.Equal(t, testWorkspaceBuild.TemplateVersionID, versionTest.ID)
require.Equal(t, defaultWorkspaceBuild.TemplateVersionID, versionDefault.ID)
verifyAuditWorkspaceCreated(t, auditor, defaultWorkspace.Name)
assert.True(t, auditor.Contains(t, database.AuditLog{
ResourceType: database.ResourceTypeWorkspace,
Action: database.AuditActionCreate,
ResourceTarget: defaultWorkspace.Name,
}))
})

t.Run("InvalidCombinationOfTemplateAndTemplateVersion", func(t *testing.T) {
Expand DownExpand Up@@ -2741,7 +2749,11 @@ func TestWorkspaceDormant(t *testing.T) {
Dormant: true,
})
require.NoError(t, err)
require.Len(t, auditRecorder.AuditLogs(), 1)
require.True(t, auditRecorder.Contains(t, database.AuditLog{
Action: database.AuditActionWrite,
ResourceType: database.ResourceTypeWorkspace,
ResourceTarget: workspace.Name,
}))

workspace = coderdtest.MustWorkspace(t, client, workspace.ID)
require.NoError(t, err, "fetch provisioned workspace")
Expand DownExpand Up@@ -2804,25 +2816,3 @@ func TestWorkspaceDormant(t *testing.T) {
coderdtest.MustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStop, database.WorkspaceTransitionStart)
})
}

func verifyAuditWorkspaceCreated(t *testing.T, auditor *audit.MockAuditor, workspaceName string) {
var auditLogs []database.AuditLog
ok := assert.Eventually(t, func() bool {
auditLogs = auditor.AuditLogs()

for _, auditLog := range auditLogs {
if auditLog.Action == database.AuditActionCreate &&
auditLog.ResourceType == database.ResourceTypeWorkspace &&
auditLog.ResourceTarget == workspaceName {
return true
}
}
return false
}, testutil.WaitMedium, testutil.IntervalFast)

if !ok {
for i, auditLog := range auditLogs {
t.Logf("%d. Audit: ID=%s action=%s resourceID=%s resourceType=%s resourceTarget=%s", i+1, auditLog.ID, auditLog.Action, auditLog.ResourceID, auditLog.ResourceType, auditLog.ResourceTarget)
}
}
}

[8]ページ先頭

©2009-2025 Movatter.jp