15003: Format durations as "336h", not "336h0m0s".
authorTom Clegg <tclegg@veritasgenetics.com>
Fri, 31 May 2019 20:15:05 +0000 (16:15 -0400)
committerTom Clegg <tclegg@veritasgenetics.com>
Fri, 31 May 2019 20:15:05 +0000 (16:15 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tclegg@veritasgenetics.com>

lib/config/cmd_test.go
sdk/go/arvados/duration.go
sdk/go/arvados/duration_test.go [new file with mode: 0644]

index bedcc0dd8c6c586f4f75fb8f9686ce9a2c4ffef3..e4d838f85ac26a1610012f279126d42e754c4a00 100644 (file)
@@ -79,6 +79,25 @@ Clusters:
        c.Check(stderr.String(), check.Matches, `(?ms).*unexpected object in config entry: Clusters.z1234.PostgreSQL.ConnectionPool\n.*`)
 }
 
+func (s *CommandSuite) TestDumpFormatting(c *check.C) {
+       var stdout, stderr bytes.Buffer
+       in := `
+Clusters:
+ z1234:
+  Containers:
+   CloudVMs:
+    TimeoutBooting: 600s
+  Services:
+   Controller:
+    InternalURLs:
+     http://localhost:12345: {}
+`
+       code := DumpCommand.RunCommand("arvados config-dump", nil, bytes.NewBufferString(in), &stdout, &stderr)
+       c.Check(code, check.Equals, 0)
+       c.Check(stdout.String(), check.Matches, `(?ms).*TimeoutBooting: 10m\n.*`)
+       c.Check(stdout.String(), check.Matches, `(?ms).*http://localhost:12345: {}\n.*`)
+}
+
 func (s *CommandSuite) TestDumpUnknownKey(c *check.C) {
        var stdout, stderr bytes.Buffer
        in := `
index d3e11c7a5e673aebe84cbb98a807fa0ef1806b64..2696fdb051146ca34bd311e7e29e1092b0a3723e 100644 (file)
@@ -7,6 +7,7 @@ package arvados
 import (
        "encoding/json"
        "fmt"
+       "strings"
        "time"
 )
 
@@ -27,9 +28,13 @@ func (d Duration) MarshalJSON() ([]byte, error) {
        return json.Marshal(d.String())
 }
 
-// String implements fmt.Stringer.
+// String returns a format similar to (time.Duration)String() but with
+// "0m" and "0s" removed: e.g., "1h" instead of "1h0m0s".
 func (d Duration) String() string {
-       return time.Duration(d).String()
+       s := time.Duration(d).String()
+       s = strings.Replace(s, "m0s", "m", 1)
+       s = strings.Replace(s, "h0m", "h", 1)
+       return s
 }
 
 // Duration returns a time.Duration.
diff --git a/sdk/go/arvados/duration_test.go b/sdk/go/arvados/duration_test.go
new file mode 100644 (file)
index 0000000..ee787a6
--- /dev/null
@@ -0,0 +1,45 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: Apache-2.0
+
+package arvados
+
+import (
+       "encoding/json"
+       "time"
+
+       check "gopkg.in/check.v1"
+)
+
+var _ = check.Suite(&DurationSuite{})
+
+type DurationSuite struct{}
+
+func (s *DurationSuite) TestMarshalJSON(c *check.C) {
+       var d struct {
+               D Duration
+       }
+       err := json.Unmarshal([]byte(`{"D":"1.234s"}`), &d)
+       c.Check(err, check.IsNil)
+       c.Check(d.D, check.Equals, Duration(time.Second+234*time.Millisecond))
+       buf, err := json.Marshal(d)
+       c.Check(string(buf), check.Equals, `{"D":"1.234s"}`)
+
+       for _, trial := range []struct {
+               seconds int
+               out     string
+       }{
+               {30, "30s"},
+               {60, "1m"},
+               {120, "2m"},
+               {150, "2m30s"},
+               {3600, "1h"},
+               {7201, "2h1s"},
+               {360600, "100h10m"},
+               {360610, "100h10m10s"},
+       } {
+               buf, err := json.Marshal(Duration(time.Duration(trial.seconds) * time.Second))
+               c.Check(err, check.IsNil)
+               c.Check(string(buf), check.Equals, `"`+trial.out+`"`)
+       }
+}