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 "runtime" 23 "strings" 24 "testing" 25 26 "android/soong/ui/logger" 27) 28 29func TestDumpRBEMetrics(t *testing.T) { 30 // RBE is only supported on linux. 31 if runtime.GOOS != "linux" { 32 t.Skip("RBE is only supported on linux") 33 } 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 }, 44 }, { 45 description: "rbe metrics generated", 46 env: []string{ 47 "USE_RBE=true", 48 }, 49 generated: true, 50 }} 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) 64 env.Set("RBE_output_dir", tmpDir) 65 env.Set("RBE_proxy_log_dir", tmpDir) 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 89func TestDumpRBEMetricsErrors(t *testing.T) { 90 // RBE is only supported on linux. 91 if runtime.GOOS != "linux" { 92 t.Skip("RBE is only supported on linux") 93 } 94 ctx := testContext() 95 tests := []struct { 96 description string 97 bootstrapProgram string 98 expectedErr string 99 }{{ 100 description: "stopRBE failed", 101 bootstrapProgram: "#!/bin/bash\nexit 1\n", 102 expectedErr: "shutdown failed", 103 }} 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 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 137var rbeBootstrapProgram = fmt.Sprintf("#!/bin/bash\necho 1 > $RBE_output_dir/%s\n", rbeMetricsPBFilename) 138