From 881613e3f50adcd55d9ac6759a1b4af3f9117757 Mon Sep 17 00:00:00 2001 From: Daniel Smith Date: Thu, 12 Jun 2014 12:37:35 -0700 Subject: [PATCH] First part of parsing settings client-side --- pkg/cloudcfg/cloudcfg.go | 2 +- pkg/cloudcfg/cloudcfg_test.go | 1 + pkg/cloudcfg/parse.go | 41 ++++++++++++++++ pkg/cloudcfg/parse_test.go | 88 +++++++++++++++++++++++++++++++++++ pkg/util/fake_handler.go | 11 +++-- 5 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 pkg/cloudcfg/parse.go create mode 100644 pkg/cloudcfg/parse_test.go diff --git a/pkg/cloudcfg/cloudcfg.go b/pkg/cloudcfg/cloudcfg.go index 0245fe5f7ef..55e35c466be 100644 --- a/pkg/cloudcfg/cloudcfg.go +++ b/pkg/cloudcfg/cloudcfg.go @@ -250,7 +250,7 @@ func DeleteController(name string, client client.ClientInterface) error { return err } if controller.DesiredState.Replicas != 0 { - return fmt.Errorf("controller has non-zero replicas (%d)", controller.DesiredState.Replicas) + return fmt.Errorf("controller has non-zero replicas (%d), please stop it first", controller.DesiredState.Replicas) } return client.DeleteReplicationController(name) } diff --git a/pkg/cloudcfg/cloudcfg_test.go b/pkg/cloudcfg/cloudcfg_test.go index 04dd8dd800a..f4bffb2857c 100644 --- a/pkg/cloudcfg/cloudcfg_test.go +++ b/pkg/cloudcfg/cloudcfg_test.go @@ -150,6 +150,7 @@ func TestDoRequest(t *testing.T) { fakeHandler := util.FakeHandler{ StatusCode: 200, ResponseBody: expectedBody, + T: t, } testServer := httptest.NewTLSServer(&fakeHandler) request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil) diff --git a/pkg/cloudcfg/parse.go b/pkg/cloudcfg/parse.go new file mode 100644 index 00000000000..2656bf49d73 --- /dev/null +++ b/pkg/cloudcfg/parse.go @@ -0,0 +1,41 @@ +package cloudcfg + +import ( + "encoding/json" + "fmt" + "reflect" + + "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + "gopkg.in/v1/yaml" +) + +var storageToType = map[string]reflect.Type{ + "pods": reflect.TypeOf(api.Pod{}), + "services": reflect.TypeOf(api.Service{}), + "replicationControllers": reflect.TypeOf(api.ReplicationController{}), +} + +// Takes input 'data' as either json or yaml, checks that it parses as the +// appropriate object type, and returns json for sending to the API or an +// error. +func ToWireFormat(data []byte, storage string) ([]byte, error) { + prototypeType, found := storageToType[storage] + if !found { + return nil, fmt.Errorf("unknown storage type: %v", storage) + } + + // Try parsing as json and yaml + parsed_json := reflect.New(prototypeType).Interface() + json_err := json.Unmarshal(data, parsed_json) + parsed_yaml := reflect.New(prototypeType).Interface() + yaml_err := yaml.Unmarshal(data, parsed_yaml) + + if json_err != nil && yaml_err != nil { + return nil, fmt.Errorf("Could not parse input as json (error: %v) or yaml (error: %v", json_err, yaml_err) + } + + if json_err != nil { + return json.Marshal(parsed_json) + } + return json.Marshal(parsed_yaml) +} diff --git a/pkg/cloudcfg/parse_test.go b/pkg/cloudcfg/parse_test.go new file mode 100644 index 00000000000..26bf6445f35 --- /dev/null +++ b/pkg/cloudcfg/parse_test.go @@ -0,0 +1,88 @@ +package cloudcfg + +import ( + "encoding/json" + "testing" + + "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + "gopkg.in/v1/yaml" +) + +func TestParseBadStorage(t *testing.T) { + _, err := ToWireFormat([]byte("{}"), "badstorage") + if err == nil { + t.Errorf("Expected error, received none") + } +} + +func DoParseTest(t *testing.T, storage string, obj interface{}) { + json_data, _ := json.Marshal(obj) + yaml_data, _ := yaml.Marshal(obj) + + json_got, json_err := ToWireFormat(json_data, storage) + yaml_got, yaml_err := ToWireFormat(yaml_data, storage) + + if json_err != nil { + t.Errorf("json err: %#v", json_err) + } + if yaml_err != nil { + t.Errorf("yaml err: %#v", yaml_err) + } + t.Logf("Intermediate yaml:\n%v\n", string(yaml_data)) + if string(json_got) != string(json_data) { + t.Errorf("json output didn't match:\nGot:\n%v\n\nWanted:\n%v\n", + string(json_got), string(json_data)) + } + if string(yaml_got) != string(json_data) { + t.Errorf("yaml parsed output didn't match:\nGot:\n%v\n\nWanted:\n%v\n", + string(yaml_got), string(json_data)) + } +} + +func TestParsePod(t *testing.T) { + DoParseTest(t, "pods", api.Pod{ + JSONBase: api.JSONBase{ID: "test pod"}, + DesiredState: api.PodState{ + Manifest: api.ContainerManifest{ + Id: "My manifest", + Containers: []api.Container{ + {Name: "my container"}, + }, + Volumes: []api.Volume{ + {Name: "volume"}, + }, + }, + }, + }) +} + +func TestParseService(t *testing.T) { + DoParseTest(t, "services", api.Service{ + JSONBase: api.JSONBase{ID: "my service"}, + Port: 8080, + Labels: map[string]string{ + "area": "staging", + }, + }) +} + +func TestParseController(t *testing.T) { + DoParseTest(t, "replicationControllers", api.ReplicationController{ + DesiredState: api.ReplicationControllerState{ + Replicas: 9001, + PodTemplate: api.PodTemplate{ + DesiredState: api.PodState{ + Manifest: api.ContainerManifest{ + Id: "My manifest", + Containers: []api.Container{ + {Name: "my container"}, + }, + Volumes: []api.Volume{ + {Name: "volume"}, + }, + }, + }, + }, + }, + }) +} diff --git a/pkg/util/fake_handler.go b/pkg/util/fake_handler.go index fa6a698e2f7..f6b4adb7253 100644 --- a/pkg/util/fake_handler.go +++ b/pkg/util/fake_handler.go @@ -17,7 +17,6 @@ package util import ( "io/ioutil" - "log" "net/http" ) @@ -26,12 +25,18 @@ import ( type TestInterface interface { Errorf(format string, args ...interface{}) } +type LogInterface interface { + Logf(format string, args ...interface{}) +} // FakeHandler is to assist in testing HTTP requests. type FakeHandler struct { RequestReceived *http.Request StatusCode int ResponseBody string + // For logging - you can use a *testing.T + // This will keep log messages associated with the test. + T LogInterface } func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) { @@ -40,8 +45,8 @@ func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Requ response.Write([]byte(f.ResponseBody)) bodyReceived, err := ioutil.ReadAll(request.Body) - if err != nil { - log.Printf("Received read error: %#v", err) + if err != nil && f.T != nil { + f.T.Logf("Received read error: %#v", err) } f.ResponseBody = string(bodyReceived) }