Programmatically checking connectivity to the store

With snap debug connectivity I can see human-friendly (ish) output telling me I can reach the store.

$ snap debug connectivity
Connectivity status:
 * PASS

What are the possible outputs from this command? If I wanted to programmatically check for connectivity to the store, how should I interpret this command? (i.e. will it return non-zero without connectivity?)

$ snap version
snap    2.43.3+git1.8109f8
snapd   2.43.3+git1.8109f8
series  16
ubuntu  20.04
kernel  5.4.0-9-generic

Sorry I’m not answering your question, but…

alan@KinkPad-K450:~$ snap info multipass
error: no snap found for "multipass"
alan@KinkPad-K450:~$ snap debug connectivity
Connectivity status:
 * api.snapcraft.io: unreachable
error: 1 servers unreachable

Odd that it says multipass is “not found” when in actual fact it can’t connect. I know it can’t connect because I bodged my /etc/hosts to make it broken.

alan@KinkPad-K450:~$ grep api /etc/hosts
127.0.0.1	api.snapcraft.io

Looks like it does return non-zero on lack of connectivity…

root@bionic-clean:~# vi /etc/hosts
127.0.0.1 api.snapcraft.io
<snip/>
root@bionic-clean:~# snap debug connectivity
Connectivity status:
 * api.snapcraft.io: unreachable
error: 1 servers unreachable
root@bionic-clean:~# echo $?
1
root@bionic-clean:~# vi /etc/hosts
root@bionic-clean:~# snap debug connectivity
Connectivity status:
 * PASS
root@bionic-clean:~# echo $?
0

The one thing I will say is that while it’s very unlikely we will change snap debug connectivity for any reason, it is a debug command and as such isn’t subject to the typical thing about not breaking compatibility. So if you can do this check another way, I would recommend that, we really try to keep snap debug commands for interactive debugging, not so much for programmatic checks of such things.

Perhaps some time we can try to add such a check command to the snap routine subcommand which will be stable but meant not for humans, but for tooling like xdg-desktop, see https://github.com/snapcore/snapd/pull/7589 and https://github.com/snapcore/snapd/pull/7588 for example.