Skip to content

Commit

Permalink
tests for pushgateway-error
Browse files Browse the repository at this point in the history
  • Loading branch information
deadtrickster committed Apr 15, 2016
1 parent d2bd47f commit 42eded9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
21 changes: 19 additions & 2 deletions t/pushgateway/pushgateway.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
(is (third req) nil)
(is (fourth req) nil))))

(defun test-error (address)
(subtest "ERROR"
(handler-case
(prom.pushgateway:delete "error" :gateway address)
(prom.pushgateway:pushgateway-error (e)
(ok "Pushgateway error is throwed")
(is (slot-value e 'prom.pushgateway::status-code) 404)
(is (slot-value e 'prom.pushgateway::body) "Not Found!")
(is-type (slot-value e 'prom.pushgateway::headers) 'list)))))

(subtest "PUSHGATEWAY"
(subtest "Errors & Validatoins"
(is-error-report (prom.pushgateway:delete :qwe) prom:invalid-value-error "Label value :QWE is invalid. Reason: job name is not a string")
Expand All @@ -44,7 +54,13 @@
(is-error-report (prom.pushgateway:delete "qwe" :grouping-key '(:qwe "qwe")) prom:invalid-label-name-error "Label name :QWE is invalid. Reason: label name is not a string")
(is-error-report (prom.pushgateway:delete "qwe" :grouping-key '("q/we" "qwe")) prom:invalid-label-name-error "Label name \"q/we\" is invalid. Reason: label name doesn't match regex [a-zA-Z_][a-zA-Z0-9_]*")
(is-error-report (prom.pushgateway:delete "qwe" :grouping-key '("qwe" :qwe)) prom:invalid-label-value-error "Label value :QWE is invalid. Reason: label value is not a string")
(is-error-report (prom.pushgateway:delete "qwe" :grouping-key '("qwe" "q/we")) prom:invalid-label-value-error "Label value \"q/we\" is invalid. Reason: label value contains / or %2f"))
(is-error-report (prom.pushgateway:delete "qwe" :grouping-key '("qwe" "q/we")) prom:invalid-label-value-error "Label value \"q/we\" is invalid. Reason: label value contains / or %2f")

(subtest "Pushgateway Error"
(error-class-exists prom.pushgateway:pushgateway-error)

(error-report-test prom.pushgateway:pushgateway-error ((:status-code 123 :headers 12 :body 321)
"Error talking to pushgateway. Response Code: 123, headers: 12, body: 321"))))

(subtest "API"
(let ((metrics-acceptor)
Expand All @@ -56,7 +72,8 @@
(sleep 1)
(test-replace pushgateway-address metrics-acceptor)
(test-push pushgateway-address metrics-acceptor)
(test-delete pushgateway-address metrics-acceptor))
(test-delete pushgateway-address metrics-acceptor)
(test-error pushgateway-address))
(when metrics-acceptor
(tbnl:stop metrics-acceptor :soft t))))))

Expand Down
25 changes: 18 additions & 7 deletions t/pushgateway/test-server.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@
(declare (ignore log-level format-string format-arguments)))

(defmethod tbnl:acceptor-dispatch-request ((acceptor my-acceptor) request)
(push (list (tbnl:request-method request)
(tbnl:request-uri request)
(tbnl:header-in :content-type request)
(tbnl:raw-post-data :request request :force-text t))
(my-acceptor-requests acceptor))
(setf (tbnl:return-code*) tbnl:+http-accepted+)
(tbnl:abort-request-handler))
(if (search "error" (tbnl:request-uri request))
(progn
(setf (tbnl:return-code*) tbnl:+http-not-found+)
"Error!")
(progn
(push (list (tbnl:request-method request)
(tbnl:request-uri request)
(tbnl:header-in :content-type request)
(tbnl:raw-post-data :request request :force-text t))
(my-acceptor-requests acceptor))
(setf (tbnl:return-code*) tbnl:+http-accepted+)
(tbnl:abort-request-handler))))

(defmethod tbnl::acceptor-status-message ((acceptor my-acceptor) http-status-code &rest args &key &allow-other-keys)
(case http-status-code
(404
"Not Found!")
(t (apply 'tbnl::make-cooked-message http-status-code args))))

0 comments on commit 42eded9

Please sign in to comment.