Skip to content

Commit cbc4da6

Browse files
committed
[image-builder] Add integration tests
1 parent 120eda7 commit cbc4da6

File tree

3 files changed

+177
-2
lines changed

3 files changed

+177
-2
lines changed

test/pkg/integration/apis.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,15 +572,36 @@ OuterLoop:
572572
return &config, nil
573573
}
574574

575+
// APIImageBuilderOpt configures the image builder API access
576+
type APIImageBuilderOpt func(*apiImageBuilderOpts)
577+
578+
// SelectImageBuilderMK3 selects the image builder mk3
579+
func SelectImageBuilderMK3(o *apiImageBuilderOpts) {
580+
o.SelectMK3 = true
581+
}
582+
583+
type apiImageBuilderOpts struct {
584+
SelectMK3 bool
585+
}
586+
575587
// ImageBuilder provides access to the image builder service.
576-
func (c *ComponentAPI) ImageBuilder() imgbldr.ImageBuilderClient {
588+
func (c *ComponentAPI) ImageBuilder(opts ...APIImageBuilderOpt) imgbldr.ImageBuilderClient {
589+
var cfg apiImageBuilderOpts
590+
for _, o := range opts {
591+
o(&cfg)
592+
}
593+
577594
if c.imgbldStatus.Client != nil {
578595
return c.imgbldStatus.Client
579596
}
580597

581598
err := func() error {
582599
if c.imgbldStatus.Port == 0 {
583-
pod, _, err := c.t.selectPod(ComponentImageBuilder, selectPodOptions{})
600+
cmp := ComponentImageBuilder
601+
if cfg.SelectMK3 {
602+
cmp = ComponentImageBuilderMK3
603+
}
604+
pod, _, err := c.t.selectPod(cmp, selectPodOptions{})
584605
if err != nil {
585606
return err
586607
}

test/pkg/integration/integration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,4 +571,6 @@ const (
571571
ComponentWorkspace ComponentType = "workspace"
572572
// ComponentImageBuilder points to the image-builder
573573
ComponentImageBuilder ComponentType = "image-builder"
574+
// ComponentImageBuilder points to the image-builder-mk3
575+
ComponentImageBuilderMK3 ComponentType = "image-builder-mk3"
574576
)
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package imagerbuilder_test
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"io"
11+
"testing"
12+
"time"
13+
14+
csapi "github.com/gitpod-io/gitpod/content-service/api"
15+
imgapi "github.com/gitpod-io/gitpod/image-builder/api"
16+
"github.com/gitpod-io/gitpod/test/pkg/integration"
17+
"golang.org/x/sync/errgroup"
18+
)
19+
20+
func TestBaseImageBuild(t *testing.T) {
21+
it, ctx := integration.NewTest(t, 5*time.Minute)
22+
defer it.Done()
23+
24+
client := it.API().ImageBuilder(integration.SelectImageBuilderMK3)
25+
bld, err := client.Build(ctx, &imgapi.BuildRequest{
26+
ForceRebuild: true,
27+
Source: &imgapi.BuildSource{
28+
From: &imgapi.BuildSource_File{
29+
File: &imgapi.BuildSourceDockerfile{
30+
DockerfileVersion: "some-version",
31+
DockerfilePath: ".gitpod.Dockerfile",
32+
ContextPath: ".",
33+
Source: &csapi.WorkspaceInitializer{
34+
Spec: &csapi.WorkspaceInitializer_Git{
35+
Git: &csapi.GitInitializer{
36+
RemoteUri: "https://github.com/gitpod-io/dazzle.git",
37+
TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH,
38+
CloneTaget: "main",
39+
Config: &csapi.GitConfig{
40+
Authentication: csapi.GitAuthMethod_NO_AUTH,
41+
},
42+
},
43+
},
44+
},
45+
},
46+
},
47+
},
48+
})
49+
if err != nil {
50+
t.Fatal("cannot start build", err)
51+
}
52+
53+
for {
54+
msg, err := bld.Recv()
55+
if err != nil && err != io.EOF {
56+
t.Fatal(err)
57+
}
58+
59+
if msg.Status == imgapi.BuildStatus_done_success {
60+
break
61+
} else if msg.Status == imgapi.BuildStatus_done_failure {
62+
t.Fatalf("image build failed: %s", msg.Message)
63+
} else {
64+
t.Logf("build output: %s", msg.Message)
65+
}
66+
}
67+
}
68+
69+
func TestParallelBaseImageBuild(t *testing.T) {
70+
it, ctx := integration.NewTest(t, 5*time.Minute)
71+
defer it.Done()
72+
73+
ctx, cancel := context.WithCancel(ctx)
74+
defer cancel()
75+
76+
client := it.API().ImageBuilder(integration.SelectImageBuilderMK3)
77+
req := &imgapi.BuildRequest{
78+
ForceRebuild: true,
79+
Source: &imgapi.BuildSource{
80+
From: &imgapi.BuildSource_File{
81+
File: &imgapi.BuildSourceDockerfile{
82+
DockerfileVersion: "some-version",
83+
DockerfilePath: ".gitpod.Dockerfile",
84+
ContextPath: ".",
85+
Source: &csapi.WorkspaceInitializer{
86+
Spec: &csapi.WorkspaceInitializer_Git{
87+
Git: &csapi.GitInitializer{
88+
RemoteUri: "https://github.com/gitpod-io/dazzle.git",
89+
TargetMode: csapi.CloneTargetMode_REMOTE_BRANCH,
90+
CloneTaget: "main",
91+
Config: &csapi.GitConfig{
92+
Authentication: csapi.GitAuthMethod_NO_AUTH,
93+
},
94+
},
95+
},
96+
},
97+
},
98+
},
99+
},
100+
}
101+
102+
bld0, err := client.Build(ctx, req)
103+
if err != nil {
104+
t.Fatal("cannot start build", err)
105+
return
106+
}
107+
bld1, err := client.Build(ctx, req)
108+
if err != nil {
109+
t.Fatal("cannot start build", err)
110+
return
111+
}
112+
113+
watchBuild := func(cl imgapi.ImageBuilder_BuildClient) error {
114+
for {
115+
msg, err := cl.Recv()
116+
if err != nil && err != io.EOF {
117+
return fmt.Errorf("image builder error: %v", err)
118+
}
119+
if err := ctx.Err(); err != nil {
120+
return fmt.Errorf("context error: %v", err)
121+
}
122+
123+
if msg.Status == imgapi.BuildStatus_done_success {
124+
break
125+
} else if msg.Status == imgapi.BuildStatus_done_failure {
126+
return fmt.Errorf("image build failed: %s", msg.Message)
127+
} else {
128+
t.Logf("build output: %s", msg.Message)
129+
}
130+
}
131+
return nil
132+
}
133+
134+
var eg errgroup.Group
135+
eg.Go(func() error { return watchBuild(bld0) })
136+
eg.Go(func() error { return watchBuild(bld1) })
137+
138+
blds, err := client.ListBuilds(ctx, &imgapi.ListBuildsRequest{})
139+
if err != nil {
140+
t.Fatal("cannot list builds", err)
141+
}
142+
143+
// TODO(cw): make this assertion resiliant against other on-going builds
144+
if l := len(blds.Builds); l != 1 {
145+
t.Errorf("image builder is running not just one build, but %d", l)
146+
}
147+
148+
err = eg.Wait()
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
}

0 commit comments

Comments
 (0)