Skip to content

Adding a Makefile and basic tests.#86

Closed
metcalfc wants to merge 8 commits intodocker:masterfrom
metcalfc:bats
Closed

Adding a Makefile and basic tests.#86
metcalfc wants to merge 8 commits intodocker:masterfrom
metcalfc:bats

Conversation

@metcalfc
Copy link

@metcalfc metcalfc commented Aug 8, 2020

This approach uses bats to create a generic docker-compose test. It
checks that images are pullable, projects are buildable, up works, and
if ports are exposed that something is listening.

The tests can be tailored for each example. As long as they are the same
though, you can edit lib/test.bats.example and then make update-tests
and all the tests will be synced.

This adds three project dependencies: Makefile, BATS, and nc. A full clean
test run is PAINFULLY long (~14 minutes on my desktop) mostly
dominated by the build steps.

I've also added a target to just test whats changed. Since most of the time
PRs will just change one project. By default git finds the directories with
changes compared to origin master. You can override this with
BASE_REF=<whatever> make test-changed.

metcalfc and others added 7 commits August 8, 2020 14:26
This approach uses bats to create a generic docker-compose test. It
checks that images are pullable, projects are buildable, up works, and
if ports are exposed that something is listening.

The tests can be tailored for each example. As long as they are the same
though, you can edit lib/test.bats.example and then `make update-tests`
and all the tests will be synced.

Signed-off-by: Chad Metcalf <chad@docker.com>
Signed-off-by: Chad Metcalf <chad@docker.com>
Signed-off-by: Chad Metcalf <chad@docker.com>
[ "$status" -eq 0 ] just bails out and its up to you to figure out why.
$output has the stdout/stderr so we could just print that. I think its
worth vendoring the libraries and just using those functions.

Signed-off-by: Chad Metcalf <chad@docker.com>
Signed-off-by: Chad Metcalf <chad@docker.com>
The full test suite takes a long time to test. Generally changes in this
repo only will change one project. So lets find the directories with
changes and only test those. By default we will compare against
origin/master which should cover testing PRs. If you're doing
development and want to test your current changes you could do:

BASE_REF=HEAD make test-changed

Or if you're testing against your fork:

BASE_REF=myremote/branch make test-changed

Signed-off-by: Chad Metcalf <chad@docker.com>
Signed-off-by: Chad Metcalf <chad@docker.com>
Copy link
Collaborator

@glours glours left a comment

Choose a reason for hiding this comment

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

Hi @metcalfc
Can't we use the Docker image of bats to do that instead of adding the whole bats source code in the lib directory?
Because all the test files are identical, it should be interesting to have only 1 test file and to loop over directories to execute tests.
In that way we'll only have 1 test file to maintain and we'll sure that a new sample will be automatically covered by test

@metcalfc
Copy link
Author

metcalfc commented Aug 10, 2020 via email

@metcalfc
Copy link
Author

metcalfc commented Aug 11, 2020

Over a 🥃 I spent some time trying to put all this in docker. I wanted to see if I could get away without full dind. The dockerfile:

FROM docker:19.03

ARG UID
ARG GID

RUN apk update && apk add bash bash-completion gawk make netcat-openbsd docker-compose findutils git curl && \
    rm -rf /var/lib/apt/lists/* && \
    rm /var/cache/apk/* && \
    addgroup -S -g $GID awesome && adduser -S -G awesome -u $UID awesome && \
    ln -s /opt/bats/bin/bats /usr/local/bin/bats && \
    git clone --depth 1 --branch v1.2.1 https://github.com/bats-core/bats-core.git /opt/bats

COPY bashrc /home/awesome/.bashrc

USER awesome

VOLUME  /code
WORKDIR /code

ENTRYPOINT ["bash"]

A couple of Makefile rules:

UID := $(shell id -u)
GID := $(shell grep docker /etc/group| cut -d: -f3)

docker:
	docker build --build-arg=UID=${UID} --build-arg=GID=${GID} -t awesome-compose ./extras

shell:
	docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}:/code awesome-compose

Gets you part way there. Unfortunately when it comes to testing the network ports the external network isn't available. So all those checks will fail. Unless I've missed something you would need a full dind setup. I can play around with it more when I get back from vacation.

@metcalfc
Copy link
Author

metcalfc commented Aug 11, 2020

@glours Walking the 🐕 with a ☕️ and network=host popped into my head. Not sure where it was last night. Anyway, took a pass at containing the toolchain. There is more work to do. I took the easy route and apk add docker-compose which gets a release or two ago. We should parameterize Docker and Compose to get exact versions. But its a start. This patch is... large now. I can squash it and break it up if we need to. I'll be back next week (2020/08/17).

# build the image. Since everyone's GID for docker is going to be different (Mac, Linux, WSL2, etc) there isn't 
# a point in pushing an image to a repo. Just build a local toolchain image matching the users UID/GID
make docker 

# interactive shell with all the tools (awk, nc, etc)
make shell

# the tests
make docker-test-all
make docker-test-changed

I've seen a lot more failures. Compose is not the latest. It seems to
sort of work. We should probably parameterize Docker and Compose
versions to ensure we're installing a consistent toolchain.

Signed-off-by: Chad Metcalf <chad@docker.com>
@glours
Copy link
Collaborator

glours commented Aug 11, 2020

Awesome thanks @metcalfc 👍
I'll take a look in detail during your holidays, I try to see if we can simplify some parts or how we can split it to integrate your changes easily

@metcalfc metcalfc closed this Dec 8, 2020
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