mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
commit
c3da356cd6
51
pkg/cloudcfg/proxy_server_test.go
Normal file
51
pkg/cloudcfg/proxy_server_test.go
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. 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 cloudcfg
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFileServing(t *testing.T) {
|
||||
data := "This is test data"
|
||||
dir, err := ioutil.TempDir("", "data")
|
||||
expectNoError(t, err)
|
||||
err = ioutil.WriteFile(dir+"/test.txt", []byte(data), 0755)
|
||||
expectNoError(t, err)
|
||||
handler := fileServer{
|
||||
prefix: "/foo/",
|
||||
base: dir,
|
||||
}
|
||||
server := httptest.NewServer(&handler)
|
||||
client := http.Client{}
|
||||
req, err := http.NewRequest("GET", server.URL+handler.prefix+"/test.txt", nil)
|
||||
expectNoError(t, err)
|
||||
res, err := client.Do(req)
|
||||
expectNoError(t, err)
|
||||
defer res.Body.Close()
|
||||
b, err := ioutil.ReadAll(res.Body)
|
||||
expectNoError(t, err)
|
||||
if res.StatusCode != http.StatusOK {
|
||||
t.Errorf("Unexpected status: %d", res.StatusCode)
|
||||
}
|
||||
if string(b) != data {
|
||||
t.Errorf("Data doesn't match: %s vs %s", string(b), data)
|
||||
}
|
||||
}
|
@ -22,6 +22,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"gopkg.in/v1/yaml"
|
||||
)
|
||||
|
||||
@ -60,6 +61,43 @@ func TestYAMLPrinterPrint(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(testData, poutput) {
|
||||
t.Error("Test data and unmarshaled data are not equal")
|
||||
t.Errorf("Test data and unmarshaled data are not equal: %#v vs %#v", poutput, testData)
|
||||
}
|
||||
|
||||
obj := api.Pod{
|
||||
JSONBase: api.JSONBase{ID: "foo"},
|
||||
}
|
||||
buf.Reset()
|
||||
printer.PrintObj(obj, buf)
|
||||
var objOut api.Pod
|
||||
err = yaml.Unmarshal([]byte(buf.String()), &objOut)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpeted error: %#v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(obj, objOut) {
|
||||
t.Errorf("Unexpected inequality: %#v vs %#v", obj, objOut)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdentityPrinter(t *testing.T) {
|
||||
printer := &IdentityPrinter{}
|
||||
buff := bytes.NewBuffer([]byte{})
|
||||
str := "this is a test string"
|
||||
printer.Print([]byte(str), buff)
|
||||
if buff.String() != str {
|
||||
t.Errorf("Bytes are not equal: %s vs %s", str, buff.String())
|
||||
}
|
||||
|
||||
obj := api.Pod{
|
||||
JSONBase: api.JSONBase{ID: "foo"},
|
||||
}
|
||||
buff.Reset()
|
||||
printer.PrintObj(obj, buff)
|
||||
objOut, err := api.Decode([]byte(buff.String()))
|
||||
if err != nil {
|
||||
t.Errorf("Unexpeted error: %#v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(&obj, objOut) {
|
||||
t.Errorf("Unexpected inequality: %#v vs %#v", obj, objOut)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user