snapcore / spread

Spread - Convenient full-system test (task) distribution
GNU General Public License v3.0
99 stars 58 forks source link

fix(lxd): Select server based on name from result (fixes #151) #181

Open thp-canonical opened 6 months ago

thp-canonical commented 6 months ago

The lxd help list (manpage) command help says that the filter passed in (we pass in name) can be multiple things:

To reproduce the issue, just create an LXD instance that has the same prefix as the LXD instance used, by spread, e.g. if spread creates spread-34-ubuntu-20-04, run in a separate terminal:

lxc launch ubuntu:20.04 spread-34-ubuntu-20-04-foo

After that, listing the instances might match more than one (sometimes the order appears "just right", meaning it happens that the one we're looking for just happens to be the first item):

% lxc list --format=csv spread-34-ubuntu-20-04
spread-34-ubuntu-20-04-bar,RUNNING,10.... (eth0),... (eth0),CONTAINER,0
spread-34-ubuntu-20-04,RUNNING,10.... (eth0),... (eth0),CONTAINER (EPHEMERAL),0

(--format=csv used here to make it easier to read)

Alternatively, appending a $ to the name (so it will match as a regex) would also do the trick, but this assumes that the regex-matching will always work like this, and it wouldn't accidentally match other things.

See also: #151, #154 -- I tested this on LXD 5.20

tl;dr: It could be that our exec.Command("lxc", "list", "--format=json", name) matches multiple instances. To account for that, instead of assuming it will always return a slice where the first item is the one we're interested in, iterate over the result and pick the one that matches the name exactly. If no such match exists, return lxdNoServerError (just like the previous len(sjsons) == 0 case).