blob: 8cded2da3d3a5adec21fd79c4fe3b0efda9d3dff [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package main
import (
"context"
"net/url"
"path/filepath"
"testing"
"fuchsia.googlesource.com/infra/infra/cmd/build_init/testbed"
buildbucketpb "go.chromium.org/luci/buildbucket/proto"
)
// Integration tests for Execute.
func TestExecute(t *testing.T) {
// At this commit, the infra/testproject has a subdirectory "infra/config/generated".
// The file "infra/config/generated/bucket/example" exists.
// The file "infra/config/generated/bucket/noexist" does not.
commit := &buildbucketpb.GitilesCommit{
Host: "fuchsia.googlesource.com",
Project: "infra/testproject",
Id: "866bc31d1227669adac893800df17a6a733a5e23",
}
tests := []struct {
name string
build *buildbucketpb.Build
repoURL *url.URL
specPath string
expectedCode int
}{
{
name: "Should ExitOk if the spec file exists",
build: &buildbucketpb.Build{
Builder: &buildbucketpb.BuilderID{
// Project is unimportant.
Bucket: "bucket",
Builder: "example",
},
Input: &buildbucketpb.Build_Input{
GitilesCommit: commit,
},
},
repoURL: &url.URL{
Scheme: "https",
Host: "fuchsia.googlesource.com",
Path: "/infra/testproject",
},
specPath: filepath.Join("infra", "config", "generated", "bucket", "example"),
expectedCode: ExitOk,
},
{
name: "Should ExtiFileNotFound if the spec is not found.",
build: &buildbucketpb.Build{
Builder: &buildbucketpb.BuilderID{
Bucket: "bucket",
Builder: "noexist",
},
Input: &buildbucketpb.Build_Input{
GitilesCommit: commit,
},
},
repoURL: &url.URL{
Scheme: "https",
Host: "fuchsia.googlesource.com",
Path: "/infra/testproject",
},
specPath: filepath.Join("infra", "config", "generated", "bucket", "noexist"),
expectedCode: ExitFileNotFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tb := testbed.New(t)
tb.Setup()
defer tb.Teardown()
code := Execute(context.Background(), tt.build, tt.repoURL, tt.specPath)
if code != tt.expectedCode {
t.Errorf("wanted exit code %d but got %d", tt.expectedCode, code)
}
})
}
}