diff --git a/pkg/simple/client/es/client_test.go b/pkg/simple/client/es/client_test.go index 54a53c195..249c400d7 100644 --- a/pkg/simple/client/es/client_test.go +++ b/pkg/simple/client/es/client_test.go @@ -43,6 +43,10 @@ func TestNewClient(t *testing.T) { fakeResp: "es7_detect_version_major_200.json", expected: ElasticV7, }, + { + fakeResp: "opensearchv2_detect_version_major_200.json", + expected: OpenSearchV2, + }, } for i, test := range tests { @@ -107,6 +111,50 @@ func TestClient_Search(t *testing.T) { } } +func TestOpensearchClient_Search(t *testing.T) { + var tests = []struct { + fakeVersion string + fakeResp string + fakeCode int + expected string + expectedErr string + }{ + { + fakeVersion: OpenSearchV2, + fakeResp: "opensearchv2_search_200.json", + fakeCode: http.StatusOK, + expected: "opensearchv2_search_200_result.json", + }, + } + + for i, test := range tests { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + var expected Response + err := JsonFromFile(test.expected, &expected) + if err != nil { + t.Fatal(err) + } + + srv := mockElasticsearchService("/", test.fakeResp, test.fakeCode) + defer srv.Close() + + c, err := NewClient(srv.URL, false, "", "", "ks-logstash", test.fakeVersion) + if err != nil { + t.Fatalf("create client error, %s", err) + } + result, err := c.Search(query.NewBuilder(), time.Time{}, time.Now(), false) + if test.expectedErr != "" { + if diff := cmp.Diff(fmt.Sprint(err), test.expectedErr); diff != "" { + t.Fatalf("%T differ (-got, +want): %s", test.expectedErr, diff) + } + } + if diff := cmp.Diff(result, &expected); diff != "" { + t.Fatalf("%T differ (-got, +want): %s", expected, diff) + } + }) + } +} + func mockElasticsearchService(pattern, fakeResp string, fakeCode int) *httptest.Server { mux := http.NewServeMux() mux.HandleFunc(pattern, func(res http.ResponseWriter, req *http.Request) { diff --git a/pkg/simple/client/es/testdata/opensearchv2_detect_version_major_200.json b/pkg/simple/client/es/testdata/opensearchv2_detect_version_major_200.json new file mode 100644 index 000000000..13b3a1d26 --- /dev/null +++ b/pkg/simple/client/es/testdata/opensearchv2_detect_version_major_200.json @@ -0,0 +1,17 @@ +{ + "name" : "opensearch-cluster-master-1", + "cluster_name" : "opensearch-cluster", + "cluster_uuid" : "Tyj3pwftSLqC195T-TJ46A", + "version" : { + "distribution" : "opensearch", + "number" : "2.0.1", + "build_type" : "tar", + "build_hash" : "6462a546240f6d7a158519499729bce12dc1058b", + "build_date" : "2022-06-15T08:47:42.243126494Z", + "build_snapshot" : false, + "lucene_version" : "9.1.0", + "minimum_wire_compatibility_version" : "7.10.0", + "minimum_index_compatibility_version" : "7.0.0" + }, + "tagline" : "The OpenSearch Project: https://opensearch.org/" +} diff --git a/pkg/simple/client/es/testdata/opensearchv2_search_200.json b/pkg/simple/client/es/testdata/opensearchv2_search_200.json new file mode 100644 index 000000000..604a07d26 --- /dev/null +++ b/pkg/simple/client/es/testdata/opensearchv2_search_200.json @@ -0,0 +1,189 @@ +{ + "took" : 8, + "timed_out" : false, + "_shards" : { + "total" : 1, + "successful" : 1, + "skipped" : 0, + "failed" : 0 + }, + "hits" : { + "total" : { + "value" : 10000, + "relation" : "gte" + }, + "max_score" : 1.0, + "hits" : [ + { + "_index" : "ks-logstash-log-2022.07.05", + "_id" : "bG3czYEBJ4hVKmXbxLgk", + "_score" : 1.0, + "_source" : { + "@timestamp" : "2022-07-05T10:16:45.982Z", + "log" : "[2022-07-05T10:16:45,982][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-1] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:45.982530956Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-1", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "a8f3ac4772ae75ff151742398a4b2750746d43cafcec3f54732c8927a71bffe0", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_index" : "ks-logstash-log-2022.07.05", + "_id" : "bW3czYEBJ4hVKmXbxLgk", + "_score" : 1.0, + "_source" : { + "@timestamp" : "2022-07-05T10:16:46.103Z", + "log" : "[2022-07-05T10:16:46,102][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-1] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:46.103075119Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-1", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "a8f3ac4772ae75ff151742398a4b2750746d43cafcec3f54732c8927a71bffe0", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_index" : "ks-logstash-log-2022.07.05", + "_id" : "723czYEBJ4hVKmXbw7d7", + "_score" : 1.0, + "_source" : { + "@timestamp" : "2022-07-05T10:16:43.156Z", + "log" : "level=info msg=\"Killed Fluent Bit\"\n", + "time" : "2022-07-05T10:16:43.15685138Z", + "kubernetes" : { + "pod_name" : "fluent-bit-spc9q", + "namespace_name" : "fluent", + "container_name" : "fluent-bit", + "docker_id" : "0a179f94962ccdec532f386e9bb6aa090707aa528b396a3209bb5988adb2bceb", + "container_image" : "kubesphere/fluent-bit:v1.9.4" + } + } + }, + { + "_index" : "ks-logstash-log-2022.07.05", + "_id" : "BdTczYEBDEKcFrNwxtcs", + "_score" : 1.0, + "_source" : { + "@timestamp" : "2022-07-05T10:16:46.154Z", + "log" : "[2022-07-05T10:16:46,154][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-2] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:46.154654969Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-2", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "80ef8f6428d9231d131776e7f8a827a9c27a5d2bcf8959b40494659597e0902f", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_index" : "ks-logstash-log-2022.07.05", + "_id" : "BtTczYEBDEKcFrNwxtcs", + "_score" : 1.0, + "_source" : { + "@timestamp" : "2022-07-05T10:16:46.199Z", + "log" : "[2022-07-05T10:16:46,199][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-2] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:46.199409801Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-2", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "80ef8f6428d9231d131776e7f8a827a9c27a5d2bcf8959b40494659597e0902f", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_index" : "ks-logstash-log-2022.07.05", + "_id" : "bm3czYEBJ4hVKmXbxLgk", + "_score" : 1.0, + "_source" : { + "@timestamp" : "2022-07-05T10:16:46.150Z", + "log" : "[2022-07-05T10:16:46,150][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-1] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:46.150653446Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-1", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "a8f3ac4772ae75ff151742398a4b2750746d43cafcec3f54732c8927a71bffe0", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_index" : "ks-logstash-log-2022.07.05", + "_id" : "8G3czYEBJ4hVKmXbw7d7", + "_score" : 1.0, + "_source" : { + "@timestamp" : "2022-07-05T10:16:43.156Z", + "log" : "level=info msg=\"Config file changed, stopped Fluent Bit\"\n", + "time" : "2022-07-05T10:16:43.156857206Z", + "kubernetes" : { + "pod_name" : "fluent-bit-spc9q", + "namespace_name" : "fluent", + "container_name" : "fluent-bit", + "docker_id" : "0a179f94962ccdec532f386e9bb6aa090707aa528b396a3209bb5988adb2bceb", + "container_image" : "kubesphere/fluent-bit:v1.9.4" + } + } + }, + { + "_index" : "ks-logstash-log-2022.07.05", + "_id" : "8W3czYEBJ4hVKmXbw7d7", + "_score" : 1.0, + "_source" : { + "@timestamp" : "2022-07-05T10:16:43.156Z", + "log" : "[2022/07/05 10:16:43] [engine] caught signal (SIGTERM)\n", + "time" : "2022-07-05T10:16:43.156864029Z", + "kubernetes" : { + "pod_name" : "fluent-bit-spc9q", + "namespace_name" : "fluent", + "container_name" : "fluent-bit", + "docker_id" : "0a179f94962ccdec532f386e9bb6aa090707aa528b396a3209bb5988adb2bceb", + "container_image" : "kubesphere/fluent-bit:v1.9.4" + } + } + }, + { + "_index" : "ks-logstash-log-2022.07.05", + "_id" : "b23czYEBJ4hVKmXbxLgk", + "_score" : 1.0, + "_source" : { + "@timestamp" : "2022-07-05T10:16:46.194Z", + "log" : "[2022-07-05T10:16:46,193][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-1] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:46.194249241Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-1", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "a8f3ac4772ae75ff151742398a4b2750746d43cafcec3f54732c8927a71bffe0", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_index" : "ks-logstash-log-2022.07.05", + "_id" : "8m3czYEBJ4hVKmXbw7d7", + "_score" : 1.0, + "_source" : { + "@timestamp" : "2022-07-05T10:16:43.156Z", + "log" : "[2022/07/05 10:16:43] [ info] [input] pausing systemd.0\n", + "time" : "2022-07-05T10:16:43.15689069Z", + "kubernetes" : { + "pod_name" : "fluent-bit-spc9q", + "namespace_name" : "fluent", + "container_name" : "fluent-bit", + "docker_id" : "0a179f94962ccdec532f386e9bb6aa090707aa528b396a3209bb5988adb2bceb", + "container_image" : "kubesphere/fluent-bit:v1.9.4" + } + } + } + ] + } +} diff --git a/pkg/simple/client/es/testdata/opensearchv2_search_200_result.json b/pkg/simple/client/es/testdata/opensearchv2_search_200_result.json new file mode 100644 index 000000000..3587bd3a1 --- /dev/null +++ b/pkg/simple/client/es/testdata/opensearchv2_search_200_result.json @@ -0,0 +1,150 @@ +{ + "hits" : { + "total" : { + "value" : 10000, + "relation" : "gte" + }, + "hits" : [ + { + "_source" : { + "@timestamp" : "2022-07-05T10:16:45.982Z", + "log" : "[2022-07-05T10:16:45,982][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-1] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:45.982530956Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-1", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "a8f3ac4772ae75ff151742398a4b2750746d43cafcec3f54732c8927a71bffe0", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_source" : { + "@timestamp" : "2022-07-05T10:16:46.103Z", + "log" : "[2022-07-05T10:16:46,102][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-1] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:46.103075119Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-1", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "a8f3ac4772ae75ff151742398a4b2750746d43cafcec3f54732c8927a71bffe0", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_source" : { + "@timestamp" : "2022-07-05T10:16:43.156Z", + "log" : "level=info msg=\"Killed Fluent Bit\"\n", + "time" : "2022-07-05T10:16:43.15685138Z", + "kubernetes" : { + "pod_name" : "fluent-bit-spc9q", + "namespace_name" : "fluent", + "container_name" : "fluent-bit", + "docker_id" : "0a179f94962ccdec532f386e9bb6aa090707aa528b396a3209bb5988adb2bceb", + "container_image" : "kubesphere/fluent-bit:v1.9.4" + } + } + }, + { + "_source" : { + "@timestamp" : "2022-07-05T10:16:46.154Z", + "log" : "[2022-07-05T10:16:46,154][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-2] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:46.154654969Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-2", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "80ef8f6428d9231d131776e7f8a827a9c27a5d2bcf8959b40494659597e0902f", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_source" : { + "@timestamp" : "2022-07-05T10:16:46.199Z", + "log" : "[2022-07-05T10:16:46,199][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-2] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:46.199409801Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-2", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "80ef8f6428d9231d131776e7f8a827a9c27a5d2bcf8959b40494659597e0902f", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_source" : { + "@timestamp" : "2022-07-05T10:16:46.150Z", + "log" : "[2022-07-05T10:16:46,150][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-1] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:46.150653446Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-1", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "a8f3ac4772ae75ff151742398a4b2750746d43cafcec3f54732c8927a71bffe0", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_source" : { + "@timestamp" : "2022-07-05T10:16:43.156Z", + "log" : "level=info msg=\"Config file changed, stopped Fluent Bit\"\n", + "time" : "2022-07-05T10:16:43.156857206Z", + "kubernetes" : { + "pod_name" : "fluent-bit-spc9q", + "namespace_name" : "fluent", + "container_name" : "fluent-bit", + "docker_id" : "0a179f94962ccdec532f386e9bb6aa090707aa528b396a3209bb5988adb2bceb", + "container_image" : "kubesphere/fluent-bit:v1.9.4" + } + } + }, + { + "_source" : { + "@timestamp" : "2022-07-05T10:16:43.156Z", + "log" : "[2022/07/05 10:16:43] [engine] caught signal (SIGTERM)\n", + "time" : "2022-07-05T10:16:43.156864029Z", + "kubernetes" : { + "pod_name" : "fluent-bit-spc9q", + "namespace_name" : "fluent", + "container_name" : "fluent-bit", + "docker_id" : "0a179f94962ccdec532f386e9bb6aa090707aa528b396a3209bb5988adb2bceb", + "container_image" : "kubesphere/fluent-bit:v1.9.4" + } + } + }, + { + "_source" : { + "@timestamp" : "2022-07-05T10:16:46.194Z", + "log" : "[2022-07-05T10:16:46,193][INFO ][o.o.a.u.d.DestinationMigrationCoordinator] [opensearch-cluster-master-1] Detected cluster change event for destination migration\n", + "time" : "2022-07-05T10:16:46.194249241Z", + "kubernetes" : { + "pod_name" : "opensearch-cluster-master-1", + "namespace_name" : "fluent", + "container_name" : "opensearch", + "docker_id" : "a8f3ac4772ae75ff151742398a4b2750746d43cafcec3f54732c8927a71bffe0", + "container_image" : "opensearchproject/opensearch:2.0.1" + } + } + }, + { + "_source" : { + "@timestamp" : "2022-07-05T10:16:43.156Z", + "log" : "[2022/07/05 10:16:43] [ info] [input] pausing systemd.0\n", + "time" : "2022-07-05T10:16:43.15689069Z", + "kubernetes" : { + "pod_name" : "fluent-bit-spc9q", + "namespace_name" : "fluent", + "container_name" : "fluent-bit", + "docker_id" : "0a179f94962ccdec532f386e9bb6aa090707aa528b396a3209bb5988adb2bceb", + "container_image" : "kubesphere/fluent-bit:v1.9.4" + } + } + } + ] + } +}