Skip to content

Commit aa792c2

Browse files
committed
add unit tests
1 parent 2d4e566 commit aa792c2

File tree

3 files changed

+266
-0
lines changed

3 files changed

+266
-0
lines changed

agentcfg/fetch_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package agentcfg
19+
20+
import (
21+
"net/http"
22+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
26+
27+
"github.com/elastic/apm-server/tests"
28+
"github.com/elastic/beats/libbeat/kibana"
29+
)
30+
31+
type m map[string]interface{}
32+
33+
var q = Query{}
34+
35+
func TestFetchNoClient(t *testing.T) {
36+
kb, kerr := kibana.NewKibanaClient(nil)
37+
_, _, ferr := Fetch(kb, q, kerr)
38+
require.Error(t, ferr)
39+
assert.Equal(t, ferr, kerr, kerr)
40+
}
41+
42+
func TestFetchStringConversion(t *testing.T) {
43+
kb := tests.MockKibana(http.StatusOK,
44+
m{
45+
"_id": "1",
46+
"_source": m{
47+
"settings": m{
48+
"sampling_rate": 0.5,
49+
},
50+
},
51+
})
52+
result, etag, err := Fetch(kb, q, nil)
53+
require.NoError(t, err)
54+
assert.Equal(t, "1", etag, etag)
55+
assert.Equal(t, map[string]string{"sampling_rate": "0.5"}, result, result)
56+
}
57+
58+
func TestFetchVersionCheck(t *testing.T) {
59+
kb := tests.MockKibana(http.StatusOK, m{})
60+
kb.Connection.Version.Major = 6
61+
_, _, err := Fetch(kb, q, nil)
62+
require.Error(t, err)
63+
assert.Contains(t, err.Error(), "version")
64+
}
65+
66+
func TestFetchError(t *testing.T) {
67+
kb := tests.MockKibana(http.StatusNotFound, m{"error": "an error"})
68+
_, _, err := Fetch(kb, q, nil)
69+
require.Error(t, err)
70+
assert.Equal(t, err.Error(), "{\"error\":\"an error\"}")
71+
}

beater/agent_config_handler_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package beater
19+
20+
import (
21+
"net/http"
22+
"net/http/httptest"
23+
"testing"
24+
25+
"github.com/stretchr/testify/assert"
26+
27+
"github.com/elastic/apm-server/convert"
28+
"github.com/elastic/apm-server/tests"
29+
)
30+
31+
func TestAgentConfigHandlerGetOk(t *testing.T) {
32+
33+
kb := tests.MockKibana(http.StatusOK, m{
34+
"_id": "1",
35+
"_source": m{
36+
"settings": m{
37+
"sampling_rate": 0.5,
38+
},
39+
},
40+
})
41+
42+
h := agentConfigHandler(kb, "")
43+
44+
w := httptest.NewRecorder()
45+
r := httptest.NewRequest(http.MethodGet, "/config?service.name=opbeans", nil)
46+
h.ServeHTTP(w, r)
47+
48+
etagHeader := w.Header().Get("Etag")
49+
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
50+
assert.Equal(t, "1", etagHeader, etagHeader)
51+
}
52+
53+
func TestAgentConfigHandlerPostOk(t *testing.T) {
54+
55+
kb := tests.MockKibana(http.StatusOK, m{
56+
"_id": "1",
57+
"_source": m{
58+
"settings": m{
59+
"sampling_rate": 0.5,
60+
},
61+
},
62+
})
63+
64+
h := agentConfigHandler(kb, "")
65+
66+
w := httptest.NewRecorder()
67+
r := httptest.NewRequest(http.MethodPost, "/config", convert.ToReader(m{
68+
"service": m{"name": "opbeans"}}))
69+
h.ServeHTTP(w, r)
70+
71+
assert.Equal(t, http.StatusOK, w.Code, w.Body.String())
72+
}
73+
74+
func TestAgentConfigHandlerBadMethod(t *testing.T) {
75+
76+
kb := tests.MockKibana(http.StatusOK, m{})
77+
78+
h := agentConfigHandler(kb, "")
79+
80+
w := httptest.NewRecorder()
81+
r := httptest.NewRequest(http.MethodPut, "/config?service.name=opbeans", nil)
82+
h.ServeHTTP(w, r)
83+
84+
assert.Equal(t, http.StatusMethodNotAllowed, w.Code, w.Body.String())
85+
}
86+
87+
func TestAgentConfigHandlerNoService(t *testing.T) {
88+
89+
kb := tests.MockKibana(http.StatusOK, m{})
90+
91+
h := agentConfigHandler(kb, "")
92+
93+
w := httptest.NewRecorder()
94+
r := httptest.NewRequest(http.MethodGet, "/config", nil)
95+
h.ServeHTTP(w, r)
96+
97+
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body.String())
98+
}
99+
100+
func TestAgentConfigHandlerNotFound(t *testing.T) {
101+
102+
kb := tests.MockKibana(http.StatusOK, m{})
103+
104+
h := agentConfigHandler(kb, "")
105+
106+
w := httptest.NewRecorder()
107+
r := httptest.NewRequest(http.MethodGet, "/config?service.name=opbeans", nil)
108+
h.ServeHTTP(w, r)
109+
110+
assert.Equal(t, http.StatusNotFound, w.Code, w.Body.String())
111+
}
112+
113+
func TestAgentConfigHandlerInternalError(t *testing.T) {
114+
115+
kb := tests.MockKibana(http.StatusOK, m{
116+
"_id": "1", "_source": ""},
117+
)
118+
119+
h := agentConfigHandler(kb, "")
120+
121+
w := httptest.NewRecorder()
122+
r := httptest.NewRequest(http.MethodGet, "/config?service.name=opbeans", nil)
123+
h.ServeHTTP(w, r)
124+
125+
assert.Equal(t, http.StatusInternalServerError, w.Code, w.Body.String())
126+
}
127+
128+
func TestAgentConfigHandlerNotModified(t *testing.T) {
129+
130+
kb := tests.MockKibana(http.StatusOK, m{
131+
"_id": "1",
132+
"_source": m{
133+
"settings": m{
134+
"sampling_rate": 0.5,
135+
},
136+
},
137+
})
138+
139+
h := agentConfigHandler(kb, "")
140+
141+
w := httptest.NewRecorder()
142+
r := httptest.NewRequest(http.MethodGet, "/config?service.name=opbeans", nil)
143+
r.Header.Set("If-None-Match", "1")
144+
h.ServeHTTP(w, r)
145+
146+
assert.Equal(t, http.StatusNotModified, w.Code, w.Body.String())
147+
}

tests/kibana.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package tests
19+
20+
import (
21+
"io/ioutil"
22+
"net/http"
23+
24+
"github.com/elastic/apm-server/convert"
25+
"github.com/elastic/beats/libbeat/common"
26+
"github.com/elastic/beats/libbeat/kibana"
27+
)
28+
29+
type rt struct {
30+
resp *http.Response
31+
}
32+
33+
func (rt rt) RoundTrip(r *http.Request) (*http.Response, error) {
34+
return rt.resp, nil
35+
}
36+
37+
// provides a mocked Kibana connection for unit tests
38+
func MockKibana(respCode int, respBody map[string]interface{}) *kibana.Client {
39+
resp := http.Response{StatusCode: respCode, Body: ioutil.NopCloser(convert.ToReader(respBody))}
40+
return &kibana.Client{
41+
Connection: kibana.Connection{
42+
Http: &http.Client{
43+
Transport: rt{resp: &resp},
44+
},
45+
Version: common.Version{Major: 10, Minor: 0},
46+
},
47+
}
48+
}

0 commit comments

Comments
 (0)