New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/snap: snap refresh --time with new and legacy schedules #4487
cmd/snap: snap refresh --time with new and legacy schedules #4487
Conversation
Add --timer command line switch to `snap refresh` command. The switch replaces --time (now hidden). Passing --time switch, the legacy format of the output is preserved. When using --timer instead, the output clearly indicates that what is returned is the new time schedule. Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Codecov Report
@@ Coverage Diff @@
## master #4487 +/- ##
==========================================
- Coverage 78.28% 78.18% -0.11%
==========================================
Files 453 455 +2
Lines 32334 32466 +132
==========================================
+ Hits 25312 25382 +70
- Misses 4923 4970 +47
- Partials 2099 2114 +15
Continue to review full report at Codecov.
|
A bit of a meta-question: shouldn't client.SysInfo() have the data if its the new-style of legacy schedule? Or is this purely about the prefix string (schedule or timer)? I guess I wonder if it makes sense to have something like: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but please see the little nitpick, +1
cmd/snap/cmd_snap_op.go
Outdated
if x.asksForMode() || x.asksForChannel() { | ||
return errors.New(i18n.G("--time does not take mode nor channel flags")) | ||
err := errors.New(i18n.G("--timer does not take mode nor channel flags")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: no need for err variable, just:
if x.Time
return errors.New(...)
}
return errors.New(...) // --timer error
'''
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
cmd/snap/cmd_snap_op.go
Outdated
@@ -599,7 +599,8 @@ type cmdRefresh struct { | |||
|
|||
Revision string `long:"revision"` | |||
List bool `long:"list"` | |||
Time bool `long:"time"` | |||
Time bool `long:"time" hidden:"yes"` | |||
Timer bool `long:"timer"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the PR, there's a gut feeling that this is error prone. The difference between "time" and "timer" is too subtle, and people will end up using one or the other without realizing, and getting weird behavior. I suggest just introducing the new behavior, and only displaying schedule if one is indeed set and no timer is set.
There's a very small chance that this will break someone that was trying to get the old schedule even when nothing was set, but this risk is one-off and better than the risk of having error prone behavior forever.
…y format Let the callers know whether the refresh schedule was defined using a legacy format. Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
…ules Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Adjust refresh info returned in sysinfo endpoint depending on the use of the modern 'refresh.timer' or the legacy 'refresh.schedule' settings. When the new refresh.timer setting is used only the 'timer' field will be sent. Similarly, when the legacy setting is in effect, the 'timer' field will not be sent and 'schedule' will be send instead. Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
…h info Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
cmd/snap/cmd_snap_op.go
Outdated
timerOrSchedulePrefix = "schedule" | ||
timerOrSchedule = sysinfo.Refresh.Schedule | ||
} | ||
fmt.Fprintf(Stdout, "%s: %s\n", timerOrSchedulePrefix, timerOrSchedule) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's quite verbose for what's essentially this:
if sysinfo.Refresh.Schedule != "" {
fmt.Fprintf(Stdout, "schedule: %s", sysinfo.Refresh.Schedule
} else {
fmt.Fprintf(Stdout, "timer: %s", sysinfo.Refresh.Timer)
}
Note that the server should never send a schedule if timer is set, or the reverse.
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests need adjustments:
+ '[' true = false ']'
+ snap install test-snapd-control-consumer
test-snapd-control-consumer 1.0 from 'testrootorg' installed
Snap test-snapd-control-consumer is no longer tracking stable.
+ snap interfaces
Slot Plug
[snip]
:snapd-control test-snapd-control-consumer,test-snapd-control-consumer:snapd-control-with-manage
+ echo 'When the snapd-control-with-manage plug is connected'
When the snapd-control-with-manage plug is connected
+ snap connect test-snapd-control-consumer:snapd-control-with-manage
+ echo 'Then the system knows it can be set to managed'
Then the system knows it can be set to managed
+ snap debug can-manage-refreshes
+ MATCH true
+ echo 'Then the core refresh.schedule can be set to '\''managed'\'''
Then the core refresh.schedule can be set to 'managed'
+ snap set core refresh.schedule=managed
+ journalctl -u snapd
+ grep 'cannot parse "managed"'
+ snap refresh --time
+ MATCH 'schedule: managed'
error: pattern not found, got:
timer: managed
last: 2018-01-24T15:50:00Z
next: 2018-01-25T19:22:00Z
…figuration setting Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this update! Looks good, I put some nitpicks inside but really no blockers, feel free to ignore.
client/client.go
Outdated
Schedule string `json:"schedule"` | ||
// Schedule contains the legacy refresh.schedule setting. The field is | ||
// omitted when the legacy setting is not used. | ||
Schedule string `json:"schedule,omitempty"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nitpick^10) I would put schedule and timer close to each other, probably move schedule at the end (under timer).
cmd/snap/cmd_snap_op.go
Outdated
@@ -665,7 +665,11 @@ func (x *cmdRefresh) showRefreshTimes() error { | |||
return err | |||
} | |||
|
|||
fmt.Fprintf(Stdout, "schedule: %s\n", sysinfo.Refresh.Schedule) | |||
if sysinfo.Refresh.Schedule != "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nitpick^2) I wonder if this should be the other way around (even though it does not make a practical difference I suppose). I.e. to make clear that sysinfo.Refresh.Timer
is the thing that always wins if it is set?
We should also probably show an (internal) error if no refresh schedule is set at all (and add a test for this).
overlord/snapstate/autorefresh.go
Outdated
// RefreshSchedule will return a user visible string with the current schedule | ||
// for the automatic refreshes and a flag indicating whether the schedule is a | ||
// legacy one. | ||
func (m *autoRefresh) RefreshSchedule() (string, bool, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would name the return values now to make clear what the "bool" stands for, something like func ... (schedule string, legacy bool, err error)
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
…schedule are empty Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with just trivials, thanks!
cmd/snap/cmd_snap_op.go
Outdated
} else if sysinfo.Refresh.Schedule != "" { | ||
fmt.Fprintf(Stdout, "schedule: %s\n", sysinfo.Refresh.Schedule) | ||
} else { | ||
return errors.New("internal error, both refresh.timer and refresh.schedule are empty") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either this is incorrect, or the documentation on the fields above is incorrect, since they disagree. The documentation for the fields says that for both fields, the value is omitted if not in use. That seems to imply that it is possible for them both to be empty if the user hasn't set either.
As an aside, in those cases I think we use a colon after internal error ("internal error: ...").
Next: formatRefreshTime(nextRefresh), | ||
} | ||
if !legacySchedule { | ||
refreshInfo.Timer = refreshScheduleStr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good. Seems to be the field documentation that is wrong. I suggest just dropping the "The field is omitted" part of both comments. The desired intention there seems to be implied by omitempty already.
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
Is this good to be merged? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with one word change, feel free to push in another branch if you want.
overlord/snapstate/snapmgr.go
Outdated
@@ -366,10 +366,10 @@ func (m *SnapManager) LastRefresh() (time.Time, error) { | |||
return m.autoRefresh.LastRefresh() | |||
} | |||
|
|||
// RefreshSchedule returns the current refresh schedule as a string | |||
// suitable to display to a user. | |||
// RefreshSchedule returns the current refresh schedule as a string suitable to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: suitable for
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
merge? |
Add --timer command line switch to
snap refresh
command. The switch replaces--time (now hidden). Passing --time switch, the legacy format of the output is
preserved. When using --timer instead, the output clearly indicates that what is
returned is the new time schedule.