Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame^] | 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io/ioutil" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "strings" |
| 23 | "testing" |
| 24 | |
| 25 | "android/soong/ui/logger" |
| 26 | ) |
| 27 | |
| 28 | func 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", 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 | |
| 83 | func TestDumpRBEMetricsErrors(t *testing.T) { |
| 84 | ctx := testContext() |
| 85 | tests := []struct { |
| 86 | description string |
| 87 | rbeOutputDirDefined bool |
| 88 | bootstrapProgram string |
| 89 | expectedErr string |
| 90 | }{{ |
| 91 | description: "output_dir not defined", |
| 92 | bootstrapProgram: rbeBootstrapProgram, |
| 93 | expectedErr: "RBE output dir variable not defined", |
| 94 | }, { |
| 95 | description: "stopRBE failed", |
| 96 | rbeOutputDirDefined: true, |
| 97 | bootstrapProgram: "#!/bin/bash\nexit 1", |
| 98 | expectedErr: "shutdown failed", |
| 99 | }, { |
| 100 | description: "failed to copy metrics file", |
| 101 | rbeOutputDirDefined: true, |
| 102 | bootstrapProgram: "#!/bin/bash", |
| 103 | expectedErr: "failed to copy", |
| 104 | }} |
| 105 | |
| 106 | for _, tt := range tests { |
| 107 | t.Run(tt.description, func(t *testing.T) { |
| 108 | defer logger.Recover(func(err error) { |
| 109 | got := err.Error() |
| 110 | if !strings.Contains(got, tt.expectedErr) { |
| 111 | t.Errorf("got %q, want %q to be contained in error", got, tt.expectedErr) |
| 112 | } |
| 113 | }) |
| 114 | |
| 115 | tmpDir := t.TempDir() |
| 116 | |
| 117 | rbeBootstrapCmd := filepath.Join(tmpDir, bootstrapCmd) |
| 118 | if err := ioutil.WriteFile(rbeBootstrapCmd, []byte(tt.bootstrapProgram), 0755); err != nil { |
| 119 | t.Fatalf("failed to create a fake bootstrap command file %s: %v", rbeBootstrapCmd, err) |
| 120 | } |
| 121 | |
| 122 | env := &Environment{} |
| 123 | env.Set("USE_RBE", "true") |
| 124 | env.Set("OUT_DIR", tmpDir) |
| 125 | env.Set("RBE_DIR", tmpDir) |
| 126 | |
| 127 | if tt.rbeOutputDirDefined { |
| 128 | env.Set("RBE_output_dir", t.TempDir()) |
| 129 | } |
| 130 | |
| 131 | config := Config{&configImpl{ |
| 132 | environ: env, |
| 133 | }} |
| 134 | |
| 135 | rbeMetricsFilename := filepath.Join(tmpDir, rbeMetricsPBFilename) |
| 136 | DumpRBEMetrics(ctx, config, rbeMetricsFilename) |
| 137 | t.Errorf("got nil, expecting %q as a failure", tt.expectedErr) |
| 138 | }) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | var rbeBootstrapProgram = fmt.Sprintf("#!/bin/bash\necho 1 > $RBE_output_dir/%s", rbeMetricsPBFilename) |