Skip to content

Commit

Permalink
Sentry tests (#8)
Browse files Browse the repository at this point in the history
* Improved sentry tests
  • Loading branch information
jasonrichardsmith committed Aug 24, 2018
1 parent 4959998 commit c479be0
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 6 deletions.
21 changes: 15 additions & 6 deletions sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sentry
import (
"crypto/tls"
"encoding/json"
"errors"
"flag"
"io/ioutil"
"net/http"
Expand All @@ -24,9 +25,12 @@ func init() {
}

var (
scheme = runtime.NewScheme()
codecs = serializer.NewCodecFactory(scheme)
tlscert, tlskey string
scheme = runtime.NewScheme()
codecs = serializer.NewCodecFactory(scheme)
tlscert, tlskey string
healthResponse = []byte("200 - Healthy")
wrongContentResponse = []byte("415 - Wrong Content Type")
ErrNoUID = errors.New("No UID from request")
)

type Config struct {
Expand Down Expand Up @@ -66,14 +70,14 @@ func (sh SentryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/healthz" {
log.Info("Received health check")
w.WriteHeader(http.StatusOK)
w.Write([]byte("200 - Healthy"))
w.Write(healthResponse)
return
}
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
log.Errorf("contentType=%s, expect application/json", contentType)
w.WriteHeader(http.StatusUnsupportedMediaType)
w.Write([]byte("415 - Wrong Content Type"))
w.Write(wrongContentResponse)
return
}
log.Info("Correct ContentType")
Expand All @@ -91,7 +95,12 @@ func (sh SentryHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
returnedAdmissionReview := v1beta1.AdmissionReview{}
if admissionResponse != nil {
returnedAdmissionReview.Response = admissionResponse
returnedAdmissionReview.Response.UID = receivedAdmissionReview.Request.UID
if receivedAdmissionReview.Request != nil && receivedAdmissionReview.Request.UID != "" {
returnedAdmissionReview.Response.UID = receivedAdmissionReview.Request.UID
} else {
log.Error(ErrNoUID)
returnedAdmissionReview.Response = admissionResponseError(ErrNoUID)
}
}
responseInBytes, err := json.Marshal(returnedAdmissionReview)
if err != nil {
Expand Down
115 changes: 115 additions & 0 deletions sentry/sentry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package sentry

import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"k8s.io/api/admission/v1beta1"
)

type FakeSentry struct{}

func (fs FakeSentry) Admit(ar v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {

reviewResponse := v1beta1.AdmissionResponse{}
reviewResponse.Allowed = true
return &reviewResponse
}

func TestAdmissionResponseError(t *testing.T) {
err := errors.New("test error")
ar := admissionResponseError(err)
if ar.Result.Message != err.Error() {
t.Fatal("Expected error and admission response message to match")
}
}

func TestSentryHandler(t *testing.T) {
s := SentryHandler{FakeSentry{}}
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/healthz", nil)
s.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Fatal("Expecting 200 for healthz check")
}
if w.Body == nil {
t.Fatal("Expecting body for healthz check")
}
data, err := ioutil.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(data, healthResponse) {
t.Fatal("Expected health response body")
}
r = httptest.NewRequest("POST", "/", nil)
w = httptest.NewRecorder()
s.ServeHTTP(w, r)
if w.Code != http.StatusUnsupportedMediaType {
t.Fatal("Expecting 415 for wrong content type")
}
if w.Body == nil {
t.Fatal("Expecting body for wrong content type")
}
data, err = ioutil.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(data, wrongContentResponse) {
t.Fatal("Expected wrong content type response body")
}
r.Header.Add("Content-Type", "application/json")
w = httptest.NewRecorder()
s.ServeHTTP(w, r)
rar := v1beta1.AdmissionReview{}
data, err = ioutil.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
deserializer := codecs.UniversalDeserializer()
_, _, err = deserializer.Decode(data, nil, &rar)
if err != nil {
t.Fatal(err)
}
if rar.Response.Result.Message != ErrNoUID.Error() {
t.Fatal("Expected no uid error")
}
sar := v1beta1.AdmissionReview{
Request: &v1beta1.AdmissionRequest{
UID: "test",
},
}
sarbytes, err := json.Marshal(sar)
if err != nil {
t.Fatal(err)
}
sarbytesreader := bytes.NewReader(sarbytes)
r = httptest.NewRequest("POST", "/", sarbytesreader)
r.Header.Add("Content-Type", "application/json")
s.ServeHTTP(w, r)
data, err = ioutil.ReadAll(w.Body)
if err != nil {
t.Fatal(err)
}
deserializer = codecs.UniversalDeserializer()
_, _, err = deserializer.Decode(data, nil, &rar)
if err != nil {
t.Fatal(err)
}
if rar.Response.Allowed != true {
t.Fatal("Expected allowed response")
}
}

func TestNewSentryServer(t *testing.T) {
_, err := NewSentryServer(FakeSentry{})
if err == nil {
t.Fatal("expected pem error")
}
}

0 comments on commit c479be0

Please sign in to comment.