Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

test(hcc): pin writes_total to successful replace only

Alfredo Lopezdea2c209e1b5
diff --git a/host-context-controller/package-lock.json b/host-context-controller/package-lock.json
index eacc27b90..efe01cbef 100644
--- a/host-context-controller/package-lock.json
+++ b/host-context-controller/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-host-context-controller",
- "version": "0.1.267",
+ "version": "0.1.268",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-host-context-controller",
- "version": "0.1.267",
+ "version": "0.1.268",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@clerum/image-policy": "file:../packages/image-policy",
diff --git a/host-context-controller/package.json b/host-context-controller/package.json
index 80eff8d53..f3ec65077 100644
--- a/host-context-controller/package.json
+++ b/host-context-controller/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-host-context-controller",
- "version": "0.1.267",
+ "version": "0.1.268",
"description": "Host Context Controller - provides McpServer CRDs to mcp-host via REST API",
"main": "dist/main.js",
"engines": {
diff --git a/host-context-controller/src/__tests__/replaceWithConflictRetryOrder.test.ts b/host-context-controller/src/__tests__/replaceWithConflictRetryOrder.test.ts
index 112255b4a..b6c64d8b3 100644
--- a/host-context-controller/src/__tests__/replaceWithConflictRetryOrder.test.ts
+++ b/host-context-controller/src/__tests__/replaceWithConflictRetryOrder.test.ts
@@ -347,4 +347,103 @@ describe('replaceWithConflictRetry order and retry', () => {
expect(replace).not.toHaveBeenCalled()
expect(await readWritesTotal('Service')).toBe(before)
})
+
+ it('WRITE-3: 409 then successful replace increments once, not per attempt', async () => {
+ const firstRead: k8s.V1Service = {
+ ...existing,
+ metadata: { ...existing.metadata, resourceVersion: '9' },
+ }
+ const secondRead: k8s.V1Service = {
+ ...existing,
+ metadata: { ...existing.metadata, resourceVersion: '10' },
+ }
+ const read = vi.fn().mockResolvedValueOnce(firstRead).mockResolvedValueOnce(secondRead)
+ const replace = vi.fn().mockRejectedValueOnce({ code: 409 }).mockResolvedValueOnce({})
+ const before = await readWritesTotal('Service')
+ const log = vi.spyOn(console, 'log').mockImplementation(() => {})
+
+ await replaceWithConflictRetry({
+ description: 'Service "svc"',
+ logPrefix: '[Test]',
+ body: desired,
+ mergeExisting: preserveServiceAssignedFields,
+ isUpToDate: () => false,
+ read,
+ replace,
+ })
+
+ expect(replace).toHaveBeenCalledTimes(2)
+ expect(await readWritesTotal('Service')).toBe(before + 1)
+ log.mockRestore()
+ })
+
+ it('WRITE-4: successful replace without kind increments {kind=unknown}', async () => {
+ const body = { metadata: { name: 'svc', namespace: 'ns' } }
+ const live = { metadata: { name: 'svc', namespace: 'ns', resourceVersion: '9' } }
+ const before = await readWritesTotal('unknown')
+ const log = vi.spyOn(console, 'log').mockImplementation(() => {})
+
+ await replaceWithConflictRetry({
+ description: 'object "svc"',
+ logPrefix: '[Test]',
+ body,
+ isUpToDate: () => false,
+ read: async () => live,
+ replace: async () => ({}),
+ })
+
+ expect(await readWritesTotal('unknown')).toBe(before + 1)
+ log.mockRestore()
+ })
+
+ it('WRITE-5: 409 then isUpToDate skip does not increment', async () => {
+ const drifted: k8s.V1Service = {
+ ...existing,
+ metadata: { ...existing.metadata, resourceVersion: '9' },
+ spec: {
+ ...existing.spec,
+ ports: [{ name: 'http', port: 9090, protocol: 'TCP' }],
+ },
+ }
+ const converged: k8s.V1Service = {
+ ...existing,
+ metadata: { ...existing.metadata, resourceVersion: '10' },
+ }
+ const read = vi.fn().mockResolvedValueOnce(drifted).mockResolvedValueOnce(converged)
+ const replace = vi.fn().mockRejectedValueOnce({ code: 409 })
+ const before = await readWritesTotal('Service')
+
+ await replaceWithConflictRetry({
+ description: 'Service "svc"',
+ logPrefix: '[Test]',
+ body: desired,
+ mergeExisting: preserveServiceAssignedFields,
+ isUpToDate: serviceMatchesDesired,
+ read,
+ replace,
+ })
+
+ expect(replace).toHaveBeenCalledOnce()
+ expect(await readWritesTotal('Service')).toBe(before)
+ })
+
+ it('WRITE-6: failed replace does not increment', async () => {
+ const replace = vi.fn().mockRejectedValue(apiException(404))
+ const before = await readWritesTotal('Service')
+
+ await expect(
+ replaceWithConflictRetry({
+ description: 'Service "svc"',
+ logPrefix: '[Test]',
+ body: desired,
+ mergeExisting: preserveServiceAssignedFields,
+ isUpToDate: () => false,
+ read: async () => existing,
+ replace,
+ })
+ ).rejects.toBeInstanceOf(ApiException)
+
+ expect(replace).toHaveBeenCalledOnce()
+ expect(await readWritesTotal('Service')).toBe(before)
+ })
})
diff --git a/host-context-controller/src/networkPolicyReconciler.egressFailStatic.test.ts b/host-context-controller/src/networkPolicyReconciler.egressFailStatic.test.ts
index d8f543184..3d8e358a6 100644
--- a/host-context-controller/src/networkPolicyReconciler.egressFailStatic.test.ts
+++ b/host-context-controller/src/networkPolicyReconciler.egressFailStatic.test.ts
@@ -48,6 +48,7 @@ vi.mock('node:dns/promises', () => ({
vi.mock('./metrics', () => ({
networkPolicySafetyPassDurationSeconds: { observe: vi.fn() },
networkPolicySafetyPassPoliciesTotal: { inc: vi.fn() },
+ writesTotal: { inc: vi.fn() },
}))
function makeMockNetworkingApi() {
diff --git a/host-context-controller/src/networkPolicyReconciler.orphanSweep.test.ts b/host-context-controller/src/networkPolicyReconciler.orphanSweep.test.ts
index 6497cc2ae..2939d003f 100644
--- a/host-context-controller/src/networkPolicyReconciler.orphanSweep.test.ts
+++ b/host-context-controller/src/networkPolicyReconciler.orphanSweep.test.ts
@@ -36,6 +36,7 @@ vi.mock('./metrics', () => ({
networkPolicySafetyPassPoliciesTotal: { inc: vi.fn() },
netPolOrphansDeletedTotal: { inc: vi.fn() },
netPolOrphanSweepCappedTotal: { inc: vi.fn() },
+ writesTotal: { inc: vi.fn() },
}))
function makeMockNetworkingApi() {
return {
diff --git a/host-context-controller/src/networkPolicyReconciler.test.ts b/host-context-controller/src/networkPolicyReconciler.test.ts
index 6ba7d141a..df8edaf18 100644
--- a/host-context-controller/src/networkPolicyReconciler.test.ts
+++ b/host-context-controller/src/networkPolicyReconciler.test.ts
@@ -79,6 +79,7 @@ vi.mock('./metrics', () => ({
networkPolicySafetyPassPoliciesTotal: { inc: vi.fn() },
netPolOrphansDeletedTotal: { inc: vi.fn() },
netPolOrphanSweepCappedTotal: { inc: vi.fn() },
+ writesTotal: { inc: vi.fn() },
}))
function makeMockNetworkingApi() {