Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Commit

Permalink
improving unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GRECO, FRANK committed Aug 31, 2017
1 parent 797eee1 commit ab33242
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 5 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.1.4] - 2017-08-31
### Added
- Store implementation for mock responses.
- Slack integration for TravisCI.
- Store interface implementation for mock responses.
- Slack integration for TravisCI and Coveralls.
### Changed
- Improved test coverage.
### Removed
Expand Down
10 changes: 10 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ func (m *Metrics) Add(metrics ...Metric) {
*m = append(*m, metric)
}
}

// Get retrives a specific metric by name
func (m *Metrics) Get(name string) *Metric {
for _, metric := range *m {
if metric.Name == name {
return &metric
}
}
return nil
}
10 changes: 10 additions & 0 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ func TestAdd(t *testing.T) {
assert.Equal(t, (*f)[1].Value, "valueTwo")
assert.Equal(t, (*f)[2].Value, "valueThree")
}

func TestGet(t *testing.T) {
f := &Metrics{}
f.Add(Metric{"nameOne", "valueOne", false})
f.Add(Metric{"nameTwo", "valueTwo", true}, Metric{"nameThree", "valueThree", false})
assert.Nil(t, f.Get("nameFour"))
assert.Equal(t, f.Get("nameOne"), &Metric{"nameOne", "valueOne", false})
assert.Equal(t, f.Get("nameTwo"), &Metric{"nameTwo", "valueTwo", true})
assert.Equal(t, f.Get("nameThree"), &Metric{"nameThree", "valueThree", false})
}
108 changes: 106 additions & 2 deletions steps/mockservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,117 @@
package steps

import (
"context"
"testing"
"net/http"
"io/ioutil"
"encoding/json"

"k8s.io/kubernetes/pkg/api"
"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/api/unversioned"
"github.com/northwesternmutual/kanali/spec"
"github.com/northwesternmutual/kanali/metrics"
opentracing "github.com/opentracing/opentracing-go"
)

func TestMockServiceGetName(t *testing.T) {
assert := assert.New(t)
step := MockServiceStep{}
assert.Equal(step.GetName(), "Mock Service", "step name is incorrect")
assert.Equal(t, step.GetName(), "Mock Service", "step name is incorrect")
}

func TestMockServiceDo(t *testing.T) {
cms := getTestConfigMaps()
spec.MockResponseStore.Clear()
spec.MockResponseStore.Set(cms[0])
spec.ProxyStore.Set(spec.APIProxy{
TypeMeta: unversioned.TypeMeta{},
ObjectMeta: api.ObjectMeta{
Name: "proxy-one",
Namespace: "foo",
},
Spec: spec.APIProxySpec{
Path: "api/v1/accounts",
Target: "/foo",
Mock: &spec.Mock{
ConfigMapName: "cm-one",
},
},
})
spec.ProxyStore.Set(spec.APIProxy{
TypeMeta: unversioned.TypeMeta{},
ObjectMeta: api.ObjectMeta{
Name: "proxy-one",
Namespace: "foo",
},
Spec: spec.APIProxySpec{
Path: "api/v1/balance",
Target: "/car",
Mock: &spec.Mock{
ConfigMapName: "cm-one",
},
},
})
spec.ProxyStore.Set(spec.APIProxy{
TypeMeta: unversioned.TypeMeta{},
ObjectMeta: api.ObjectMeta{
Name: "proxy-one",
Namespace: "foo",
},
Spec: spec.APIProxySpec{
Path: "api/v1/address",
Target: "/car",
Mock: &spec.Mock{
ConfigMapName: "cm-two",
},
},
})
step := MockServiceStep{}

m := &metrics.Metrics{}
req, _ := http.NewRequest("GET", "http://foo.bar.com/api/v1/accounts", nil)
res := &http.Response{}
span := opentracing.StartSpan("test span")
err := step.Do(context.Background(), m, nil, nil, req, res, span)
assert.Nil(t, err)
assert.Equal(t, m.Get("http_response_code").Value, "200")
assert.Equal(t, res.Header.Get("Content-Type"), "application/json")
body, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, string(body), `{"foo":"bar"}`)
req, _ = http.NewRequest("GET", "http://foo.bar.com/", nil)
assert.Equal(t, step.Do(context.Background(), m, nil, nil, req, res, span).Error(), "proxy not found")
req, _ = http.NewRequest("GET", "http://foo.bar.com/api/v1/accounts/bar", nil)
assert.Nil(t, step.Do(context.Background(), m, nil, nil, req, res, span))
req, _ = http.NewRequest("GET", "http://foo.bar.com/api/v1/balance", nil)
assert.Equal(t, step.Do(context.Background(), m, nil, nil, req, res, span).Error(), "no mock response found")
req, _ = http.NewRequest("GET", "http://foo.bar.com/api/v1/address", nil)
assert.Equal(t, step.Do(context.Background(), m, nil, nil, req, res, span).Error(), "no mock response found")
}

func getTestConfigMaps() []api.ConfigMap {

mockOne, _ := json.Marshal([]spec.Route{
{
Route: "/foo",
Code: 200,
Method: "GET",
Body: map[string]interface{}{
"foo": "bar",
},
},
})

return []api.ConfigMap{
{
TypeMeta: unversioned.TypeMeta{},
ObjectMeta: api.ObjectMeta{
Name: "cm-one",
Namespace: "foo",
},
Data: map[string]string{
"response": string(mockOne),
},
},
}

}

0 comments on commit ab33242

Please sign in to comment.