blob: 266f76b359d8acd79caa2457821eed9e4f4ef715 [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"
22 "strings"
23 "testing"
24
25 "android/soong/ui/logger"
26)
27
28func TestDumpRBEMetrics(t *testing.T) {
29 ctx := testContext()
30 tests := []struct {
31 description string
32 env []string
33 generated bool
34 }{{
35 description: "RBE disabled",
36 env: []string{
37 "NOSTART_RBE=true",
38 },
Patrice Arruda79dcf732020-08-01 16:49:35 +000039 }, {
Patrice Arruda62f1bf22020-07-07 12:48:26 +000040 description: "rbe metrics generated",
41 env: []string{
42 "USE_RBE=true",
43 },
44 generated: true,
Patrice Arruda79dcf732020-08-01 16:49:35 +000045 }}
Patrice Arruda62f1bf22020-07-07 12:48:26 +000046
47 for _, tt := range tests {
48 t.Run(tt.description, func(t *testing.T) {
49 tmpDir := t.TempDir()
50
51 rbeBootstrapCmd := filepath.Join(tmpDir, bootstrapCmd)
52 if err := ioutil.WriteFile(rbeBootstrapCmd, []byte(rbeBootstrapProgram), 0755); err != nil {
53 t.Fatalf("failed to create a fake bootstrap command file %s: %v", rbeBootstrapCmd, err)
54 }
55
56 env := Environment(tt.env)
57 env.Set("OUT_DIR", tmpDir)
58 env.Set("RBE_DIR", tmpDir)
Kousik Kumar4c180ad2022-05-27 07:48:37 -040059 env.Set("RBE_output_dir", tmpDir)
60 env.Set("RBE_proxy_log_dir", tmpDir)
Patrice Arruda62f1bf22020-07-07 12:48:26 +000061 config := Config{&configImpl{
62 environ: &env,
63 }}
64
65 rbeMetricsFilename := filepath.Join(tmpDir, rbeMetricsPBFilename)
66 DumpRBEMetrics(ctx, config, rbeMetricsFilename)
67
68 // Validate that the rbe metrics file exists if RBE is enabled.
69 if _, err := os.Stat(rbeMetricsFilename); err == nil {
70 if !tt.generated {
71 t.Errorf("got true, want false for rbe metrics file %s to exist.", rbeMetricsFilename)
72 }
73 } else if os.IsNotExist(err) {
74 if tt.generated {
75 t.Errorf("got false, want true for rbe metrics file %s to exist.", rbeMetricsFilename)
76 }
77 } else {
78 t.Errorf("unknown error found on checking %s exists: %v", rbeMetricsFilename, err)
79 }
80 })
81 }
82}
83
84func TestDumpRBEMetricsErrors(t *testing.T) {
85 ctx := testContext()
86 tests := []struct {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040087 description string
88 bootstrapProgram string
89 expectedErr string
Patrice Arruda62f1bf22020-07-07 12:48:26 +000090 }{{
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040091 description: "stopRBE failed",
92 bootstrapProgram: "#!/bin/bash\nexit 1\n",
93 expectedErr: "shutdown failed",
Patrice Arruda79dcf732020-08-01 16:49:35 +000094 }}
Patrice Arruda62f1bf22020-07-07 12:48:26 +000095
96 for _, tt := range tests {
97 t.Run(tt.description, func(t *testing.T) {
98 defer logger.Recover(func(err error) {
99 got := err.Error()
100 if !strings.Contains(got, tt.expectedErr) {
101 t.Errorf("got %q, want %q to be contained in error", got, tt.expectedErr)
102 }
103 })
104
105 tmpDir := t.TempDir()
106
107 rbeBootstrapCmd := filepath.Join(tmpDir, bootstrapCmd)
108 if err := ioutil.WriteFile(rbeBootstrapCmd, []byte(tt.bootstrapProgram), 0755); err != nil {
109 t.Fatalf("failed to create a fake bootstrap command file %s: %v", rbeBootstrapCmd, err)
110 }
111
112 env := &Environment{}
113 env.Set("USE_RBE", "true")
114 env.Set("OUT_DIR", tmpDir)
115 env.Set("RBE_DIR", tmpDir)
116
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000117 config := Config{&configImpl{
118 environ: env,
119 }}
120
121 rbeMetricsFilename := filepath.Join(tmpDir, rbeMetricsPBFilename)
122 DumpRBEMetrics(ctx, config, rbeMetricsFilename)
123 t.Errorf("got nil, expecting %q as a failure", tt.expectedErr)
124 })
125 }
126}
127
Patrice Arruda79dcf732020-08-01 16:49:35 +0000128var rbeBootstrapProgram = fmt.Sprintf("#!/bin/bash\necho 1 > $RBE_output_dir/%s\n", rbeMetricsPBFilename)