mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 17:31:30 +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.
306 lines
7.6 KiB
Go
306 lines
7.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/certctl-io/certctl/internal/domain"
|
|
"github.com/certctl-io/certctl/internal/repository"
|
|
)
|
|
|
|
// mockHealthCheckSvc implements HealthCheckServicer for testing.
|
|
type mockHealthCheckSvc struct {
|
|
createErr error
|
|
getErr error
|
|
updateErr error
|
|
deleteErr error
|
|
listErr error
|
|
getHistoryErr error
|
|
acknowledgeErr error
|
|
getSummaryErr error
|
|
checks map[string]*domain.EndpointHealthCheck
|
|
summary *domain.HealthCheckSummary
|
|
}
|
|
|
|
func newMockHealthCheckSvc() *mockHealthCheckSvc {
|
|
return &mockHealthCheckSvc{
|
|
checks: make(map[string]*domain.EndpointHealthCheck),
|
|
summary: &domain.HealthCheckSummary{
|
|
Healthy: 1,
|
|
Degraded: 0,
|
|
Down: 0,
|
|
CertMismatch: 0,
|
|
Unknown: 0,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (m *mockHealthCheckSvc) Create(ctx context.Context, check *domain.EndpointHealthCheck) error {
|
|
if m.createErr != nil {
|
|
return m.createErr
|
|
}
|
|
check.ID = "hc-created-1"
|
|
m.checks[check.ID] = check
|
|
return nil
|
|
}
|
|
|
|
func (m *mockHealthCheckSvc) Get(ctx context.Context, id string) (*domain.EndpointHealthCheck, error) {
|
|
if m.getErr != nil {
|
|
return nil, m.getErr
|
|
}
|
|
if check, ok := m.checks[id]; ok {
|
|
return check, nil
|
|
}
|
|
return nil, errors.New("not found")
|
|
}
|
|
|
|
func (m *mockHealthCheckSvc) Update(ctx context.Context, check *domain.EndpointHealthCheck) error {
|
|
if m.updateErr != nil {
|
|
return m.updateErr
|
|
}
|
|
m.checks[check.ID] = check
|
|
return nil
|
|
}
|
|
|
|
func (m *mockHealthCheckSvc) Delete(ctx context.Context, id string) error {
|
|
if m.deleteErr != nil {
|
|
return m.deleteErr
|
|
}
|
|
delete(m.checks, id)
|
|
return nil
|
|
}
|
|
|
|
func (m *mockHealthCheckSvc) List(ctx context.Context, filter *repository.HealthCheckFilter) ([]*domain.EndpointHealthCheck, int, error) {
|
|
if m.listErr != nil {
|
|
return nil, 0, m.listErr
|
|
}
|
|
checks := make([]*domain.EndpointHealthCheck, 0, len(m.checks))
|
|
for _, check := range m.checks {
|
|
checks = append(checks, check)
|
|
}
|
|
return checks, len(checks), nil
|
|
}
|
|
|
|
func (m *mockHealthCheckSvc) GetHistory(ctx context.Context, healthCheckID string, limit int) ([]*domain.HealthHistoryEntry, error) {
|
|
if m.getHistoryErr != nil {
|
|
return nil, m.getHistoryErr
|
|
}
|
|
return make([]*domain.HealthHistoryEntry, 0), nil
|
|
}
|
|
|
|
func (m *mockHealthCheckSvc) AcknowledgeIncident(ctx context.Context, id string, actor string) error {
|
|
if m.acknowledgeErr != nil {
|
|
return m.acknowledgeErr
|
|
}
|
|
if check, ok := m.checks[id]; ok {
|
|
check.Acknowledged = true
|
|
check.AcknowledgedBy = actor
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *mockHealthCheckSvc) GetSummary(ctx context.Context) (*domain.HealthCheckSummary, error) {
|
|
if m.getSummaryErr != nil {
|
|
return nil, m.getSummaryErr
|
|
}
|
|
return m.summary, nil
|
|
}
|
|
|
|
// Tests
|
|
|
|
func TestListHealthChecks_Success(t *testing.T) {
|
|
svc := newMockHealthCheckSvc()
|
|
svc.checks["hc-1"] = &domain.EndpointHealthCheck{
|
|
ID: "hc-1",
|
|
Endpoint: "api.example.com:443",
|
|
Status: domain.HealthStatusHealthy,
|
|
}
|
|
handler := NewHealthCheckHandler(svc)
|
|
|
|
req := httptest.NewRequest("GET", "/api/v1/health-checks", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.ListHealthChecks(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("Expected status 200, got %d", w.Code)
|
|
}
|
|
|
|
var resp PagedResponse
|
|
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("Failed to decode response: %v", err)
|
|
}
|
|
|
|
if resp.Total != 1 {
|
|
t.Errorf("Expected 1 health check, got %d", resp.Total)
|
|
}
|
|
}
|
|
|
|
func TestListHealthChecks_MethodNotAllowed(t *testing.T) {
|
|
handler := NewHealthCheckHandler(newMockHealthCheckSvc())
|
|
|
|
req := httptest.NewRequest("POST", "/api/v1/health-checks", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.ListHealthChecks(w, req)
|
|
|
|
if w.Code != http.StatusMethodNotAllowed {
|
|
t.Errorf("Expected status 405, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestGetHealthCheck_Success(t *testing.T) {
|
|
svc := newMockHealthCheckSvc()
|
|
check := &domain.EndpointHealthCheck{
|
|
ID: "hc-1",
|
|
Endpoint: "api.example.com:443",
|
|
Status: domain.HealthStatusHealthy,
|
|
}
|
|
svc.checks["hc-1"] = check
|
|
handler := NewHealthCheckHandler(svc)
|
|
|
|
req := httptest.NewRequest("GET", "/api/v1/health-checks/hc-1", nil)
|
|
req.SetPathValue("id", "hc-1")
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.GetHealthCheck(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("Expected status 200, got %d", w.Code)
|
|
}
|
|
|
|
var resp domain.EndpointHealthCheck
|
|
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("Failed to decode response: %v", err)
|
|
}
|
|
|
|
if resp.ID != "hc-1" {
|
|
t.Errorf("Expected ID hc-1, got %s", resp.ID)
|
|
}
|
|
}
|
|
|
|
func TestGetHealthCheck_NotFound(t *testing.T) {
|
|
handler := NewHealthCheckHandler(newMockHealthCheckSvc())
|
|
|
|
req := httptest.NewRequest("GET", "/api/v1/health-checks/nonexistent", nil)
|
|
req.SetPathValue("id", "nonexistent")
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.GetHealthCheck(w, req)
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
t.Errorf("Expected status 404, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestCreateHealthCheck_Success(t *testing.T) {
|
|
svc := newMockHealthCheckSvc()
|
|
handler := NewHealthCheckHandler(svc)
|
|
|
|
check := domain.EndpointHealthCheck{
|
|
Endpoint: "web.example.com:443",
|
|
Enabled: true,
|
|
}
|
|
body, _ := json.Marshal(check)
|
|
|
|
req := httptest.NewRequest("POST", "/api/v1/health-checks", bytes.NewReader(body))
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.CreateHealthCheck(w, req)
|
|
|
|
if w.Code != http.StatusCreated {
|
|
t.Errorf("Expected status 201, got %d", w.Code)
|
|
}
|
|
|
|
var resp domain.EndpointHealthCheck
|
|
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("Failed to decode response: %v", err)
|
|
}
|
|
|
|
if resp.Endpoint != "web.example.com:443" {
|
|
t.Errorf("Expected endpoint web.example.com:443, got %s", resp.Endpoint)
|
|
}
|
|
}
|
|
|
|
func TestDeleteHealthCheck_Success(t *testing.T) {
|
|
svc := newMockHealthCheckSvc()
|
|
svc.checks["hc-1"] = &domain.EndpointHealthCheck{
|
|
ID: "hc-1",
|
|
Endpoint: "api.example.com:443",
|
|
}
|
|
handler := NewHealthCheckHandler(svc)
|
|
|
|
req := httptest.NewRequest("DELETE", "/api/v1/health-checks/hc-1", nil)
|
|
req.SetPathValue("id", "hc-1")
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.DeleteHealthCheck(w, req)
|
|
|
|
if w.Code != http.StatusNoContent {
|
|
t.Errorf("Expected status 204, got %d", w.Code)
|
|
}
|
|
|
|
if _, ok := svc.checks["hc-1"]; ok {
|
|
t.Fatal("Expected check to be deleted")
|
|
}
|
|
}
|
|
|
|
func TestAcknowledgeHealthCheck_Success(t *testing.T) {
|
|
svc := newMockHealthCheckSvc()
|
|
svc.checks["hc-1"] = &domain.EndpointHealthCheck{
|
|
ID: "hc-1",
|
|
Endpoint: "api.example.com:443",
|
|
Status: domain.HealthStatusDown,
|
|
}
|
|
handler := NewHealthCheckHandler(svc)
|
|
|
|
req := httptest.NewRequest("POST", "/api/v1/health-checks/hc-1/acknowledge", bytes.NewReader([]byte(`{"actor":"user@example.com"}`)))
|
|
req.SetPathValue("id", "hc-1")
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.AcknowledgeHealthCheck(w, req)
|
|
|
|
if w.Code != http.StatusNoContent {
|
|
t.Errorf("Expected status 204, got %d", w.Code)
|
|
}
|
|
|
|
if !svc.checks["hc-1"].Acknowledged {
|
|
t.Fatal("Expected check to be acknowledged")
|
|
}
|
|
}
|
|
|
|
func TestGetHealthCheckSummary_Success(t *testing.T) {
|
|
svc := newMockHealthCheckSvc()
|
|
svc.summary = &domain.HealthCheckSummary{
|
|
Healthy: 3,
|
|
Degraded: 1,
|
|
Down: 0,
|
|
CertMismatch: 0,
|
|
Unknown: 1,
|
|
}
|
|
handler := NewHealthCheckHandler(svc)
|
|
|
|
req := httptest.NewRequest("GET", "/api/v1/health-checks/summary", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
handler.GetHealthCheckSummary(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("Expected status 200, got %d", w.Code)
|
|
}
|
|
|
|
var resp domain.HealthCheckSummary
|
|
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("Failed to decode response: %v", err)
|
|
}
|
|
|
|
if resp.Healthy != 3 {
|
|
t.Errorf("Expected 3 healthy checks, got %d", resp.Healthy)
|
|
}
|
|
}
|