Skip to content

Commit 220f522

Browse files
authored
Merge pull request #25 from gitpod-io/rl/add-integration-tests
Add integration tests
2 parents d0a9eeb + 87d0841 commit 220f522

File tree

11 files changed

+370
-26
lines changed

11 files changed

+370
-26
lines changed

.github/workflows/build.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ jobs:
6262
runs-on: ubuntu-latest
6363
needs:
6464
- build-go
65+
services:
66+
# Local registry
67+
registr:
68+
# Docker Hub image
69+
image: registry:2
70+
ports:
71+
- 5000:5000
6572
steps:
6673
- name: Create checkout directory
6774
run: mkdir -p ~/go/src/github.com/gitpod-io
@@ -81,7 +88,25 @@ jobs:
8188
- name: Download and vendor all required packages
8289
run: |
8390
go mod download
91+
- name: Build the test runner
92+
run: pushd pkg/test/runner; ./build.sh; popd
93+
- name: Install the latest buildkit release
94+
run: |
95+
BUILDKIT_URL="$(curl -sL https://api.github.com/repos/moby/buildkit/releases \
96+
| jq -r 'map(select(.name|startswith("v")))|sort_by(.name)[-1].assets[]|select(.name|endswith(".linux-amd64.tar.gz")).browser_download_url')"
97+
curl -L "${BUILDKIT_URL}" | sudo tar -xz -C /usr/local
98+
- name: Start buildkit daemon
99+
run: |
100+
sudo --non-interactive --shell <<END_SUDO
101+
install -d -m 0750 -o root -g docker /run/buildkit
102+
buildkitd &
103+
while ! test -S /run/buildkit/buildkitd.sock; do sleep 0.1; done
104+
chgrp docker /run/buildkit/buildkitd.sock
105+
END_SUDO
84106
- name: Run all unit tests
107+
env:
108+
BUILDKIT_ADDR: unix:///run/buildkit/buildkitd.sock
109+
TARGET_REF: 127.0.0.1:5000
85110
run: go test -v -coverprofile=coverage.out $(go list ./...)
86111
- name: Generate code coverage artifacts
87112
uses: actions/upload-artifact@v2

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@
2727
"args": [
2828
"eyJEZXNjIjoiaXQgc2hvdWxkIGhhdmUgR28gaW4gdmVyc2lvbiAxLjEzIiwiU2tpcCI6ZmFsc2UsIlVzZXIiOiIiLCJDb21tYW5kIjpbImdvIiwidmVyc2lvbiJdLCJFbnRyeXBvaW50IjpudWxsLCJFbnYiOm51bGwsIkFzc2VydGlvbnMiOlsic3Rkb3V0LmluZGV4T2YoXCJnbzEuMTFcIikgIT0gLTEiXX0="
2929
]
30+
},
31+
{
32+
"name": "Launch build integration test",
33+
"type": "go",
34+
"request": "launch",
35+
"mode": "test",
36+
"program": "${fileWorkspaceFolder}/pkg/dazzle/build_test.go",
37+
"args": [
38+
"-test.run",
39+
"TestProjectChunk_test_integration"
40+
],
41+
"env": {
42+
"BUILDKIT_ADDR": "unix:///run/buildkit/buildkitd.sock",
43+
"TARGET_REF": "127.0.0.1:5000",
44+
}
3045
}
3146
]
3247
}

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,15 @@ They can be extracted using jq e.g.:
204204
$ go run pkg/test/runner/main.go eyJEZXNjIjoiaXQgc2hvdWxkIGhhdmUgR28gaW4gdmVyc2lvbiAxLjEzIiwiU2tpcCI6ZmFsc2UsIlVzZXIiOiIiLCJDb21tYW5kIjpbImdvIiwidmVyc2lvbiJdLCJFbnRyeXBvaW50IjpudWxsLCJFbnYiOm51bGwsIkFzc2VydGlvbnMiOlsic3Rkb3V0LmluZGV4T2YoXCJnbzEuMTFcIikgIT0gLTEiXX0= | jq -r '.Stdout | @base64d'
205205
go version go1.16.4 linux/amd64
206206
```
207+
208+
### Integration tests
209+
There is an integration test for the build command in pkg/dazzle/build_test.go - TestProjectChunk_test_integration and a shell script to run it.
210+
The integration test does an end-to-end check along with editing a test and re-running to ensure only the test image is updated.
211+
212+
It requires a running Buildkitd instance at unix:///run/buildkit/buildkitd.sock and a docker registry on 127.0.0.1:5000 (i.e. as this workspace is setup on startup).
213+
214+
Override the env vars BUILDKIT_ADDR and TARGET_REF as required prior to running in a different environment.
215+
216+
```bash
217+
$ ./integration_tests.sh
218+
```

integration_tests.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
# Run the integration tests
4+
# NOTE: the TARGET_REF is hostname:port
5+
# shellcheck disable=SC2034
6+
BUILDKIT_ADDR=unix:///run/buildkit/buildkitd.sock TARGET_REF=127.0.0.1:5000 go test -count 1 -run ^Test.*_integration$ github.com/csweichel/dazzle/pkg/dazzle -v

pkg/dazzle/build.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (p *Project) Build(ctx context.Context, session *BuildSession) error {
168168
session.baseBuildFinished(absbaseref, basemf, basecfg)
169169

170170
for _, chk := range p.Chunks {
171-
_, err := chk.test(ctx, session)
171+
_, _, err := chk.test(ctx, session)
172172
if err != nil {
173173
return fmt.Errorf("cannot build chunk %s: %w", chk.Name, err)
174174
}
@@ -546,52 +546,52 @@ func (p *ProjectChunk) buildAsBase(ctx context.Context, dest reference.Named, se
546546
return resref, nil
547547
}
548548

549-
func (p *ProjectChunk) test(ctx context.Context, sess *BuildSession) (ok bool, err error) {
549+
func (p *ProjectChunk) test(ctx context.Context, sess *BuildSession) (ok bool, didRun bool, err error) {
550550
if sess == nil {
551-
return false, errors.New("cannot test without a session")
551+
return false, false, errors.New("cannot test without a session")
552552
}
553553
if sess.opts.NoTests || len(p.Tests) == 0 {
554-
return true, nil
554+
return true, false, nil
555555
}
556556

557557
resultRef, err := p.ImageName(imageTypeTestResult, sess)
558558
if err != nil {
559-
return false, err
559+
return false, false, err
560560
}
561561
r, err := pullTestResult(ctx, sess.opts.Registry, resultRef)
562562
if err != nil && !errdefs.IsNotFound(err) {
563-
return false, err
563+
return false, false, err
564564
}
565565
if r != nil && r.Passed {
566566
// tests have run before and have passed
567-
return true, nil
567+
return true, false, nil
568568
}
569569

570570
// build temp image for testing
571571
testRef, _, err := p.buildImage(ctx, ImageTypeTest, sess)
572572
if err != nil {
573-
return false, err
573+
return false, false, err
574574
}
575575

576576
_, _, imgcfg, err := getImageMetadata(ctx, testRef, sess.opts.Registry)
577577
if err != nil {
578-
return false, err
578+
return false, false, err
579579
}
580580

581581
log.WithField("chunk", p.Name).Warn("running tests")
582582
executor := buildkit.NewExecutor(sess.Client, testRef.String(), imgcfg)
583583
_, ok = test.RunTests(ctx, executor, p.Tests)
584584
if !ok {
585-
return false, fmt.Errorf("%s: tests failed", p.Name)
585+
return false, true, fmt.Errorf("%s: tests failed", p.Name)
586586
}
587587

588588
// tests have passed - mark them as such
589589
_, err = pushTestResult(ctx, sess.opts.Registry, resultRef, StoredTestResult{true})
590590
if err != nil && !errdefs.IsAlreadyExists(err) {
591-
return true, err
591+
return true, true, err
592592
}
593593

594-
return true, nil
594+
return true, true, nil
595595
}
596596

597597
func (p *ProjectChunk) build(ctx context.Context, sess *BuildSession) (chkRef reference.NamedTagged, didBuild bool, err error) {

0 commit comments

Comments
 (0)