mirror of https://github.com/helm/helm
parent
87d360afda
commit
05a3965602
@ -0,0 +1,40 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kubernetes/deployment-manager/cmd/manager/router"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHealthz(t *testing.T) {
|
||||||
|
c := mockContext()
|
||||||
|
s := httpHarness(c, "GET /", healthz)
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
res, err := http.Get(s.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to GET healthz: %s", err)
|
||||||
|
} else if res.StatusCode != 200 {
|
||||||
|
t.Fatalf("Unexpected status: %d", res.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Get the body and check on the content type and the body.
|
||||||
|
}
|
||||||
|
|
||||||
|
// httpHarness is a simple test server fixture.
|
||||||
|
// Simple fixture for standing up a test server with a single route.
|
||||||
|
//
|
||||||
|
// You must Close() the returned server.
|
||||||
|
func httpHarness(c *router.Context, route string, fn router.HandlerFunc) *httptest.Server {
|
||||||
|
r := router.NewRoutes()
|
||||||
|
r.Add(route, fn)
|
||||||
|
h := router.NewHandler(c, r)
|
||||||
|
return httptest.NewServer(h)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mockContext() *router.Context {
|
||||||
|
// TODO: We need mocks for credentials and manager.
|
||||||
|
return &router.Context{}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||||
|
|
||||||
|
Licensed 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 httputil provides common HTTP tools.
|
||||||
|
|
||||||
|
This package provides tools for working with HTTP requests and responses.
|
||||||
|
*/
|
||||||
|
package httputil
|
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||||
|
|
||||||
|
Licensed 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 httputil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// LogAccess is for logging access messages. Form: Access r.Method, r.URL
|
||||||
|
LogAccess = "Access: %s %s"
|
||||||
|
// LogNotFound is for logging 404 errors. Form: Not Found r.Method, r.URL
|
||||||
|
LogNotFound = "Not Found: %s %s"
|
||||||
|
// LogFatal is for logging 500 errors. Form: Internal Server Error r.Method r.URL message
|
||||||
|
LogFatal = "Internal Server Error: %s %s %s"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NotFound writes a 404 error to the client and logs an error.
|
||||||
|
func NotFound(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Printf(LogNotFound, r.Method, r.URL)
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
fmt.Fprintln(w, "File Not Found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fatal writes a 500 response to the client and logs the message.
|
||||||
|
//
|
||||||
|
// Additional arguments are past into the the formatter as params to msg.
|
||||||
|
func Fatal(w http.ResponseWriter, r *http.Request, msg string, v ...interface{}) {
|
||||||
|
log.Printf(LogFatal, r.Method, r.URL, fmt.Sprintf(msg, v...))
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Fprintln(w, "Internal Server Error")
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||||
|
|
||||||
|
Licensed 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 httputil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNotFound(t *testing.T) {
|
||||||
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
NotFound(w, r)
|
||||||
|
}
|
||||||
|
testStatusCode(http.HandlerFunc(fn), 404, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFatal(t *testing.T) {
|
||||||
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
Fatal(w, r, "fatal %s", "foo")
|
||||||
|
}
|
||||||
|
testStatusCode(http.HandlerFunc(fn), 500, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testStatusCode(fn http.HandlerFunc, expect int, t *testing.T) {
|
||||||
|
s := httptest.NewServer(fn)
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
res, err := http.Get(s.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if res.StatusCode != expect {
|
||||||
|
t.Errorf("Expected %d, got %d", expect, res.StatusCode)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue