github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/inputprocessor_test.go (about) 1 // Copyright 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package inputprocessor 16 17 import ( 18 "context" 19 "path/filepath" 20 "testing" 21 22 "github.com/bazelbuild/reclient/internal/pkg/execroot" 23 24 "github.com/bazelbuild/remote-apis-sdks/go/pkg/command" 25 "github.com/google/go-cmp/cmp" 26 "github.com/google/go-cmp/cmp/cmpopts" 27 ) 28 29 var ( 30 strSliceCmp = cmpopts.SortSlices(func(a, b string) bool { return a < b }) 31 ) 32 33 func TestBasePreprocessor(t *testing.T) { 34 er, cleanup := execroot.Setup(t, nil) 35 defer cleanup() 36 tests := []struct { 37 name string 38 options Options 39 existingFiles map[string][]byte 40 wantSpec *ActionSpec 41 }{{ 42 name: "basic", 43 options: Options{ 44 WorkingDir: "wd", 45 Cmd: []string{filepath.Join("..", "unzip_basic"), "foo.zip", "-o", "foo"}, 46 }, 47 existingFiles: map[string][]byte{"unzip_basic": nil, filepath.Join("wd", "foo.zip"): nil}, 48 wantSpec: &ActionSpec{ 49 InputSpec: &command.InputSpec{ 50 Inputs: []string{"unzip_basic"}, 51 }, 52 }, 53 }, { 54 name: "with remote toolchain inputs", 55 options: Options{ 56 WorkingDir: "wd", 57 Cmd: []string{filepath.Join("..", "unzip_wrti"), "foo.zip", "-o", "foo"}, 58 }, 59 existingFiles: map[string][]byte{ 60 "unzip_wrti": nil, 61 "unzip_wrti_remote_toolchain_inputs": []byte("lib"), 62 "lib": nil, 63 filepath.Join("wd", "foo.zip"): nil, 64 }, 65 wantSpec: &ActionSpec{ 66 InputSpec: &command.InputSpec{ 67 Inputs: []string{"unzip_wrti", "lib"}, 68 }, 69 }, 70 }, { 71 name: "environment variable rewrite", 72 options: Options{ 73 WorkingDir: "wd", 74 Cmd: []string{filepath.Join("..", "unzip_evr"), "foo.zip", "-o", "foo"}, 75 Inputs: &command.InputSpec{ 76 EnvironmentVariables: map[string]string{"FOO": filepath.Join(er, "lib")}, 77 }, 78 }, 79 existingFiles: map[string][]byte{ 80 "unzip_evr": nil, 81 filepath.Join("wd", "foo.zip"): nil, 82 }, 83 wantSpec: &ActionSpec{ 84 InputSpec: &command.InputSpec{ 85 Inputs: []string{"unzip_evr"}, 86 EnvironmentVariables: map[string]string{"FOO": filepath.Join("..", "lib")}, 87 }, 88 }, 89 }, { 90 name: "filter under exec root", 91 options: Options{ 92 WorkingDir: "wd", 93 Cmd: []string{filepath.Join("..", "unzip_fuer"), "foo.zip", "-o", "foo"}, 94 Inputs: &command.InputSpec{ 95 Inputs: []string{"badfile"}, 96 }, 97 }, 98 existingFiles: map[string][]byte{ 99 "unzip_fuer": nil, 100 filepath.Join("wd", "foo.zip"): nil, 101 }, 102 wantSpec: &ActionSpec{ 103 InputSpec: &command.InputSpec{ 104 Inputs: []string{"unzip_fuer"}, 105 }, 106 }, 107 }, { 108 name: "with remote toolchain inputs having missing files", 109 options: Options{ 110 WorkingDir: "wd", 111 Cmd: []string{filepath.Join("..", "unzip_wrtihmf"), "foo.zip", "-o", "foo"}, 112 ShallowFallback: true, 113 }, 114 existingFiles: map[string][]byte{ 115 "unzip_wrtihmf": nil, 116 "unzip_wrtihmf_remote_toolchain_inputs": []byte("bar"), 117 "lib": nil, 118 filepath.Join("wd", "foo.zip"): nil, 119 }, 120 wantSpec: &ActionSpec{ 121 InputSpec: &command.InputSpec{ 122 Inputs: []string{"unzip_wrtihmf"}, 123 }, 124 UsedShallowMode: false, 125 }, 126 }} 127 128 for _, test := range tests { 129 t.Run(test.name, func(t *testing.T) { 130 ctx := context.Background() 131 execroot.AddFilesWithContent(t, er, test.existingFiles) 132 test.options.ExecRoot = er 133 bp := &BasePreprocessor{Ctx: ctx, Options: test.options} 134 gotSpec, err := Compute(bp, test.options) 135 if err != nil { 136 t.Errorf("Compute() returned error: %v", err) 137 } 138 139 if diff := cmp.Diff(test.wantSpec, gotSpec, strSliceCmp); diff != "" { 140 t.Errorf("Compute() returned diff in ActionSpec, (-want +got): %s", diff) 141 } 142 }) 143 } 144 }