From 6e73eff3926a2e7345333edd02531e8e6fbe15ef Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Fri, 31 May 2019 16:15:05 -0400 Subject: [PATCH] 15003: Format durations as "336h", not "336h0m0s". Arvados-DCO-1.1-Signed-off-by: Tom Clegg --- lib/config/cmd_test.go | 19 ++++++++++++++ sdk/go/arvados/duration.go | 9 +++++-- sdk/go/arvados/duration_test.go | 45 +++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 sdk/go/arvados/duration_test.go diff --git a/lib/config/cmd_test.go b/lib/config/cmd_test.go index bedcc0dd8c..e4d838f85a 100644 --- a/lib/config/cmd_test.go +++ b/lib/config/cmd_test.go @@ -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 := ` diff --git a/sdk/go/arvados/duration.go b/sdk/go/arvados/duration.go index d3e11c7a5e..2696fdb051 100644 --- a/sdk/go/arvados/duration.go +++ b/sdk/go/arvados/duration.go @@ -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 index 0000000000..ee787a6a76 --- /dev/null +++ b/sdk/go/arvados/duration_test.go @@ -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+`"`) + } +} -- 2.30.2