1// Copyright 2012-present Oliver Eilhard. All rights reserved.
2// Use of this source code is governed by a MIT-license.
3// See http://olivere.mit-license.org/license.txt for details.
4
5package elastic
6
7import (
8	"context"
9	"testing"
10)
11
12func TestNodesStats(t *testing.T) {
13	client, err := NewClient()
14	if err != nil {
15		t.Fatal(err)
16	}
17
18	info, err := client.NodesStats().Human(true).Do(context.TODO())
19	if err != nil {
20		t.Fatal(err)
21	}
22	if info == nil {
23		t.Fatal("expected nodes stats")
24	}
25
26	if info.ClusterName == "" {
27		t.Errorf("expected cluster name; got: %q", info.ClusterName)
28	}
29	if len(info.Nodes) == 0 {
30		t.Errorf("expected some nodes; got: %d", len(info.Nodes))
31	}
32	for id, node := range info.Nodes {
33		if id == "" {
34			t.Errorf("expected node id; got: %q", id)
35		}
36		if node == nil {
37			t.Fatalf("expected node info; got: %v", node)
38		}
39		if len(node.Name) == 0 {
40			t.Errorf("expected node name; got: %q", node.Name)
41		}
42		if node.Timestamp == 0 {
43			t.Errorf("expected timestamp; got: %q", node.Timestamp)
44		}
45	}
46}
47
48func TestNodesStatsBuildURL(t *testing.T) {
49	tests := []struct {
50		NodeIds      []string
51		Metrics      []string
52		IndexMetrics []string
53		Expected     string
54	}{
55		{
56			NodeIds:      nil,
57			Metrics:      nil,
58			IndexMetrics: nil,
59			Expected:     "/_nodes/stats",
60		},
61		{
62			NodeIds:      []string{"node1"},
63			Metrics:      nil,
64			IndexMetrics: nil,
65			Expected:     "/_nodes/node1/stats",
66		},
67		{
68			NodeIds:      []string{"node1", "node2"},
69			Metrics:      nil,
70			IndexMetrics: nil,
71			Expected:     "/_nodes/node1%2Cnode2/stats",
72		},
73		{
74			NodeIds:      nil,
75			Metrics:      []string{"indices"},
76			IndexMetrics: nil,
77			Expected:     "/_nodes/stats/indices",
78		},
79		{
80			NodeIds:      nil,
81			Metrics:      []string{"indices", "jvm"},
82			IndexMetrics: nil,
83			Expected:     "/_nodes/stats/indices%2Cjvm",
84		},
85		{
86			NodeIds:      []string{"node1"},
87			Metrics:      []string{"indices", "jvm"},
88			IndexMetrics: nil,
89			Expected:     "/_nodes/node1/stats/indices%2Cjvm",
90		},
91		{
92			NodeIds:      nil,
93			Metrics:      nil,
94			IndexMetrics: []string{"fielddata"},
95			Expected:     "/_nodes/stats/_all/fielddata",
96		},
97		{
98			NodeIds:      []string{"node1"},
99			Metrics:      nil,
100			IndexMetrics: []string{"fielddata"},
101			Expected:     "/_nodes/node1/stats/_all/fielddata",
102		},
103		{
104			NodeIds:      nil,
105			Metrics:      []string{"indices"},
106			IndexMetrics: []string{"fielddata"},
107			Expected:     "/_nodes/stats/indices/fielddata",
108		},
109		{
110			NodeIds:      []string{"node1"},
111			Metrics:      []string{"indices"},
112			IndexMetrics: []string{"fielddata"},
113			Expected:     "/_nodes/node1/stats/indices/fielddata",
114		},
115		{
116			NodeIds:      []string{"node1", "node2"},
117			Metrics:      []string{"indices", "jvm"},
118			IndexMetrics: []string{"fielddata", "docs"},
119			Expected:     "/_nodes/node1%2Cnode2/stats/indices%2Cjvm/fielddata%2Cdocs",
120		},
121	}
122
123	client, err := NewClient()
124	if err != nil {
125		t.Fatal(err)
126	}
127	for i, tt := range tests {
128		svc := client.NodesStats().NodeId(tt.NodeIds...).Metric(tt.Metrics...).IndexMetric(tt.IndexMetrics...)
129		path, _, err := svc.buildURL()
130		if err != nil {
131			t.Errorf("#%d: expected no error, got %v", i, err)
132		} else {
133			if want, have := tt.Expected, path; want != have {
134				t.Errorf("#%d: expected %q, got %q", i, want, have)
135			}
136		}
137	}
138}
139