util.EncodeJSON proven harmful, remove it everywhere

People were misusing EncodeJSON in tests when they should be using
runtime.EncodeOrDie(testapi.Codec(), obj).  Removing the potential
for cutting self on sharp objects.
This commit is contained in:
Clayton Coleman
2014-11-13 10:38:13 -05:00
parent d4108ec47e
commit 6d31c2bf8a
7 changed files with 49 additions and 81 deletions

View File

@@ -30,7 +30,6 @@ import (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/google/cadvisor/info"
)
@@ -97,6 +96,15 @@ func newServerTest() *serverTestFramework {
return fw
}
// encodeJSON returns obj marshalled as a JSON string, panicing on any errors
func encodeJSON(obj interface{}) string {
data, err := json.Marshal(obj)
if err != nil {
panic(err)
}
return string(data)
}
func readResp(resp *http.Response) (string, error) {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
@@ -124,7 +132,7 @@ func TestContainer(t *testing.T) {
},
},
}
body := bytes.NewBuffer([]byte(util.EncodeJSON(expected[0]))) // Only send a single ContainerManifest
body := bytes.NewBuffer([]byte(encodeJSON(expected[0]))) // Only send a single ContainerManifest
resp, err := http.Post(fw.testHTTPServer.URL+"/container", "application/json", body)
if err != nil {
t.Errorf("Post returned: %v", err)
@@ -199,7 +207,7 @@ func TestContainers(t *testing.T) {
},
},
}
body := bytes.NewBuffer([]byte(util.EncodeJSON(expected)))
body := bytes.NewBuffer([]byte(encodeJSON(expected)))
resp, err := http.Post(fw.testHTTPServer.URL+"/containers", "application/json", body)
if err != nil {
t.Errorf("Post returned: %v", err)