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 := `
import (
"encoding/json"
"fmt"
+ "strings"
"time"
)
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.
--- /dev/null
+// 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+`"`)
+ }
+}