test: полный прогон POSTGRES example — changes, delete/recreate, new function
This commit is contained in:
@@ -36,6 +36,7 @@ exports.info = async (event) => {
|
||||
node_version: process.version,
|
||||
pg_version: versionRes.rows[0].v,
|
||||
table_rows: parseInt(countRes.rows[0].cnt, 10),
|
||||
code_version: 'v2-agent-test',
|
||||
};
|
||||
} finally {
|
||||
await client.end();
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
# 2026-03-19
|
||||
# pg_stats.py — тестовая функция (Test 7): возвращает агрегированную статистику
|
||||
# по таблице terraform_demo_table: кол-во строк, дата первой и последней записи.
|
||||
# Создаётся и удаляется в рамках тестового прогона.
|
||||
#
|
||||
# Entrypoint: pg_stats.get_stats
|
||||
|
||||
import os
|
||||
import psycopg2
|
||||
import json
|
||||
|
||||
_CODE_VERSION = "v1-test7"
|
||||
|
||||
|
||||
def get_stats(event):
|
||||
conn = psycopg2.connect(
|
||||
host=os.environ["PGHOST"],
|
||||
port=int(os.environ.get("PGPORT", "5432")),
|
||||
dbname=os.environ["PGDATABASE"],
|
||||
user=os.environ["PGUSER"],
|
||||
password=os.environ["PGPASSWORD"],
|
||||
sslmode=os.environ.get("PGSSLMODE", "require"),
|
||||
)
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"SELECT COUNT(*) AS cnt, MIN(created_at) AS first, MAX(created_at) AS last "
|
||||
"FROM terraform_demo_table"
|
||||
)
|
||||
row = cur.fetchone()
|
||||
return {
|
||||
"version": _CODE_VERSION,
|
||||
"total_rows": row[0],
|
||||
"first_row_at": str(row[1]) if row[1] else None,
|
||||
"last_row_at": str(row[2]) if row[2] else None,
|
||||
}
|
||||
finally:
|
||||
conn.close()
|
||||
@@ -0,0 +1 @@
|
||||
psycopg2-binary==2.9.9
|
||||
@@ -1,13 +1,16 @@
|
||||
# 2026-03-19
|
||||
# 2026-03-19 — добавлен version и hostname в ответ list_rows для тестирования обновления кода
|
||||
# table_rw.py — чтение и запись строк в terraform_demo_table.
|
||||
# Два entrypoint в одном файле: list_rows (JSON API) и add_row (HTML-страница + POST-обработчик).
|
||||
# ENV: PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD, PGSSLMODE
|
||||
|
||||
import os
|
||||
import json
|
||||
import socket
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
|
||||
_CODE_VERSION = "v2-with-hostname"
|
||||
|
||||
|
||||
def _connect():
|
||||
return psycopg2.connect(
|
||||
@@ -29,7 +32,7 @@ def list_rows(event):
|
||||
"SELECT id, title, created_at::text FROM terraform_demo_table ORDER BY created_at DESC"
|
||||
)
|
||||
rows = [dict(r) for r in cur.fetchall()]
|
||||
return {"rows": rows, "count": len(rows)}
|
||||
return {"rows": rows, "count": len(rows), "version": _CODE_VERSION, "host": socket.gethostname()}
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
@@ -1,25 +1,23 @@
|
||||
|
||||
# resource "nubes_lucee" "app1" {
|
||||
# # Lucee-приложение, зависит от Postgres
|
||||
# # Lucee-приложение, подключается к nubes_postgres.npg (наш Postgres в реалме var.realm).
|
||||
# resource_name = "lucy_teststand_0"
|
||||
# # resource_realm = "k8s-3.ext.nubes.ru"
|
||||
# resource_realm = nubes_postgres.db2.resource_realm
|
||||
# # resource_realm = "k8s-4-sandbox-nubes-ru"
|
||||
# resource_realm = nubes_postgres.npg.resource_realm
|
||||
# domain = "web-test-stand"
|
||||
|
||||
# git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testlucee"
|
||||
# git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testlucee"
|
||||
|
||||
# json_env = jsonencode({
|
||||
# # 🔗 Настройки Data Source 'testds' для Lucee (Application.cfc)
|
||||
# testds_class = "org.postgresql.Driver" # 📂 Драйвер БД
|
||||
# testds_bundleName = "org.postgresql.jdbc" # 📦 Имя бандла JDBC
|
||||
# testds_bundleVersion = "42.6.0" # 🔢 Версия драйвера
|
||||
# testds_connectionString = "jdbc:postgresql://${nubes_postgres.db2.state_out_flat["internalConnect.master"]}:5432/postgres?sslmode=require" # 🚀 Строка подключения
|
||||
# testds_username = nubes_postgres_user.db2_user.username # 👤 Логин
|
||||
# testds_password = jsondecode(nubes_postgres.db2.vault_secrets["users"])[nubes_postgres_user.db2_user.username]["password"] # 🔑 Пароль
|
||||
# testds_connectionLimit = "5" # 🚦 Лимит соединений
|
||||
# testds_liveTimeout = "15" # ⏳ Таймаут жизни
|
||||
# testds_validate = "false" # ✅ Валидация при запросе
|
||||
# json_env = jsonencode({
|
||||
# # Настройки Data Source 'testds' для Lucee (Application.cfc)
|
||||
# testds_class = "org.postgresql.Driver"
|
||||
# testds_bundleName = "org.postgresql.jdbc"
|
||||
# testds_bundleVersion = "42.6.0"
|
||||
# testds_connectionString = "jdbc:postgresql://${local.pg_host}:5432/${local.pg_database}?sslmode=require"
|
||||
# testds_username = local.pg_username
|
||||
# testds_password = local.pg_password
|
||||
# testds_connectionLimit = "5"
|
||||
# testds_liveTimeout = "15"
|
||||
# testds_validate = "false"
|
||||
# })
|
||||
|
||||
# resource_c_p_u = 300
|
||||
@@ -27,40 +25,40 @@
|
||||
# resource_instances = 1
|
||||
# app_version = "5.4"
|
||||
|
||||
# depends_on = [nubes_postgres.db2]
|
||||
# depends_on = [nubes_postgres.npg]
|
||||
# }
|
||||
|
||||
# resource "nubes_nodejs" "app3" {
|
||||
# # NodeJS демо, работающий с тем же Postgres.
|
||||
# resource_name = "node_01"
|
||||
# resource_realm = nubes_postgres.db2.resource_realm
|
||||
# domain = "node07"
|
||||
# git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testnode.git"
|
||||
# health_path = "/healthz"
|
||||
# app_version = "23"
|
||||
|
||||
# json_env = jsonencode({
|
||||
# # Переменные подключения к Postgres.
|
||||
# PGHOST = nubes_postgres.db2.state_out_flat["internalConnect.master"]
|
||||
# PGPORT = "5432"
|
||||
# PGUSER = nubes_postgres_user.db2_user.username
|
||||
# PGPASSWORD = jsondecode(nubes_postgres.db2.vault_secrets["users"])[nubes_postgres_user.db2_user.username]["password"]
|
||||
# PGDATABASE = nubes_postgres_database.db2_app.db_name
|
||||
# PGSSLMODE = "require"
|
||||
# DATABASE_URL = format(
|
||||
# # NodeJS — застрял при создании на тест-стенде (операция в ожидании).
|
||||
# # Раскомментировать когда тест-стенд стабилен.
|
||||
# resource_name = "node_01"
|
||||
# resource_realm = nubes_postgres.npg.resource_realm
|
||||
# domain = "node07"
|
||||
# git_path = "https://gitea-naeel.giteak8s.services.ngcloud.ru/naeel/testnode.git"
|
||||
# health_path = "/healthz"
|
||||
# app_version = "23"
|
||||
#
|
||||
# json_env = jsonencode({
|
||||
# PGHOST = local.pg_host
|
||||
# PGPORT = "5432"
|
||||
# PGUSER = local.pg_username
|
||||
# PGPASSWORD = local.pg_password
|
||||
# PGDATABASE = local.pg_database
|
||||
# PGSSLMODE = "require"
|
||||
# DATABASE_URL = format(
|
||||
# "postgresql://%s:%s@%s:5432/%s?sslmode=require",
|
||||
# nubes_postgres_user.db2_user.username,
|
||||
# jsondecode(nubes_postgres.db2.vault_secrets["users"])[nubes_postgres_user.db2_user.username]["password"],
|
||||
# nubes_postgres.db2.state_out_flat["internalConnect.master"],
|
||||
# nubes_postgres_database.db2_app.db_name
|
||||
# )
|
||||
# })
|
||||
|
||||
# resource_c_p_u = 300
|
||||
# resource_memory = 256
|
||||
# resource_instances = 1
|
||||
|
||||
# depends_on = [nubes_postgres.db2]
|
||||
# local.pg_username,
|
||||
# local.pg_password,
|
||||
# local.pg_host,
|
||||
# local.pg_database
|
||||
# )
|
||||
# })
|
||||
#
|
||||
# resource_c_p_u = 300
|
||||
# resource_memory = 256
|
||||
# resource_instances = 1
|
||||
#
|
||||
# depends_on = [nubes_postgres.npg]
|
||||
# }
|
||||
|
||||
# output "pg_vault_secrets" {
|
||||
@@ -68,6 +66,4 @@
|
||||
# sensitive = true
|
||||
# }
|
||||
|
||||
# terraform output -json pg_vault_secrets
|
||||
|
||||
|
||||
|
||||
@@ -166,8 +166,8 @@ resource "sless_function" "postgres_table_writer" {
|
||||
name = "pg-table-writer"
|
||||
runtime = "python3.11"
|
||||
entrypoint = "table_rw.add_row"
|
||||
memory_mb = 128
|
||||
timeout_sec = 30
|
||||
memory_mb = 256
|
||||
timeout_sec = 45
|
||||
|
||||
env_vars = {
|
||||
PGHOST = local.pg_host
|
||||
|
||||
Reference in New Issue
Block a user