blob: d1b8e26445fadb4f98f2284e8b4e0d5c579ce71f [file] [log] [blame]
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001// 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
15package build
16
17import (
18 "fmt"
19 "io/ioutil"
20 "os"
21 "path/filepath"
Christopher Ferris1db36e32024-07-25 12:42:47 -070022 "runtime"
Patrice Arruda62f1bf22020-07-07 12:48:26 +000023 "strings"
24 "testing"
25
26 "android/soong/ui/logger"
27)
28
29func TestDumpRBEMetrics(t *testing.T) {
Christopher Ferris1db36e32024-07-25 12:42:47 -070030 // RBE is only supported on linux.
31 if runtime.GOOS != "linux" {
32 t.Skip("RBE is only supported on linux")
33 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +000034 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 Arruda79dcf732020-08-01 16:49:35 +000044 }, {
Patrice Arruda62f1bf22020-07-07 12:48:26 +000045 description: "rbe metrics generated",
46 env: []string{
47 "USE_RBE=true",
48 },
49 generated: true,
Patrice Arruda79dcf732020-08-01 16:49:35 +000050 }}
Patrice Arruda62f1bf22020-07-07 12:48:26 +000051
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 Kumar4c180ad2022-05-27 07:48:37 -040064 env.Set("RBE_output_dir", tmpDir)
65 env.Set("RBE_proxy_log_dir", tmpDir)
Patrice Arruda62f1bf22020-07-07 12:48:26 +000066 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
89func TestDumpRBEMetricsErrors(t *testing.T) {
Christopher Ferris1db36e32024-07-25 12:42:47 -070090 // RBE is only supported on linux.
91 if runtime.GOOS != "linux" {
92 t.Skip("RBE is only supported on linux")
93 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +000094 ctx := testContext()
95 tests := []struct {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040096 description string
97 bootstrapProgram string
98 expectedErr string
Patrice Arruda62f1bf22020-07-07 12:48:26 +000099 }{{
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400100 description: "stopRBE failed",
101 bootstrapProgram: "#!/bin/bash\nexit 1\n",
102 expectedErr: "shutdown failed",
Patrice Arruda79dcf732020-08-01 16:49:35 +0000103 }}
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000104
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 Arruda62f1bf22020-07-07 12:48:26 +0000126 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 Arruda79dcf732020-08-01 16:49:35 +0000137var rbeBootstrapProgram = fmt.Sprintf("#!/bin/bash\necho 1 > $RBE_output_dir/%s\n", rbeMetricsPBFilename)