docs-v2/test
Jason Stirnaman 5c74bf4ff6
5156 Writes: add v1 to Serverless get-started (#5233)
* chore(ci): Install Telegraf to test container

* chore(v3): test telegraf config and startup:

- creates the config file for the test run. TODO: remove the duplication and use the content for test setup.
- fix placeholder name

* fix(v3): make test pass for curl API example

* chore(ci): make tests pass for write. Add dependencies to Dockerfile.

* chore(tests): update test build

- Compose metadata: Add profile to test service. We can decouple services later and apply the profile to each. Rename. Pass the image name to the container.
- Fetch the parse_yaml.sh script during docker build, so we only fetch when necessary. Track the script in git. This is still a little janky, but better.
- test.sh: Use `HEAD` for diff (duh). Go back to using docker compose up so we're rebuilding only as needed and reusing the container - much faster, but the output isn't as pretty as `run`.
- pytest.ini, requirements.txt: Use pytest-dotenv to load env.products variables.

* fix(write): update JS instructions and pass tests.

* chore(ci): Install influx v2 CLI into the Docker test container.

* chore(ci): move .env file loading to pytest and run product tests separately with the proper .env file:

- Moves .env configs to be loaded by the test runner instead of by Docker. I'd prefer to ignore any product divergence and have them run interchangeably, but the pros outweigh the cons for now.
- Add example for passing custom commands in the compose file.

* fix(v3): replace curl example with v1 and v2, simplify JS example, pass tests.

feat(v3): add v1 write, test, update text:

- adds Serverless v1 /write example with note about autogen behavior
- adds link from v1 examples to Migrate guide
- adds description of autogen behavior to v1-compat guide
- updates "powered by..." text
- adapts examples to make them more testable, asserts `/write` status code
- replace "shared" examples (only used once) with inline to make them testable

fix(v3): promote migrate link

* Update content/influxdb/cloud-dedicated/get-started/write.md

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>

* chore(v3): Write: fix wording for curl success  (#5156)

---------

Co-authored-by: Scott Anderson <sanderson@users.noreply.github.com>
2023-11-15 14:41:29 -06:00
..
.dockerignore chore(qol): Test python and shell code samples. WIP - use for local testing only, not ready for CI. (#5154) 2023-09-26 14:56:21 -05:00
.gitignore 5156 Writes: add v1 to Serverless get-started (#5233) 2023-11-15 14:41:29 -06:00
README.md chore(qol): Test python and shell code samples. WIP - use for local testing only, not ready for CI. (#5154) 2023-09-26 14:56:21 -05:00
parse_yaml.sh 5156 Writes: add v1 to Serverless get-started (#5233) 2023-11-15 14:41:29 -06:00
pytest.ini 5156 Writes: add v1 to Serverless get-started (#5233) 2023-11-15 14:41:29 -06:00
requirements.txt 5156 Writes: add v1 to Serverless get-started (#5233) 2023-11-15 14:41:29 -06:00
run-tests.sh 5156 Writes: add v1 to Serverless get-started (#5233) 2023-11-15 14:41:29 -06:00

README.md

Test code blocks in Markdown files.

This project contains the following:

  • test.sh: The primary entrypoint for running tests. Copies Markdown files to a temporary directory shared with the test Docker image and runs the test container.
  • test/run-tests.sh: The Docker image entrypoint. Substitutes placeholders with environment variables in code blocks. Passes test files to test runners (for example, pytest --codeblocks for Python and shell code samples).
  • compose.yaml and Dockerfile: Docker image for the test service that installs test dependencies and passes test files to test runners.

Set configuration values

To set your InfluxDB credentials for testing, create the file .env.influxdbv3 and add key=value properties for the following:

INFLUX_HOST=https://us-east-1-1.aws.cloud2.influxdata.com
INFLUX_TOKEN=3S3SFrpFbnNR_pZ3Cr6LMN...==
INFLUX_ORG=28d1f2f.........
INFLUX_DATABASE=get-started

Storing configuration properties in a .env (dotenv) file is generally preferable to using environment variables.

Build the image

  1. Install Docker for your system.

  2. Build the Docker image.

    docker compose build test
    

    After editing configuration or files used by the image, re-run the preceding build command.

Run tests

Test code blocks in Markdown files that have changed relative to your git master branch:

sh test.sh

Test code blocks in files that match a pattern:

sh test.sh ./content/**/*.md

test.sh copies files into ./test/tmp/ for testing and runs the tests in Docker.

Test runners

Experimental--work in progress

pytest-codeblocks extracts code from python and shell Markdown code blocks and executes assertions for the code. If you don't assert a value, --codeblocks considers a non-zero exit code to be a failure. Note: pytest --codeblocks uses Python's subprocess.run() to execute shell code.

To assert (and display) the expected output of your code, follow the code block with the <!--pytest-codeblocks:expected-output--> comment tag, and then the expected output in a code block--for example:

print("Hello, world!")

If successful, the output is the following:

Hello, world!

pytest-codeblocks has features for skipping tests and marking blocks as failed. To learn more, see the pytest-codeblocks README and tests.

Other tools and ideas for testing code blocks

The codedown NPM package extracts code from Markdown code blocks for each language and can pipe the output to a test runner for the language.

pytest and pytest-codeblocks use the Python Assertions module to keep testing overhead low. Node.js also provides an Assertions package.

The runmd NPM package runs javascript code blocks in Markdown and generates a new Markdown file with the code block output inserted.

Troubleshoot

Pytest collected 0 items

Potential reasons:

  • See the test discovery options in pytest.ini.

  • For Python code blocks, use the following delimiter:

    # Codeblocks runs this block.
    

    pytest --codeblocks ignores code blocks that use the following:

    # Codeblocks ignores this block.