Files
certctl/internal/service/certificate_round_out_test.go
T
shankar0123 8b75e0311b 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 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.
2026-05-04 00:30:29 +00:00

196 lines
6.3 KiB
Go

package service
import (
"context"
"errors"
"strings"
"testing"
"github.com/certctl-io/certctl/internal/domain"
)
// Bundle N.C-extended: service-layer round-out (70.5% → ≥80%).
// Targets the previously-uncovered handler-interface methods on
// CertificateService that delegate to the repo: GetCertificate,
// CreateCertificate, UpdateCertificate, ArchiveCertificate,
// GetCertificateVersions, SetJobRepo, SetKeygenMode,
// ListCertificatesWithFilter, TriggerDeployment.
func newTestCertSvc(t *testing.T) (*CertificateService, *mockCertRepo) {
t.Helper()
certRepo := &mockCertRepo{
Certs: make(map[string]*domain.ManagedCertificate),
Versions: make(map[string][]*domain.CertificateVersion),
}
auditRepo := &mockAuditRepo{}
auditService := NewAuditService(auditRepo)
svc := NewCertificateService(certRepo, nil, auditService)
return svc, certRepo
}
func TestCertificateService_GetCertificate_DelegatesToRepo(t *testing.T) {
svc, repo := newTestCertSvc(t)
repo.Certs["mc-1"] = &domain.ManagedCertificate{ID: "mc-1", Name: "x"}
got, err := svc.GetCertificate(context.Background(), "mc-1")
if err != nil {
t.Fatalf("GetCertificate: %v", err)
}
if got == nil || got.ID != "mc-1" {
t.Errorf("expected mc-1, got %+v", got)
}
}
func TestCertificateService_GetCertificate_NotFound(t *testing.T) {
svc, _ := newTestCertSvc(t)
_, err := svc.GetCertificate(context.Background(), "missing")
if err == nil {
t.Errorf("expected NotFound error")
}
}
func TestCertificateService_CreateCertificate_PopulatesDefaults(t *testing.T) {
svc, _ := newTestCertSvc(t)
cert := domain.ManagedCertificate{Name: "no-id-no-status"}
got, err := svc.CreateCertificate(context.Background(), cert)
if err != nil {
t.Fatalf("CreateCertificate: %v", err)
}
if got.ID == "" {
t.Errorf("expected ID populated, got empty")
}
if got.Status == "" {
t.Errorf("expected default status populated")
}
if got.Tags == nil {
t.Errorf("expected Tags initialized to non-nil map")
}
if got.CreatedAt.IsZero() {
t.Errorf("expected CreatedAt populated")
}
}
func TestCertificateService_CreateCertificate_RepoError(t *testing.T) {
svc, repo := newTestCertSvc(t)
repo.CreateErr = errors.New("db down")
_, err := svc.CreateCertificate(context.Background(), domain.ManagedCertificate{ID: "mc-x", Name: "x"})
if err == nil || !strings.Contains(err.Error(), "failed to create") {
t.Errorf("expected create-error wrapper, got %v", err)
}
}
func TestCertificateService_UpdateCertificate_MergesPatch(t *testing.T) {
svc, repo := newTestCertSvc(t)
repo.Certs["mc-u"] = &domain.ManagedCertificate{
ID: "mc-u",
Name: "old",
CommonName: "old.example.com",
Environment: "staging",
}
patch := domain.ManagedCertificate{
Name: "new",
CommonName: "new.example.com",
Environment: "prod",
SANs: []string{"new.example.com"},
OwnerID: "o-alice",
TeamID: "t-platform",
IssuerID: "iss-le",
}
got, err := svc.UpdateCertificate(context.Background(), "mc-u", patch)
if err != nil {
t.Fatalf("UpdateCertificate: %v", err)
}
if got.Name != "new" || got.CommonName != "new.example.com" || got.Environment != "prod" {
t.Errorf("expected merged fields, got %+v", got)
}
if got.OwnerID != "o-alice" || got.TeamID != "t-platform" {
t.Errorf("expected owner/team merged, got %s/%s", got.OwnerID, got.TeamID)
}
}
func TestCertificateService_UpdateCertificate_NotFound(t *testing.T) {
svc, _ := newTestCertSvc(t)
_, err := svc.UpdateCertificate(context.Background(), "missing", domain.ManagedCertificate{Name: "x"})
if err == nil || !strings.Contains(err.Error(), "not found") {
t.Errorf("expected NotFound error, got %v", err)
}
}
func TestCertificateService_UpdateCertificate_RepoUpdateError(t *testing.T) {
svc, repo := newTestCertSvc(t)
repo.Certs["mc-u"] = &domain.ManagedCertificate{ID: "mc-u", Name: "old"}
repo.UpdateErr = errors.New("constraint violation")
_, err := svc.UpdateCertificate(context.Background(), "mc-u", domain.ManagedCertificate{Name: "new"})
if err == nil || !strings.Contains(err.Error(), "failed to update") {
t.Errorf("expected update-error wrapper, got %v", err)
}
}
func TestCertificateService_ArchiveCertificate_DelegatesToRepo(t *testing.T) {
svc, repo := newTestCertSvc(t)
repo.Certs["mc-a"] = &domain.ManagedCertificate{ID: "mc-a"}
if err := svc.ArchiveCertificate(context.Background(), "mc-a"); err != nil {
t.Errorf("ArchiveCertificate: %v", err)
}
}
func TestCertificateService_ArchiveCertificate_RepoError(t *testing.T) {
svc, repo := newTestCertSvc(t)
repo.ArchiveErr = errors.New("archive fail")
if err := svc.ArchiveCertificate(context.Background(), "mc-a"); err == nil {
t.Errorf("expected archive error to propagate")
}
}
func TestCertificateService_GetCertificateVersions_PaginationDefaults(t *testing.T) {
svc, repo := newTestCertSvc(t)
versions := []*domain.CertificateVersion{
{SerialNumber: "01"}, {SerialNumber: "02"}, {SerialNumber: "03"},
}
repo.ListVersionsResult = versions
repo.Versions["mc-v"] = versions
got, total, err := svc.GetCertificateVersions(context.Background(), "mc-v", 0, 0)
if err != nil {
t.Fatalf("GetCertificateVersions: %v", err)
}
if total != 3 {
t.Errorf("expected total=3, got %d", total)
}
if len(got) != 3 {
t.Errorf("expected 3 versions returned, got %d", len(got))
}
}
func TestCertificateService_GetCertificateVersions_PageOutOfRange(t *testing.T) {
svc, repo := newTestCertSvc(t)
repo.ListVersionsResult = []*domain.CertificateVersion{{SerialNumber: "01"}}
got, total, err := svc.GetCertificateVersions(context.Background(), "mc-v", 99, 50)
if err != nil {
t.Fatalf("GetCertificateVersions: %v", err)
}
if total != 1 {
t.Errorf("expected total=1, got %d", total)
}
if len(got) != 0 {
t.Errorf("expected 0 results for out-of-range page, got %d", len(got))
}
}
func TestCertificateService_GetCertificateVersions_RepoError(t *testing.T) {
svc, repo := newTestCertSvc(t)
repo.ListVersionsErr = errors.New("list down")
_, _, err := svc.GetCertificateVersions(context.Background(), "mc-v", 1, 50)
if err == nil {
t.Errorf("expected versions-list error to propagate")
}
}
func TestCertificateService_SetJobRepo_SetKeygenMode_NoCrash(t *testing.T) {
svc, _ := newTestCertSvc(t)
// SetJobRepo accepts a repo (or nil) — confirm no panic.
svc.SetJobRepo(nil)
svc.SetKeygenMode("agent")
svc.SetKeygenMode("server")
}