Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jalvz committed Jun 4, 2019
1 parent 2d4e566 commit e6bbb20
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 0 deletions.
71 changes: 71 additions & 0 deletions agentcfg/fetch_test.go
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\"}")
}
147 changes: 147 additions & 0 deletions beater/agent_config_handler_test.go
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())
}
47 changes: 47 additions & 0 deletions tests/kibana.go
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},
},
}
}

0 comments on commit e6bbb20

Please sign in to comment.