2015-04-06 18:57:06 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2015-04-06 18:57:06 +00:00
|
|
|
|
|
|
|
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 rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2015-09-09 09:44:13 +00:00
|
|
|
"reflect"
|
2015-04-06 18:57:06 +00:00
|
|
|
"testing"
|
2015-09-09 09:44:13 +00:00
|
|
|
|
2015-12-10 18:32:29 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-09-09 09:44:13 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/errors"
|
2015-04-06 18:57:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInputStreamReader(t *testing.T) {
|
|
|
|
resultString := "Test output"
|
|
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Write([]byte(resultString))
|
|
|
|
}))
|
2016-04-21 11:50:55 +00:00
|
|
|
defer s.Close()
|
2015-04-06 18:57:06 +00:00
|
|
|
u, err := url.Parse(s.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error parsing server URL: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
streamer := &LocationStreamer{
|
|
|
|
Location: u,
|
|
|
|
}
|
2015-06-17 00:10:58 +00:00
|
|
|
readCloser, _, _, err := streamer.InputStream("", "")
|
2015-04-06 18:57:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when getting stream: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer readCloser.Close()
|
|
|
|
result, err := ioutil.ReadAll(readCloser)
|
|
|
|
if string(result) != resultString {
|
|
|
|
t.Errorf("Stream content does not match. Got: %s. Expected: %s.", string(result), resultString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputStreamNullLocation(t *testing.T) {
|
|
|
|
streamer := &LocationStreamer{
|
|
|
|
Location: nil,
|
|
|
|
}
|
2015-06-17 00:10:58 +00:00
|
|
|
readCloser, _, _, err := streamer.InputStream("", "")
|
2015-04-06 18:57:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when getting stream with null location: %v", err)
|
|
|
|
}
|
|
|
|
if readCloser != nil {
|
|
|
|
t.Errorf("Expected stream to be nil. Got: %#v", readCloser)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testTransport struct {
|
|
|
|
body string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *testTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
r := bufio.NewReader(bytes.NewBufferString(tt.body))
|
|
|
|
return http.ReadResponse(r, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeTransport(mime, message string) http.RoundTripper {
|
|
|
|
content := fmt.Sprintf("HTTP/1.1 200 OK\nContent-Type: %s\n\n%s", mime, message)
|
|
|
|
return &testTransport{body: content}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputStreamContentType(t *testing.T) {
|
|
|
|
location, _ := url.Parse("http://www.example.com")
|
|
|
|
streamer := &LocationStreamer{
|
|
|
|
Location: location,
|
|
|
|
Transport: fakeTransport("application/json", "hello world"),
|
|
|
|
}
|
2015-06-17 00:10:58 +00:00
|
|
|
readCloser, _, contentType, err := streamer.InputStream("", "")
|
2015-04-06 18:57:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when getting stream: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer readCloser.Close()
|
|
|
|
if contentType != "application/json" {
|
|
|
|
t.Errorf("Unexpected content type. Got: %s. Expected: application/json", contentType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputStreamTransport(t *testing.T) {
|
|
|
|
message := "hello world"
|
|
|
|
location, _ := url.Parse("http://www.example.com")
|
|
|
|
streamer := &LocationStreamer{
|
|
|
|
Location: location,
|
|
|
|
Transport: fakeTransport("text/plain", message),
|
|
|
|
}
|
2015-06-17 00:10:58 +00:00
|
|
|
readCloser, _, _, err := streamer.InputStream("", "")
|
2015-04-06 18:57:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when getting stream: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer readCloser.Close()
|
|
|
|
result, err := ioutil.ReadAll(readCloser)
|
|
|
|
if string(result) != message {
|
|
|
|
t.Errorf("Stream content does not match. Got: %s. Expected: %s.", string(result), message)
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 09:44:13 +00:00
|
|
|
|
|
|
|
func fakeInternalServerErrorTransport(mime, message string) http.RoundTripper {
|
|
|
|
content := fmt.Sprintf("HTTP/1.1 500 \"Internal Server Error\"\nContent-Type: %s\n\n%s", mime, message)
|
|
|
|
return &testTransport{body: content}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInputStreamInternalServerErrorTransport(t *testing.T) {
|
|
|
|
message := "Pod is in PodPending"
|
|
|
|
location, _ := url.Parse("http://www.example.com")
|
|
|
|
streamer := &LocationStreamer{
|
|
|
|
Location: location,
|
|
|
|
Transport: fakeInternalServerErrorTransport("text/plain", message),
|
2015-12-10 18:32:29 +00:00
|
|
|
ResponseChecker: NewGenericHttpResponseChecker(api.Resource(""), ""),
|
2015-09-09 09:44:13 +00:00
|
|
|
}
|
|
|
|
expectedError := errors.NewInternalError(fmt.Errorf("%s", message))
|
|
|
|
|
|
|
|
_, _, _, err := streamer.InputStream("", "")
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("unexpected non-error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(err, expectedError) {
|
|
|
|
t.Errorf("StreamInternalServerError does not match. Got: %s. Expected: %s.", err, expectedError)
|
|
|
|
}
|
|
|
|
}
|