Skip to content

Commit a756680

Browse files
corneliusweigbalopat
authored andcommitted
Include runtime dependencies for taggers in gcr.io/k8s-skaffold/skaffold (#1987)
* Include necessary dependencies for taggers * Check that the taggers produce an image with the expected tag * Avoid recursive copy of test files For the git tagger test, set up a dummy repo inside the testdata folder. * Bundle timezone data with the skaffold binary This ensures that te dateTime tagger works on windows when go is not installed. * Fix integration test setup for git commit tagger Signed-off-by: Cornelius Weig <[email protected]>
1 parent 86d72b0 commit a756680

File tree

10 files changed

+1416
-11
lines changed

10 files changed

+1416
-11
lines changed

Gopkg.lock

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,7 @@
7878
[[constraint]]
7979
name = "github.com/golang/protobuf"
8080
branch = "master"
81+
82+
[[constraint]]
83+
branch = "master"
84+
name = "4d63.com/tz"

deploy/skaffold/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ FROM gcr.io/gcp-runtimes/ubuntu_16_0_4 as runtime_deps
1616

1717
RUN apt-get update && \
1818
apt-get install --no-install-recommends --no-install-suggests -y \
19+
git \
1920
python && \
2021
rm -rf /var/lib/apt/lists/*
2122

@@ -25,8 +26,8 @@ RUN curl -Lo /usr/local/bin/kubectl https://storage.googleapis.com/kubernetes-re
2526

2627
ENV HELM_VERSION v2.8.1
2728
RUN curl -LO https://storage.googleapis.com/kubernetes-helm/helm-${HELM_VERSION}-linux-amd64.tar.gz && \
28-
tar -zxvf helm-${HELM_VERSION}-linux-amd64.tar.gz && \
29-
mv linux-amd64/helm /usr/local/bin/helm
29+
tar -xvf helm-${HELM_VERSION}-linux-amd64.tar.gz -C /usr/local/bin --strip-components 1 && \
30+
rm -f helm-${HELM_VERSION}-linux-amd64.tar.gz
3031

3132
ENV CLOUD_SDK_VERSION 217.0.0
3233
RUN curl -LO https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \
@@ -68,7 +69,6 @@ RUN apt-get update && apt-get install --no-install-recommends --no-install-sugge
6869
ca-certificates \
6970
curl \
7071
build-essential \
71-
git \
7272
gcc \
7373
python-setuptools \
7474
lsb-release \

integration/build_test.go

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@ limitations under the License.
1717
package integration
1818

1919
import (
20+
"context"
21+
"os"
22+
"os/exec"
2023
"testing"
24+
"time"
2125

26+
"4d63.com/tz"
2227
"github.com/GoogleContainerTools/skaffold/integration/skaffold"
28+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
29+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
30+
"github.com/docker/docker/api/types"
2331
)
2432

33+
const imageName = "simple-build:"
34+
2535
func TestBuild(t *testing.T) {
2636
if testing.Short() {
2737
t.Skip("skipping integration test")
@@ -31,32 +41,126 @@ func TestBuild(t *testing.T) {
3141
description string
3242
dir string
3343
args []string
44+
expectImage string
45+
setup func(t *testing.T, workdir string) (teardown func())
3446
}{
3547
{
3648
description: "docker build",
3749
dir: "testdata/build",
38-
}, {
50+
},
51+
{
3952
description: "git tagger",
4053
dir: "testdata/tagPolicy",
4154
args: []string{"-p", "gitCommit"},
42-
}, {
55+
setup: setupGitRepo,
56+
expectImage: imageName + "v1",
57+
},
58+
{
4359
description: "sha256 tagger",
4460
dir: "testdata/tagPolicy",
4561
args: []string{"-p", "sha256"},
46-
}, {
62+
expectImage: imageName + "latest",
63+
},
64+
{
4765
description: "dateTime tagger",
4866
dir: "testdata/tagPolicy",
4967
args: []string{"-p", "dateTime"},
50-
}, {
68+
// around midnight this test might fail, if the tests above run slowly
69+
expectImage: imageName + nowInChicago(),
70+
},
71+
{
5172
description: "envTemplate tagger",
5273
dir: "testdata/tagPolicy",
5374
args: []string{"-p", "envTemplate"},
75+
expectImage: imageName + "tag",
5476
},
5577
}
5678

5779
for _, test := range tests {
5880
t.Run(test.description, func(t *testing.T) {
81+
if test.setup != nil {
82+
teardown := test.setup(t, test.dir)
83+
defer teardown()
84+
}
85+
86+
// remove image in case it is already present
87+
removeImage(t, test.expectImage)
5988
skaffold.Build(test.args...).InDir(test.dir).RunOrFail(t)
89+
checkImageExists(t, test.expectImage)
6090
})
6191
}
6292
}
93+
94+
// removeImage removes the given image if present.
95+
func removeImage(t *testing.T, image string) {
96+
t.Helper()
97+
98+
if image == "" {
99+
return
100+
}
101+
102+
client, err := docker.NewAPIClient(false, nil)
103+
failNowIfError(t, err)
104+
105+
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
106+
defer cancel()
107+
_, _ = client.ImageRemove(ctx, image, types.ImageRemoveOptions{
108+
Force: true,
109+
PruneChildren: true,
110+
})
111+
}
112+
113+
// checkImageExists asserts that the given image is present
114+
func checkImageExists(t *testing.T, image string) {
115+
t.Helper()
116+
117+
if image == "" {
118+
return
119+
}
120+
121+
client, err := docker.NewAPIClient(false, nil)
122+
failNowIfError(t, err)
123+
124+
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
125+
defer cancel()
126+
if !client.ImageExists(ctx, image) {
127+
t.Errorf("expected image '%s' not present", image)
128+
}
129+
}
130+
131+
// setupGitRepo sets up a clean repo with tag v1
132+
func setupGitRepo(t *testing.T, dir string) func() {
133+
gitArgs := [][]string{
134+
{"init"},
135+
{"config", "user.email", "[email protected]"},
136+
{"config", "user.name", "John Doe"},
137+
{"add", "."},
138+
{"commit", "-m", "Initial commit"},
139+
{"tag", "v1"},
140+
}
141+
142+
for _, args := range gitArgs {
143+
cmd := exec.Command("git", args...)
144+
cmd.Dir = dir
145+
if buf, err := util.RunCmdOut(cmd); err != nil {
146+
t.Logf(string(buf))
147+
t.Fatal(err)
148+
}
149+
}
150+
151+
return func() {
152+
os.RemoveAll(dir + "/.git")
153+
}
154+
}
155+
156+
// nowInChicago returns the dateTime string as generated by the dateTime tagger
157+
func nowInChicago() string {
158+
loc, _ := tz.LoadLocation("America/Chicago")
159+
return time.Now().In(loc).Format("2006-01-02")
160+
}
161+
162+
func failNowIfError(t *testing.T, err error) {
163+
if err != nil {
164+
t.Fatal(err)
165+
}
166+
}

integration/skaffold/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (b *RunBuilder) cmd(ctx context.Context) *exec.Cmd {
203203
// If the test is killed by a timeout, go test will wait for
204204
// os.Stderr and os.Stdout to close as a result.
205205
//
206-
// However, the `cmd` will stil run in the background
206+
// However, the `cmd` will still run in the background
207207
// and hold those descriptors open.
208208
// As a result, go test will hang forever.
209209
//

integration/testdata/tagPolicy/skaffold.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ profiles:
2020
tagPolicy:
2121
dateTime:
2222
format: "2006-01-02"
23-
timezone: "UTC"
23+
# UTC does not require timezone data lookup
24+
# Would have caught #1979
25+
timezone: "America/Chicago"
2426
- name: envTemplate
2527
build:
2628
tagPolicy:

pkg/skaffold/build/tag/date_time.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"time"
2222

23+
"4d63.com/tz"
2324
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
2425
)
2526

@@ -60,7 +61,7 @@ func (tagger *dateTimeTagger) GenerateFullyQualifiedImageName(workingDir, imageN
6061
timezone = tagger.TimeZone
6162
}
6263

63-
loc, err := time.LoadLocation(timezone)
64+
loc, err := tz.LoadLocation(timezone)
6465
if err != nil {
6566
return "", fmt.Errorf("bad timezone provided: \"%s\", error: %s", timezone, err)
6667
}

vendor/4d63.com/tz/LICENSE

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/4d63.com/tz/tz.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/4d63.com/tz/zoneinfo.go

Lines changed: 1194 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)