Integration testing docs in GitHub Actions
Tags: devops, bash, markdown, beginners
Published: 2023-03-30 01:53:32

Learn how to test your docs in CI/CD with Runme's latest v1.0 release, which comes with new cool features you can utilize to deliver seamless developer experience. We will leverage Runme's new kernel architecture enabling seamless interoperability between notebook and terminal UX.

> ⏯ Never heard of Runme before? It's an open source toolkit that let's you run your README.md and other markdown docs in both terminal and as runnable notebook inside VS Code. [Check it out](https://runme.dev).

![Integration testing docs in GitHub Actions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swn4byrmpzolwlged2it.gif)
> Integration testing docs in GitHub Actions

## Let's See How
Let's jump right into a showcase. A “thank you” goes out to our friends at Buoyant (creators of Linkerd, the open-source service mesh), who openly maintain [Linkerd’s docs](https://github.com/linkerd/website/blob/main/linkerd.io/content/2.12/getting-started/_index.md) in Markdown. That same Markdown generates [static HTML on their website](https://linkerd.io/2.12/getting-started/) for developers to copy commands into the terminals. It turns out that Linkerd’s Getting Started Guide makes a great real-world example illustrating how to harness Runme in CI/CD (with minimal to no edits).

![Markdown are everywhere describing Developer Experience](https://media.graphassets.com/u4CIorabRuS0qcI4o6gG)
> Markdown docs are everywhere describing Developer Experience

## The End-to-End Demo
[![](https://badgen.net/badge/Run%20repo's/README.md/5B3ADF?icon=https://runme.dev/img/logo.svg)](https://www.runme.dev/api/runme?repository=https%3A%2F%2Fgithub.com%2Fstateful%2Flinkerd-website.git&fileToOpen=tests/runme/README.md)

While you could watch the GitHub Action running in real-time, we can’t guarantee that the GitHub Action is running at all times, so we created a video. The time-lapse video (eventual consistency in Kubernetes takes time) illustrates side-by-side what’s going on when testing Linkerd’s Getting Started guide and Kubernetes cluster state. This exact setup will be executed inside a CI/CD job.

{% embed https://www.youtube.com/watch?v=StYPex_PTKY %}
> Full end-to-end demo as time-lapse video

## Inside of GitHub Actions

Once packaged up in a Github Action workflow, running on every commit is just a matter of configuration. Go ahead and send a PR to follow along in real time! It’s all public:

[![GitHub Action](https://media.graphassets.com/3fuZcKSCWJ1qg4Igv84g)](https://github.com/stateful/linkerd-website/actions/workflows/dx.yml)
> Live at [https://github.com/stateful/linkerd-website/actions/workflows/dx.yml](https://github.com/stateful/linkerd-website/actions/workflows/dx.yml)

For the curious ones, you can find the details of how we wired up the GitHub Action’s workflow with GCP and Runme [inside the repo](https://github.com/stateful/linkerd-website/blob/main/tests/runme/RUNNING_CI.md). The hard part’s done. Now, in about 5min after a commit, you will have assurance (watch out for that 🟢 light!) whether or not the Getting Started Guide still works and your Developer Experience remains intact.

# Breaking It Down

Before getting into improvements, let’s take a closer look at the moving pieces of this demo. Linkerd (version 2) is a Kubernetes-native service mesh. We will need a functioning cluster to test the Getting Started Guide via the Linkerd CLI. There are many permutations of CI/CD and cluster setups/provider combos. We won’t be covering them.

### Tools and Infrastructure

Instead, we’ve chosen well-known and established ingredients to keep it simple. The setup:

- Standard Kubernetes Cluster in Google’s GKE: 3 nodes with 2 CPUs & 2 GB memory each
- Bats ([Bash Automated Testing System](https://github.com/bats-core/bats-core)) plus [assertion libraries for kubectl](https://github.com/bats-core/bats-detik)
- GitHub Action that handles scheduling/queuing of subsequent jobs against shared cluster

### Anatomy of a Test Case

The idea is as simple as compelling. Runme’s CLI allows referencing code snippets by name (explicitly defined in command block annotations for clarity). This mirrors precisely what’s being presented to developers on the website’s Getting Started section to copy & paste into their terminals. It goes without saying that Runme’s UX is much more elegant than copy & paste.

![Getting Started Guide markdown snippet](https://media.graphassets.com/xkTWNk6TSMGs9DhDbfHY)

### Asserting Test Conditions

Inside our Bats files, we replicate the sequence of steps and the contained instructions as test cases. We can define pre-conditions, execute the Markdown command block, and check post-conditions for returned exit code, output, and desired Kubernetes cluster state. The [Bats DETIK](https://github.com/bats-core/bats-detik) assertion library does a phenomenal job of expressing Kubernetes assertions fluently. Bats is also capable of handling the [setup and teardown](https://github.com/stateful/linkerd-website/blob/main/tests/runme/setup_suite.bash) of a test suite. Feel free to ask us about the details.

```bash
@test "Verify linkerd injection (step 4)" {
  DETIK_CLIENT_NAMESPACE="emojivoto"
  run "runme run kubectl-get"
  assert_line -p "deployment \"emoji\" injected"
  assert_line -p "deployment \"vote-bot\" injected"
  assert_line -p "deployment \"voting\" injected"
  assert_line -p "deployment \"web\" injected"
  try "at most 10 times every 30s to get pods named 'emoji' and verify that 'status' is 'running'"
  try "at most 10 times every 30s to get pods named 'vote-bot' and verify that 'status' is 'running'"
  try "at most 10 times every 30s to get pods named 'voting' and verify that 'status' is 'running'"
  try "at most 10 times every 30s to get pods named 'web' and verify that 'status' is 'running'"
  assert_success
}
```

While Bats will support parallel execution, we want these test cases to run serially in a predetermined order. Humans run one command at a time. And, in stateful nature, downstream execution results depend on completing prior commands. While your run-unit-tests-in-parallel-self might object at first, this is, in fact, a good thing. It replicates how developers consume software docs in reality.

The same idea is transferrable to e.g., internal repos that house libraries, apps, or services. Imagine every repo’s README.md, BUILD.md, or DEPLOY.md being tested this way.

### Let Us Know What You Think

While we’re happy with our demo’s results, there is much room for improvement. Here are some items we have discussed and would love your feedback on:

1. Leverage notebook-serialized cell output for test assertions? The idea is to record cell outputs stored alongside the inputs and provide the functionality to define common assertions, e.g., substrings, expected exit code, or fuzzy threshold diffing.

2. Runme-integrated test-harness? While Bats fits the demo’s use case well, it is likely desirable to use Runme’s notebook parser and structural awareness to maintain a test suite inside of notebooks instead of describing them separately, in this case, `*.bats` files.

3. Provide an official Runme Github Action for easy integration? Significantly reduce boilerplate, ideally where the workflow auto-detects Runme-enabled Markdown files inside of the repo and runs the associated test suite.

The common theme in the ideas above is to absorb commonly required capabilities to express test cases into Runme and minimize overhead. Unlock docs testing value faster. The better we understand your use cases, the more effectively we can evolve Runme following this philosophy. Please, we urge you, [get in touch](https://discord.gg/stateful)!

# Get Involved

Outside of CI/CD-related features, the team is currently heads-down working through Runme's final touches of the Notebook Terminal output UX (experimentally available for non-interactive and background tasks) directly inside your READMEs. Beyond that, our roadmap is full of UX improvements, big and small, to continue streamlining the daily workflows of developers.

Now go try [it out](https://marketplace.visualstudio.com/items?itemName=stateful.runme) and help us prioritize! We’d love to hear what you think. If you run into any problems, please don’t hesitate to report them [as a GitHub issue](https://github.com/stateful/runme) or talk to us [on Discord](https://discord.gg/stateful).

Happy Docs Testing 🤩!