Merge branch '12287-preserve-json-numbers' closes #12287
authorPeter Amstutz <peter.amstutz@curoverse.com>
Tue, 26 Sep 2017 19:28:32 +0000 (15:28 -0400)
committerPeter Amstutz <peter.amstutz@curoverse.com>
Tue, 26 Sep 2017 19:28:58 +0000 (15:28 -0400)
Arvados-DCO-1.1-Signed-off-by: Peter Amstutz <pamstutz@veritasgenetics.com>

services/crunch-run/crunchrun.go
services/crunch-run/crunchrun_test.go

index 6ee861b788fed4e5ef2b2969024543fb30b7c0a5..27bfa88730788402d13282c18f15a21a4d93cfae 100644 (file)
@@ -650,14 +650,11 @@ func (runner *ContainerRunner) LogContainerRecord() (err error) {
                return fmt.Errorf("While retrieving container record from the API server: %v", err)
        }
        defer reader.Close()
-       // Read the API server response as []byte
-       json_bytes, err := ioutil.ReadAll(reader)
-       if err != nil {
-               return fmt.Errorf("While reading container record API server response: %v", err)
-       }
-       // Decode the JSON []byte
+
+       dec := json.NewDecoder(reader)
+       dec.UseNumber()
        var cr map[string]interface{}
-       if err = json.Unmarshal(json_bytes, &cr); err != nil {
+       if err = dec.Decode(&cr); err != nil {
                return fmt.Errorf("While decoding the container record JSON response: %v", err)
        }
        // Re-encode it using indentation to improve readability
@@ -1304,9 +1301,8 @@ func (runner *ContainerRunner) Run() (err error) {
                runner.CrunchLog.Close()
        }()
 
-       err = runner.ArvClient.Get("containers", runner.Container.UUID, nil, &runner.Container)
+       err = runner.fetchContainerRecord()
        if err != nil {
-               err = fmt.Errorf("While getting container record: %v", err)
                return
        }
 
@@ -1369,6 +1365,24 @@ func (runner *ContainerRunner) Run() (err error) {
        return
 }
 
+// Fetch the current container record (uuid = runner.Container.UUID)
+// into runner.Container.
+func (runner *ContainerRunner) fetchContainerRecord() error {
+       reader, err := runner.ArvClient.CallRaw("GET", "containers", runner.Container.UUID, "", nil)
+       if err != nil {
+               return fmt.Errorf("error fetching container record: %v", err)
+       }
+       defer reader.Close()
+
+       dec := json.NewDecoder(reader)
+       dec.UseNumber()
+       err = dec.Decode(&runner.Container)
+       if err != nil {
+               return fmt.Errorf("error decoding container record: %v", err)
+       }
+       return nil
+}
+
 // NewContainerRunner creates a new container runner.
 func NewContainerRunner(api IArvadosClient,
        kc IKeepClient,
index 9fdc689e79d06cba29d257a93bf9bc58e8b163c2..935c61a1100f6400af82b39e57b0a28c1b1ae41b 100644 (file)
@@ -54,6 +54,7 @@ type ArvTestClient struct {
        Logs map[string]*bytes.Buffer
        sync.Mutex
        WasSetRunning bool
+       callraw       bool
 }
 
 type KeepTestClient struct {
@@ -220,17 +221,22 @@ func (client *ArvTestClient) Call(method, resourceType, uuid, action string, par
 
 func (client *ArvTestClient) CallRaw(method, resourceType, uuid, action string,
        parameters arvadosclient.Dict) (reader io.ReadCloser, err error) {
-       j := []byte(`{
-               "command": ["sleep", "1"],
-               "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
-               "cwd": ".",
-               "environment": {},
-               "mounts": {"/tmp": {"kind": "tmp"} },
-               "output_path": "/tmp",
-               "priority": 1,
-               "runtime_constraints": {}
-       }`)
-       return ioutil.NopCloser(bytes.NewReader(j)), nil
+       var j []byte
+       if method == "GET" && resourceType == "containers" && action == "" && !client.callraw {
+               j, err = json.Marshal(client.Container)
+       } else {
+               j = []byte(`{
+                       "command": ["sleep", "1"],
+                       "container_image": "d4ab34d3d4f8a72f5c4973051ae69fab+122",
+                       "cwd": ".",
+                       "environment": {},
+                       "mounts": {"/tmp": {"kind": "tmp"}, "/json": {"kind": "json", "content": {"number": 123456789123456789}}},
+                       "output_path": "/tmp",
+                       "priority": 1,
+                       "runtime_constraints": {}
+               }`)
+       }
+       return ioutil.NopCloser(bytes.NewReader(j)), err
 }
 
 func (client *ArvTestClient) Get(resourceType string, uuid string, parameters arvadosclient.Dict, output interface{}) error {
@@ -1101,7 +1107,7 @@ func (s *TestSuite) TestSetupMounts(c *C) {
        }{
                {in: "foo", out: `"foo"`},
                {in: nil, out: `null`},
-               {in: map[string]int{"foo": 123}, out: `{"foo":123}`},
+               {in: map[string]int64{"foo": 123456789123456789}, out: `{"foo":123456789123456789}`},
        } {
                i = 0
                cr.ArvMountPoint = ""
@@ -1607,3 +1613,13 @@ func (s *TestSuite) TestStderrMount(c *C) {
 
        c.Check(api.CalledWith("collection.manifest_text", "./a b1946ac92492d2347c6235b4d2611184+6 0:6:out.txt\n./b 38af5c54926b620264ab1501150cf189+5 0:5:err.txt\n"), NotNil)
 }
+
+func (s *TestSuite) TestNumberRoundTrip(c *C) {
+       cr := NewContainerRunner(&ArvTestClient{callraw: true}, &KeepTestClient{}, nil, "zzzzz-zzzzz-zzzzzzzzzzzzzzz")
+       cr.fetchContainerRecord()
+
+       jsondata, err := json.Marshal(cr.Container.Mounts["/json"].Content)
+
+       c.Check(err, IsNil)
+       c.Check(string(jsondata), Equals, `{"number":123456789123456789}`)
+}