Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. All rights reserved. |
| 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 bazel |
| 16 | |
| 17 | import ( |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 18 | "encoding/json" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 19 | "fmt" |
| 20 | "reflect" |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 21 | "sort" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 22 | "testing" |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 23 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 24 | analysis_v2_proto "prebuilts/bazel/common/proto/analysis_v2" |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 25 | |
| 26 | "github.com/google/blueprint/metrics" |
| 27 | "google.golang.org/protobuf/proto" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | func TestAqueryMultiArchGenrule(t *testing.T) { |
| 31 | // This input string is retrieved from a real build of bionic-related genrules. |
| 32 | const inputString = ` |
| 33 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 34 | "Artifacts": [ |
| 35 | { "Id": 1, "path_fragment_id": 1 }, |
| 36 | { "Id": 2, "path_fragment_id": 6 }, |
| 37 | { "Id": 3, "path_fragment_id": 8 }, |
| 38 | { "Id": 4, "path_fragment_id": 12 }, |
| 39 | { "Id": 5, "path_fragment_id": 19 }, |
| 40 | { "Id": 6, "path_fragment_id": 20 }, |
| 41 | { "Id": 7, "path_fragment_id": 21 }], |
| 42 | "Actions": [{ |
| 43 | "target_id": 1, |
| 44 | "action_key": "ab53f6ecbdc2ee8cb8812613b63205464f1f5083f6dca87081a0a398c0f1ecf7", |
| 45 | "Mnemonic": "Genrule", |
| 46 | "configuration_id": 1, |
| 47 | "Arguments": ["/bin/bash", "-c", "source ../bazel_tools/tools/genrule/genrule-setup.sh; ../sourceroot/bionic/libc/tools/gensyscalls.py arm ../sourceroot/bionic/libc/SYSCALLS.TXT \u003e bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-arm.S"], |
| 48 | "environment_variables": [{ |
| 49 | "Key": "PATH", |
| 50 | "Value": "/bin:/usr/bin:/usr/local/bin" |
| 51 | }], |
| 52 | "input_dep_set_ids": [1], |
| 53 | "output_ids": [4], |
| 54 | "primary_output_id": 4 |
| 55 | }, { |
| 56 | "target_id": 2, |
| 57 | "action_key": "9f4309ce165dac458498cb92811c18b0b7919782cc37b82a42d2141b8cc90826", |
| 58 | "Mnemonic": "Genrule", |
| 59 | "configuration_id": 1, |
| 60 | "Arguments": ["/bin/bash", "-c", "source ../bazel_tools/tools/genrule/genrule-setup.sh; ../sourceroot/bionic/libc/tools/gensyscalls.py x86 ../sourceroot/bionic/libc/SYSCALLS.TXT \u003e bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-x86.S"], |
| 61 | "environment_variables": [{ |
| 62 | "Key": "PATH", |
| 63 | "Value": "/bin:/usr/bin:/usr/local/bin" |
| 64 | }], |
| 65 | "input_dep_set_ids": [2], |
| 66 | "output_ids": [5], |
| 67 | "primary_output_id": 5 |
| 68 | }, { |
| 69 | "target_id": 3, |
| 70 | "action_key": "50d6c586103ebeed3a218195540bcc30d329464eae36377eb82f8ce7c36ac342", |
| 71 | "Mnemonic": "Genrule", |
| 72 | "configuration_id": 1, |
| 73 | "Arguments": ["/bin/bash", "-c", "source ../bazel_tools/tools/genrule/genrule-setup.sh; ../sourceroot/bionic/libc/tools/gensyscalls.py x86_64 ../sourceroot/bionic/libc/SYSCALLS.TXT \u003e bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-x86_64.S"], |
| 74 | "environment_variables": [{ |
| 75 | "Key": "PATH", |
| 76 | "Value": "/bin:/usr/bin:/usr/local/bin" |
| 77 | }], |
| 78 | "input_dep_set_ids": [3], |
| 79 | "output_ids": [6], |
| 80 | "primary_output_id": 6 |
| 81 | }, { |
| 82 | "target_id": 4, |
| 83 | "action_key": "f30cbe442f5216f4223cf16a39112cad4ec56f31f49290d85cff587e48647ffa", |
| 84 | "Mnemonic": "Genrule", |
| 85 | "configuration_id": 1, |
| 86 | "Arguments": ["/bin/bash", "-c", "source ../bazel_tools/tools/genrule/genrule-setup.sh; ../sourceroot/bionic/libc/tools/gensyscalls.py arm64 ../sourceroot/bionic/libc/SYSCALLS.TXT \u003e bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-arm64.S"], |
| 87 | "environment_variables": [{ |
| 88 | "Key": "PATH", |
| 89 | "Value": "/bin:/usr/bin:/usr/local/bin" |
| 90 | }], |
| 91 | "input_dep_set_ids": [4], |
| 92 | "output_ids": [7], |
| 93 | "primary_output_id": 7 |
| 94 | }], |
| 95 | "Targets": [ |
| 96 | { "Id": 1, "Label": "@sourceroot//bionic/libc:syscalls-arm", "rule_class_id": 1 }, |
| 97 | { "Id": 2, "Label": "@sourceroot//bionic/libc:syscalls-x86", "rule_class_id": 1 }, |
| 98 | { "Id": 3, "Label": "@sourceroot//bionic/libc:syscalls-x86_64", "rule_class_id": 1 }, |
| 99 | { "Id": 4, "Label": "@sourceroot//bionic/libc:syscalls-arm64", "rule_class_id": 1 }], |
| 100 | "dep_set_of_files": [ |
| 101 | { "Id": 1, "direct_artifact_ids": [1, 2, 3] }, |
| 102 | { "Id": 2, "direct_artifact_ids": [1, 2, 3] }, |
| 103 | { "Id": 3, "direct_artifact_ids": [1, 2, 3] }, |
| 104 | { "Id": 4, "direct_artifact_ids": [1, 2, 3] }], |
| 105 | "Configuration": [{ |
| 106 | "Id": 1, |
| 107 | "Mnemonic": "k8-fastbuild", |
| 108 | "platform_name": "k8", |
| 109 | "Checksum": "485c362832c178e367d972177f68e69e0981e51e67ef1c160944473db53fe046" |
| 110 | }], |
| 111 | "rule_classes": [{ "Id": 1, "Name": "genrule"}], |
| 112 | "path_fragments": [ |
| 113 | { "Id": 5, "Label": ".." }, |
| 114 | { "Id": 4, "Label": "sourceroot", "parent_id": 5 }, |
| 115 | { "Id": 3, "Label": "bionic", "parent_id": 4 }, |
| 116 | { "Id": 2, "Label": "libc", "parent_id": 3 }, |
| 117 | { "Id": 1, "Label": "SYSCALLS.TXT", "parent_id": 2 }, |
| 118 | { "Id": 7, "Label": "tools", "parent_id": 2 }, |
| 119 | { "Id": 6, "Label": "gensyscalls.py", "parent_id": 7 }, |
| 120 | { "Id": 11, "Label": "bazel_tools", "parent_id": 5 }, |
| 121 | { "Id": 10, "Label": "tools", "parent_id": 11 }, |
| 122 | { "Id": 9, "Label": "genrule", "parent_id": 10 }, |
| 123 | { "Id": 8, "Label": "genrule-setup.sh", "parent_id": 9 }, |
| 124 | { "Id": 18, "Label": "bazel-out" }, |
| 125 | { "Id": 17, "Label": "sourceroot", "parent_id": 18 }, |
| 126 | { "Id": 16, "Label": "k8-fastbuild", "parent_id": 17 }, |
| 127 | { "Id": 15, "Label": "bin", "parent_id": 16 }, |
| 128 | { "Id": 14, "Label": "bionic", "parent_id": 15 }, |
| 129 | { "Id": 13, "Label": "libc", "parent_id": 14 }, |
| 130 | { "Id": 12, "Label": "syscalls-arm.S", "parent_id": 13 }, |
| 131 | { "Id": 19, "Label": "syscalls-x86.S", "parent_id": 13 }, |
| 132 | { "Id": 20, "Label": "syscalls-x86_64.S", "parent_id": 13 }, |
| 133 | { "Id": 21, "Label": "syscalls-arm64.S", "parent_id": 13 }] |
| 134 | } |
| 135 | ` |
| 136 | data, err := JsonToActionGraphContainer(inputString) |
| 137 | if err != nil { |
| 138 | t.Error(err) |
| 139 | return |
| 140 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 141 | actualbuildStatements, actualDepsets, _ := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 142 | var expectedBuildStatements []*BuildStatement |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 143 | for _, arch := range []string{"arm", "arm64", "x86", "x86_64"} { |
| 144 | expectedBuildStatements = append(expectedBuildStatements, |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 145 | &BuildStatement{ |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 146 | Command: fmt.Sprintf( |
| 147 | "/bin/bash -c 'source ../bazel_tools/tools/genrule/genrule-setup.sh; ../sourceroot/bionic/libc/tools/gensyscalls.py %s ../sourceroot/bionic/libc/SYSCALLS.TXT > bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-%s.S'", |
| 148 | arch, arch), |
| 149 | OutputPaths: []string{ |
| 150 | fmt.Sprintf("bazel-out/sourceroot/k8-fastbuild/bin/bionic/libc/syscalls-%s.S", arch), |
| 151 | }, |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 152 | Env: []*analysis_v2_proto.KeyValuePair{ |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 153 | {Key: "PATH", Value: "/bin:/usr/bin:/usr/local/bin"}, |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 154 | }, |
| 155 | Mnemonic: "Genrule", |
| 156 | }) |
| 157 | } |
| 158 | assertBuildStatements(t, expectedBuildStatements, actualbuildStatements) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 159 | |
| 160 | expectedFlattenedInputs := []string{ |
| 161 | "../sourceroot/bionic/libc/SYSCALLS.TXT", |
| 162 | "../sourceroot/bionic/libc/tools/gensyscalls.py", |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 163 | } |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 164 | // In this example, each depset should have the same expected inputs. |
| 165 | for _, actualDepset := range actualDepsets { |
| 166 | actualFlattenedInputs := flattenDepsets([]string{actualDepset.ContentHash}, actualDepsets) |
| 167 | if !reflect.DeepEqual(actualFlattenedInputs, expectedFlattenedInputs) { |
| 168 | t.Errorf("Expected flattened inputs %v, but got %v", expectedFlattenedInputs, actualFlattenedInputs) |
| 169 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 170 | } |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | func TestInvalidOutputId(t *testing.T) { |
| 174 | const inputString = ` |
| 175 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 176 | "artifacts": [ |
| 177 | { "id": 1, "path_fragment_id": 1 }, |
| 178 | { "id": 2, "path_fragment_id": 2 }], |
| 179 | "actions": [{ |
| 180 | "target_id": 1, |
| 181 | "action_key": "x", |
| 182 | "mnemonic": "x", |
| 183 | "arguments": ["touch", "foo"], |
| 184 | "input_dep_set_ids": [1], |
| 185 | "output_ids": [3], |
| 186 | "primary_output_id": 3 |
| 187 | }], |
| 188 | "dep_set_of_files": [ |
| 189 | { "id": 1, "direct_artifact_ids": [1, 2] }], |
| 190 | "path_fragments": [ |
| 191 | { "id": 1, "label": "one" }, |
| 192 | { "id": 2, "label": "two" }] |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 193 | }` |
| 194 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 195 | data, err := JsonToActionGraphContainer(inputString) |
| 196 | if err != nil { |
| 197 | t.Error(err) |
| 198 | return |
| 199 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 200 | _, _, err = AqueryBuildStatements(data, &metrics.EventHandler{}) |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 201 | assertError(t, err, "undefined outputId 3") |
| 202 | } |
| 203 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 204 | func TestInvalidInputDepsetIdFromAction(t *testing.T) { |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 205 | const inputString = ` |
| 206 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 207 | "artifacts": [ |
| 208 | { "id": 1, "path_fragment_id": 1 }, |
| 209 | { "id": 2, "path_fragment_id": 2 }], |
| 210 | "actions": [{ |
| 211 | "target_id": 1, |
| 212 | "action_key": "x", |
| 213 | "mnemonic": "x", |
| 214 | "arguments": ["touch", "foo"], |
| 215 | "input_dep_set_ids": [2], |
| 216 | "output_ids": [1], |
| 217 | "primary_output_id": 1 |
| 218 | }], |
| 219 | "dep_set_of_files": [ |
| 220 | { "id": 1, "direct_artifact_ids": [1, 2] }], |
| 221 | "path_fragments": [ |
| 222 | { "id": 1, "label": "one" }, |
| 223 | { "id": 2, "label": "two" }] |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 224 | }` |
| 225 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 226 | data, err := JsonToActionGraphContainer(inputString) |
| 227 | if err != nil { |
| 228 | t.Error(err) |
| 229 | return |
| 230 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 231 | _, _, err = AqueryBuildStatements(data, &metrics.EventHandler{}) |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 232 | assertError(t, err, "undefined (not even empty) input depsetId 2") |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 233 | } |
| 234 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 235 | func TestInvalidInputDepsetIdFromDepset(t *testing.T) { |
| 236 | const inputString = ` |
| 237 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 238 | "artifacts": [ |
| 239 | { "id": 1, "path_fragment_id": 1 }, |
| 240 | { "id": 2, "path_fragment_id": 2 }], |
| 241 | "actions": [{ |
| 242 | "target_id": 1, |
| 243 | "action_key": "x", |
| 244 | "mnemonic": "x", |
| 245 | "arguments": ["touch", "foo"], |
| 246 | "input_dep_set_ids": [1], |
| 247 | "output_ids": [1], |
| 248 | "primary_output_id": 1 |
| 249 | }], |
| 250 | "dep_set_of_files": [ |
| 251 | { "id": 1, "direct_artifact_ids": [1, 2], "transitive_dep_set_ids": [42] }], |
| 252 | "path_fragments": [ |
| 253 | { "id": 1, "label": "one"}, |
| 254 | { "id": 2, "label": "two" }] |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 255 | }` |
| 256 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 257 | data, err := JsonToActionGraphContainer(inputString) |
| 258 | if err != nil { |
| 259 | t.Error(err) |
| 260 | return |
| 261 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 262 | _, _, err = AqueryBuildStatements(data, &metrics.EventHandler{}) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 263 | assertError(t, err, "undefined input depsetId 42 (referenced by depsetId 1)") |
| 264 | } |
| 265 | |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 266 | func TestInvalidInputArtifactId(t *testing.T) { |
| 267 | const inputString = ` |
| 268 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 269 | "artifacts": [ |
| 270 | { "id": 1, "path_fragment_id": 1 }, |
| 271 | { "id": 2, "path_fragment_id": 2 }], |
| 272 | "actions": [{ |
| 273 | "target_id": 1, |
| 274 | "action_key": "x", |
| 275 | "mnemonic": "x", |
| 276 | "arguments": ["touch", "foo"], |
| 277 | "input_dep_set_ids": [1], |
| 278 | "output_ids": [1], |
| 279 | "primary_output_id": 1 |
| 280 | }], |
| 281 | "dep_set_of_files": [ |
| 282 | { "id": 1, "direct_artifact_ids": [1, 3] }], |
| 283 | "path_fragments": [ |
| 284 | { "id": 1, "label": "one" }, |
| 285 | { "id": 2, "label": "two" }] |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 286 | }` |
| 287 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 288 | data, err := JsonToActionGraphContainer(inputString) |
| 289 | if err != nil { |
| 290 | t.Error(err) |
| 291 | return |
| 292 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 293 | _, _, err = AqueryBuildStatements(data, &metrics.EventHandler{}) |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 294 | assertError(t, err, "undefined input artifactId 3") |
| 295 | } |
| 296 | |
| 297 | func TestInvalidPathFragmentId(t *testing.T) { |
| 298 | const inputString = ` |
| 299 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 300 | "artifacts": [ |
| 301 | { "id": 1, "path_fragment_id": 1 }, |
| 302 | { "id": 2, "path_fragment_id": 2 }], |
| 303 | "actions": [{ |
| 304 | "target_id": 1, |
| 305 | "action_key": "x", |
| 306 | "mnemonic": "x", |
| 307 | "arguments": ["touch", "foo"], |
| 308 | "input_dep_set_ids": [1], |
| 309 | "output_ids": [1], |
| 310 | "primary_output_id": 1 |
| 311 | }], |
| 312 | "dep_set_of_files": [ |
| 313 | { "id": 1, "direct_artifact_ids": [1, 2] }], |
| 314 | "path_fragments": [ |
| 315 | { "id": 1, "label": "one" }, |
| 316 | { "id": 2, "label": "two", "parent_id": 3 }] |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 317 | }` |
| 318 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 319 | data, err := JsonToActionGraphContainer(inputString) |
| 320 | if err != nil { |
| 321 | t.Error(err) |
| 322 | return |
| 323 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 324 | _, _, err = AqueryBuildStatements(data, &metrics.EventHandler{}) |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 325 | assertError(t, err, "undefined path fragment id 3") |
| 326 | } |
| 327 | |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 328 | func TestDepfiles(t *testing.T) { |
| 329 | const inputString = ` |
| 330 | { |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 331 | "artifacts": [ |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 332 | { "id": 1, "path_fragment_id": 1 }, |
| 333 | { "id": 2, "path_fragment_id": 2 }, |
| 334 | { "id": 3, "path_fragment_id": 3 }], |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 335 | "actions": [{ |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 336 | "target_Id": 1, |
| 337 | "action_Key": "x", |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 338 | "mnemonic": "x", |
| 339 | "arguments": ["touch", "foo"], |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 340 | "input_dep_set_ids": [1], |
| 341 | "output_ids": [2, 3], |
| 342 | "primary_output_id": 2 |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 343 | }], |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 344 | "dep_set_of_files": [ |
| 345 | { "id": 1, "direct_Artifact_Ids": [1, 2, 3] }], |
| 346 | "path_fragments": [ |
Sasha Smundak | e3cf1ab | 2022-06-30 11:36:18 -0700 | [diff] [blame] | 347 | { "id": 1, "label": "one" }, |
| 348 | { "id": 2, "label": "two" }, |
| 349 | { "id": 3, "label": "two.d" }] |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 350 | }` |
| 351 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 352 | data, err := JsonToActionGraphContainer(inputString) |
| 353 | if err != nil { |
| 354 | t.Error(err) |
| 355 | return |
| 356 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 357 | actual, _, err := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 358 | if err != nil { |
| 359 | t.Errorf("Unexpected error %q", err) |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 360 | return |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 361 | } |
| 362 | if expected := 1; len(actual) != expected { |
| 363 | t.Fatalf("Expected %d build statements, got %d", expected, len(actual)) |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 364 | return |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | bs := actual[0] |
| 368 | expectedDepfile := "two.d" |
| 369 | if bs.Depfile == nil { |
| 370 | t.Errorf("Expected depfile %q, but there was none found", expectedDepfile) |
| 371 | } else if *bs.Depfile != expectedDepfile { |
| 372 | t.Errorf("Expected depfile %q, but got %q", expectedDepfile, *bs.Depfile) |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | func TestMultipleDepfiles(t *testing.T) { |
| 377 | const inputString = ` |
| 378 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 379 | "artifacts": [ |
| 380 | { "id": 1, "path_fragment_id": 1 }, |
| 381 | { "id": 2, "path_fragment_id": 2 }, |
| 382 | { "id": 3, "path_fragment_id": 3 }, |
| 383 | { "id": 4, "path_fragment_id": 4 }], |
| 384 | "actions": [{ |
| 385 | "target_id": 1, |
| 386 | "action_key": "x", |
| 387 | "mnemonic": "x", |
| 388 | "arguments": ["touch", "foo"], |
| 389 | "input_dep_set_ids": [1], |
| 390 | "output_ids": [2,3,4], |
| 391 | "primary_output_id": 2 |
| 392 | }], |
| 393 | "dep_set_of_files": [{ |
| 394 | "id": 1, |
| 395 | "direct_artifact_ids": [1, 2, 3, 4] |
| 396 | }], |
| 397 | "path_fragments": [ |
| 398 | { "id": 1, "label": "one" }, |
| 399 | { "id": 2, "label": "two" }, |
| 400 | { "id": 3, "label": "two.d" }, |
| 401 | { "id": 4, "label": "other.d" }] |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 402 | }` |
| 403 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 404 | data, err := JsonToActionGraphContainer(inputString) |
| 405 | if err != nil { |
| 406 | t.Error(err) |
| 407 | return |
| 408 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 409 | _, _, err = AqueryBuildStatements(data, &metrics.EventHandler{}) |
Liz Kammer | de11685 | 2021-03-25 16:42:37 -0400 | [diff] [blame] | 410 | assertError(t, err, `found multiple potential depfiles "two.d", "other.d"`) |
| 411 | } |
| 412 | |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 413 | func TestTransitiveInputDepsets(t *testing.T) { |
| 414 | // The input aquery for this test comes from a proof-of-concept starlark rule which registers |
| 415 | // a single action with many inputs given via a deep depset. |
| 416 | const inputString = ` |
| 417 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 418 | "artifacts": [ |
| 419 | { "id": 1, "path_fragment_id": 1 }, |
| 420 | { "id": 2, "path_fragment_id": 7 }, |
| 421 | { "id": 3, "path_fragment_id": 8 }, |
| 422 | { "id": 4, "path_fragment_id": 9 }, |
| 423 | { "id": 5, "path_fragment_id": 10 }, |
| 424 | { "id": 6, "path_fragment_id": 11 }, |
| 425 | { "id": 7, "path_fragment_id": 12 }, |
| 426 | { "id": 8, "path_fragment_id": 13 }, |
| 427 | { "id": 9, "path_fragment_id": 14 }, |
| 428 | { "id": 10, "path_fragment_id": 15 }, |
| 429 | { "id": 11, "path_fragment_id": 16 }, |
| 430 | { "id": 12, "path_fragment_id": 17 }, |
| 431 | { "id": 13, "path_fragment_id": 18 }, |
| 432 | { "id": 14, "path_fragment_id": 19 }, |
| 433 | { "id": 15, "path_fragment_id": 20 }, |
| 434 | { "id": 16, "path_fragment_id": 21 }, |
| 435 | { "id": 17, "path_fragment_id": 22 }, |
| 436 | { "id": 18, "path_fragment_id": 23 }, |
| 437 | { "id": 19, "path_fragment_id": 24 }, |
| 438 | { "id": 20, "path_fragment_id": 25 }, |
| 439 | { "id": 21, "path_fragment_id": 26 }], |
| 440 | "actions": [{ |
| 441 | "target_id": 1, |
| 442 | "action_key": "3b826d17fadbbbcd8313e456b90ec47c078c438088891dd45b4adbcd8889dc50", |
| 443 | "mnemonic": "Action", |
| 444 | "configuration_id": 1, |
| 445 | "arguments": ["/bin/bash", "-c", "touch bazel-out/sourceroot/k8-fastbuild/bin/testpkg/test_out"], |
| 446 | "input_dep_set_ids": [1], |
| 447 | "output_ids": [21], |
| 448 | "primary_output_id": 21 |
| 449 | }], |
| 450 | "dep_set_of_files": [ |
| 451 | { "id": 3, "direct_artifact_ids": [1, 2, 3, 4, 5] }, |
| 452 | { "id": 4, "direct_artifact_ids": [6, 7, 8, 9, 10] }, |
| 453 | { "id": 2, "transitive_dep_set_ids": [3, 4], "direct_artifact_ids": [11, 12, 13, 14, 15] }, |
| 454 | { "id": 5, "direct_artifact_ids": [16, 17, 18, 19] }, |
| 455 | { "id": 1, "transitive_dep_set_ids": [2, 5], "direct_artifact_ids": [20] }], |
| 456 | "path_fragments": [ |
| 457 | { "id": 6, "label": "bazel-out" }, |
| 458 | { "id": 5, "label": "sourceroot", "parent_id": 6 }, |
| 459 | { "id": 4, "label": "k8-fastbuild", "parent_id": 5 }, |
| 460 | { "id": 3, "label": "bin", "parent_id": 4 }, |
| 461 | { "id": 2, "label": "testpkg", "parent_id": 3 }, |
| 462 | { "id": 1, "label": "test_1", "parent_id": 2 }, |
| 463 | { "id": 7, "label": "test_2", "parent_id": 2 }, |
| 464 | { "id": 8, "label": "test_3", "parent_id": 2 }, |
| 465 | { "id": 9, "label": "test_4", "parent_id": 2 }, |
| 466 | { "id": 10, "label": "test_5", "parent_id": 2 }, |
| 467 | { "id": 11, "label": "test_6", "parent_id": 2 }, |
| 468 | { "id": 12, "label": "test_7", "parent_id": 2 }, |
| 469 | { "id": 13, "label": "test_8", "parent_id": 2 }, |
| 470 | { "id": 14, "label": "test_9", "parent_id": 2 }, |
| 471 | { "id": 15, "label": "test_10", "parent_id": 2 }, |
| 472 | { "id": 16, "label": "test_11", "parent_id": 2 }, |
| 473 | { "id": 17, "label": "test_12", "parent_id": 2 }, |
| 474 | { "id": 18, "label": "test_13", "parent_id": 2 }, |
| 475 | { "id": 19, "label": "test_14", "parent_id": 2 }, |
| 476 | { "id": 20, "label": "test_15", "parent_id": 2 }, |
| 477 | { "id": 21, "label": "test_16", "parent_id": 2 }, |
| 478 | { "id": 22, "label": "test_17", "parent_id": 2 }, |
| 479 | { "id": 23, "label": "test_18", "parent_id": 2 }, |
| 480 | { "id": 24, "label": "test_19", "parent_id": 2 }, |
| 481 | { "id": 25, "label": "test_root", "parent_id": 2 }, |
| 482 | { "id": 26,"label": "test_out", "parent_id": 2 }] |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 483 | }` |
| 484 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 485 | data, err := JsonToActionGraphContainer(inputString) |
| 486 | if err != nil { |
| 487 | t.Error(err) |
| 488 | return |
| 489 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 490 | actualbuildStatements, actualDepsets, _ := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 491 | |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 492 | expectedBuildStatements := []*BuildStatement{ |
| 493 | &BuildStatement{ |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 494 | Command: "/bin/bash -c 'touch bazel-out/sourceroot/k8-fastbuild/bin/testpkg/test_out'", |
| 495 | OutputPaths: []string{"bazel-out/sourceroot/k8-fastbuild/bin/testpkg/test_out"}, |
| 496 | Mnemonic: "Action", |
| 497 | SymlinkPaths: []string{}, |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 498 | }, |
| 499 | } |
| 500 | assertBuildStatements(t, expectedBuildStatements, actualbuildStatements) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 501 | |
| 502 | // Inputs for the action are test_{i} from 1 to 20, and test_root. These inputs |
| 503 | // are given via a deep depset, but the depset is flattened when returned as a |
| 504 | // BuildStatement slice. |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 505 | var expectedFlattenedInputs []string |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 506 | for i := 1; i < 20; i++ { |
| 507 | expectedFlattenedInputs = append(expectedFlattenedInputs, fmt.Sprintf("bazel-out/sourceroot/k8-fastbuild/bin/testpkg/test_%d", i)) |
| 508 | } |
| 509 | expectedFlattenedInputs = append(expectedFlattenedInputs, "bazel-out/sourceroot/k8-fastbuild/bin/testpkg/test_root") |
| 510 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 511 | actualDepsetHashes := actualbuildStatements[0].InputDepsetHashes |
| 512 | actualFlattenedInputs := flattenDepsets(actualDepsetHashes, actualDepsets) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 513 | if !reflect.DeepEqual(actualFlattenedInputs, expectedFlattenedInputs) { |
| 514 | t.Errorf("Expected flattened inputs %v, but got %v", expectedFlattenedInputs, actualFlattenedInputs) |
| 515 | } |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 516 | } |
| 517 | |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 518 | func TestSymlinkTree(t *testing.T) { |
| 519 | const inputString = ` |
| 520 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 521 | "artifacts": [ |
| 522 | { "id": 1, "path_fragment_id": 1 }, |
| 523 | { "id": 2, "path_fragment_id": 2 }], |
| 524 | "actions": [{ |
| 525 | "target_id": 1, |
| 526 | "action_key": "x", |
| 527 | "mnemonic": "SymlinkTree", |
| 528 | "configuration_id": 1, |
| 529 | "input_dep_set_ids": [1], |
| 530 | "output_ids": [2], |
| 531 | "primary_output_id": 2, |
| 532 | "execution_platform": "//build/bazel/platforms:linux_x86_64" |
| 533 | }], |
| 534 | "path_fragments": [ |
| 535 | { "id": 1, "label": "foo.manifest" }, |
| 536 | { "id": 2, "label": "foo.runfiles/MANIFEST" }], |
| 537 | "dep_set_of_files": [ |
| 538 | { "id": 1, "direct_artifact_ids": [1] }] |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 539 | } |
| 540 | ` |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 541 | data, err := JsonToActionGraphContainer(inputString) |
| 542 | if err != nil { |
| 543 | t.Error(err) |
| 544 | return |
| 545 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 546 | actual, _, err := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 547 | if err != nil { |
| 548 | t.Errorf("Unexpected error %q", err) |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 549 | return |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 550 | } |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 551 | assertBuildStatements(t, []*BuildStatement{ |
| 552 | &BuildStatement{ |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 553 | Command: "", |
| 554 | OutputPaths: []string{"foo.runfiles/MANIFEST"}, |
| 555 | Mnemonic: "SymlinkTree", |
| 556 | InputPaths: []string{"foo.manifest"}, |
| 557 | SymlinkPaths: []string{}, |
Sasha Smundak | c180dbd | 2022-07-03 14:55:58 -0700 | [diff] [blame] | 558 | }, |
| 559 | }, actual) |
| 560 | } |
| 561 | |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 562 | func TestBazelOutRemovalFromInputDepsets(t *testing.T) { |
| 563 | const inputString = `{ |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 564 | "artifacts": [ |
| 565 | { "id": 1, "path_fragment_id": 10 }, |
| 566 | { "id": 2, "path_fragment_id": 20 }, |
| 567 | { "id": 3, "path_fragment_id": 30 }, |
| 568 | { "id": 4, "path_fragment_id": 40 }], |
| 569 | "dep_set_of_files": [{ |
| 570 | "id": 1111, |
| 571 | "direct_artifact_ids": [3 , 4] |
| 572 | }, { |
| 573 | "id": 2222, |
| 574 | "direct_artifact_ids": [3] |
| 575 | }], |
| 576 | "actions": [{ |
| 577 | "target_id": 100, |
| 578 | "action_key": "x", |
| 579 | "input_dep_set_ids": [1111, 2222], |
| 580 | "mnemonic": "x", |
| 581 | "arguments": ["bogus", "command"], |
| 582 | "output_ids": [2], |
| 583 | "primary_output_id": 1 |
| 584 | }], |
| 585 | "path_fragments": [ |
| 586 | { "id": 10, "label": "input" }, |
| 587 | { "id": 20, "label": "output" }, |
| 588 | { "id": 30, "label": "dep1", "parent_id": 50 }, |
| 589 | { "id": 40, "label": "dep2", "parent_id": 60 }, |
| 590 | { "id": 50, "label": "bazel_tools", "parent_id": 60 }, |
| 591 | { "id": 60, "label": ".."} |
| 592 | ] |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 593 | }` |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 594 | /* depsets |
| 595 | 1111 2222 |
| 596 | / \ | |
| 597 | ../dep2 ../bazel_tools/dep1 |
| 598 | */ |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 599 | data, err := JsonToActionGraphContainer(inputString) |
| 600 | if err != nil { |
| 601 | t.Error(err) |
| 602 | return |
| 603 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 604 | actualBuildStatements, actualDepsets, _ := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 605 | if len(actualDepsets) != 1 { |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 606 | t.Errorf("expected 1 depset but found %#v", actualDepsets) |
| 607 | return |
| 608 | } |
| 609 | dep2Found := false |
| 610 | for _, dep := range flattenDepsets([]string{actualDepsets[0].ContentHash}, actualDepsets) { |
| 611 | if dep == "../bazel_tools/dep1" { |
| 612 | t.Errorf("dependency %s expected to be removed but still exists", dep) |
| 613 | } else if dep == "../dep2" { |
| 614 | dep2Found = true |
| 615 | } |
| 616 | } |
| 617 | if !dep2Found { |
| 618 | t.Errorf("dependency ../dep2 expected but not found") |
| 619 | } |
| 620 | |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 621 | expectedBuildStatement := &BuildStatement{ |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 622 | Command: "bogus command", |
| 623 | OutputPaths: []string{"output"}, |
| 624 | Mnemonic: "x", |
| 625 | SymlinkPaths: []string{}, |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 626 | } |
| 627 | buildStatementFound := false |
| 628 | for _, actualBuildStatement := range actualBuildStatements { |
| 629 | if buildStatementEquals(actualBuildStatement, expectedBuildStatement) == "" { |
| 630 | buildStatementFound = true |
| 631 | break |
| 632 | } |
| 633 | } |
| 634 | if !buildStatementFound { |
| 635 | t.Errorf("expected but missing %#v in %#v", expectedBuildStatement, actualBuildStatements) |
| 636 | return |
| 637 | } |
| 638 | } |
| 639 | |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 640 | func TestBazelOutRemovalFromTransitiveInputDepsets(t *testing.T) { |
| 641 | const inputString = `{ |
| 642 | "artifacts": [ |
| 643 | { "id": 1, "path_fragment_id": 10 }, |
| 644 | { "id": 2, "path_fragment_id": 20 }, |
| 645 | { "id": 3, "path_fragment_id": 30 }], |
| 646 | "dep_set_of_files": [{ |
| 647 | "id": 1111, |
| 648 | "transitive_dep_set_ids": [2222] |
| 649 | }, { |
| 650 | "id": 2222, |
| 651 | "direct_artifact_ids": [3] |
| 652 | }, { |
| 653 | "id": 3333, |
| 654 | "direct_artifact_ids": [3] |
| 655 | }, { |
| 656 | "id": 4444, |
| 657 | "transitive_dep_set_ids": [3333] |
| 658 | }], |
| 659 | "actions": [{ |
| 660 | "target_id": 100, |
| 661 | "action_key": "x", |
| 662 | "input_dep_set_ids": [1111, 4444], |
| 663 | "mnemonic": "x", |
| 664 | "arguments": ["bogus", "command"], |
| 665 | "output_ids": [2], |
| 666 | "primary_output_id": 1 |
| 667 | }], |
| 668 | "path_fragments": [ |
| 669 | { "id": 10, "label": "input" }, |
| 670 | { "id": 20, "label": "output" }, |
| 671 | { "id": 30, "label": "dep", "parent_id": 50 }, |
| 672 | { "id": 50, "label": "bazel_tools", "parent_id": 60 }, |
| 673 | { "id": 60, "label": ".."} |
| 674 | ] |
| 675 | }` |
| 676 | /* depsets |
| 677 | 1111 4444 |
| 678 | || || |
| 679 | 2222 3333 |
| 680 | | | |
| 681 | ../bazel_tools/dep |
| 682 | Note: in dep_set_of_files: |
| 683 | 1111 appears BEFORE its dependency,2222 while |
| 684 | 4444 appears AFTER its dependency 3333 |
| 685 | and this test shows that that order doesn't affect empty depset pruning |
| 686 | */ |
| 687 | data, err := JsonToActionGraphContainer(inputString) |
| 688 | if err != nil { |
| 689 | t.Error(err) |
| 690 | return |
| 691 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 692 | actualBuildStatements, actualDepsets, _ := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 693 | if len(actualDepsets) != 0 { |
| 694 | t.Errorf("expected 0 depsets but found %#v", actualDepsets) |
| 695 | return |
| 696 | } |
| 697 | |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 698 | expectedBuildStatement := &BuildStatement{ |
Usta Shrestha | 13fd5ae | 2023-01-27 10:55:34 -0500 | [diff] [blame] | 699 | Command: "bogus command", |
| 700 | OutputPaths: []string{"output"}, |
| 701 | Mnemonic: "x", |
| 702 | } |
| 703 | buildStatementFound := false |
| 704 | for _, actualBuildStatement := range actualBuildStatements { |
| 705 | if buildStatementEquals(actualBuildStatement, expectedBuildStatement) == "" { |
| 706 | buildStatementFound = true |
| 707 | break |
| 708 | } |
| 709 | } |
| 710 | if !buildStatementFound { |
| 711 | t.Errorf("expected but missing %#v in %#v", expectedBuildStatement, actualBuildStatements) |
| 712 | return |
| 713 | } |
| 714 | } |
| 715 | |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 716 | func TestMiddlemenAction(t *testing.T) { |
| 717 | const inputString = ` |
| 718 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 719 | "artifacts": [ |
| 720 | { "id": 1, "path_fragment_id": 1 }, |
| 721 | { "id": 2, "path_fragment_id": 2 }, |
| 722 | { "id": 3, "path_fragment_id": 3 }, |
| 723 | { "id": 4, "path_fragment_id": 4 }, |
| 724 | { "id": 5, "path_fragment_id": 5 }, |
| 725 | { "id": 6, "path_fragment_id": 6 }], |
| 726 | "path_fragments": [ |
| 727 | { "id": 1, "label": "middleinput_one" }, |
| 728 | { "id": 2, "label": "middleinput_two" }, |
| 729 | { "id": 3, "label": "middleman_artifact" }, |
| 730 | { "id": 4, "label": "maininput_one" }, |
| 731 | { "id": 5, "label": "maininput_two" }, |
| 732 | { "id": 6, "label": "output" }], |
| 733 | "dep_set_of_files": [ |
| 734 | { "id": 1, "direct_artifact_ids": [1, 2] }, |
| 735 | { "id": 2, "direct_artifact_ids": [3, 4, 5] }], |
| 736 | "actions": [{ |
| 737 | "target_id": 1, |
| 738 | "action_key": "x", |
| 739 | "mnemonic": "Middleman", |
| 740 | "arguments": ["touch", "foo"], |
| 741 | "input_dep_set_ids": [1], |
| 742 | "output_ids": [3], |
| 743 | "primary_output_id": 3 |
| 744 | }, { |
| 745 | "target_id": 2, |
| 746 | "action_key": "y", |
| 747 | "mnemonic": "Main action", |
| 748 | "arguments": ["touch", "foo"], |
| 749 | "input_dep_set_ids": [2], |
| 750 | "output_ids": [6], |
| 751 | "primary_output_id": 6 |
| 752 | }] |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 753 | }` |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 754 | data, err := JsonToActionGraphContainer(inputString) |
| 755 | if err != nil { |
| 756 | t.Error(err) |
| 757 | return |
| 758 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 759 | actualBuildStatements, actualDepsets, err := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 760 | if err != nil { |
| 761 | t.Errorf("Unexpected error %q", err) |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 762 | return |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 763 | } |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 764 | if expected := 2; len(actualBuildStatements) != expected { |
| 765 | t.Fatalf("Expected %d build statements, got %d %#v", expected, len(actualBuildStatements), actualBuildStatements) |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 766 | return |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 767 | } |
| 768 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 769 | expectedDepsetFiles := [][]string{ |
Usta Shrestha | 2ccdb42 | 2022-06-02 10:19:13 -0400 | [diff] [blame] | 770 | {"middleinput_one", "middleinput_two", "maininput_one", "maininput_two"}, |
| 771 | {"middleinput_one", "middleinput_two"}, |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 772 | } |
| 773 | assertFlattenedDepsets(t, actualDepsets, expectedDepsetFiles) |
| 774 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 775 | bs := actualBuildStatements[0] |
| 776 | if len(bs.InputPaths) > 0 { |
| 777 | t.Errorf("Expected main action raw inputs to be empty, but got %q", bs.InputPaths) |
| 778 | } |
| 779 | |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 780 | expectedOutputs := []string{"output"} |
| 781 | if !reflect.DeepEqual(bs.OutputPaths, expectedOutputs) { |
| 782 | t.Errorf("Expected main action outputs %q, but got %q", expectedOutputs, bs.OutputPaths) |
| 783 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 784 | |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 785 | expectedFlattenedInputs := []string{"middleinput_one", "middleinput_two", "maininput_one", "maininput_two"} |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 786 | actualFlattenedInputs := flattenDepsets(bs.InputDepsetHashes, actualDepsets) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 787 | |
| 788 | if !reflect.DeepEqual(actualFlattenedInputs, expectedFlattenedInputs) { |
| 789 | t.Errorf("Expected flattened inputs %v, but got %v", expectedFlattenedInputs, actualFlattenedInputs) |
| 790 | } |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 791 | |
| 792 | bs = actualBuildStatements[1] |
| 793 | if bs != nil { |
| 794 | t.Errorf("Expected nil action for skipped") |
| 795 | } |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | // Returns the contents of given depsets in concatenated post order. |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 799 | func flattenDepsets(depsetHashesToFlatten []string, allDepsets []AqueryDepset) []string { |
| 800 | depsetsByHash := map[string]AqueryDepset{} |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 801 | for _, depset := range allDepsets { |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 802 | depsetsByHash[depset.ContentHash] = depset |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 803 | } |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 804 | var result []string |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 805 | for _, depsetId := range depsetHashesToFlatten { |
| 806 | result = append(result, flattenDepset(depsetId, depsetsByHash)...) |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 807 | } |
| 808 | return result |
| 809 | } |
| 810 | |
| 811 | // Returns the contents of a given depset in post order. |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 812 | func flattenDepset(depsetHashToFlatten string, allDepsets map[string]AqueryDepset) []string { |
| 813 | depset := allDepsets[depsetHashToFlatten] |
Usta Shrestha | 16ac135 | 2022-06-22 11:01:55 -0400 | [diff] [blame] | 814 | var result []string |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 815 | for _, depsetId := range depset.TransitiveDepSetHashes { |
Chris Parsons | 1a7aca0 | 2022-04-25 22:35:15 -0400 | [diff] [blame] | 816 | result = append(result, flattenDepset(depsetId, allDepsets)...) |
| 817 | } |
| 818 | result = append(result, depset.DirectArtifacts...) |
| 819 | return result |
Chris Parsons | c4fb133 | 2021-05-18 12:31:25 -0400 | [diff] [blame] | 820 | } |
| 821 | |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 822 | func assertFlattenedDepsets(t *testing.T, actualDepsets []AqueryDepset, expectedDepsetFiles [][]string) { |
| 823 | t.Helper() |
| 824 | if len(actualDepsets) != len(expectedDepsetFiles) { |
Sasha Smundak | f10c3ac | 2022-06-08 11:46:31 -0700 | [diff] [blame] | 825 | t.Errorf("Expected %d depsets, but got %d depsets", len(expectedDepsetFiles), len(actualDepsets)) |
Chris Parsons | 0bfb1c0 | 2022-05-12 16:43:01 -0400 | [diff] [blame] | 826 | } |
| 827 | for i, actualDepset := range actualDepsets { |
| 828 | actualFlattenedInputs := flattenDepsets([]string{actualDepset.ContentHash}, actualDepsets) |
| 829 | if !reflect.DeepEqual(actualFlattenedInputs, expectedDepsetFiles[i]) { |
| 830 | t.Errorf("Expected depset files: %v, but got %v", expectedDepsetFiles[i], actualFlattenedInputs) |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 835 | func TestSimpleSymlink(t *testing.T) { |
| 836 | const inputString = ` |
| 837 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 838 | "artifacts": [ |
| 839 | { "id": 1, "path_fragment_id": 3 }, |
| 840 | { "id": 2, "path_fragment_id": 5 }], |
| 841 | "actions": [{ |
| 842 | "target_id": 1, |
| 843 | "action_key": "x", |
| 844 | "mnemonic": "Symlink", |
| 845 | "input_dep_set_ids": [1], |
| 846 | "output_ids": [2], |
| 847 | "primary_output_id": 2 |
| 848 | }], |
| 849 | "dep_set_of_files": [ |
| 850 | { "id": 1, "direct_artifact_ids": [1] }], |
| 851 | "path_fragments": [ |
| 852 | { "id": 1, "label": "one" }, |
| 853 | { "id": 2, "label": "file_subdir", "parent_id": 1 }, |
| 854 | { "id": 3, "label": "file", "parent_id": 2 }, |
| 855 | { "id": 4, "label": "symlink_subdir", "parent_id": 1 }, |
| 856 | { "id": 5, "label": "symlink", "parent_id": 4 }] |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 857 | }` |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 858 | data, err := JsonToActionGraphContainer(inputString) |
| 859 | if err != nil { |
| 860 | t.Error(err) |
| 861 | return |
| 862 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 863 | actual, _, err := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 864 | |
| 865 | if err != nil { |
| 866 | t.Errorf("Unexpected error %q", err) |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 867 | return |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 868 | } |
| 869 | |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 870 | expectedBuildStatements := []*BuildStatement{ |
| 871 | &BuildStatement{ |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 872 | Command: "mkdir -p one/symlink_subdir && " + |
| 873 | "rm -f one/symlink_subdir/symlink && " + |
Liz Kammer | c773778 | 2021-11-04 10:56:13 -0400 | [diff] [blame] | 874 | "ln -sf $PWD/one/file_subdir/file one/symlink_subdir/symlink", |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 875 | InputPaths: []string{"one/file_subdir/file"}, |
| 876 | OutputPaths: []string{"one/symlink_subdir/symlink"}, |
| 877 | SymlinkPaths: []string{"one/symlink_subdir/symlink"}, |
| 878 | Mnemonic: "Symlink", |
| 879 | }, |
| 880 | } |
| 881 | assertBuildStatements(t, actual, expectedBuildStatements) |
| 882 | } |
| 883 | |
| 884 | func TestSymlinkQuotesPaths(t *testing.T) { |
| 885 | const inputString = ` |
| 886 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 887 | "artifacts": [ |
| 888 | { "id": 1, "path_fragment_id": 3 }, |
| 889 | { "id": 2, "path_fragment_id": 5 }], |
| 890 | "actions": [{ |
| 891 | "target_id": 1, |
| 892 | "action_key": "x", |
| 893 | "mnemonic": "SolibSymlink", |
| 894 | "input_dep_set_ids": [1], |
| 895 | "output_ids": [2], |
| 896 | "primary_output_id": 2 |
| 897 | }], |
| 898 | "dep_set_of_files": [ |
| 899 | { "id": 1, "direct_artifact_ids": [1] }], |
| 900 | "path_fragments": [ |
| 901 | { "id": 1, "label": "one" }, |
| 902 | { "id": 2, "label": "file subdir", "parent_id": 1 }, |
| 903 | { "id": 3, "label": "file", "parent_id": 2 }, |
| 904 | { "id": 4, "label": "symlink subdir", "parent_id": 1 }, |
| 905 | { "id": 5, "label": "symlink", "parent_id": 4 }] |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 906 | }` |
| 907 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 908 | data, err := JsonToActionGraphContainer(inputString) |
| 909 | if err != nil { |
| 910 | t.Error(err) |
| 911 | return |
| 912 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 913 | actual, _, err := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 914 | if err != nil { |
| 915 | t.Errorf("Unexpected error %q", err) |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 916 | return |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 917 | } |
| 918 | |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 919 | expectedBuildStatements := []*BuildStatement{ |
| 920 | &BuildStatement{ |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 921 | Command: "mkdir -p 'one/symlink subdir' && " + |
| 922 | "rm -f 'one/symlink subdir/symlink' && " + |
Liz Kammer | c773778 | 2021-11-04 10:56:13 -0400 | [diff] [blame] | 923 | "ln -sf $PWD/'one/file subdir/file' 'one/symlink subdir/symlink'", |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 924 | InputPaths: []string{"one/file subdir/file"}, |
| 925 | OutputPaths: []string{"one/symlink subdir/symlink"}, |
| 926 | SymlinkPaths: []string{"one/symlink subdir/symlink"}, |
| 927 | Mnemonic: "SolibSymlink", |
| 928 | }, |
| 929 | } |
Liz Kammer | c773778 | 2021-11-04 10:56:13 -0400 | [diff] [blame] | 930 | assertBuildStatements(t, expectedBuildStatements, actual) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | func TestSymlinkMultipleInputs(t *testing.T) { |
| 934 | const inputString = ` |
| 935 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 936 | "artifacts": [ |
| 937 | { "id": 1, "path_fragment_id": 1 }, |
| 938 | { "id": 2, "path_fragment_id": 2 }, |
| 939 | { "id": 3, "path_fragment_id": 3 }], |
| 940 | "actions": [{ |
| 941 | "target_id": 1, |
| 942 | "action_key": "x", |
| 943 | "mnemonic": "Symlink", |
| 944 | "input_dep_set_ids": [1], |
| 945 | "output_ids": [3], |
| 946 | "primary_output_id": 3 |
| 947 | }], |
| 948 | "dep_set_of_files": [{ "id": 1, "direct_artifact_ids": [1,2] }], |
| 949 | "path_fragments": [ |
| 950 | { "id": 1, "label": "file" }, |
| 951 | { "id": 2, "label": "other_file" }, |
| 952 | { "id": 3, "label": "symlink" }] |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 953 | }` |
| 954 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 955 | data, err := JsonToActionGraphContainer(inputString) |
| 956 | if err != nil { |
| 957 | t.Error(err) |
| 958 | return |
| 959 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 960 | _, _, err = AqueryBuildStatements(data, &metrics.EventHandler{}) |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 961 | assertError(t, err, `Expect 1 input and 1 output to symlink action, got: input ["file" "other_file"], output ["symlink"]`) |
| 962 | } |
| 963 | |
| 964 | func TestSymlinkMultipleOutputs(t *testing.T) { |
| 965 | const inputString = ` |
| 966 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 967 | "artifacts": [ |
| 968 | { "id": 1, "path_fragment_id": 1 }, |
| 969 | { "id": 3, "path_fragment_id": 3 }], |
| 970 | "actions": [{ |
| 971 | "target_id": 1, |
| 972 | "action_key": "x", |
| 973 | "mnemonic": "Symlink", |
| 974 | "input_dep_set_ids": [1], |
| 975 | "output_ids": [2,3], |
| 976 | "primary_output_id": 2 |
| 977 | }], |
| 978 | "dep_set_of_files": [ |
| 979 | { "id": 1, "direct_artifact_ids": [1] }], |
| 980 | "path_fragments": [ |
| 981 | { "id": 1, "label": "file" }, |
| 982 | { "id": 2, "label": "symlink" }, |
| 983 | { "id": 3, "label": "other_symlink" }] |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 984 | }` |
| 985 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 986 | data, err := JsonToActionGraphContainer(inputString) |
| 987 | if err != nil { |
| 988 | t.Error(err) |
| 989 | return |
| 990 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 991 | _, _, err = AqueryBuildStatements(data, &metrics.EventHandler{}) |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 992 | assertError(t, err, "undefined outputId 2") |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 993 | } |
| 994 | |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 995 | func TestTemplateExpandActionSubstitutions(t *testing.T) { |
| 996 | const inputString = ` |
| 997 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 998 | "artifacts": [{ |
| 999 | "id": 1, |
| 1000 | "path_fragment_id": 1 |
| 1001 | }], |
| 1002 | "actions": [{ |
| 1003 | "target_id": 1, |
| 1004 | "action_key": "x", |
| 1005 | "mnemonic": "TemplateExpand", |
| 1006 | "configuration_id": 1, |
| 1007 | "output_ids": [1], |
| 1008 | "primary_output_id": 1, |
| 1009 | "execution_platform": "//build/bazel/platforms:linux_x86_64", |
| 1010 | "template_content": "Test template substitutions: %token1%, %python_binary%", |
| 1011 | "substitutions": [ |
| 1012 | { "key": "%token1%", "value": "abcd" }, |
| 1013 | { "key": "%python_binary%", "value": "python3" }] |
| 1014 | }], |
| 1015 | "path_fragments": [ |
| 1016 | { "id": 1, "label": "template_file" }] |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 1017 | }` |
| 1018 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 1019 | data, err := JsonToActionGraphContainer(inputString) |
| 1020 | if err != nil { |
| 1021 | t.Error(err) |
| 1022 | return |
| 1023 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 1024 | actual, _, err := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 1025 | if err != nil { |
| 1026 | t.Errorf("Unexpected error %q", err) |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1027 | return |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 1030 | expectedBuildStatements := []*BuildStatement{ |
| 1031 | &BuildStatement{ |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 1032 | Command: "/bin/bash -c 'echo \"Test template substitutions: abcd, python3\" | sed \"s/\\\\\\\\n/\\\\n/g\" > template_file && " + |
| 1033 | "chmod a+x template_file'", |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 1034 | OutputPaths: []string{"template_file"}, |
| 1035 | Mnemonic: "TemplateExpand", |
| 1036 | SymlinkPaths: []string{}, |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 1037 | }, |
| 1038 | } |
| 1039 | assertBuildStatements(t, expectedBuildStatements, actual) |
| 1040 | } |
| 1041 | |
| 1042 | func TestTemplateExpandActionNoOutput(t *testing.T) { |
| 1043 | const inputString = ` |
| 1044 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 1045 | "artifacts": [ |
| 1046 | { "id": 1, "path_fragment_id": 1 }], |
| 1047 | "actions": [{ |
| 1048 | "target_id": 1, |
| 1049 | "action_key": "x", |
| 1050 | "mnemonic": "TemplateExpand", |
| 1051 | "configuration_id": 1, |
| 1052 | "primary_output_id": 1, |
| 1053 | "execution_platform": "//build/bazel/platforms:linux_x86_64", |
| 1054 | "templateContent": "Test template substitutions: %token1%, %python_binary%", |
| 1055 | "substitutions": [ |
| 1056 | { "key": "%token1%", "value": "abcd" }, |
| 1057 | { "key": "%python_binary%", "value": "python3" }] |
| 1058 | }], |
| 1059 | "path_fragments": [ |
| 1060 | { "id": 1, "label": "template_file" }] |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 1061 | }` |
| 1062 | |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 1063 | data, err := JsonToActionGraphContainer(inputString) |
| 1064 | if err != nil { |
| 1065 | t.Error(err) |
| 1066 | return |
| 1067 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 1068 | _, _, err = AqueryBuildStatements(data, &metrics.EventHandler{}) |
Wei Li | 455ba83 | 2021-11-04 22:58:12 +0000 | [diff] [blame] | 1069 | assertError(t, err, `Expect 1 output to template expand action, got: output []`) |
| 1070 | } |
| 1071 | |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1072 | func TestFileWrite(t *testing.T) { |
| 1073 | const inputString = ` |
| 1074 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 1075 | "artifacts": [ |
| 1076 | { "id": 1, "path_fragment_id": 1 }], |
| 1077 | "actions": [{ |
| 1078 | "target_id": 1, |
| 1079 | "action_key": "x", |
| 1080 | "mnemonic": "FileWrite", |
| 1081 | "configuration_id": 1, |
| 1082 | "output_ids": [1], |
| 1083 | "primary_output_id": 1, |
| 1084 | "execution_platform": "//build/bazel/platforms:linux_x86_64", |
| 1085 | "file_contents": "file data\n" |
| 1086 | }], |
| 1087 | "path_fragments": [ |
| 1088 | { "id": 1, "label": "foo.manifest" }] |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1089 | } |
| 1090 | ` |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 1091 | data, err := JsonToActionGraphContainer(inputString) |
| 1092 | if err != nil { |
| 1093 | t.Error(err) |
| 1094 | return |
| 1095 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 1096 | actual, _, err := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1097 | if err != nil { |
| 1098 | t.Errorf("Unexpected error %q", err) |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1099 | return |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1100 | } |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 1101 | assertBuildStatements(t, []*BuildStatement{ |
| 1102 | &BuildStatement{ |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1103 | OutputPaths: []string{"foo.manifest"}, |
| 1104 | Mnemonic: "FileWrite", |
| 1105 | FileContents: "file data\n", |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 1106 | SymlinkPaths: []string{}, |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1107 | }, |
| 1108 | }, actual) |
| 1109 | } |
| 1110 | |
| 1111 | func TestSourceSymlinkManifest(t *testing.T) { |
| 1112 | const inputString = ` |
| 1113 | { |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 1114 | "artifacts": [ |
| 1115 | { "id": 1, "path_fragment_id": 1 }], |
| 1116 | "actions": [{ |
| 1117 | "target_id": 1, |
| 1118 | "action_key": "x", |
| 1119 | "mnemonic": "SourceSymlinkManifest", |
| 1120 | "configuration_id": 1, |
| 1121 | "output_ids": [1], |
| 1122 | "primary_output_id": 1, |
| 1123 | "execution_platform": "//build/bazel/platforms:linux_x86_64", |
| 1124 | "file_contents": "symlink target\n" |
| 1125 | }], |
| 1126 | "path_fragments": [ |
| 1127 | { "id": 1, "label": "foo.manifest" }] |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1128 | } |
| 1129 | ` |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 1130 | data, err := JsonToActionGraphContainer(inputString) |
| 1131 | if err != nil { |
| 1132 | t.Error(err) |
| 1133 | return |
| 1134 | } |
Liz Kammer | 690fbac | 2023-02-10 11:11:17 -0500 | [diff] [blame] | 1135 | actual, _, err := AqueryBuildStatements(data, &metrics.EventHandler{}) |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1136 | if err != nil { |
| 1137 | t.Errorf("Unexpected error %q", err) |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1138 | return |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1139 | } |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 1140 | assertBuildStatements(t, []*BuildStatement{ |
| 1141 | &BuildStatement{ |
Liz Kammer | 00629db | 2023-02-09 14:28:15 -0500 | [diff] [blame] | 1142 | OutputPaths: []string{"foo.manifest"}, |
| 1143 | Mnemonic: "SourceSymlinkManifest", |
| 1144 | SymlinkPaths: []string{}, |
Sasha Smundak | 1da064c | 2022-06-08 16:36:16 -0700 | [diff] [blame] | 1145 | }, |
| 1146 | }, actual) |
| 1147 | } |
| 1148 | |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1149 | func TestUnresolvedSymlink(t *testing.T) { |
| 1150 | const inputString = ` |
| 1151 | { |
| 1152 | "artifacts": [ |
| 1153 | { "id": 1, "path_fragment_id": 1 } |
| 1154 | ], |
| 1155 | "actions": [{ |
| 1156 | "target_id": 1, |
| 1157 | "action_key": "x", |
| 1158 | "mnemonic": "UnresolvedSymlink", |
| 1159 | "configuration_id": 1, |
| 1160 | "output_ids": [1], |
| 1161 | "primary_output_id": 1, |
| 1162 | "execution_platform": "//build/bazel/platforms:linux_x86_64", |
| 1163 | "unresolved_symlink_target": "symlink/target" |
| 1164 | }], |
| 1165 | "path_fragments": [ |
| 1166 | { "id": 1, "label": "path/to/symlink" } |
| 1167 | ] |
| 1168 | } |
| 1169 | ` |
| 1170 | data, err := JsonToActionGraphContainer(inputString) |
| 1171 | if err != nil { |
| 1172 | t.Error(err) |
| 1173 | return |
| 1174 | } |
| 1175 | actual, _, err := AqueryBuildStatements(data, &metrics.EventHandler{}) |
| 1176 | if err != nil { |
| 1177 | t.Errorf("Unexpected error %q", err) |
| 1178 | return |
| 1179 | } |
| 1180 | assertBuildStatements(t, []*BuildStatement{{ |
| 1181 | Command: "mkdir -p path/to && rm -f path/to/symlink && ln -sf symlink/target path/to/symlink", |
| 1182 | OutputPaths: []string{"path/to/symlink"}, |
| 1183 | Mnemonic: "UnresolvedSymlink", |
| 1184 | SymlinkPaths: []string{"path/to/symlink"}, |
| 1185 | }}, actual) |
| 1186 | } |
| 1187 | |
| 1188 | func TestUnresolvedSymlinkBazelSandwich(t *testing.T) { |
| 1189 | const inputString = ` |
| 1190 | { |
| 1191 | "artifacts": [ |
| 1192 | { "id": 1, "path_fragment_id": 1 } |
| 1193 | ], |
| 1194 | "actions": [{ |
| 1195 | "target_id": 1, |
| 1196 | "action_key": "x", |
| 1197 | "mnemonic": "UnresolvedSymlink", |
| 1198 | "configuration_id": 1, |
| 1199 | "output_ids": [1], |
| 1200 | "primary_output_id": 1, |
| 1201 | "execution_platform": "//build/bazel/platforms:linux_x86_64", |
| 1202 | "unresolved_symlink_target": "bazel_sandwich:{\"target\":\"target/product/emulator_x86_64/system\"}" |
| 1203 | }], |
| 1204 | "path_fragments": [ |
| 1205 | { "id": 1, "label": "path/to/symlink" } |
| 1206 | ] |
| 1207 | } |
| 1208 | ` |
| 1209 | data, err := JsonToActionGraphContainer(inputString) |
| 1210 | if err != nil { |
| 1211 | t.Error(err) |
| 1212 | return |
| 1213 | } |
| 1214 | actual, _, err := AqueryBuildStatements(data, &metrics.EventHandler{}) |
| 1215 | if err != nil { |
| 1216 | t.Errorf("Unexpected error %q", err) |
| 1217 | return |
| 1218 | } |
| 1219 | assertBuildStatements(t, []*BuildStatement{{ |
| 1220 | Command: "mkdir -p path/to && rm -f path/to/symlink && ln -sf {DOTDOTS_TO_OUTPUT_ROOT}../../target/product/emulator_x86_64/system path/to/symlink", |
| 1221 | OutputPaths: []string{"path/to/symlink"}, |
| 1222 | Mnemonic: "UnresolvedSymlink", |
| 1223 | SymlinkPaths: []string{"path/to/symlink"}, |
| 1224 | ImplicitDeps: []string{"target/product/emulator_x86_64/system"}, |
| 1225 | }}, actual) |
| 1226 | } |
| 1227 | |
| 1228 | func TestUnresolvedSymlinkBazelSandwichWithAlternativeDeps(t *testing.T) { |
| 1229 | const inputString = ` |
| 1230 | { |
| 1231 | "artifacts": [ |
| 1232 | { "id": 1, "path_fragment_id": 1 } |
| 1233 | ], |
| 1234 | "actions": [{ |
| 1235 | "target_id": 1, |
| 1236 | "action_key": "x", |
| 1237 | "mnemonic": "UnresolvedSymlink", |
| 1238 | "configuration_id": 1, |
| 1239 | "output_ids": [1], |
| 1240 | "primary_output_id": 1, |
| 1241 | "execution_platform": "//build/bazel/platforms:linux_x86_64", |
| 1242 | "unresolved_symlink_target": "bazel_sandwich:{\"depend_on_target\":false,\"implicit_deps\":[\"target/product/emulator_x86_64/obj/PACKAGING/systemimage_intermediates/staging_dir.stamp\"],\"target\":\"target/product/emulator_x86_64/system\"}" |
| 1243 | }], |
| 1244 | "path_fragments": [ |
| 1245 | { "id": 1, "label": "path/to/symlink" } |
| 1246 | ] |
| 1247 | } |
| 1248 | ` |
| 1249 | data, err := JsonToActionGraphContainer(inputString) |
| 1250 | if err != nil { |
| 1251 | t.Error(err) |
| 1252 | return |
| 1253 | } |
| 1254 | actual, _, err := AqueryBuildStatements(data, &metrics.EventHandler{}) |
| 1255 | if err != nil { |
| 1256 | t.Errorf("Unexpected error %q", err) |
| 1257 | return |
| 1258 | } |
| 1259 | assertBuildStatements(t, []*BuildStatement{{ |
| 1260 | Command: "mkdir -p path/to && rm -f path/to/symlink && ln -sf {DOTDOTS_TO_OUTPUT_ROOT}../../target/product/emulator_x86_64/system path/to/symlink", |
| 1261 | OutputPaths: []string{"path/to/symlink"}, |
| 1262 | Mnemonic: "UnresolvedSymlink", |
| 1263 | SymlinkPaths: []string{"path/to/symlink"}, |
| 1264 | // Note that the target of the symlink, target/product/emulator_x86_64/system, is not listed here |
| 1265 | ImplicitDeps: []string{"target/product/emulator_x86_64/obj/PACKAGING/systemimage_intermediates/staging_dir.stamp"}, |
| 1266 | }}, actual) |
| 1267 | } |
| 1268 | |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1269 | func assertError(t *testing.T, err error, expected string) { |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 1270 | t.Helper() |
Chris Parsons | 943f243 | 2021-01-19 11:36:50 -0500 | [diff] [blame] | 1271 | if err == nil { |
| 1272 | t.Errorf("expected error '%s', but got no error", expected) |
| 1273 | } else if err.Error() != expected { |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 1274 | t.Errorf("expected error:\n\t'%s', but got:\n\t'%s'", expected, err.Error()) |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | // Asserts that the given actual build statements match the given expected build statements. |
| 1279 | // Build statement equivalence is determined using buildStatementEquals. |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 1280 | func assertBuildStatements(t *testing.T, expected []*BuildStatement, actual []*BuildStatement) { |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 1281 | t.Helper() |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1282 | if len(expected) != len(actual) { |
Liz Kammer | c319299 | 2021-11-16 17:01:11 -0500 | [diff] [blame] | 1283 | t.Errorf("expected %d build statements, but got %d,\n expected: %#v,\n actual: %#v", |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1284 | len(expected), len(actual), expected, actual) |
| 1285 | return |
| 1286 | } |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1287 | type compareFn = func(i int, j int) bool |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 1288 | byCommand := func(slice []*BuildStatement) compareFn { |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1289 | return func(i int, j int) bool { |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 1290 | if slice[i] == nil { |
| 1291 | return false |
| 1292 | } else if slice[j] == nil { |
| 1293 | return false |
| 1294 | } |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1295 | return slice[i].Command < slice[j].Command |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1296 | } |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1297 | } |
| 1298 | sort.SliceStable(expected, byCommand(expected)) |
| 1299 | sort.SliceStable(actual, byCommand(actual)) |
| 1300 | for i, actualStatement := range actual { |
| 1301 | expectedStatement := expected[i] |
| 1302 | if differingField := buildStatementEquals(actualStatement, expectedStatement); differingField != "" { |
| 1303 | t.Errorf("%s differs\nunexpected build statement %#v.\nexpected: %#v", |
Usta Shrestha | ef92225 | 2022-06-02 14:23:02 -0400 | [diff] [blame] | 1304 | differingField, actualStatement, expectedStatement) |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1305 | return |
| 1306 | } |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1307 | } |
| 1308 | } |
| 1309 | |
Liz Kammer | a4655a9 | 2023-02-10 17:17:28 -0500 | [diff] [blame] | 1310 | func buildStatementEquals(first *BuildStatement, second *BuildStatement) string { |
| 1311 | if (first == nil) != (second == nil) { |
| 1312 | return "Nil" |
| 1313 | } |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1314 | if first.Mnemonic != second.Mnemonic { |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1315 | return "Mnemonic" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1316 | } |
| 1317 | if first.Command != second.Command { |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1318 | return "Command" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1319 | } |
| 1320 | // Ordering is significant for environment variables. |
| 1321 | if !reflect.DeepEqual(first.Env, second.Env) { |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1322 | return "Env" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1323 | } |
| 1324 | // Ordering is irrelevant for input and output paths, so compare sets. |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1325 | if !reflect.DeepEqual(sortedStrings(first.InputPaths), sortedStrings(second.InputPaths)) { |
| 1326 | return "InputPaths" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1327 | } |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1328 | if !reflect.DeepEqual(sortedStrings(first.OutputPaths), sortedStrings(second.OutputPaths)) { |
| 1329 | return "OutputPaths" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1330 | } |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1331 | if !reflect.DeepEqual(sortedStrings(first.SymlinkPaths), sortedStrings(second.SymlinkPaths)) { |
| 1332 | return "SymlinkPaths" |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 1333 | } |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1334 | if !reflect.DeepEqual(sortedStrings(first.ImplicitDeps), sortedStrings(second.ImplicitDeps)) { |
| 1335 | return "ImplicitDeps" |
| 1336 | } |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 1337 | if first.Depfile != second.Depfile { |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1338 | return "Depfile" |
Liz Kammer | c49e682 | 2021-06-08 15:04:11 -0400 | [diff] [blame] | 1339 | } |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1340 | return "" |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1341 | } |
| 1342 | |
Usta Shrestha | 1c4a3ea | 2022-06-23 16:09:32 -0400 | [diff] [blame] | 1343 | func sortedStrings(stringSlice []string) []string { |
| 1344 | sorted := make([]string, len(stringSlice)) |
| 1345 | copy(sorted, stringSlice) |
| 1346 | sort.Strings(sorted) |
| 1347 | return sorted |
Chris Parsons | 4f06989 | 2021-01-15 12:22:41 -0500 | [diff] [blame] | 1348 | } |
Jason Wu | 118fd2b | 2022-10-27 18:41:15 +0000 | [diff] [blame] | 1349 | |
| 1350 | // Transform the json format to ActionGraphContainer |
| 1351 | func JsonToActionGraphContainer(inputString string) ([]byte, error) { |
| 1352 | var aqueryProtoResult analysis_v2_proto.ActionGraphContainer |
| 1353 | err := json.Unmarshal([]byte(inputString), &aqueryProtoResult) |
| 1354 | if err != nil { |
| 1355 | return []byte(""), err |
| 1356 | } |
| 1357 | data, _ := proto.Marshal(&aqueryProtoResult) |
| 1358 | return data, err |
| 1359 | } |