Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [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 build |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io/ioutil" |
| 20 | "os" |
| 21 | "path/filepath" |
Christopher Ferris | 1db36e3 | 2024-07-25 12:42:47 -0700 | [diff] [blame] | 22 | "runtime" |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 23 | "strings" |
| 24 | "testing" |
| 25 | |
| 26 | "android/soong/ui/logger" |
| 27 | ) |
| 28 | |
| 29 | func TestDumpRBEMetrics(t *testing.T) { |
Christopher Ferris | 1db36e3 | 2024-07-25 12:42:47 -0700 | [diff] [blame] | 30 | // RBE is only supported on linux. |
| 31 | if runtime.GOOS != "linux" { |
| 32 | t.Skip("RBE is only supported on linux") |
| 33 | } |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 34 | ctx := testContext() |
| 35 | tests := []struct { |
| 36 | description string |
| 37 | env []string |
| 38 | generated bool |
| 39 | }{{ |
| 40 | description: "RBE disabled", |
| 41 | env: []string{ |
| 42 | "NOSTART_RBE=true", |
| 43 | }, |
Patrice Arruda | 79dcf73 | 2020-08-01 16:49:35 +0000 | [diff] [blame] | 44 | }, { |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 45 | description: "rbe metrics generated", |
| 46 | env: []string{ |
| 47 | "USE_RBE=true", |
| 48 | }, |
| 49 | generated: true, |
Patrice Arruda | 79dcf73 | 2020-08-01 16:49:35 +0000 | [diff] [blame] | 50 | }} |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 51 | |
| 52 | for _, tt := range tests { |
| 53 | t.Run(tt.description, func(t *testing.T) { |
| 54 | tmpDir := t.TempDir() |
| 55 | |
| 56 | rbeBootstrapCmd := filepath.Join(tmpDir, bootstrapCmd) |
| 57 | if err := ioutil.WriteFile(rbeBootstrapCmd, []byte(rbeBootstrapProgram), 0755); err != nil { |
| 58 | t.Fatalf("failed to create a fake bootstrap command file %s: %v", rbeBootstrapCmd, err) |
| 59 | } |
| 60 | |
| 61 | env := Environment(tt.env) |
| 62 | env.Set("OUT_DIR", tmpDir) |
| 63 | env.Set("RBE_DIR", tmpDir) |
Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 64 | env.Set("RBE_output_dir", tmpDir) |
| 65 | env.Set("RBE_proxy_log_dir", tmpDir) |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 66 | config := Config{&configImpl{ |
| 67 | environ: &env, |
| 68 | }} |
| 69 | |
| 70 | rbeMetricsFilename := filepath.Join(tmpDir, rbeMetricsPBFilename) |
| 71 | DumpRBEMetrics(ctx, config, rbeMetricsFilename) |
| 72 | |
| 73 | // Validate that the rbe metrics file exists if RBE is enabled. |
| 74 | if _, err := os.Stat(rbeMetricsFilename); err == nil { |
| 75 | if !tt.generated { |
| 76 | t.Errorf("got true, want false for rbe metrics file %s to exist.", rbeMetricsFilename) |
| 77 | } |
| 78 | } else if os.IsNotExist(err) { |
| 79 | if tt.generated { |
| 80 | t.Errorf("got false, want true for rbe metrics file %s to exist.", rbeMetricsFilename) |
| 81 | } |
| 82 | } else { |
| 83 | t.Errorf("unknown error found on checking %s exists: %v", rbeMetricsFilename, err) |
| 84 | } |
| 85 | }) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestDumpRBEMetricsErrors(t *testing.T) { |
Christopher Ferris | 1db36e3 | 2024-07-25 12:42:47 -0700 | [diff] [blame] | 90 | // RBE is only supported on linux. |
| 91 | if runtime.GOOS != "linux" { |
| 92 | t.Skip("RBE is only supported on linux") |
| 93 | } |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 94 | ctx := testContext() |
| 95 | tests := []struct { |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 96 | description string |
| 97 | bootstrapProgram string |
| 98 | expectedErr string |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 99 | }{{ |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 100 | description: "stopRBE failed", |
| 101 | bootstrapProgram: "#!/bin/bash\nexit 1\n", |
| 102 | expectedErr: "shutdown failed", |
Patrice Arruda | 79dcf73 | 2020-08-01 16:49:35 +0000 | [diff] [blame] | 103 | }} |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 104 | |
| 105 | for _, tt := range tests { |
| 106 | t.Run(tt.description, func(t *testing.T) { |
| 107 | defer logger.Recover(func(err error) { |
| 108 | got := err.Error() |
| 109 | if !strings.Contains(got, tt.expectedErr) { |
| 110 | t.Errorf("got %q, want %q to be contained in error", got, tt.expectedErr) |
| 111 | } |
| 112 | }) |
| 113 | |
| 114 | tmpDir := t.TempDir() |
| 115 | |
| 116 | rbeBootstrapCmd := filepath.Join(tmpDir, bootstrapCmd) |
| 117 | if err := ioutil.WriteFile(rbeBootstrapCmd, []byte(tt.bootstrapProgram), 0755); err != nil { |
| 118 | t.Fatalf("failed to create a fake bootstrap command file %s: %v", rbeBootstrapCmd, err) |
| 119 | } |
| 120 | |
| 121 | env := &Environment{} |
| 122 | env.Set("USE_RBE", "true") |
| 123 | env.Set("OUT_DIR", tmpDir) |
| 124 | env.Set("RBE_DIR", tmpDir) |
| 125 | |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 126 | config := Config{&configImpl{ |
| 127 | environ: env, |
| 128 | }} |
| 129 | |
| 130 | rbeMetricsFilename := filepath.Join(tmpDir, rbeMetricsPBFilename) |
| 131 | DumpRBEMetrics(ctx, config, rbeMetricsFilename) |
| 132 | t.Errorf("got nil, expecting %q as a failure", tt.expectedErr) |
| 133 | }) |
| 134 | } |
| 135 | } |
| 136 | |
Patrice Arruda | 79dcf73 | 2020-08-01 16:49:35 +0000 | [diff] [blame] | 137 | var rbeBootstrapProgram = fmt.Sprintf("#!/bin/bash\necho 1 > $RBE_output_dir/%s\n", rbeMetricsPBFilename) |