|
| 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