-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package agentcfg | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/elastic/apm-server/tests" | ||
"github.com/elastic/beats/libbeat/kibana" | ||
) | ||
|
||
type m map[string]interface{} | ||
|
||
var q = Query{} | ||
|
||
func TestFetchNoClient(t *testing.T) { | ||
kb, kerr := kibana.NewKibanaClient(nil) | ||
_, _, ferr := Fetch(kb, q, kerr) | ||
require.Error(t, ferr) | ||
assert.Equal(t, ferr, kerr, kerr) | ||
} | ||
|
||
func TestFetchStringConversion(t *testing.T) { | ||
kb := tests.MockKibana(http.StatusOK, | ||
m{ | ||
"_id": "1", | ||
"_source": m{ | ||
"settings": m{ | ||
"sampling_rate": 0.5, | ||
}, | ||
}, | ||
}) | ||
result, etag, err := Fetch(kb, q, nil) | ||
require.NoError(t, err) | ||
assert.Equal(t, "1", etag, etag) | ||
assert.Equal(t, map[string]string{"sampling_rate": "0.5"}, result, result) | ||
} | ||
|
||
func TestFetchVersionCheck(t *testing.T) { | ||
kb := tests.MockKibana(http.StatusOK, m{}) | ||
kb.Connection.Version.Major = 6 | ||
_, _, err := Fetch(kb, q, nil) | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), "version") | ||
} | ||
|
||
func TestFetchError(t *testing.T) { | ||
kb := tests.MockKibana(http.StatusNotFound, m{"error": "an error"}) | ||
_, _, err := Fetch(kb, q, nil) | ||
require.Error(t, err) | ||
assert.Equal(t, err.Error(), "{\"error\":\"an error\"}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package beater | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/apm-server/convert" | ||
"github.com/elastic/apm-server/tests" | ||
) | ||
|
||
func TestAgentConfigHandlerGetOk(t *testing.T) { | ||
|
||
kb := tests.MockKibana(http.StatusOK, m{ | ||
"_id": "1", | ||
"_source": m{ | ||
"settings": m{ | ||
"sampling_rate": 0.5, | ||
}, | ||
}, | ||
}) | ||
|
||
h := agentConfigHandler(kb, "") | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/config?service.name=opbeans", nil) | ||
h.ServeHTTP(w, r) | ||
|
||
etagHeader := w.Header().Get("Etag") | ||
assert.Equal(t, http.StatusOK, w.Code, w.Body.String()) | ||
assert.Equal(t, "1", etagHeader, etagHeader) | ||
} | ||
|
||
func TestAgentConfigHandlerPostOk(t *testing.T) { | ||
|
||
kb := tests.MockKibana(http.StatusOK, m{ | ||
"_id": "1", | ||
"_source": m{ | ||
"settings": m{ | ||
"sampling_rate": 0.5, | ||
}, | ||
}, | ||
}) | ||
|
||
h := agentConfigHandler(kb, "") | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodPost, "/config", convert.ToReader(m{ | ||
"service": m{"name": "opbeans"}})) | ||
h.ServeHTTP(w, r) | ||
|
||
assert.Equal(t, http.StatusOK, w.Code, w.Body.String()) | ||
} | ||
|
||
func TestAgentConfigHandlerBadMethod(t *testing.T) { | ||
|
||
kb := tests.MockKibana(http.StatusOK, m{}) | ||
|
||
h := agentConfigHandler(kb, "") | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodPut, "/config?service.name=opbeans", nil) | ||
h.ServeHTTP(w, r) | ||
|
||
assert.Equal(t, http.StatusMethodNotAllowed, w.Code, w.Body.String()) | ||
} | ||
|
||
func TestAgentConfigHandlerNoService(t *testing.T) { | ||
|
||
kb := tests.MockKibana(http.StatusOK, m{}) | ||
|
||
h := agentConfigHandler(kb, "") | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/config", nil) | ||
h.ServeHTTP(w, r) | ||
|
||
assert.Equal(t, http.StatusBadRequest, w.Code, w.Body.String()) | ||
} | ||
|
||
func TestAgentConfigHandlerNotFound(t *testing.T) { | ||
|
||
kb := tests.MockKibana(http.StatusOK, m{}) | ||
|
||
h := agentConfigHandler(kb, "") | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/config?service.name=opbeans", nil) | ||
h.ServeHTTP(w, r) | ||
|
||
assert.Equal(t, http.StatusNotFound, w.Code, w.Body.String()) | ||
} | ||
|
||
func TestAgentConfigHandlerInternalError(t *testing.T) { | ||
|
||
kb := tests.MockKibana(http.StatusOK, m{ | ||
"_id": "1", "_source": ""}, | ||
) | ||
|
||
h := agentConfigHandler(kb, "") | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/config?service.name=opbeans", nil) | ||
h.ServeHTTP(w, r) | ||
|
||
assert.Equal(t, http.StatusInternalServerError, w.Code, w.Body.String()) | ||
} | ||
|
||
func TestAgentConfigHandlerNotModified(t *testing.T) { | ||
|
||
kb := tests.MockKibana(http.StatusOK, m{ | ||
"_id": "1", | ||
"_source": m{ | ||
"settings": m{ | ||
"sampling_rate": 0.5, | ||
}, | ||
}, | ||
}) | ||
|
||
h := agentConfigHandler(kb, "") | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/config?service.name=opbeans", nil) | ||
r.Header.Set("If-None-Match", "1") | ||
h.ServeHTTP(w, r) | ||
|
||
assert.Equal(t, http.StatusNotModified, w.Code, w.Body.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package tests | ||
|
||
import ( | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/elastic/apm-server/convert" | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/kibana" | ||
) | ||
|
||
type rt struct { | ||
resp *http.Response | ||
} | ||
|
||
func (rt rt) RoundTrip(r *http.Request) (*http.Response, error) { | ||
return rt.resp, nil | ||
} | ||
|
||
func MockKibana(respCode int, respBody map[string]interface{}) *kibana.Client { | ||
resp := http.Response{StatusCode: respCode, Body: ioutil.NopCloser(convert.ToReader(respBody))} | ||
return &kibana.Client{ | ||
Connection: kibana.Connection{ | ||
Http: &http.Client{ | ||
Transport: rt{resp: &resp}, | ||
}, | ||
Version: common.Version{Major: 10, Minor: 0}, | ||
}, | ||
} | ||
} |