blob: 81ee5b2b1778f5185fcb91f97e233f36b9b4df8c [file] [log] [blame]
// Copyright 2023 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 recipes
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
buildbucketpb "go.chromium.org/luci/buildbucket/proto"
"google.golang.org/protobuf/types/known/structpb"
)
func TestResolveBuildProperties(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputProperties map[string]any
versionedProperties map[string]any
expectProperties map[string]any
expectErr bool
}{
{
name: "passes through versioned properties",
inputProperties: map[string]any{},
versionedProperties: map[string]any{
"foo": "bar",
},
expectProperties: map[string]any{
"foo": "bar",
},
},
{
name: "request properties override versioned properties",
inputProperties: map[string]any{
"foo": "request_value",
},
versionedProperties: map[string]any{
"foo": "versioned_value",
"bar": "baz",
},
expectProperties: map[string]any{
"foo": "request_value",
"bar": "baz",
},
},
{
// "recipe" is a special case, and the only property for which the
// versioned value should always override the value in the build
// input.
name: "versioned recipe property overrides request property",
inputProperties: map[string]any{
"foo": "request_foo",
"recipe": "request_recipe",
},
versionedProperties: map[string]any{
"foo": "versioned_foo",
"bar": "versioned_bar",
"recipe": "versioned_recipe",
},
expectProperties: map[string]any{
"foo": "request_foo",
"bar": "versioned_bar",
"recipe": "versioned_recipe",
},
},
{
// On the off chance that the versioned properties don't specify a
// recipe, we should respect the recipe from the build input.
name: "request recipe used if no versioned property",
inputProperties: map[string]any{
"recipe": "request_recipe",
},
versionedProperties: map[string]any{},
expectProperties: map[string]any{
"recipe": "request_recipe",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
builder := &buildbucketpb.BuilderID{
Project: "fuchsia", Bucket: "ci", Builder: "bldr",
}
build := &buildbucketpb.Build{
Builder: builder,
Input: &buildbucketpb.Build_Input{},
}
var err error
build.Input.Properties, err = structpb.NewStruct(test.inputProperties)
if err != nil {
t.Fatal(err)
}
integrationDir := t.TempDir()
propertiesPath := filepath.Join(
integrationDir,
propertiesFileForBuilder(builder))
jsonProps, err := json.Marshal(test.versionedProperties)
if err != nil {
t.Fatal(err)
}
writeFile(t, propertiesPath, []byte(jsonProps))
if err := resolveBuildProperties(ctx, integrationDir, build); err != nil {
if !test.expectErr {
t.Fatalf("got unexpected error: %s", err)
}
return
} else if test.expectErr {
t.Fatalf("expected error, got nil")
}
if diff := cmp.Diff(test.expectProperties, build.Input.Properties.AsMap()); diff != "" {
t.Errorf("resolveBuildProperties(): output differs (-want, +got):\n%s", diff)
}
})
}
}
func TestReadVersionedProperties(t *testing.T) {
builder := &buildbucketpb.BuilderID{
Project: "fuchsia", Bucket: "ci", Builder: "bldr",
}
props := map[string]any{
"nested_map": map[string]any{
"server": "url",
},
"number": float64(1234),
"bool": true,
}
integrationDir := t.TempDir()
writeFile(
t,
filepath.Join(integrationDir, propertiesFileForBuilder(builder)),
mustMarshalJSON(t, props))
got, err := versionedProperties(context.Background(), integrationDir, builder)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(props, got.AsMap()); diff != "" {
t.Errorf("readVersionedProperties() output differs (-want, +got):\n%s", diff)
}
// Shadow builders should use same properties file as non-shadow counterparts.
shadowBuilder := &buildbucketpb.BuilderID{
Project: "fuchsia", Bucket: "ci.shadow", Builder: "bldr",
}
got, err = versionedProperties(context.Background(), integrationDir, shadowBuilder)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(props, got.AsMap()); diff != "" {
t.Errorf("readVersionedProperties() output differs (-want, +got):\n%s", diff)
}
}
func writeFile(t *testing.T, path string, contents []byte) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, contents, 0o600); err != nil {
t.Fatal(err)
}
}
func mustMarshalJSON(t *testing.T, obj any) []byte {
res, err := json.Marshal(obj)
if err != nil {
t.Fatal(err)
}
return res
}