Skip to content

Commit

Permalink
exeucte tests from local / implemented tags, build-args / added missi…
Browse files Browse the repository at this point in the history
…ng tests
  • Loading branch information
damoon committed Nov 15, 2020
1 parent c5b502f commit e241091
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 96 deletions.
45 changes: 28 additions & 17 deletions pkg/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wedding
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
Expand All @@ -28,13 +29,13 @@ wedding builds only support these arguments: context, tag, buildargs, cachefrom,
)

type buildConfig struct {
//buildArgs map[string]string
//labels map[string]string
buildArgs map[string]string
labels map[string]string
cpuMilliseconds int
dockerfile string // TODO test path/Dockerfile
dockerfile string
memoryBytes int
target string // TODO test
tags []string // TODO test
target string
tags []string
registryAuth dockerConfig
contextFilePath string
}
Expand Down Expand Up @@ -124,17 +125,15 @@ func buildParameters(r *http.Request) (*buildConfig, error) {
return cfg, fmt.Errorf("unsupported argument rm set to '%s'", rm)
}

// TODO implement
// err := json.Unmarshal([]byte(r.URL.Query().Get("buildargs")), &cfg.buildArgs)
// if err != nil {
// return cfg, fmt.Errorf("decode buildargs: %v", err)
// }
err := json.Unmarshal([]byte(r.URL.Query().Get("buildargs")), &cfg.buildArgs)
if err != nil {
return cfg, fmt.Errorf("decode buildargs: %v", err)
}

// TODO implement
// err = json.Unmarshal([]byte(r.URL.Query().Get("labels")), &cfg.labels)
// if err != nil {
// return cfg, fmt.Errorf("decode labels: %v", err)
// }
err = json.Unmarshal([]byte(r.URL.Query().Get("labels")), &cfg.labels)
if err != nil {
return cfg, fmt.Errorf("decode labels: %v", err)
}

// cpu limit
cpuquota, err := strconv.Atoi(r.URL.Query().Get("cpuquota"))
Expand Down Expand Up @@ -287,7 +286,7 @@ func (s Service) executeBuild(ctx context.Context, cfg *buildConfig, w http.Resp

destination := "--output type=image,push=true,name=wedding-registry:5000/digests"
if imageNames != "" {
destination = fmt.Sprintf("--output type=image,push=true,\"name=%s\"", imageNames)
destination = fmt.Sprintf(`--output type=image,push=true,\"name=%s\"`, imageNames)
}

dockerfileName := filepath.Base(cfg.dockerfile)
Expand All @@ -298,6 +297,16 @@ func (s Service) executeBuild(ctx context.Context, cfg *buildConfig, w http.Resp
target = fmt.Sprintf("--opt target=%s", cfg.target)
}

buildargs := ""
for k, v := range cfg.buildArgs {
buildargs += fmt.Sprintf("--opt build-arg:%s='%s' ", k, v)
}

labels := ""
for k, v := range cfg.labels {
buildargs += fmt.Sprintf("--opt label:%s='%s' ", k, v)
}

buildScript := fmt.Sprintf(`
set -euo pipefail
Expand All @@ -316,9 +325,11 @@ buildctl-daemonless.sh \
--opt filename=%s \
%s \
%s \
%s \
%s \
--export-cache=type=registry,ref=wedding-registry:5000/cache-repo,mode=max \
--import-cache=type=registry,ref=wedding-registry:5000/cache-repo
`, presignedContextURL, dockerfileDir, dockerfileName, target, destination)
`, presignedContextURL, dockerfileDir, dockerfileName, buildargs, labels, target, destination)

pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down
5 changes: 4 additions & 1 deletion pkg/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"log"
"os"
"time"

corev1 "k8s.io/api/core/v1"
Expand All @@ -28,7 +29,9 @@ func (s Service) executePod(ctx context.Context, pod *corev1.Pod, w io.Writer) e
failed := false

defer func() {
if failed {
// helpful for development: remove all failed pods
// kubectl get po | grep -E 'wedding-(push|pull|tag|build)' | awk '{ print $1 }' | xargs kubectl delete po
if failed && os.Getenv("KEEP_FAILED_PODS") != "" {
w.Write([]byte("Pod failed. Skipping cleanup.\n"))
return
}
Expand Down
4 changes: 0 additions & 4 deletions tests/Dockerfile

This file was deleted.

22 changes: 15 additions & 7 deletions tests/Tiltfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@

docker_build('testing-image', './..', dockerfile='Dockerfile')

k8s_yaml('docker-build.yaml')
k8s_resource('test-docker-build', resource_deps=['wedding'])
local_resource ('test docker build',
'time timeout 60 bash docker-build.sh',
deps=['..'],
resource_deps=['wedding'],
allow_parallel=True,
)

k8s_yaml('docker-pull-tag-push.yaml')
k8s_resource('test-docker-pull-tag-push', resource_deps=['wedding'])
local_resource ('test docker pull tag push',
'time timeout 60 bash docker-pull-tag-push.sh',
deps=['..'],
resource_deps=['wedding'],
allow_parallel=True,
)

local_resource ('test tilt ci',
'bash tilt-ci.sh',
'time timeout 180 bash tilt-ci.sh',
deps=['..'],
resource_deps=['wedding'],
allow_parallel=True,
)
10 changes: 10 additions & 0 deletions tests/docker-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!bash
set -uexo pipefail
export DOCKER_HOST=tcp://127.0.0.1:12376
until docker version; do sleep 1; done

docker build -t wedding-build-test-a -t wedding-build-test-b ./docker -f ./docker/dir/Dockerfile

if docker build ./docker-broken; then echo "this should fail"; false; else echo "exit code propagated"; fi

echo "done"
27 changes: 0 additions & 27 deletions tests/docker-build.yaml

This file was deleted.

15 changes: 15 additions & 0 deletions tests/docker-pull-tag-push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!bash
set -uexo pipefail
export DOCKER_HOST=tcp://127.0.0.1:12376
until docker version; do sleep 1; done

docker pull mirror.gcr.io/library/alpine
if docker pull mirror.gcr.io/library/missing; then echo "this should fail"; false; else echo "exit code propagated"; fi

docker tag mirror.gcr.io/library/alpine wedding-registry:5000/test-push:alpine
if docker tag missing b; then echo "this should fail"; false; else echo "exit code propagated"; fi

docker push wedding-registry:5000/test-push:alpine
if docker push missing; then echo "this should fail"; false; else echo "exit code propagated"; fi

echo "done"
33 changes: 0 additions & 33 deletions tests/docker-pull-tag-push.yaml

This file was deleted.

File renamed without changes.
8 changes: 5 additions & 3 deletions tests/tilt-ci.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!bash
set -uexo pipefail

export DOCKER_HOST=tcp://127.0.0.1:12376
until docker version; do sleep 1; done

cd tilt

timeout 120 tilt ci --port 0
tilt down
tilt ci --port 0
tilt down
4 changes: 2 additions & 2 deletions tests/tilt/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ min_tilt_version('0.15.0') # includes fix for auto_init+False with tilt ci

k8s_yaml('kubernetes.yaml')

docker_build('service-a-image', './image-a')
docker_build('service-b-image', './image-b')
docker_build('service-a-image', './image-a', build_args={'ECHO': 'a'})
docker_build('service-b-image', './image-b', target='http')

k8s_yaml('verify-a.yaml')
k8s_yaml('verify-b.yaml')
Expand Down
3 changes: 2 additions & 1 deletion tests/tilt/image-a/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FROM nginx

COPY index.html /usr/share/nginx/html/index.html
ARG ECHO
RUN echo $ECHO > /usr/share/nginx/html/index.html
4 changes: 3 additions & 1 deletion tests/tilt/image-b/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
FROM nginx
FROM nginx AS http

COPY index.html /usr/share/nginx/html/index.html

FROM alpine as cli

0 comments on commit e241091

Please sign in to comment.