-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
o/h/ctlcmd: add a snapctl fail command
The snapctl fail commands rejects changes in a registry transaction. Signed-off-by: Miguel Pires <[email protected]>
- Loading branch information
1 parent
ac897ee
commit dfed99a
Showing
4 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2024 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package ctlcmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/snapcore/snapd/i18n" | ||
"github.com/snapcore/snapd/overlord/registrystate" | ||
) | ||
|
||
type failCommand struct { | ||
baseCommand | ||
|
||
Positional struct { | ||
Reason string `positional-args:"true" positional-arg-name:":<rejection-reason>"` | ||
} `positional-args:"yes" required:"yes"` | ||
} | ||
|
||
var shortFailHelp = i18n.G("Fail a registry change") | ||
var longFailHelp = i18n.G(` | ||
The fail command rejects the registry changes currently being proposed, | ||
providing a reason for the rejection. It may only be used in a | ||
change-view-<plug> hook. | ||
`) | ||
|
||
func init() { | ||
addCommand("fail", shortFailHelp, longFailHelp, func() command { | ||
return &failCommand{} | ||
}) | ||
} | ||
|
||
func (c *failCommand) Execute(args []string) error { | ||
ctx, err := c.ensureContext() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx.Lock() | ||
defer ctx.Unlock() | ||
|
||
if ctx.IsEphemeral() || !strings.HasPrefix(ctx.HookName(), "change-view-") { | ||
return errors.New(i18n.G("cannot use `snapctl fail` outside of a \"change-view\" hook")) | ||
} | ||
|
||
t, _ := ctx.Task() | ||
tx, commitTask, err := registrystate.GetStoredTransaction(t) | ||
if err != nil { | ||
return fmt.Errorf(i18n.G("internal error: cannot get registry transaction to fail: %v"), err) | ||
} | ||
|
||
tx.Abort(ctx.InstanceName(), c.Positional.Reason) | ||
commitTask.Set("registry-transaction", tx) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2024 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package ctlcmd_test | ||
|
||
import ( | ||
. "gopkg.in/check.v1" | ||
|
||
"github.com/snapcore/snapd/i18n" | ||
"github.com/snapcore/snapd/overlord/hookstate" | ||
"github.com/snapcore/snapd/overlord/hookstate/ctlcmd" | ||
"github.com/snapcore/snapd/overlord/registrystate" | ||
"github.com/snapcore/snapd/snap" | ||
) | ||
|
||
func (s *registrySuite) TestFailAbortsRegistryTransaction(c *C) { | ||
s.state.Lock() | ||
chg := s.state.NewChange("test", "") | ||
commitTask := s.state.NewTask("commit-registry-tx", "") | ||
chg.AddTask(commitTask) | ||
tx, err := registrystate.NewTransaction(s.state, "my-acc", "my-reg") | ||
c.Assert(err, IsNil) | ||
|
||
err = tx.Set("foo", "bar") | ||
c.Assert(err, IsNil) | ||
|
||
commitTask.Set("registry-transaction", tx) | ||
|
||
task := s.state.NewTask("run-hook", "") | ||
chg.AddTask(task) | ||
setup := &hookstate.HookSetup{Snap: "test-snap", Revision: snap.R(1), Hook: "change-view-plug"} | ||
task.Set("commit-task", commitTask.ID()) | ||
s.state.Unlock() | ||
|
||
mockContext, err := hookstate.NewContext(task, s.state, setup, s.mockHandler, "") | ||
c.Assert(err, IsNil) | ||
|
||
stdout, stderr, err := ctlcmd.Run(mockContext, []string{"fail", "don't like changes"}, 0) | ||
c.Assert(err, IsNil) | ||
c.Check(stdout, IsNil) | ||
c.Check(stderr, IsNil) | ||
|
||
tx = nil | ||
s.state.Lock() | ||
err = commitTask.Get("registry-transaction", &tx) | ||
s.state.Unlock() | ||
c.Assert(err, IsNil) | ||
|
||
snap, reason := tx.AbortInfo() | ||
c.Assert(reason, Equals, "don't like changes") | ||
c.Assert(snap, Equals, "test-snap") | ||
} | ||
|
||
func (s *registrySuite) TestFailErrors(c *C) { | ||
stdout, stderr, err := ctlcmd.Run(s.mockContext, []string{"fail"}, 0) | ||
c.Assert(err, ErrorMatches, i18n.G("the required argument `:<rejection-reason>` was not provided")) | ||
c.Check(stdout, IsNil) | ||
c.Check(stderr, IsNil) | ||
|
||
s.state.Lock() | ||
task := s.state.NewTask("run-hook", "my test task") | ||
setup := &hookstate.HookSetup{Snap: "test-snap", Revision: snap.R(1), Hook: "other-hook"} | ||
s.state.Unlock() | ||
|
||
s.mockContext, err = hookstate.NewContext(task, s.state, setup, s.mockHandler, "") | ||
c.Assert(err, IsNil) | ||
|
||
stdout, stderr, err = ctlcmd.Run(s.mockContext, []string{"fail", "reason"}, 0) | ||
c.Assert(err, ErrorMatches, i18n.G("cannot use `snapctl fail` outside of a \"change-view\" hook")) | ||
c.Check(stdout, IsNil) | ||
c.Check(stderr, IsNil) | ||
|
||
setup = &hookstate.HookSetup{Snap: "test-snap", Revision: snap.R(1), Hook: "change-view-plug"} | ||
s.mockContext, err = hookstate.NewContext(task, s.state, setup, s.mockHandler, "") | ||
c.Assert(err, IsNil) | ||
|
||
stdout, stderr, err = ctlcmd.Run(s.mockContext, []string{"fail", "reason"}, 0) | ||
// this shouldn't happen but check we handle it well anyway | ||
c.Assert(err, ErrorMatches, i18n.G("internal error: cannot get registry transaction to fail: no state entry for key \"commit-task\"")) | ||
c.Check(stdout, IsNil) | ||
c.Check(stderr, IsNil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters