blob: 8ff96bcbb6dc7d91d2f657579d0cb91c4abb7b92 [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)
59 env.Set("RBE_output_dir", t.TempDir())
60 config := Config{&configImpl{
61 environ: &env,
62 }}
63
64 rbeMetricsFilename := filepath.Join(tmpDir, rbeMetricsPBFilename)
65 DumpRBEMetrics(ctx, config, rbeMetricsFilename)
66
67 // Validate that the rbe metrics file exists if RBE is enabled.
68 if _, err := os.Stat(rbeMetricsFilename); err == nil {
69 if !tt.generated {
70 t.Errorf("got true, want false for rbe metrics file %s to exist.", rbeMetricsFilename)
71 }
72 } else if os.IsNotExist(err) {
73 if tt.generated {
74 t.Errorf("got false, want true for rbe metrics file %s to exist.", rbeMetricsFilename)
75 }
76 } else {
77 t.Errorf("unknown error found on checking %s exists: %v", rbeMetricsFilename, err)
78 }
79 })
80 }
81}
82
83func TestDumpRBEMetricsErrors(t *testing.T) {
84 ctx := testContext()
85 tests := []struct {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040086 description string
87 bootstrapProgram string
88 expectedErr string
Patrice Arruda62f1bf22020-07-07 12:48:26 +000089 }{{
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040090 description: "stopRBE failed",
91 bootstrapProgram: "#!/bin/bash\nexit 1\n",
92 expectedErr: "shutdown failed",
Patrice Arruda79dcf732020-08-01 16:49:35 +000093 }}
Patrice Arruda62f1bf22020-07-07 12:48:26 +000094
95 for _, tt := range tests {
96 t.Run(tt.description, func(t *testing.T) {
97 defer logger.Recover(func(err error) {
98 got := err.Error()
99 if !strings.Contains(got, tt.expectedErr) {
100 t.Errorf("got %q, want %q to be contained in error", got, tt.expectedErr)
101 }
102 })
103
104 tmpDir := t.TempDir()
105
106 rbeBootstrapCmd := filepath.Join(tmpDir, bootstrapCmd)
107 if err := ioutil.WriteFile(rbeBootstrapCmd, []byte(tt.bootstrapProgram), 0755); err != nil {
108 t.Fatalf("failed to create a fake bootstrap command file %s: %v", rbeBootstrapCmd, err)
109 }
110
111 env := &Environment{}
112 env.Set("USE_RBE", "true")
113 env.Set("OUT_DIR", tmpDir)
114 env.Set("RBE_DIR", tmpDir)
115
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000116 config := Config{&configImpl{
117 environ: env,
118 }}
119
120 rbeMetricsFilename := filepath.Join(tmpDir, rbeMetricsPBFilename)
121 DumpRBEMetrics(ctx, config, rbeMetricsFilename)
122 t.Errorf("got nil, expecting %q as a failure", tt.expectedErr)
123 })
124 }
125}
126
Patrice Arruda79dcf732020-08-01 16:49:35 +0000127var rbeBootstrapProgram = fmt.Sprintf("#!/bin/bash\necho 1 > $RBE_output_dir/%s\n", rbeMetricsPBFilename)