From 60ce1296d4b78c31ffb97df99013b1938a433f0d Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Tue, 24 Jun 2014 17:41:30 -0700 Subject: [PATCH] Added some tests --- pkg/cloudcfg/proxy_server_test.go | 51 +++++++++++++++++++++++++++ pkg/cloudcfg/resource_printer_test.go | 40 ++++++++++++++++++++- 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 pkg/cloudcfg/proxy_server_test.go diff --git a/pkg/cloudcfg/proxy_server_test.go b/pkg/cloudcfg/proxy_server_test.go new file mode 100644 index 00000000000..4cf90aa0a5a --- /dev/null +++ b/pkg/cloudcfg/proxy_server_test.go @@ -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) + } +} diff --git a/pkg/cloudcfg/resource_printer_test.go b/pkg/cloudcfg/resource_printer_test.go index b5dfac20b89..77c4beaa425 100644 --- a/pkg/cloudcfg/resource_printer_test.go +++ b/pkg/cloudcfg/resource_printer_test.go @@ -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) } }