Files
certctl/internal/service/concurrent_test.go
T
shankar0123 5dc698307b chore: rename Go module path to github.com/certctl-io/certctl
Mechanical sed across the main go.mod's module declaration, the f5-mock-icontrol
sub-module's go.mod, every Go file's import path (361 files), and a rebuild of
the checked-in f5-mock-icontrol binary so its embedded build-info reflects the
new module path. No behavior change.

Choice B from cowork/transfer-certctl-to-org.md, executed 2026-05-04. Choice A
(keep module path declared as github.com/shankar0123/certctl regardless of
repo URL) shipped on the day of the org transfer (2026-05-03) since we had no
external Go consumers; this commit closes that deferral.

Backward-compat: GitHub HTTP redirects continue to forward
github.com/shankar0123/certctl → github.com/certctl-io/certctl at the URL
level, but Go's module proxy uses the path declared in go.mod as the
canonical name. Pre-fix, anyone trying `go get github.com/certctl-io/certctl/...`
hit a "module path mismatch" error because go.mod said
github.com/shankar0123/certctl and the URL they fetched it from said
certctl-io/certctl. Post-fix, the canonical name and the URL agree, so
go get / go install / external Go consumers / Go-tooling integrations
work cleanly via either the new path (preferred) or the old path (which
redirects and Go follows the redirect for source fetch).

Anyone still importing the old path inside their own code keeps working
provided they update their go.mod's `require` line to match — the module
path declared in their consumer's go.sum / go.mod is the authoritative
import name, so a mass sed across their import statements is the migration
on the consumer side. No external consumers exist today.

Diff shape:
  361 *.go files  — import path replacement only
    2 go.mod     — module declaration replacement only
    1 binary     — deploy/test/f5-mock-icontrol/f5-mock-icontrol rebuilt
                   so embedded build-info reflects the new path (8618965 vs
                   8618933 bytes; 32-byte diff is the build-info change)

  Total: 364 files, 730 insertions / 730 deletions, net-zero size, pure
  mechanical substitution.

Verification:
  gofmt: 17 files needed re-alignment after sed (the new path is one char
    shorter than the old, so column-aligned import groups drifted). Applied
    `gofmt -w` to fix.
  go mod tidy: clean exit on both modules.
  go vet ./...: clean exit.
  go build ./...: clean exit.
  go test -short -count=1 on representative packages: all green
    (internal/domain, internal/validation, internal/crypto, internal/crypto/signer,
    cmd/agent). Test output now reads `ok github.com/certctl-io/certctl/...`
    confirming the module path resolves correctly.
  binary: f5-mock-icontrol rebuilt; `strings | grep shankar0123` returns
    nothing; `strings | grep certctl-io/certctl` shows the new module path
    embedded in build-info.

Files intentionally NOT touched in this commit:
  README.md / CHANGELOG.md / docs/ / etc. — already swept to certctl-io
    URLs in commit bc6039a (the post-transfer URL refresh). This commit is
    purely the Go-tooling layer.
  Scarf pixels (`shankar0123.docker.scarf.sh/...`) — Scarf-account
    namespace, not a Go import or GitHub repo URL. Stays.

This is a non-blocking, non-customer-impacting change. Operators pulling
container images, running `make verify`, hitting the API, or installing the
agent see no functional difference. Only Go-tooling consumers (none today)
are affected, and they're enabled — not broken — by this commit.
2026-05-04 00:30:29 +00:00

472 lines
11 KiB
Go

package service
import (
"context"
"fmt"
"log/slog"
"os"
"sync"
"testing"
"github.com/certctl-io/certctl/internal/domain"
"github.com/certctl-io/certctl/internal/repository"
)
// TestConcurrentCertificateList tests that 10 goroutines can safely list certificates simultaneously
func TestConcurrentCertificateList(t *testing.T) {
mockCertRepo := newMockCertificateRepository()
// Add test certificates
for i := 0; i < 20; i++ {
mockCertRepo.AddCert(&domain.ManagedCertificate{
ID: fmt.Sprintf("mc-test-%d", i),
CommonName: fmt.Sprintf("test-%d.example.com", i),
})
}
certSvc := NewCertificateService(mockCertRepo, nil, nil)
var wg sync.WaitGroup
const goroutines = 10
errChan := make(chan error, goroutines)
for i := 0; i < goroutines; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
ctx := context.Background()
certs, total, err := certSvc.List(ctx, &repository.CertificateFilter{})
if err != nil {
errChan <- fmt.Errorf("goroutine %d: failed to list: %w", idx, err)
return
}
if certs == nil {
errChan <- fmt.Errorf("goroutine %d: returned nil certs slice", idx)
return
}
if total != 20 {
errChan <- fmt.Errorf("goroutine %d: expected 20 certs, got %d", idx, total)
return
}
}(i)
}
wg.Wait()
close(errChan)
// Verify no errors occurred
for err := range errChan {
t.Errorf("concurrent list error: %v", err)
}
}
// TestConcurrentJobStatusUpdates tests that 10 goroutines can safely update different jobs simultaneously
func TestConcurrentJobStatusUpdates(t *testing.T) {
mockJobRepo := newMockJobRepository()
// Create 10 jobs
for i := 0; i < 10; i++ {
job := &domain.Job{
ID: fmt.Sprintf("job-%d", i),
Status: domain.JobStatusPending,
}
mockJobRepo.AddJob(job)
}
var wg sync.WaitGroup
const goroutines = 10
errChan := make(chan error, goroutines)
for i := 0; i < goroutines; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
ctx := context.Background()
jobID := fmt.Sprintf("job-%d", idx)
newStatus := domain.JobStatusRunning
err := mockJobRepo.UpdateStatus(ctx, jobID, newStatus, "")
if err != nil {
errChan <- fmt.Errorf("goroutine %d: failed to update job %s: %w", idx, jobID, err)
return
}
// Verify the update
job, err := mockJobRepo.Get(ctx, jobID)
if err != nil {
errChan <- fmt.Errorf("goroutine %d: failed to get job %s: %w", idx, jobID, err)
return
}
if job.Status != newStatus {
errChan <- fmt.Errorf("goroutine %d: job %s status is %s, expected %s", idx, jobID, job.Status, newStatus)
return
}
}(i)
}
wg.Wait()
close(errChan)
// Verify no errors occurred
for err := range errChan {
t.Errorf("concurrent job update error: %v", err)
}
}
// TestConcurrentAgentHeartbeats tests that 10 goroutines can safely send heartbeats for different agents simultaneously
func TestConcurrentAgentHeartbeats(t *testing.T) {
mockAgentRepo := newMockAgentRepository()
// Create 10 agents
for i := 0; i < 10; i++ {
agent := &domain.Agent{
ID: fmt.Sprintf("agent-%d", i),
Name: fmt.Sprintf("agent-%d", i),
Hostname: fmt.Sprintf("host-%d", i),
}
mockAgentRepo.AddAgent(agent)
}
issuerRegistry := NewIssuerRegistry(slog.Default())
agentSvc := NewAgentService(
mockAgentRepo,
nil, // certRepo
nil, // jobRepo
nil, // targetRepo
nil, // auditService
issuerRegistry,
nil, // renewalService
)
var wg sync.WaitGroup
const goroutines = 10
errChan := make(chan error, goroutines)
for i := 0; i < goroutines; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
ctx := context.Background()
agentID := fmt.Sprintf("agent-%d", idx)
metadata := &domain.AgentMetadata{
OS: "linux",
Architecture: "x86_64",
}
err := agentSvc.Heartbeat(ctx, agentID, metadata)
if err != nil {
errChan <- fmt.Errorf("goroutine %d: failed heartbeat for agent %s: %w", idx, agentID, err)
return
}
// Verify the heartbeat was recorded
agent, err := mockAgentRepo.Get(ctx, agentID)
if err != nil {
errChan <- fmt.Errorf("goroutine %d: failed to get agent %s: %w", idx, agentID, err)
return
}
if agent.LastHeartbeatAt == nil {
errChan <- fmt.Errorf("goroutine %d: agent %s has no heartbeat", idx, agentID)
return
}
}(i)
}
wg.Wait()
close(errChan)
// Verify no errors occurred
for err := range errChan {
t.Errorf("concurrent heartbeat error: %v", err)
}
}
// TestConcurrentTargetCRUD tests concurrent create/list/delete operations on targets
func TestConcurrentTargetCRUD(t *testing.T) {
mockTargetRepo := &mockTargetRepo{
Targets: make(map[string]*domain.DeploymentTarget),
}
targetSvc := NewTargetService(mockTargetRepo, nil, nil, "", slog.New(slog.NewTextHandler(os.Stderr, nil)))
var mu sync.Mutex
createdTargets := make([]string, 0)
var wg sync.WaitGroup
// Phase 1: Create 5 targets in parallel
for i := 0; i < 5; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
ctx := context.Background()
target := &domain.DeploymentTarget{
ID: fmt.Sprintf("target-create-%d", idx),
Name: fmt.Sprintf("target-%d", idx),
Type: domain.TargetTypeNGINX,
}
err := targetSvc.Create(ctx, target, "test-user")
if err != nil {
t.Errorf("concurrent create error: %v", err)
return
}
mu.Lock()
createdTargets = append(createdTargets, target.ID)
mu.Unlock()
}(i)
}
wg.Wait()
// Phase 2: List targets in parallel
for i := 0; i < 5; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
ctx := context.Background()
_, _, err := targetSvc.List(ctx, 1, 50)
if err != nil {
t.Errorf("goroutine %d: concurrent list error: %v", idx, err)
return
}
}(i)
}
wg.Wait()
// Phase 3: Delete created targets in parallel
for _, targetID := range createdTargets {
targetIDCopy := targetID // Capture for closure
wg.Add(1)
go func() {
defer wg.Done()
ctx := context.Background()
err := targetSvc.Delete(ctx, targetIDCopy, "test-user")
if err != nil {
t.Errorf("concurrent delete error: %v", err)
return
}
}()
}
wg.Wait()
// Verify all targets were deleted
targets, err := mockTargetRepo.List(context.Background())
if err != nil {
t.Fatalf("failed to list targets: %v", err)
}
if len(targets) != 0 {
t.Errorf("expected 0 targets after deletion, got %d", len(targets))
}
}
// TestConcurrentNotificationProcessing tests concurrent notification sends
func TestConcurrentNotificationProcessing(t *testing.T) {
mockNotifRepo := newMockNotificationRepository()
mockNotifier := newMockNotifier()
var wg sync.WaitGroup
const goroutines = 10
errChan := make(chan error, goroutines)
for i := 0; i < goroutines; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
ctx := context.Background()
notif := &domain.NotificationEvent{
ID: fmt.Sprintf("notif-%d", idx),
Type: domain.NotificationTypeExpirationWarning,
Recipient: fmt.Sprintf("user-%d@example.com", idx),
Message: fmt.Sprintf("Notification message %d", idx),
Status: "pending",
}
err := mockNotifRepo.Create(ctx, notif)
if err != nil {
errChan <- fmt.Errorf("goroutine %d: failed to create notification: %w", idx, err)
return
}
// Simulate sending notification
err = mockNotifier.Send(ctx, notif.Recipient, "Certificate Expiring", notif.Message)
if err != nil {
errChan <- fmt.Errorf("goroutine %d: failed to send notification: %w", idx, err)
return
}
}(i)
}
wg.Wait()
close(errChan)
// Verify no errors occurred
for err := range errChan {
t.Errorf("concurrent notification error: %v", err)
}
// Verify all notifications were processed
if len(mockNotifRepo.Notifications) != goroutines {
t.Errorf("expected %d notifications, got %d", goroutines, len(mockNotifRepo.Notifications))
}
if len(mockNotifier.messages) != goroutines {
t.Errorf("expected %d sent messages, got %d", goroutines, len(mockNotifier.messages))
}
}
// TestConcurrentAuditRecording tests concurrent audit event recording
func TestConcurrentAuditRecording(t *testing.T) {
mockAuditRepo := newMockAuditRepository()
auditSvc := &AuditService{auditRepo: mockAuditRepo}
var wg sync.WaitGroup
const goroutines = 10
errChan := make(chan error, goroutines)
for i := 0; i < goroutines; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
ctx := context.Background()
actor := fmt.Sprintf("user-%d", idx)
eventType := "create_certificate"
resourceID := fmt.Sprintf("cert-%d", idx)
err := auditSvc.RecordEvent(
ctx,
actor,
domain.ActorTypeUser,
eventType,
"certificate",
resourceID,
map[string]interface{}{"index": idx},
)
if err != nil {
errChan <- fmt.Errorf("goroutine %d: failed to record audit event: %w", idx, err)
return
}
}(i)
}
wg.Wait()
close(errChan)
// Verify no errors occurred
for err := range errChan {
t.Errorf("concurrent audit error: %v", err)
}
// Verify all audit events were recorded
if len(mockAuditRepo.Events) != goroutines {
t.Errorf("expected %d audit events, got %d", goroutines, len(mockAuditRepo.Events))
}
}
// TestConcurrentMixedOperations tests mixed concurrent operations on multiple services
func TestConcurrentMixedOperations(t *testing.T) {
// Setup repositories
mockCertRepo := newMockCertificateRepository()
mockJobRepo := newMockJobRepository()
mockAuditRepo := newMockAuditRepository()
mockTargetRepo := &mockTargetRepo{
Targets: make(map[string]*domain.DeploymentTarget),
}
// Add initial test data
for i := 0; i < 5; i++ {
mockCertRepo.AddCert(&domain.ManagedCertificate{
ID: fmt.Sprintf("mc-mixed-%d", i),
CommonName: fmt.Sprintf("mixed-%d.example.com", i),
})
mockJobRepo.AddJob(&domain.Job{
ID: fmt.Sprintf("job-mixed-%d", i),
Status: domain.JobStatusPending,
})
}
// Setup services
auditSvc := &AuditService{auditRepo: mockAuditRepo}
certSvc := NewCertificateService(mockCertRepo, nil, auditSvc)
targetSvc := NewTargetService(mockTargetRepo, auditSvc, nil, "", slog.New(slog.NewTextHandler(os.Stderr, nil)))
var wg sync.WaitGroup
errChan := make(chan error, 30)
// Launch mixed concurrent operations
for i := 0; i < 10; i++ {
// Certificate operations
wg.Add(1)
go func(idx int) {
defer wg.Done()
ctx := context.Background()
_, _, err := certSvc.List(ctx, &repository.CertificateFilter{})
if err != nil {
errChan <- fmt.Errorf("cert list %d: %w", idx, err)
}
}(i)
// Target operations
wg.Add(1)
go func(idx int) {
defer wg.Done()
ctx := context.Background()
_, _, err := targetSvc.List(ctx, 1, 50)
if err != nil {
errChan <- fmt.Errorf("target list %d: %w", idx, err)
}
}(i)
// Audit operations
wg.Add(1)
go func(idx int) {
defer wg.Done()
ctx := context.Background()
err := auditSvc.RecordEvent(
ctx,
fmt.Sprintf("user-%d", idx),
domain.ActorTypeUser,
"test_event",
"test",
fmt.Sprintf("test-%d", idx),
nil,
)
if err != nil {
errChan <- fmt.Errorf("audit record %d: %w", idx, err)
}
}(i)
}
wg.Wait()
close(errChan)
// Verify no errors occurred
errorCount := 0
for err := range errChan {
t.Logf("concurrent mixed error: %v", err)
errorCount++
}
if errorCount > 0 {
t.Errorf("had %d concurrent operation errors", errorCount)
}
}