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

WIP: On-device tests #146

Merged
merged 2 commits into from
Dec 12, 2021
Merged

WIP: On-device tests #146

merged 2 commits into from
Dec 12, 2021

Conversation

t184256
Copy link
Collaborator

@t184256 t184256 commented Dec 4, 2021

I've decided to quick-and-dirty automate a few things I often check manually on PRs and releases inside a disposable installation.

@Gerschtli thoughts? Other high-profile scenarios I'm missing?

@t184256 t184256 changed the title On-device tests WIP: On-device tests Dec 4, 2021
Copy link
Collaborator

@Gerschtli Gerschtli left a comment

Choose a reason for hiding this comment

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

I love it! Thanks a lot, the first step for test automation :) Had some comments, but beware, I did not run/test any of my suggestions, so there may be some typos.

I do not see any big thing missing, but I think at some point we should have a test for every option, but that doesn't need to be a on-device test.

cp -r ~/.config.bak ~/.config
fi

nix-shell -p bats --run 'bats .'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Scripts should depend on the working directory from where they are called.

With dirname "$(readlink -f "$0")", you get the directory where your script is, so

# somewhere at the top
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

# then here
nix-shell -p bats --run "bats ${SCRIPT_DIR}"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Incorporated.

if [[ ! -d ~/.config.bak ]]; then
mv ~/.config ~/.config.bak
cp -r ~/.config.bak ~/.config
fi
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wouldn't be backupping the ~/.config/nixpkgs directory be enough? That would reduce possible unwanted side effects.

Comment on lines +2 to +5
out=$(nix-shell -p hello --run hello)
[[ "$out" == 'Hello, world!' ]]
Copy link
Collaborator

Choose a reason for hiding this comment

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

A bit more idiomatic :)

Suggested change
out=$(nix-shell -p hello --run hello)
[[ "$out" == 'Hello, world!' ]]
run nix-shell -p hello --run hello
[[ "$output" == 'Hello, world!' ]]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

doesn't check the retcode that way

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah okay, then maybe be more explicit with what you want to test with [[ $status -eq 0 ]].

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

run also collects stderr into "$output", nah

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you want to have stdout and stderr separate, then you need --separate-stderr

Additionally, you can use --separate-stderr to split stdout and stderr into $output/$stderr and ${lines[@]}/${stderr_lines[@]}.

https://bats-core.readthedocs.io/en/stable/writing-tests.html#run-test-other-commands

@@ -0,0 +1,18 @@
switch_to_default_config() {
rm -rf ~/.config/nix
Copy link
Collaborator

Choose a reason for hiding this comment

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

This shouldn't be needed, if you always you the cli args for nix

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried to follow what we recommend in the README, not what's easier to script. But I'm having second thoughts, yeah

Comment on lines 3 to 27
CHANNEL_DIR=$HOME/.cache/nix-on-droid-self-test/channel
cp $CHANNEL_DIR/modules/environment/login/nix-on-droid.nix.default \
~/.config/nixpkgs/nix-on-droid.nix
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
CHANNEL_DIR=$HOME/.cache/nix-on-droid-self-test/channel
cp $CHANNEL_DIR/modules/environment/login/nix-on-droid.nix.default \
~/.config/nixpkgs/nix-on-droid.nix
CHANNEL_DIR=$HOME/.cache/nix-on-droid-self-test/channel
cp "$(nix-instantiate --eval --expr "<nix-on-droid/modules/environment/login/nix-on-droid.nix.default>")" \
~/.config/nixpkgs/nix-on-droid.nix

{ pkgs, lib, ... }:
{
home.stateVersion = "21.05";
nixpkgs.overlays = lib.mkIf (lib.hasAttrByPath [ "nixpkgs" "overlays" ] config) config.nixpkgs.overlays;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not enable home-manager.useGlobalPkgs instead of this monstrosity? I am still not sure if that complexity is really necessary for setting just the overlay :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

because that's what we recommend to users

Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't be needed for 21.11 anymore

@t184256
Copy link
Collaborator Author

t184256 commented Dec 10, 2021

Not finished yet, will need more time with this.

cp -r ~/.config.bak ~/.config
fi

pushd "$SCRIPT_DIR" # for lib.sh import
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can replace the pushd stuff with this nice feature to load shared code: https://bats-core.readthedocs.io/en/stable/writing-tests.html#load-share-common-code

@t184256 t184256 force-pushed the on-device-test branch 2 times, most recently from 7324bd7 to f607198 Compare December 10, 2021 22:41
@t184256
Copy link
Collaborator Author

t184256 commented Dec 10, 2021

added config-flake-h-m test, it currently fails with attribute nixpkgs missing

@Gerschtli
Copy link
Collaborator

Gerschtli commented Dec 10, 2021

Sorry, my phone is really slow today. So can you test for me if the tests would fail with the following changes?

diff --git i/tests/on-device/config-flake-h-m.cfg.nix w/tests/on-device/config-flake-h-m.cfg.nix
index 2df66f5..9c0310b 100644
--- i/tests/on-device/config-flake-h-m.cfg.nix
+++ w/tests/on-device/config-flake-h-m.cfg.nix
@@ -8,12 +8,5 @@
   # no nixpkgs.overlays defined
   environment.packages = with pkgs; [ zsh ];
 
-  home-manager.config =
-    { pkgs, ... }:
-    {
-      home.stateVersion = "21.05";
-
-      nixpkgs.overlays = config.nixpkgs.overlays;
-      home.packages = with pkgs; [ dash ];
-    };
+  nixpkgs.overlays = [];
 }

Because I am not sure, if the problem is in nix-on-droid's or home-manager's nixpkgs definition..

@t184256
Copy link
Collaborator Author

t184256 commented Dec 10, 2021

(after also commenting out the now-missing experimentalFeatures) In a flake setup, the options nixpkgs.* should be used...

@Gerschtli
Copy link
Collaborator

That should not print this message for nixpkgs.overlays = [];.. But this is at least a different error than with home-manager. I checked their source code and they always define the option definitions, so I really do not have a clue. I am trying to reproduce it on my phone..

@Gerschtli
Copy link
Collaborator

I totally messed up the condition of the assertion.. :D I will push it to my PR in a second.

cp -r ~/.config.bak ~/.config
fi

bats "${SCRIPT_DIR}" --verbose-run -x -T
Copy link
Collaborator

Choose a reason for hiding this comment

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

These options throw an error on my phone..

@@ -0,0 +1,7 @@
# Copyright (c) 2019-2021, see AUTHORS. Licensed under MIT License, see LICENSE.

load lib.sh
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you need to write load lib and rename lib.sh to lib.bash

@Gerschtli
Copy link
Collaborator

The tests just seem to hang when I run them.. Only the lines

1..6
ok 1 default config can be switched into

@t184256
Copy link
Collaborator Author

t184256 commented Dec 11, 2021

Yeah, flake ones take a lot of time to run... I'm actually working on an environment to debug them on PC, hope this makes sense in the end.

@Gerschtli
Copy link
Collaborator

So my testing branch (based of latest release-21.11 (#147) + cherry-picked commits from this PR + 7a1f8eb) runs perfectly fine :)

So if you incorporate 7a1f8eb into this PR, from my point of view, #147 and this PR can be merged :)

@t184256
Copy link
Collaborator Author

t184256 commented Dec 11, 2021

Awesome! Now I just must somehow contain the testing scope creep...

@Gerschtli
Copy link
Collaborator

Gerschtli commented Dec 11, 2021

Oh boy, that script looks complicated :D

@Gerschtli
Copy link
Collaborator

While using your tests for my flake PR just as a quick heads up before I lose the thought: If you are changing nix-on-droid script itself you need to do a manual switch and run the test and do manual rollbacks in case of failures. Maybe (not necessarily in this PR) we find a good solution for improving UX for that case :)

@t184256 t184256 merged commit 343ec2c into master Dec 12, 2021
@t184256 t184256 deleted the on-device-test branch December 15, 2021 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants