Skip to content

Commit

Permalink
cmd/snap: allow normal snap --version on WSL 2 (#14198)
Browse files Browse the repository at this point in the history
Commit 1adae13 ("sanity: refuse to try
to do things on WSL.") disabled snap version from attempting to talk to
snapd whenever WSL was detected, as WSL 1 didn't support enough of the
Linux features for snapd to meaningfully operate. Ever since WSL 2 was
released, this is now significantly different, with almost all of the
features working out of the box.

Adjust the condition to look for WSL 1 specifically and add missing
tests checking both WSL 1 and WSL 2 behavior.

Jira: https://warthogs.atlassian.net/browse/SNAPDENG-23939

Signed-off-by: Zygmunt Krynicki <[email protected]>
  • Loading branch information
zyga authored Jul 17, 2024
1 parent 463948c commit 84400ca
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/snap/cmd_version_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func serverVersion(cli *client.Client) *client.ServerVersion {
if release.OnWSL {
if release.OnWSL && release.WSLVersion == 1 {
return &client.ServerVersion{
Version: i18n.G("unavailable"),
Series: release.Series,
Expand Down
43 changes: 43 additions & 0 deletions cmd/snap/cmd_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
. "gopkg.in/check.v1"

snap "github.com/snapcore/snapd/cmd/snap"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snapdtool"
"github.com/snapcore/snapd/testutil"
)

func (s *SnapSuite) TestVersionCommandOnClassic(c *C) {
Expand Down Expand Up @@ -73,3 +75,44 @@ func (s *SnapSuite) TestVersionCommandOnClassicNoOsVersion(c *C) {
c.Assert(s.Stdout(), Equals, "snap 4.56\nsnapd 7.89\nseries 56\narch -\n")
c.Assert(s.Stderr(), Equals, "")
}

func (s *SnapSuite) TestVersionCommandOnWSL1(c *C) {
defer MockWSL(1)()
s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
c.Error("This should not talk to snapd")
})
defer mockArgs("snap", "version")()
defer snapdtool.MockVersion("4.56")()

_, err := snap.Parser(snap.Client()).ParseArgs([]string{"version"})
c.Assert(err, IsNil)
c.Assert(s.Stdout(), testutil.Contains, "unavailable\n")
c.Assert(s.Stderr(), Equals, "")
}

func (s *SnapSuite) TestVersionCommandOnWSL2(c *C) {
defer MockWSL(2)()
s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `{"type":"sync","status-code":200,"status":"OK","result":{"on-classic":true,"os-release":{"id":"ubuntu","version-id":"12.34"},"series":"56","version":"7.89","architecture":"ia64"}}`)
})
defer mockArgs("snap", "version")()
defer snapdtool.MockVersion("4.56")()

_, err := snap.Parser(snap.Client()).ParseArgs([]string{"version"})
c.Assert(err, IsNil)
c.Assert(s.Stdout(), Not(testutil.Contains), "unavailable\n")
c.Assert(s.Stderr(), Equals, "")
}

func MockWSL(version int) (restore func()) {
oldVersion := release.WSLVersion
oldFlag := release.OnWSL

release.OnWSL = true
release.WSLVersion = version

return func() {
release.WSLVersion = oldVersion
release.OnWSL = oldFlag
}
}

0 comments on commit 84400ca

Please sign in to comment.