zabbix-graphql-api/src/test/template_deleter.test.ts
Andreas Hilbig fb5e9cbe81 feat: improve Zabbix multi-version compatibility and introduce local development environment
This update enhances compatibility across multiple Zabbix versions and introduces tools for easier local development and testing.

Key improvements and verified version support:
- Verified Zabbix version support: 6.2, 6.4, 7.0, and 7.4.
- Version-specific feature handling:
  - `history.push` is enabled only for Zabbix 7.0+; older versions skip it with a clear error or notice.
  - Conditional JSON-RPC authentication: the `auth` field is automatically added to the request body for versions older than 6.4.
  - Implemented static Zabbix version caching in the datasource to minimize redundant API calls.
- Query optimization refinements:
  - Added mapping for implied fields (e.g., `state` -> `items`, `deviceType` -> `tags`).
  - Automatically prune unnecessary Zabbix parameters (like `selectItems` or `selectTags`) when not requested.
- Local development environment:
  - Added a new `zabbix-local` Docker Compose profile that includes PostgreSQL, Zabbix Server, and Zabbix Web.
  - Supports testing different versions by passing the `ZABBIX_VERSION` environment variable (e.g., 6.2, 6.4, 7.0, 7.4).
  - Provided a sample environment file at `samples/zabbix-local.env`.
- Documentation and Roadmap:
  - Updated README with a comprehensive version compatibility matrix and local environment instructions.
  - Created a new guide: `docs/howtos/local_development.md`.
  - Updated maintenance guides and added "Local Development Environment" as an achieved milestone in the roadmap.
- Test suite enhancements:
  - Improved Smoketest and RegressionTest executors with more reliable resource cleanup and error reporting.
  - Made tests version-aware to prevent failures on older Zabbix instances.

BREAKING CHANGE: Dropped Zabbix 6.0 specific workarounds; the minimum supported version is now 6.2.
2026-02-04 14:01:53 +01:00

162 lines
5.9 KiB
TypeScript

import {TemplateDeleter} from "../execution/template_deleter.js";
import {zabbixAPI} from "../datasources/zabbix-api.js";
// Mocking ZabbixAPI
jest.mock("../datasources/zabbix-api.js", () => ({
zabbixAPI: {
executeRequest: jest.fn(),
post: jest.fn(),
getVersion: jest.fn().mockResolvedValue("7.0.0"),
}
}));
describe("TemplateDeleter", () => {
beforeEach(() => {
jest.clearAllMocks();
});
test("deleteTemplates - success", async () => {
const templateids = [1, 2];
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["1", "2"] });
const result = await TemplateDeleter.deleteTemplates(templateids, null, "token");
expect(result).toHaveLength(2);
expect(result[0].id).toBe(1);
expect(result[0].message).toContain("deleted successfully");
expect(result[1].id).toBe(2);
expect(zabbixAPI.post).toHaveBeenCalledWith("template.delete", expect.objectContaining({
body: expect.objectContaining({
params: [1, 2]
})
}));
});
test("deleteTemplates - error", async () => {
const templateids = [1];
const zabbixError = {
error: {
code: -32602,
message: "Invalid params.",
data: "Template does not exist"
}
};
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(zabbixError);
const result = await TemplateDeleter.deleteTemplates(templateids, null, "token");
expect(result).toHaveLength(1);
expect(result[0].error).toBeDefined();
expect(result[0].message).toContain("Invalid params.");
expect(result[0].message).toContain("Template does not exist");
});
test("deleteTemplates - by name_pattern", async () => {
// Mock template.get
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([
{ templateid: "10", host: "PatternTemplate 1" },
{ templateid: "11", host: "PatternTemplate 2" }
]);
// Mock template.delete
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["10", "11"] });
const result = await TemplateDeleter.deleteTemplates(null, "PatternTemplate%", "token");
expect(result).toHaveLength(2);
expect(result.map(r => r.id)).toContain(10);
expect(result.map(r => r.id)).toContain(11);
expect(zabbixAPI.post).toHaveBeenCalledWith("template.get", expect.objectContaining({
body: expect.objectContaining({
params: expect.objectContaining({
search: expect.objectContaining({ name: "PatternTemplate%" })
})
})
}));
expect(zabbixAPI.post).toHaveBeenCalledWith("template.delete", expect.objectContaining({
body: expect.objectContaining({
params: expect.arrayContaining([10, 11])
})
}));
});
test("deleteTemplates - merged IDs and name_pattern", async () => {
// Mock template.get
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([
{ templateid: "10", host: "PatternTemplate 1" },
{ templateid: "12", host: "PatternTemplate 3" }
]);
// Mock template.delete
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ templateids: ["10", "11", "12"] });
const result = await TemplateDeleter.deleteTemplates([11], "PatternTemplate%", "token");
expect(result).toHaveLength(3);
expect(result.map(r => r.id)).toContain(10);
expect(result.map(r => r.id)).toContain(11);
expect(result.map(r => r.id)).toContain(12);
expect(zabbixAPI.post).toHaveBeenCalledWith("template.delete", expect.objectContaining({
body: expect.objectContaining({
params: expect.arrayContaining([10, 11, 12])
})
}));
});
test("deleteTemplateGroups - success", async () => {
const groupids = [101];
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ groupids: ["101"] });
const result = await TemplateDeleter.deleteTemplateGroups(groupids, null, "token");
expect(result).toHaveLength(1);
expect(result[0].id).toBe(101);
expect(result[0].message).toContain("deleted successfully");
});
test("deleteTemplateGroups - error", async () => {
const groupids = [101];
const zabbixError = {
error: {
code: -32602,
message: "Invalid params.",
data: "Group is in use"
}
};
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce(zabbixError);
const result = await TemplateDeleter.deleteTemplateGroups(groupids, null, "token");
expect(result).toHaveLength(1);
expect(result[0].error).toBeDefined();
expect(result[0].message).toContain("Group is in use");
});
test("deleteTemplateGroups - by name_pattern", async () => {
// Mock templategroup.get
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce([
{ groupid: "201", name: "PatternGroup 1" }
]);
// Mock templategroup.delete
(zabbixAPI.post as jest.Mock).mockResolvedValueOnce({ groupids: ["201"] });
const result = await TemplateDeleter.deleteTemplateGroups(null, "PatternGroup%", "token");
expect(result).toHaveLength(1);
expect(result[0].id).toBe(201);
expect(zabbixAPI.post).toHaveBeenCalledWith("templategroup.get", expect.objectContaining({
body: expect.objectContaining({
params: expect.objectContaining({
search: expect.objectContaining({ name: "PatternGroup%" })
})
})
}));
expect(zabbixAPI.post).toHaveBeenCalledWith("templategroup.delete", expect.objectContaining({
body: expect.objectContaining({
params: [201]
})
}));
});
});