#!/bin/bash

USER="elastic"
PASSWORD="YourSecureESPass123!"

echo "=== Elasticsearch 认证测试 ==="

# 1. 测试基础连接
echo "1. 测试基础连接:"
curl -s -u $USER:$PASSWORD -X GET "localhost:9200/" | grep -E '"name"|"cluster_name"|"version"'

# 2. 测试健康状态
echo -e "\n2. 健康状态:"
curl -s -u $USER:$PASSWORD -X GET "localhost:9200/_cat/health?v"

# 3. 测试索引操作
echo -e "\n3. 创建测试索引:"
curl -s -u $USER:$PASSWORD -X PUT "localhost:9200/deployment-test"

# 4. 测试文档操作
echo -e "\n4. 添加测试文档:"
curl -s -u $USER:$PASSWORD -X POST "localhost:9200/deployment-test/_doc/1" -H 'Content-Type: application/json' -d'
{
  "message": "部署测试成功",
  "timestamp": "'$(date +%Y-%m-%dT%H:%M:%S)'"
}'

# 5. 测试查询
echo -e "\n5. 查询测试文档:"
curl -s -u $USER:$PASSWORD -X GET "localhost:9200/deployment-test/_search?pretty"

# 6. 清理测试数据
echo -e "\n6. 清理测试数据:"
curl -s -u $USER:$PASSWORD -X DELETE "localhost:9200/deployment-test"

echo -e "\n=== 测试完成 ==="
