• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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		},
39	}, {
40		description: "rbe metrics generated",
41		env: []string{
42			"USE_RBE=true",
43		},
44		generated: true,
45	}}
46
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", tmpDir)
60			env.Set("RBE_proxy_log_dir", tmpDir)
61			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 {
87		description      string
88		bootstrapProgram string
89		expectedErr      string
90	}{{
91		description:      "stopRBE failed",
92		bootstrapProgram: "#!/bin/bash\nexit 1\n",
93		expectedErr:      "shutdown failed",
94	}}
95
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
117			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
128var rbeBootstrapProgram = fmt.Sprintf("#!/bin/bash\necho 1 > $RBE_output_dir/%s\n", rbeMetricsPBFilename)
129