1# Running Tests 2 3## Setting Up Your Environment 4 5For the purposes of this tutorial, we'll be setting up an environment in Docker. 6Docker is handy because it's fairly portable, and won't interfere with the rest 7of your machine setup. It also offers a strong guarantee that you're testing 8the same way that others working on the project are. Finally, we can get away 9with using the same Docker image that's run by the OpenBMC continuous 10integration bot, so we have even more confidence that we're running relevant 11tests the way they'd be run upstream. 12 13### Install Docker 14 15Installation of Docker CE (Community Edition) varies by platform, and may differ 16in your organization. But the 17[Docker Docs](https://docs.docker.com/v17.12/install) are a good place to 18start looking. 19 20Check that the installation was successful by running `sudo docker run 21hello-world`. 22 23### Download OpenBMC Continuous Integration Image 24 25You'll want a couple of different repositories, so start by making a place for 26it all to go, and clone the CI scripts: 27 28```shell 29mkdir openbmc-ci-tests 30cd openbmc-ci-tests 31git clone https://github.com/openbmc/openbmc-build-scripts.git 32``` 33 34### Add `phosphor-host-ipmid` 35 36We also need to put a copy of the project you want to test against here. But 37you've probably got a copy checked out already, so we're going to make a 38*git worktree*. You can read more about those by running `git help worktree`, 39but the basic idea is that it's like having a second copy of your repo - but the 40second copy is in sync with your main copy, knows about your local branches, and 41protects you from checking out the same branch in two places. 42 43Your new worktree doesn't know about any untracked files you have in your main 44worktree, so you should get in the habit of committing everything you want to 45run tests against. However, because of the implementation of 46`run-unit-test-docker.sh`, you can't run the CI with untracked changes anyways, 47so this isn't the worst thing in the world. (If you make untracked changes in 48your testing worktree, it's easy to update a commit with those.) 49 50Note the placeholders in the following steps; modify the commands to match your 51directory layout. 52 53```shell 54cd /my/dir/for/phosphor-host-ipmid 55git worktree add /path/to/openbmc-ci-tests/phosphor-host-ipmid 56``` 57 58Now, if you `cd /path/to/openbmc-ci-tests`, you should see a directory 59`phosphor-host-ipmid/`, and if you enter it and run `git status` you will see 60that you're likely on a new branch named `phosphor-host-ipmid`. This is just for 61convenience, since you can't check out a branch in your worktree that's already 62checked out somewhere else; you can safely ignore or delete that branch later. 63 64However, Git won't be able to figure out how to get to your main worktree 65(`/my/dir/for/phosphor-host-ipmid`), so we'll need to mount it when we run. Open 66up `/path/to/openbmc-ci-tests/openbmc-build-scripts/run-unit-test-docker.sh` and 67find where we call `docker run`, way down at the bottom. Add an additional 68argument, remembering to escape the newline ('\'): 69 70```shell 71PHOSPHOR_IPMI_HOST_PATH="/my/dir/for/phosphor-host-ipmid" 72 73docker run --blah-blah-existing-flags \ 74 -v ${PHOSPHOR_IPMI_HOST_PATH}:${PHOSPHOR_IPMI_HOST_PATH} \ 75 -other \ 76 -args 77``` 78 79Then commit this, so you can make sure not to lose it if you update the scripts 80repo: 81 82```shell 83cd openbmc-build-scripts 84git add run-unit-test-docker.sh 85git commit -m "mount phosphor-host-ipmid" 86``` 87 88NOTE: There are other ways to do this besides a worktree; other approaches 89trade the cruft of mounting extra paths to the Docker container for different 90cruft: 91 92You can create a local upstream: 93```shell 94cd openbmc-ci-tests 95mkdir phosphor-host-ipmid 96cd phosphor-host-ipmid 97git init 98cd /my/dir/for/phosphor-host-ipmid 99git remote add /path/to/openbmc-ci-tests/phosphor-host-ipmid ci 100git push ci 101``` 102This method would require you to push your topic branch to `ci` and then `git 103checkout` the appropriate branch every time you switched topics: 104```shell 105cd /my/dir/for/phosphor-host-ipmid 106git commit -m "my changes to be tested" 107git push ci 108cd /path/to/openbmc-ci-tests/phosphor-host-ipmid 109git checkout topic-branch 110``` 111 112You can also create a symlink from your Git workspace into `openbmc-ci-tests/`. 113This is especially not recommended, since you won't be able to work on your code 114in parallel while the tests run, and since the CI scripts are unhappy when you 115have untracked changes - which you're likely to have during active development. 116 117## Building and Running 118 119The OpenBMC CI scripts take care of the build for you, and run the test suite. 120Build and run like so: 121 122```shell 123sudo WORKSPACE=$(pwd) UNIT_TEST_PKG=phosphor-host-ipmid \ 124 ./openbmc-build-scripts/run-unit-test-docker.sh 125``` 126 127The first run will take a long time! But afterwards it shouldn't be so bad, as 128many parts of the Docker container are already downloaded and configured. 129 130## Reading Output 131 132Your results will appear in 133`openbmc-ci-tests/phosphor-host-ipmid/test/test-suite.log`, as well as being 134printed to `stdout`. You will also see other `.log` files generated for each 135test file, for example `sample_unittest.log`. All these `*.log` files are 136human-readable and can be examined to determine why something failed 137 138# Writing Tests 139 140## Setting Up Your Environment 141 142## Best Practices 143 144## Sending for Review 145 146# Reviewing Tests 147 148## Best Practices 149 150## Quickly Running At Home 151 152# Credits 153 154Thanks very much to Patrick Venture for his prior work putting together 155documentation on this topic internal to Google. 156