mirror of
https://github.com/openobserve/openobserve.git
synced 2026-11-05 08:09:22 +00:00
## Summary
Comprehensive rework of the `tests/api-testing/` suite. 30+ commits
implementing all 6 phases of the [revamp
plan](../../tests/ui-testing/MD_Files/api%20tests%20revamp/API_TESTS_REVAMP.md).
**Before → After:**
| | Baseline | Final | Δ |
|---|---|---|---|
| passed | 421 | **443** | **+22 ✓** |
| failed | 3 | **2** | -1 (only same pre-existing) |
| skipped | 53 | 39 | -14 (unskipped dead tests now run) |
| xfailed | 1 | 2 | +1 (new visible backend-bug signal) |
| runtime | 588s | **387s** | **-201s (34% faster)** |
**Zero tests that passed in baseline are now failing.** The 2 remaining
failures are the exact pre-existing ones
(`test_enrichment_table_join.py` backend panic + `test_workflow.py` SMTP
env-miss).
## What's in the diff
- **New `support/` framework** — `OpenObserveClient`, endpoint wrappers
(`alerts`, `dashboards`, `streams`, `search`, etc.), `wait_until()`,
payload factories, pytest fixtures, SSE parser. 14 framework selfcheck
tests.
- **16 of 16 OLD-tier test files rewritten** — using the new framework,
unique-suffix names, body validation, `wait_until` instead of
`time.sleep`, parametrize where appropriate.
- **29 files moved into 12 feature folders** mirroring the UI suite
layout
(`tests/{alerts,dashboards,ingest,orgs,pipelines,regression,rum,search,streams,users,workflow}/`).
- **−6,500 LOC net** (copy-paste + dead code removal; new framework adds
back ~1,500).
- **Ruff config** in pyproject.toml: T201 ban on `print()`, PT pytest
rules, B bugbear, UP pyupgrade.
## CI workflow changes
- New job `api_regression_tests`: `strategy.matrix` over the 4 env-flag
combinations (UTF8_VIEW × JOIN_MATCH_ONE) — now runs in parallel instead
of as 4 sequential server reboots in the main job.
- New job `api_fuzz_tests`: schemathesis OpenAPI-driven fuzz against
`/api-doc/openapi.json` (214 endpoints tested). Informational
(`continue-on-error: true`). Each run surfaces **7–10 distinct 5xx
panics** (e.g., empty `sql` body returning 500 instead of 400, traces
endpoints crashing on unescaped path params that get interpolated into
SQL). Filed to backend team for separate triage.
- Server `o2_*.log` files uploaded as artifacts on failure (was:
silently lost).
- `api_tests_summary` now requires BOTH integration + regression to
pass.
### CI infrastructure fixes
A short hardening pass landed after the initial CI run surfaced several
issues:
- **pytest-sugar count inflation** — dropped `--force-sugar` from both
pytest invocations. The flag double-registered sugar on top of the
auto-loaded entry point, inflating the CI test counter ~3x (display
showed 1364 when collection was 486). Counter is now honest.
- **schemathesis 4.x flag migration** — `--base-url` → `-u`,
`--hypothesis-max-examples` → `-n`, `--hypothesis-deadline` →
`--request-timeout`, `--data-generation-method` → `-m`, dropped
`--auth-type basic`. The fuzz job was failing immediately on flag-parse
before this.
- **Regression matrix binary path** — used `${{ github.workspace
}}/target/debug/openobserve` so the absolute path resolves correctly
under `working-directory: tests/api-testing/` (was: every matrix job
failed with "No such file or directory").
- **`permissions: contents: read`** added to all 4 workflow jobs —
closes CodeQL alert #369.
- **Removed redundant server boot** in `api_integration_tests` (leftover
from main's structure after the regression matrix split; was silently
failing to bind to :5080 and getting masked by the first server).
## Backend bugs surfaced (for backend team to track)
The rework uncovered and visibly flagged these as `xfail`/documented
test comments:
1. `DELETE /api/organizations/{id}` returns 404 unconditionally even for
just-created orgs — endpoint appears unimplemented.
2. `test_dashboard_missing_required_fields` triggers backend panic
(returns 500 instead of 400/422).
3. JS `getEnrichmentTableData()` restricted to `_meta` org AND not
exposed in the `/functions/test` endpoint runtime — `ReferenceError`
even with explicit `enrichment_tables: [...]`.
4. Bulk API silently sanitizes invalid `_index` values (`****` → `_`)
without flagging — user data lands in unexpected index.
5. `AVG` of a single value drifts by ~1e-14 (likely float reduction
order; documented tolerance in `test_advanced_sql.py`).
6. Org name validation rejects hyphens (only
`A-Z/a-z/0-9/spaces/underscores`) — not in spec.
Plus 7–10 additional 5xx panics surfaced per fuzz run (input validation
gaps — separate triage list in fuzz job logs).
## Test bugs uncovered + fixed
- `test_pipeline.py`: had two function definitions named
`test_pipeline_creation_and_action_with_schedule` — Python's last-wins
meant the first test's body never executed for years.
- `test_alertsV2.py`: `assert alert_disabled == "False"` was indented
inside an `else:` branch that never executed because the response is
always a dict; AND `enabled` is a bool, not the string `"False"`. Two
layers of broken.
- `test_bulk.py`: variable typo (`valid_bulk_payload` reused on the POST
call) made the invalid-index test silently duplicate the valid case.
- `test_organisations.py`: `test_create_organization` used pre-pytest-4
`yield`-style fixtures — silently ignored by pytest 8.
- `test_js_result_array.py`: 8 tests skipped with `"Temporarily disabled
- failing test"` — the actual cause was that JS functions are restricted
to `_meta` org. One-line fix unskipped all 8.
- `test_fuzzy.py`: 4 tests `@pytest.mark.skip`-ed because they used a
1-minute time window against older static test data → 0 hits → flake →
skip. Wider window unskips them.
- 547 LOC of zombie commented-out test blocks deleted (15 commented `#
def test_` definitions, including one orphan case where the `def` was
commented but the indented body wasn't).
## Folder layout
```
tests/api-testing/
├── support/ # NEW framework
│ ├── client.py # OpenObserveClient
│ ├── endpoints/ # 7 per-resource wrappers
│ ├── factories.py # unique_name, time_window, *_payload
│ ├── wait.py # wait_until + WaitTimeout
│ ├── sse.py # read_sse_response
│ └── fixtures.py # client, temp_user_email, temp_stream_name, ...
├── helpers/ # Non-test shared code (was scattered in tests/)
│ ├── sourcemap_helpers.py
│ └── workflow/ # workflow chain + pages/ live here now
├── fixtures/
│ └── rum/ # Standalone RUM ingest scripts (was in tests/)
└── tests/
├── alerts/ # 3 files
├── dashboards/ # 4 files
├── ingest/ # 4 files
├── orgs/ # 2 files
├── pipelines/ # 10 files
├── regression/ # 2 files (env-flag matrix targets)
├── rum/ # 3 files
├── search/ # 8 files
├── streams/ # 1 file
├── users/ # 1 file
├── workflow/ # 3 files
├── conftest.py
└── test_framework_selfcheck.py
```
## Test plan
- [x] `rye run pytest --collect-only` — 486 tests discovered
- [x] Full default-set run (`--ignore=tests/regression`) — 443 pass / 2
fail / 39 skip / 2 xfail in 387s
- [x] `rye run pytest -m framework` — 14/14 selfcheck tests pass
- [x] `rye run ruff check support/ tests/test_framework_selfcheck.py` —
clean
- [x] CI run on this PR — `api_integration_tests` + all 4
`api_regression_tests` matrix jobs green
- [x] CI run — `api_fuzz_tests` runs cleanly (informational; surfaces
7–10 unique 5xx panics per run)
## Note on test count
The `test_streaming.py` rewrite folded 7 "tests" (`test_ingest_data`,
`test_ingest_join`, `test_disable_streaming`,
`test_update_max_query_range`, `test_enable_streaming`,
`test_delete_stream`, `test_delete_stream_join`) into pytest fixtures.
Each only checked `status_code == 200` of a setup/teardown operation —
they were imperative steps masquerading as tests. The fixtures still
perform the same operations and assert their success; they're just no
longer counted as discrete pytest items. **Functional coverage is
identical.**
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
764 lines
32 KiB
Python
764 lines
32 KiB
Python
import random
|
|
import uuid
|
|
from requests.auth import HTTPBasicAuth
|
|
import time
|
|
|
|
class AlertPage:
|
|
# Make Unique_value_alert a class variable
|
|
Unique_value_alert = f"d4m21_{random.randint(100000, 999999)}"
|
|
|
|
def __init__(self, session, base_url, org_id):
|
|
self.session = session
|
|
self.base_url = base_url
|
|
self.org_id = org_id
|
|
|
|
def create_standard_alert(self, session, base_url, user_email, user_password, org_id, stream_name, template_name, destination_name, alert_name):
|
|
headers = {"Content-Type": "application/json", "Custom-Header": "value"}
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
payload = {
|
|
"name": alert_name,
|
|
"row_template": template_name,
|
|
"stream_type": "logs",
|
|
"stream_name": stream_name,
|
|
"is_real_time": False,
|
|
"context_attributes": {},
|
|
"query_condition": {
|
|
"conditions": [ {
|
|
"column": "log",
|
|
"operator": "=",
|
|
"value": "200",
|
|
"type": None, # Set it to None or remove it
|
|
"id": str(uuid.uuid4())
|
|
} ],
|
|
"search_event_type": "ui",
|
|
"sql": "",
|
|
"promql": "",
|
|
"type": "custom",
|
|
"promql_condition": None,
|
|
"vrl_function": None,
|
|
"multi_time_range": []
|
|
},
|
|
"trigger_condition": {
|
|
"period": 10,
|
|
"operator": ">=",
|
|
"frequency": 3,
|
|
"cron": "",
|
|
"threshold": 3,
|
|
"silence": 10,
|
|
"frequency_type": "minutes",
|
|
"timezone": "UTC",
|
|
"tolerance_in_secs": 0
|
|
},
|
|
"org_id": org_id,
|
|
"destinations": [destination_name],
|
|
"enabled": True,
|
|
"description": "test"
|
|
}
|
|
|
|
response = session.post(f"{base_url}api/v2/{org_id}/alerts?type=logs", json=payload)
|
|
if response.status_code == 409:
|
|
# Handle alert already exists scenario
|
|
print(f"Alert already exists: {payload['name']}.")
|
|
return False # Indicate that creation failed due to existing alert
|
|
assert response.status_code == 200, f"Failed to create alert: {response.content}"
|
|
return True # Indicate successful creation
|
|
|
|
def create_standard_alert_sql(self, session, base_url, user_email, user_password, org_id, stream_name, template_name, destination_name, alert_name):
|
|
headers = {"Content-Type": "application/json", "Custom-Header": "value"}
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
payload = {
|
|
"name": alert_name,
|
|
"row_template": template_name,
|
|
"stream_type": "logs",
|
|
"stream_name": stream_name,
|
|
"is_real_time": False,
|
|
"context_attributes": {},
|
|
"query_condition": {
|
|
"conditions": [ {
|
|
"column": "log",
|
|
"operator": "=",
|
|
"value": "200",
|
|
"type": None, # Set it to None or remove it
|
|
"id": str(uuid.uuid4())
|
|
} ],
|
|
"search_event_type": "ui",
|
|
"sql": f"select log from \"{stream_name}\" where log = 200",
|
|
"promql": "",
|
|
"type": "sql",
|
|
"promql_condition": None,
|
|
"vrl_function": None,
|
|
"multi_time_range": []
|
|
},
|
|
"trigger_condition": {
|
|
"period": 10,
|
|
"operator": ">=",
|
|
"frequency": 3,
|
|
"cron": "",
|
|
"threshold": 3,
|
|
"silence": 10,
|
|
"frequency_type": "minutes",
|
|
"timezone": "UTC",
|
|
"tolerance_in_secs": 0
|
|
},
|
|
"org_id": org_id,
|
|
"destinations": [destination_name],
|
|
"enabled": True,
|
|
"description": "test"
|
|
}
|
|
|
|
response = session.post(f"{base_url}api/v2/{org_id}/alerts?type=logs", json=payload)
|
|
if response.status_code == 409:
|
|
# Handle alert already exists scenario
|
|
print(f"Alert already exists: {payload['name']}.")
|
|
return False
|
|
assert response.status_code == 200, f"Failed to create alert: {response.content}"
|
|
return True
|
|
|
|
def create_real_time_alert(self, session, base_url, user_email, user_password, org_id, stream_name, template_name, destination_name, alert_name):
|
|
headers = {"Content-Type": "application/json", "Custom-Header": "value"}
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
payload = {
|
|
"name": alert_name,
|
|
"row_template": template_name,
|
|
"stream_type": "logs",
|
|
"stream_name": stream_name,
|
|
"is_real_time": True,
|
|
"context_attributes": {},
|
|
"query_condition": {
|
|
"conditions": [ {
|
|
"column": "log",
|
|
"operator": "=",
|
|
"value": "200",
|
|
"type": None, # Set it to None or remove it
|
|
"id": str(uuid.uuid4())
|
|
} ],
|
|
"search_event_type": "ui",
|
|
"sql": "",
|
|
"promql": "",
|
|
"type": "custom",
|
|
"promql_condition": None,
|
|
"vrl_function": None,
|
|
"multi_time_range": []
|
|
},
|
|
"trigger_condition": {
|
|
"period": 10,
|
|
"operator": ">=",
|
|
"frequency": 3,
|
|
"cron": "",
|
|
"threshold": 3,
|
|
"silence": 10,
|
|
"frequency_type": "minutes",
|
|
"timezone": "UTC",
|
|
"tolerance_in_secs": 0
|
|
},
|
|
"org_id": org_id,
|
|
"destinations": [destination_name],
|
|
"enabled": True,
|
|
"description": "test"
|
|
}
|
|
|
|
response = session.post(f"{base_url}api/v2/{org_id}/alerts?type=logs", json=payload)
|
|
if response.status_code == 409:
|
|
# Handle alert already exists scenario
|
|
print(f"Alert already exists: {payload['name']}.")
|
|
return False # Indicate that creation failed due to existing alert
|
|
assert response.status_code == 200, f"Failed to create alert: {response.content}"
|
|
return True # Indicate successful creation
|
|
|
|
def create_standard_alert_cron(self, session, base_url, user_email, user_password, org_id, stream_name, template_name, destination_name, alert_name):
|
|
headers = {"Content-Type": "application/json", "Custom-Header": "value"}
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
payload = {
|
|
"name": alert_name,
|
|
"row_template": template_name,
|
|
"stream_type": "logs",
|
|
"stream_name": stream_name,
|
|
"is_real_time": False,
|
|
"context_attributes": {},
|
|
"query_condition": {
|
|
"conditions": [ {
|
|
"column": "log",
|
|
"operator": "=",
|
|
"value": "200",
|
|
"type": None, # Set it to None or remove it
|
|
"id": str(uuid.uuid4())
|
|
} ],
|
|
"search_event_type": "ui",
|
|
"sql": "",
|
|
"promql": "",
|
|
"type": "custom",
|
|
"promql_condition": None,
|
|
"vrl_function": None,
|
|
"multi_time_range": []
|
|
},
|
|
"trigger_condition": {
|
|
"period": 10,
|
|
"operator": ">=",
|
|
"frequency": 3,
|
|
"cron": "1 40 * * * *",
|
|
"threshold": 3,
|
|
"silence": 10,
|
|
"frequency_type": "cron",
|
|
"timezone": "UTC",
|
|
"tz_offset": 0
|
|
},
|
|
"org_id": org_id,
|
|
"destinations": [destination_name],
|
|
"enabled": True,
|
|
"description": "test"
|
|
}
|
|
|
|
response = session.post(f"{base_url}api/v2/{org_id}/alerts?type=logs", json=payload)
|
|
if response.status_code == 409:
|
|
# Handle alert already exists scenario
|
|
print(f"Alert already exists: {payload['name']}.")
|
|
return False # Indicate that creation failed due to existing alert
|
|
assert response.status_code == 200, f"Failed to create alert: {response.content}"
|
|
return True # Indicate successful creation
|
|
|
|
def retrieve_alerts_standard(self, session, base_url, user_email, user_password, org_id, stream_name):
|
|
"""Retrieve standard alerts."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts?type=logs")
|
|
assert response.status_code == 200, f"Failed to retrieve alerts: {response.content}"
|
|
|
|
# Parse the response
|
|
response_data = response.json()
|
|
|
|
# Handle the case where response is wrapped in a 'data' field
|
|
alerts = response_data.get('data', []) if isinstance(response_data, dict) else response_data
|
|
|
|
|
|
|
|
# Ensure alerts is a list
|
|
if not isinstance(alerts, list):
|
|
print(f"Unexpected alerts format: {alerts}")
|
|
return {"count": 0, "alerts": []}
|
|
|
|
# Filter alerts
|
|
standard_alerts = []
|
|
for alert in alerts:
|
|
try:
|
|
if (isinstance(alert, dict) and
|
|
isinstance(alert.get('name'), str) and
|
|
alert['name'].startswith(f"standard_{AlertPage.Unique_value_alert}")):
|
|
standard_alerts.append(alert)
|
|
except (KeyError, TypeError) as e:
|
|
print(f"Error processing alert {alert}: {str(e)}")
|
|
continue
|
|
|
|
print(f"Filtered standard alerts: {standard_alerts}")
|
|
|
|
assert len(standard_alerts) > 0, f"No alerts found with title starting with 'standard_{AlertPage.Unique_value_alert}'"
|
|
|
|
return {
|
|
"count": len(standard_alerts),
|
|
"alerts": standard_alerts
|
|
}
|
|
|
|
def retrieve_alert_standard(self, session, base_url, user_email, user_password, org_id, stream_name, alert_name):
|
|
"""Retrieve a standard alert."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts/{alert_name}?type=logs")
|
|
assert response.status_code == 200, f"Failed to retrieve alert: {response.content}"
|
|
alert_standard_webhook = response.json() # Assuming the response is a single alert, not a list.
|
|
|
|
# Assert that the response is a valid template object
|
|
assert isinstance(alert_standard_webhook, dict), "Response is not a valid alert object"
|
|
|
|
return {
|
|
"name_alert_standard_webhook": alert_standard_webhook["name"]
|
|
}
|
|
|
|
def retrieve_alerts_standard_sql(self, session, base_url, user_email, user_password, org_id, stream_name):
|
|
"""Retrieve standard SQL alerts."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts?type=logs")
|
|
assert response.status_code == 200, f"Failed to retrieve alerts: {response.content}"
|
|
|
|
# Parse the response
|
|
response_data = response.json()
|
|
|
|
# Handle the case where response is wrapped in a 'data' field
|
|
alerts = response_data.get('data', []) if isinstance(response_data, dict) else response_data
|
|
|
|
|
|
# Ensure alerts is a list
|
|
if not isinstance(alerts, list):
|
|
# print(f"Unexpected alerts format: {alerts}")
|
|
return {"count": 0, "alerts": []}
|
|
|
|
# Filter alerts
|
|
standard_sql_alerts = []
|
|
for alert in alerts:
|
|
try:
|
|
if (isinstance(alert, dict) and
|
|
isinstance(alert.get('name'), str) and
|
|
"alert_sql" in alert['name'] and
|
|
alert['name'].startswith(f"alert_sql_{AlertPage.Unique_value_alert}")):
|
|
standard_sql_alerts.append(alert)
|
|
except (KeyError, TypeError) as e:
|
|
print(f"Error processing alert {alert}: {str(e)}")
|
|
continue
|
|
|
|
# Assert that the response is a valid alert object
|
|
assert isinstance(standard_sql_alerts, list), "Response is not a valid alert object"
|
|
|
|
return {
|
|
"count": len(standard_sql_alerts),
|
|
"alerts": standard_sql_alerts
|
|
}
|
|
|
|
def retrieve_alert_standard_sql(self, session, base_url, user_email, user_password, org_id, stream_name, alert_name):
|
|
"""Retrieve a standard SQL alert."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts/{alert_name}?type=logs")
|
|
assert response.status_code == 200, f"Failed to retrieve alert: {response.content}"
|
|
alert_standard_sql = response.json() # Assuming the response is a single alert, not a list.
|
|
|
|
# Assert that the response is a valid alert object
|
|
assert isinstance(alert_standard_sql, dict), "Response is not a valid alert object"
|
|
|
|
return {
|
|
"name_alert_standard_sql": alert_standard_sql["name"]
|
|
}
|
|
|
|
def retrieve_alerts_sql(self, session, base_url, user_email, user_password, org_id, stream_name):
|
|
"""Retrieve all SQL alerts."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts?type=logs")
|
|
assert response.status_code == 200, f"Failed to retrieve alerts: {response.content}"
|
|
|
|
# Parse the JSON response
|
|
alerts = response.json()
|
|
assert len(alerts) > 0, "No alerts found"
|
|
|
|
# Filter templates that contain 'template_webhook' and match the expected name
|
|
sql_alerts = [alert for alert in alerts
|
|
if "sql" in alert["name"]
|
|
and alert["name"].startswith(f"alert_sql_{AlertPage.Unique_value_alert}")]
|
|
|
|
assert len(sql_alerts) > 0, f"No alerts found containing 'sql' with title 'alert_sql_{AlertPage.Unique_value_alert}'"
|
|
|
|
return {
|
|
"count": len(sql_alerts),
|
|
"alerts": sql_alerts
|
|
}
|
|
|
|
def retrieve_alert_sql(self, session, base_url, user_email, user_password, org_id, stream_name, alert_name):
|
|
"""Retrieve a SQL alert."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts/{alert_name}?type=logs")
|
|
assert response.status_code == 200, f"Failed to retrieve alert: {response.content}"
|
|
alert_sql = response.json() # Assuming the response is a single alert, not a list.
|
|
|
|
# Assert that the response is a valid alert object
|
|
assert isinstance(alert_sql, dict), "Response is not a valid alert object"
|
|
|
|
return {
|
|
"name_alert_sql": alert_sql["name"]
|
|
}
|
|
|
|
def retrieve_alerts_real_time(self, session, base_url, user_email, user_password, org_id, stream_name):
|
|
"""Retrieve real-time alerts."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts?type=logs")
|
|
assert response.status_code == 200, f"Failed to retrieve alerts: {response.content}"
|
|
|
|
alerts = response.json()
|
|
if not isinstance(alerts, list):
|
|
alerts = alerts.get('data', []) if isinstance(alerts, dict) else []
|
|
|
|
real_time_alerts = [
|
|
alert for alert in alerts
|
|
if isinstance(alert, dict) and
|
|
isinstance(alert.get('name'), str) and
|
|
"real_time" in alert["name"] and
|
|
alert["name"].startswith(f"alert_real_time _{AlertPage.Unique_value_alert}")
|
|
]
|
|
|
|
return {
|
|
"count": len(real_time_alerts),
|
|
"alerts": real_time_alerts
|
|
}
|
|
|
|
def retrieve_alert_real_time(self, session, base_url, user_email, user_password, org_id, stream_name, alert_name):
|
|
"""Retrieve a real-time alert."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts/{alert_name}?type=logs")
|
|
assert response.status_code == 200, f"Failed to retrieve alert: {response.content}"
|
|
alert_real_time = response.json() # Assuming the response is a single alert, not a list.
|
|
|
|
# Assert that the response is a valid alert object
|
|
assert isinstance(alert_real_time, dict), "Response is not a valid alert object"
|
|
|
|
return {
|
|
"name_alert_real_time": alert_real_time["name"]
|
|
}
|
|
|
|
def retrieve_alerts_cron(self, session, base_url, user_email, user_password, org_id, stream_name):
|
|
"""Retrieve cron alerts."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts?type=logs")
|
|
assert response.status_code == 200, f"Failed to retrieve alerts: {response.content}"
|
|
|
|
alerts = response.json()
|
|
if not isinstance(alerts, list):
|
|
alerts = alerts.get('data', []) if isinstance(alerts, dict) else []
|
|
|
|
cron_alerts = [
|
|
alert for alert in alerts
|
|
if isinstance(alert, dict) and
|
|
isinstance(alert.get('name'), str) and
|
|
"cron" in alert["name"] and
|
|
alert["name"].startswith(f"alert_cron_{AlertPage.Unique_value_alert}")
|
|
]
|
|
|
|
return {
|
|
"count": len(cron_alerts),
|
|
"alerts": cron_alerts
|
|
}
|
|
|
|
def retrieve_alert_cron(self, session, base_url, user_email, user_password, org_id, stream_name, alert_name):
|
|
"""Retrieve a cron alert."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts/{alert_name}?type=logs")
|
|
assert response.status_code == 200, f"Failed to retrieve alert: {response.content}"
|
|
alert_cron = response.json() # Assuming the response is a single alert, not a list.
|
|
|
|
# Assert that the response is a valid alert object
|
|
assert isinstance(alert_cron, dict), "Response is not a valid alert object"
|
|
|
|
return {
|
|
"name_alert_cron": alert_cron["name"]
|
|
}
|
|
|
|
|
|
def update_standard_alert(self, session, base_url, user_email, user_password, org_id, stream_name, template_name, destination_name, alert_name):
|
|
"""Update a standard alert."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
|
|
# Update the alert with the new payload
|
|
payload = {
|
|
"name": alert_name,
|
|
"row_template": template_name,
|
|
"stream_type": "logs",
|
|
"stream_name": stream_name,
|
|
"is_real_time": False,
|
|
"context_attributes": {},
|
|
"query_condition": {
|
|
"conditions": [ {
|
|
"column": "log",
|
|
"operator": "=",
|
|
"value": "200",
|
|
"type": None, # Set it to None or remove it
|
|
"id": str(uuid.uuid4())
|
|
} ],
|
|
"search_event_type": "ui",
|
|
"sql": "",
|
|
"promql": "",
|
|
"type": "custom",
|
|
"promql_condition": None,
|
|
"vrl_function": None,
|
|
"multi_time_range": []
|
|
},
|
|
"trigger_condition": {
|
|
"period": 10,
|
|
"operator": ">=",
|
|
"frequency": 3,
|
|
"cron": "",
|
|
"threshold": 3,
|
|
"silence": 10,
|
|
"frequency_type": "minutes",
|
|
"timezone": "UTC",
|
|
"tolerance_in_secs": 0
|
|
},
|
|
"org_id": org_id,
|
|
"destinations": [destination_name],
|
|
"enabled": True,
|
|
"description": "test updated"
|
|
}
|
|
|
|
|
|
response = session.put(f"{base_url}api/v2/{org_id}/alerts/{alert_name}?type=logs", json=payload)
|
|
assert response.status_code == 200, f"Failed to update alert: {response.content}"
|
|
return response
|
|
|
|
def update_standard_alert_sql(self, session, base_url, user_email, user_password, org_id, stream_name, template_name, destination_name, alert_name):
|
|
"""Update a standard SQL alert."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
|
|
# Update the alert with the new payload
|
|
payload = {
|
|
"name": alert_name,
|
|
"row_template": template_name,
|
|
"stream_type": "logs",
|
|
"stream_name": stream_name,
|
|
"is_real_time": False,
|
|
"context_attributes": {},
|
|
"query_condition": {
|
|
"conditions": [ {
|
|
"column": "log",
|
|
"operator": "=",
|
|
"value": "200",
|
|
"type": None, # Set it to None or remove it
|
|
"id": str(uuid.uuid4())
|
|
} ],
|
|
"search_event_type": "ui",
|
|
"sql": f"select log from \"{stream_name}\" where log = 200",
|
|
"promql": "",
|
|
"type": "sql",
|
|
"promql_condition": None,
|
|
"vrl_function": None,
|
|
"multi_time_range": []
|
|
},
|
|
"trigger_condition": {
|
|
"period": 10,
|
|
"operator": ">=",
|
|
"frequency": 3,
|
|
"cron": "",
|
|
"threshold": 3,
|
|
"silence": 10,
|
|
"frequency_type": "minutes",
|
|
"timezone": "UTC",
|
|
"tolerance_in_secs": 0
|
|
},
|
|
"org_id": org_id,
|
|
"destinations": [destination_name],
|
|
"enabled": True,
|
|
"description": "test updated"
|
|
}
|
|
|
|
response = session.put(f"{base_url}api/v2/{org_id}/alerts/{alert_name}?type=logs", json=payload)
|
|
assert response.status_code == 200, f"Failed to update alert: {response.content}"
|
|
return response
|
|
|
|
def update_real_time_alert(self, session, base_url, user_email, user_password, org_id, stream_name, template_name, destination_name, alert_name):
|
|
"""Update a real-time alert."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
|
|
# Update the alert with the new payload
|
|
payload = {
|
|
"name": alert_name,
|
|
"row_template": template_name,
|
|
"stream_type": "logs",
|
|
"stream_name": stream_name,
|
|
"is_real_time": True,
|
|
"context_attributes": {},
|
|
"query_condition": {
|
|
"conditions": [ {
|
|
"column": "log",
|
|
"operator": "=",
|
|
"value": "200",
|
|
"type": None, # Set it to None or remove it
|
|
"id": str(uuid.uuid4())
|
|
} ],
|
|
"search_event_type": "ui",
|
|
"sql": "",
|
|
"promql": "",
|
|
"type": "custom",
|
|
"promql_condition": None,
|
|
"vrl_function": None,
|
|
"multi_time_range": []
|
|
},
|
|
"trigger_condition": {
|
|
"period": 10,
|
|
"operator": ">=",
|
|
"frequency": 3,
|
|
"cron": "",
|
|
"threshold": 3,
|
|
"silence": 10,
|
|
"frequency_type": "minutes",
|
|
"timezone": "UTC",
|
|
"tolerance_in_secs": 0
|
|
},
|
|
"org_id": org_id,
|
|
"destinations": [destination_name],
|
|
"enabled": True,
|
|
"description": "test updated"
|
|
}
|
|
|
|
response = session.put(f"{base_url}api/v2/{org_id}/alerts/{alert_name}?type=logs", json=payload)
|
|
assert response.status_code == 200, f"Failed to update alert: {response.content}"
|
|
return response
|
|
|
|
def update_standard_alert_cron(self, session, base_url, user_email, user_password, org_id, stream_name, template_name, destination_name, alert_name):
|
|
"""Update a standard cron alert."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
|
|
# Update the alert with the new payload
|
|
payload = {
|
|
"name": alert_name,
|
|
"row_template": template_name,
|
|
"stream_type": "logs",
|
|
"stream_name": stream_name,
|
|
"is_real_time": False,
|
|
"context_attributes": {},
|
|
"query_condition": {
|
|
"conditions": [ {
|
|
"column": "log",
|
|
"operator": "=",
|
|
"value": "200",
|
|
"type": None, # Set it to None or remove it
|
|
"id": str(uuid.uuid4())
|
|
} ],
|
|
"search_event_type": "ui",
|
|
"sql": "",
|
|
"promql": "",
|
|
"type": "custom",
|
|
"promql_condition": None,
|
|
"vrl_function": None,
|
|
"multi_time_range": []
|
|
},
|
|
"trigger_condition": {
|
|
"period": 10,
|
|
"operator": ">=",
|
|
"frequency": 3,
|
|
"cron": "1 40 * * * *",
|
|
"threshold": 3,
|
|
"silence": 10,
|
|
"frequency_type": "cron",
|
|
"timezone": "UTC",
|
|
"tz_offset": 0
|
|
},
|
|
"org_id": org_id,
|
|
"destinations": [destination_name],
|
|
"enabled": True,
|
|
"description": "test updated"
|
|
}
|
|
|
|
response = session.put(f"{base_url}api/v2/{org_id}/alerts/{alert_name}?type=logs", json=payload)
|
|
assert response.status_code == 200, f"Failed to update alert: {response.content}"
|
|
return response
|
|
|
|
|
|
|
|
def delete_alert(self, session, base_url, user_email, user_password, org_id, stream_name, alert_name):
|
|
"""Delete alerts of a specified type."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
|
|
resp_delete_alert = session.delete(f"{base_url}api/v2/{org_id}/alerts/{alert_name}")
|
|
assert resp_delete_alert.status_code == 200, f"Failed to delete alert {alert_name}: {resp_delete_alert.content}"
|
|
|
|
# Wait for a few seconds to allow the data to be deleted
|
|
time.sleep(10) # Increase this time if necessary
|
|
|
|
# Verify deletion
|
|
resp_ver_alert = session.get(f"{base_url}api/v2/{org_id}/alerts/{alert_name}")
|
|
assert resp_ver_alert.status_code == 404, f"Expected 404 for {alert_name}, but got {resp_ver_alert.status_code}"
|
|
|
|
return resp_delete_alert # Return the list of deleted alert names for verification if needed
|
|
|
|
def validate_deleted_standard_alert(self, session, base_url, user_email, user_password, org_id, stream_name, alert_name):
|
|
"""Running an E2E test for validating deleted alerts in SC."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
# Retrieve all alerts
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts?type=logs")
|
|
assert response.status_code == 200, f"Failed to retrieve alerts: {response.content}"
|
|
|
|
# Get the response data
|
|
response_data = response.json()
|
|
|
|
|
|
# Handle different response formats
|
|
if isinstance(response_data, dict):
|
|
alerts = response_data.get('data', [])
|
|
elif isinstance(response_data, str):
|
|
# print(f"Unexpected string response: {response_data}")
|
|
alerts = []
|
|
elif isinstance(response_data, list):
|
|
alerts = response_data
|
|
else:
|
|
print(f"Unexpected response type: {type(response_data)}")
|
|
alerts = []
|
|
|
|
|
|
# Filter alerts with proper type checking
|
|
standard_alerts = []
|
|
for alert in alerts:
|
|
if not isinstance(alert, dict):
|
|
# print(f"Skipping non-dict alert: {alert}")
|
|
continue
|
|
|
|
name = alert.get('name')
|
|
if not isinstance(name, str):
|
|
# print(f"Skipping alert with non-string name: {alert}")
|
|
continue
|
|
|
|
if ("standard" in name and
|
|
name.startswith(f"standard_{AlertPage.Unique_value_alert}")):
|
|
standard_alerts.append(alert)
|
|
|
|
|
|
# Check if any alerts of the specified type exist
|
|
if len(standard_alerts) == 0:
|
|
print(f"No alerts found of type '{alert_name}' (expected for deletion verification)")
|
|
else:
|
|
print(f"Alerts found of type '{alert_name}': {[alert.get('name') for alert in standard_alerts]}")
|
|
|
|
return standard_alerts
|
|
|
|
def validate_deleted_standard_alert_sql(self, session, base_url, user_email, user_password, org_id, stream_name, alert_name):
|
|
"""Running an E2E test for validating deleted templates in SC."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
# Retrieve all templates
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts")
|
|
assert response.status_code == 200, f"Failed to retrieve templates: {response.content}"
|
|
|
|
alerts = response.json()
|
|
|
|
# Filter templates that contain 'template_email' and match the expected name
|
|
standard_sql_alerts = [alert for alert in alerts
|
|
if "standard_sql" in alert["name"]
|
|
and alert["name"].startswith(f"standard_sql_{AlertPage.Unique_value_alert}")]
|
|
|
|
# Check if any templates of the specified type exist
|
|
if len(standard_sql_alerts) == 0:
|
|
print(f"No alerts found of type '{alert_name}'")
|
|
# Optionally, you can choose to skip the assertion or handle it differently
|
|
else:
|
|
assert len(standard_sql_alerts) > 0, f"No alerts found of type '{alert_name}'"
|
|
# print(f"Alerts found of type '{alert_name}': {[alert['name'] for alert in standard_sql_alerts]}")
|
|
|
|
return standard_sql_alerts
|
|
|
|
def validate_deleted_real_time_alert(self, session, base_url, user_email, user_password, org_id, stream_name, alert_name):
|
|
"""Running an E2E test for validating deleted alerts in SC."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
# Retrieve all alerts
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts")
|
|
assert response.status_code == 200, f"Failed to retrieve alerts: {response.content}"
|
|
|
|
alerts = response.json()
|
|
|
|
# Filter alerts that contain 'real_time' and match the expected name
|
|
real_time_alerts = [alert for alert in alerts
|
|
if "real_time" in alert["name"]
|
|
and alert["name"].startswith(f"real_time_{AlertPage.Unique_value_alert}")]
|
|
|
|
if len(real_time_alerts) == 0:
|
|
print(f"No alerts found of type '{alert_name}'")
|
|
else:
|
|
print(f"Alerts found of type '{alert_name}': {[alert['name'] for alert in real_time_alerts]}")
|
|
return real_time_alerts
|
|
|
|
def validate_deleted_standard_alert_cron(self, session, base_url, user_email, user_password, org_id, stream_name, alert_name):
|
|
"""Running an E2E test for validating deleted alerts in SC."""
|
|
session.auth = HTTPBasicAuth(user_email, user_password)
|
|
# Retrieve all alerts
|
|
response = session.get(f"{base_url}api/v2/{org_id}/alerts")
|
|
assert response.status_code == 200, f"Failed to retrieve alerts: {response.content}"
|
|
|
|
alerts = response.json()
|
|
|
|
# Filter alerts that contain 'cron' and match the expected name
|
|
standard_cron_alerts = [alert for alert in alerts
|
|
if "standard_cron" in alert["name"]
|
|
and alert["name"].startswith(f"standard_cron_{AlertPage.Unique_value_alert}")]
|
|
|
|
if len(standard_cron_alerts) == 0:
|
|
print(f"No alerts found of type '{alert_name}'")
|
|
else:
|
|
print(f"Alerts found of type '{alert_name}': {[alert['name'] for alert in standard_cron_alerts]}")
|
|
return standard_cron_alerts
|
|
|