mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 20:01:31 +00:00
8b75e0311b
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 0729ee4 (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.
240 lines
7.2 KiB
Go
240 lines
7.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/certctl-io/certctl/internal/domain"
|
|
"github.com/certctl-io/certctl/internal/repository"
|
|
)
|
|
|
|
// TestCertificateService_ListWithCancelledContext verifies that List respects a cancelled context
|
|
func TestCertificateService_ListWithCancelledContext(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel() // Cancel immediately
|
|
|
|
mockCertRepo := newMockCertificateRepository()
|
|
certSvc := NewCertificateService(mockCertRepo, nil, nil)
|
|
|
|
_, _, err := certSvc.List(ctx, &repository.CertificateFilter{})
|
|
|
|
// The service should propagate context cancellation errors
|
|
// even though our mock may not check context, we verify the call goes through
|
|
// and the context error becomes part of the error chain
|
|
if err == nil || ctx.Err() == context.Canceled {
|
|
// Either the service respects context and returns an error,
|
|
// or the context was cancelled. Both are valid findings.
|
|
return
|
|
}
|
|
t.Logf("List with cancelled context returned: %v", err)
|
|
}
|
|
|
|
// TestCertificateService_GetWithCancelledContext verifies that Get respects a cancelled context
|
|
func TestCertificateService_GetWithCancelledContext(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel() // Cancel immediately
|
|
|
|
mockCertRepo := newMockCertificateRepository()
|
|
mockCertRepo.AddCert(&domain.ManagedCertificate{ID: "mc-test-1", CommonName: "test.example.com"})
|
|
certSvc := NewCertificateService(mockCertRepo, nil, nil)
|
|
|
|
_, err := certSvc.Get(ctx, "mc-test-1")
|
|
|
|
// Service should handle cancelled context
|
|
if err == nil || ctx.Err() == context.Canceled {
|
|
return
|
|
}
|
|
t.Logf("Get with cancelled context returned: %v", err)
|
|
}
|
|
|
|
// TestRenewalService_ProcessWithCancelledContext verifies that renewal processing respects a cancelled context
|
|
func TestRenewalService_ProcessWithCancelledContext(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel() // Cancel immediately
|
|
|
|
mockCertRepo := newMockCertificateRepository()
|
|
mockJobRepo := newMockJobRepository()
|
|
mockPolicyRepo := newMockRenewalPolicyRepository()
|
|
mockProfileRepo := &mockCertificateProfileRepository{
|
|
Profiles: make(map[string]*domain.CertificateProfile),
|
|
}
|
|
mockAuditSvc := &AuditService{auditRepo: newMockAuditRepository()}
|
|
mockNotifSvc := &NotificationService{
|
|
notifRepo: newMockNotificationRepository(),
|
|
ownerRepo: nil,
|
|
notifierRegistry: make(map[string]Notifier),
|
|
}
|
|
|
|
issuerRegistry := NewIssuerRegistry(slog.Default())
|
|
renewalSvc := NewRenewalService(
|
|
mockCertRepo,
|
|
mockJobRepo,
|
|
mockPolicyRepo,
|
|
mockProfileRepo,
|
|
mockAuditSvc,
|
|
mockNotifSvc,
|
|
issuerRegistry,
|
|
"agent",
|
|
)
|
|
|
|
// Attempt to check expiring certificates with cancelled context
|
|
err := renewalSvc.CheckExpiringCertificates(ctx)
|
|
|
|
// Should handle cancelled context gracefully
|
|
if err == nil || ctx.Err() == context.Canceled {
|
|
return
|
|
}
|
|
t.Logf("CheckExpiringCertificates with cancelled context returned: %v", err)
|
|
}
|
|
|
|
// mockCertificateProfileRepository is a mock for testing
|
|
type mockCertificateProfileRepository struct {
|
|
Profiles map[string]*domain.CertificateProfile
|
|
GetErr error
|
|
ListErr error
|
|
}
|
|
|
|
func (m *mockCertificateProfileRepository) List(ctx context.Context) ([]*domain.CertificateProfile, error) {
|
|
if m.ListErr != nil {
|
|
return nil, m.ListErr
|
|
}
|
|
var profiles []*domain.CertificateProfile
|
|
for _, p := range m.Profiles {
|
|
profiles = append(profiles, p)
|
|
}
|
|
return profiles, nil
|
|
}
|
|
|
|
func (m *mockCertificateProfileRepository) Get(ctx context.Context, id string) (*domain.CertificateProfile, error) {
|
|
if m.GetErr != nil {
|
|
return nil, m.GetErr
|
|
}
|
|
profile, ok := m.Profiles[id]
|
|
if !ok {
|
|
return nil, errNotFound
|
|
}
|
|
return profile, nil
|
|
}
|
|
|
|
func (m *mockCertificateProfileRepository) Create(ctx context.Context, profile *domain.CertificateProfile) error {
|
|
m.Profiles[profile.ID] = profile
|
|
return nil
|
|
}
|
|
|
|
func (m *mockCertificateProfileRepository) Update(ctx context.Context, profile *domain.CertificateProfile) error {
|
|
m.Profiles[profile.ID] = profile
|
|
return nil
|
|
}
|
|
|
|
func (m *mockCertificateProfileRepository) Delete(ctx context.Context, id string) error {
|
|
delete(m.Profiles, id)
|
|
return nil
|
|
}
|
|
|
|
// TestTargetService_ListWithCancelledContext verifies that target listing respects a cancelled context
|
|
func TestTargetService_ListWithCancelledContext(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel() // Cancel immediately
|
|
|
|
mockTargetRepo := &mockTargetRepo{
|
|
Targets: make(map[string]*domain.DeploymentTarget),
|
|
}
|
|
targetSvc := NewTargetService(mockTargetRepo, nil, nil, "", slog.New(slog.NewTextHandler(os.Stderr, nil)))
|
|
|
|
_, _, err := targetSvc.List(ctx, 1, 50)
|
|
|
|
// Service should handle cancelled context
|
|
if err == nil || ctx.Err() == context.Canceled {
|
|
return
|
|
}
|
|
t.Logf("TargetService.List with cancelled context returned: %v", err)
|
|
}
|
|
|
|
// TestAgentService_HeartbeatWithCancelledContext verifies that heartbeat respects a cancelled context
|
|
func TestAgentService_HeartbeatWithCancelledContext(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel() // Cancel immediately
|
|
|
|
mockAgentRepo := newMockAgentRepository()
|
|
mockAgentRepo.AddAgent(&domain.Agent{
|
|
ID: "agent-1",
|
|
Name: "test-agent",
|
|
Hostname: "localhost",
|
|
})
|
|
|
|
issuerRegistry := NewIssuerRegistry(slog.Default())
|
|
agentSvc := NewAgentService(
|
|
mockAgentRepo,
|
|
nil, // certRepo
|
|
nil, // jobRepo
|
|
nil, // targetRepo
|
|
nil, // auditService
|
|
issuerRegistry,
|
|
nil, // renewalService
|
|
)
|
|
|
|
err := agentSvc.Heartbeat(ctx, "agent-1", &domain.AgentMetadata{})
|
|
|
|
// Service should handle cancelled context
|
|
if err == nil || ctx.Err() == context.Canceled {
|
|
return
|
|
}
|
|
t.Logf("Heartbeat with cancelled context returned: %v", err)
|
|
}
|
|
|
|
// Test with timeout context (should trigger deadline exceeded)
|
|
func TestCertificateService_ListWithDeadlineExceeded(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 0) // Immediate timeout
|
|
defer cancel()
|
|
|
|
mockCertRepo := newMockCertificateRepository()
|
|
certSvc := NewCertificateService(mockCertRepo, nil, nil)
|
|
|
|
time.Sleep(10 * time.Millisecond) // Ensure deadline is exceeded
|
|
|
|
_, _, err := certSvc.List(ctx, &repository.CertificateFilter{})
|
|
|
|
// Should handle deadline exceeded gracefully
|
|
if err == nil || ctx.Err() == context.DeadlineExceeded {
|
|
return
|
|
}
|
|
t.Logf("List with deadline exceeded returned: %v", err)
|
|
}
|
|
|
|
// Test with timeout context on agent heartbeat
|
|
func TestAgentService_HeartbeatWithDeadlineExceeded(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 0) // Immediate timeout
|
|
defer cancel()
|
|
|
|
mockAgentRepo := newMockAgentRepository()
|
|
mockAgentRepo.AddAgent(&domain.Agent{
|
|
ID: "agent-1",
|
|
Name: "test-agent",
|
|
Hostname: "localhost",
|
|
})
|
|
|
|
issuerRegistry := NewIssuerRegistry(slog.Default())
|
|
agentSvc := NewAgentService(
|
|
mockAgentRepo,
|
|
nil, // certRepo
|
|
nil, // jobRepo
|
|
nil, // targetRepo
|
|
nil, // auditService
|
|
issuerRegistry,
|
|
nil, // renewalService
|
|
)
|
|
|
|
time.Sleep(10 * time.Millisecond) // Ensure deadline is exceeded
|
|
|
|
err := agentSvc.Heartbeat(ctx, "agent-1", &domain.AgentMetadata{})
|
|
|
|
// Service should handle deadline exceeded
|
|
if err == nil || ctx.Err() == context.DeadlineExceeded {
|
|
return
|
|
}
|
|
t.Logf("Heartbeat with deadline exceeded returned: %v", err)
|
|
}
|