Skip to content
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

tests: use gojq - part 1 #14686

Merged
merged 53 commits into from
Nov 18, 2024
Merged

Conversation

bboozzoo
Copy link
Contributor

@bboozzoo bboozzoo commented Nov 4, 2024

https://github.com/itchyny/gojq is almost-compatible replacement for jq, with additional features such as native support for YAML input and output.

Attempt to switch the tests to use gojq instead of jq. Where appropriate drop use remarshal, since YAML support is built in.

Related: SNAPDENG-33306

@bboozzoo bboozzoo added ⛔ Blocked Run nested The PR also runs tests inluded in nested suite labels Nov 4, 2024
@github-actions github-actions bot added the Run Nested -auto- Label automatically added in case nested tests need to be executed label Nov 4, 2024
Copy link

codecov bot commented Nov 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.03%. Comparing base (96ea7b0) to head (2706405).
Report is 80 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #14686      +/-   ##
==========================================
+ Coverage   78.95%   79.03%   +0.07%     
==========================================
  Files        1084     1087       +3     
  Lines      146638   147628     +990     
==========================================
+ Hits       115773   116672     +899     
- Misses      23667    23728      +61     
- Partials     7198     7228      +30     
Flag Coverage Δ
unittests 79.03% <ø> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First pass mostly looks good. I left a few questions about things that seemed like they could be broken out to another polar quest later.

# - with --yaml-input, can parse YAML
GOBIN=$PROJECT_PATH/tests/bin \
CGO_ENABLED=0 \
go install github.com/itchyny/gojq/cmd/[email protected]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are those built with CGO disabled?

Comment on lines 31 to 43
snap ack "$TESTSLIB/assertions/developer1.account"
snap ack "$TESTSLIB/assertions/developer1.account-key"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this related?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cherry picked along with store-state fixes into #14725


execute: |
inspect_connection() {
CONN="$1"
# shellcheck disable=SC2002
cat /var/lib/snapd/state.json | jq --arg CONN "$CONN" -r '.data["conns"] | has($CONN)'
# shellcheck disable=SC2002,SC2016
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is 2016?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.shellcheck.net/wiki/SC2016 I have no clue why it wasn't picked up before due to -r '... | has ($CONN)'

@@ -1,6 +1,7 @@
name: SNAPNAME
summary: generic snap
version: '1.0'
base: core22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add this in the other pass later?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to avoid pulling in core because it masked bugs and wrong assumptions in the tests.

@@ -40,6 +29,10 @@ execute: |
snap info core | MATCH "store-url:.*https://snapcraft.io"
fi

# install dependecies before switching to fake store
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems related to the base change, but I don't really understand the significance. Can we add this in a second pass later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, fake store does not forward install requests to the real store. The test snap installed here had no base, hence defaulted to 'core'. We installed jq & remarshal both of which pulled in 'core'. Since the install happened before switching over to fake store, it all worked by accident as the dependencies happened to be satisfied. Without jq or remarshal nothing pulls in the dependencies. That's why the snap's base was switched to core22 too, just to avoid pulling in core.

@bboozzoo bboozzoo marked this pull request as ready for review November 12, 2024 12:14
@bboozzoo bboozzoo force-pushed the bboozzoo/jq-to-gojq branch 2 times, most recently from e781775 to fbe5e86 Compare November 13, 2024 11:13
Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two comments:

Let's try to separate changes to bases to another PR so that it is clear they are not a factor here. Use core if you have to have something.

The store endpoint for find is sometimes throttled, perhaps we can use info instead?

@bboozzoo bboozzoo changed the title tests: use gojq [WIP] tests: use gojq - part 1 Nov 13, 2024
Copy link
Member

@olivercalder olivercalder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think I spotted a few typos, but otherwise looks good.

Comment on lines 76 to 77
# slurp our two json files together into one json document, then convert
# back to yaml and write out to the snap.yaml in the unpacked gadget snap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# slurp our two json files together into one json document, then convert
# back to yaml and write out to the snap.yaml in the unpacked gadget snap
# slurp our two yaml files together into one yaml document, then write
# out to the snap.yaml in the unpacked gadget snap

# back to yaml and write out to the snap.yaml in the unpacked gadget snap
jq -s '.[0] * .[1]' <(cat snap-yaml-extras.json) <(cat pc-gadget/meta/snap.json) | json2yaml | tee pc-gadget/meta/snap.yaml
gojq -s --yaml-input --yaml-output '.[0] * .[1]' <(cat snap-yaml-extras.json) <(cat pc-gadget/meta/snap.json) | \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
gojq -s --yaml-input --yaml-output '.[0] * .[1]' <(cat snap-yaml-extras.json) <(cat pc-gadget/meta/snap.json) | \
gojq -s --yaml-input --yaml-output '.[0] * .[1]' <(cat snap-yaml-extras.yaml) <(cat pc-gadget/meta/snap.yaml) | \

Comment on lines 77 to 78
# slurp our two json files together into one json document, then convert
# back to yaml and write out to the snap.yaml in the unpacked gadget snap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# slurp our two json files together into one json document, then convert
# back to yaml and write out to the snap.yaml in the unpacked gadget snap
# slurp our two yaml files together into one taml document, then write
# out to the snap.yaml in the unpacked gadget snap

Copy link
Contributor

@maykathm maykathm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work

@bboozzoo
Copy link
Contributor Author

I've rebased the branch on top of #14725

Copy link
Member

@olivercalder olivercalder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks!

Signed-off-by: Maciej Borzecki <[email protected]>
Signed-off-by: Maciej Borzecki <[email protected]>
Signed-off-by: Maciej Borzecki <[email protected]>
Signed-off-by: Maciej Borzecki <[email protected]>
…nder tests/bin

Since gojq may be running on a different system, make sure to build it
statically. Also place the binary under $PROJECT_PATH/tests/bin which is
automaticall added to $PATH.

Signed-off-by: Maciej Borzecki <[email protected]>
…save-via-hook: use gojq

Signed-off-by: Maciej Borzecki <[email protected]>
…save-via-hook: use gojq

Signed-off-by: Maciej Borzecki <[email protected]>
@bboozzoo
Copy link
Contributor Author

Failures:

  • google:ubuntu-20.04-64:tests/main/interfaces-microstack-support - unrelated
  • google-distro-1:debian-11-64:tests/main/registries-cross-config - unrelated
  • google*:ubuntu--64:tests/main/apparmor-prompting-integration-tests: - unrelated
  • google-nested:ubuntu-20.04-64:tests/nested/manual/snapd-removes-vulnerable-snap-confine-revs:*
    bunch of unrelated UC24 nested:
  • google-nested:ubuntu-24.04-64:tests/nested/core/core20-kernel-reseal
  • google-nested:ubuntu-24.04-64:tests/nested/manual/core20-early-config
  • google-nested:ubuntu-24.04-64:tests/nested/manual/core20-initramfs-time-moves-forward
  • google-nested:ubuntu-24.04-64:tests/nested/manual/muinstaller-core:install_optional_all
  • google-nested:ubuntu-24.04-64:tests/nested/manual/muinstaller-core:install_optional_snap
  • google-nested:ubuntu-24.04-64:tests/nested/manual/muinstaller-core:plain
  • google-nested:ubuntu-24.04-64:tests/nested/manual/muinstaller-real:partial
  • google-nested:ubuntu-24.04-64:tests/nested/manual/muinstaller-real:plain
  • google-nested:ubuntu-24.04-64:tests/nested/manual/remodel-min-size
  • google-nested:ubuntu-24.04-64:tests/nested/manual/core20-4k-sector-size:logical
  • google-nested:ubuntu-24.04-64:tests/nested/manual/devmode-snap-seeded-dangerous
  • google-nested:ubuntu-24.04-64:tests/nested/manual/hybrid-remodel

Copy link
Contributor

@zyga zyga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Thank you for pushing this forward.

@bboozzoo bboozzoo added this to the 2.67 milestone Nov 18, 2024
@ernestl ernestl merged commit ede0457 into canonical:master Nov 18, 2024
47 of 56 checks passed
sergiocazzolato added a commit to sergiocazzolato/snapd that referenced this pull request Nov 18, 2024
@bboozzoo bboozzoo deleted the bboozzoo/jq-to-gojq branch November 19, 2024 12:36
sespiros added a commit to sespiros/snapd that referenced this pull request Nov 19, 2024
* master: (44 commits)
  wrappers: do not reload activation units (canonical#14724)
  gadget/install: support for no{exec,dev,suid} mount flags
  interfaces/builtin/mount_control: add support for nfs mounts (canonical#14694)
  tests: use gojq - part 1 (canonical#14686)
  interfaces/desktop-legacy: allow DBus access to com.canonical.dbusmenu
  interfaces/builtin/fwupd.go: allow access to nvmem for thunderbolt plugin
  tests: removing uc16 executions (canonical#14575)
  tests: Added arm github runner to build snapd (canonical#14504)
  tests: no need to run spread when there are not tests matching the filter (canonical#14728)
  tests/lib/tools/store-state: exit on errors, update relevant tests (canonical#14725)
  tests: udpate the github workflow to run tests suggested by spread-filter tool (canonical#14519)
  testtime: add mockable timers for use in tests (canonical#14672)
  interface/screen_inhibit_control: Improve screen inhibit control for use on core (canonical#14134)
  tests: use images with 20G disk in openstack (canonical#14720)
  i/builtin: allow @ in custom-device filepaths (canonical#14651)
  tests: refactor test-snapd-desktop-layout-with-content
  tests: fix broken app definition
  tests: capitalize sentences in comments
  tests: wrap very long shell line
  tests: fix raciness in async startup and sync install
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Run Nested -auto- Label automatically added in case nested tests need to be executed Run nested The PR also runs tests inluded in nested suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants