rbe_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. package build
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path/filepath"
  20. "strings"
  21. "testing"
  22. "android/soong/ui/logger"
  23. )
  24. func TestDumpRBEMetrics(t *testing.T) {
  25. ctx := testContext()
  26. tests := []struct {
  27. description string
  28. env []string
  29. generated bool
  30. }{{
  31. description: "RBE disabled",
  32. env: []string{
  33. "NOSTART_RBE=true",
  34. },
  35. }, {
  36. description: "rbe metrics generated",
  37. env: []string{
  38. "USE_RBE=true",
  39. },
  40. generated: true,
  41. }}
  42. for _, tt := range tests {
  43. t.Run(tt.description, func(t *testing.T) {
  44. tmpDir := t.TempDir()
  45. rbeBootstrapCmd := filepath.Join(tmpDir, bootstrapCmd)
  46. if err := ioutil.WriteFile(rbeBootstrapCmd, []byte(rbeBootstrapProgram), 0755); err != nil {
  47. t.Fatalf("failed to create a fake bootstrap command file %s: %v", rbeBootstrapCmd, err)
  48. }
  49. env := Environment(tt.env)
  50. env.Set("OUT_DIR", tmpDir)
  51. env.Set("RBE_DIR", tmpDir)
  52. env.Set("RBE_output_dir", tmpDir)
  53. env.Set("RBE_proxy_log_dir", tmpDir)
  54. config := Config{&configImpl{
  55. environ: &env,
  56. }}
  57. rbeMetricsFilename := filepath.Join(tmpDir, rbeMetricsPBFilename)
  58. DumpRBEMetrics(ctx, config, rbeMetricsFilename)
  59. // Validate that the rbe metrics file exists if RBE is enabled.
  60. if _, err := os.Stat(rbeMetricsFilename); err == nil {
  61. if !tt.generated {
  62. t.Errorf("got true, want false for rbe metrics file %s to exist.", rbeMetricsFilename)
  63. }
  64. } else if os.IsNotExist(err) {
  65. if tt.generated {
  66. t.Errorf("got false, want true for rbe metrics file %s to exist.", rbeMetricsFilename)
  67. }
  68. } else {
  69. t.Errorf("unknown error found on checking %s exists: %v", rbeMetricsFilename, err)
  70. }
  71. })
  72. }
  73. }
  74. func TestDumpRBEMetricsErrors(t *testing.T) {
  75. ctx := testContext()
  76. tests := []struct {
  77. description string
  78. bootstrapProgram string
  79. expectedErr string
  80. }{{
  81. description: "stopRBE failed",
  82. bootstrapProgram: "#!/bin/bash\nexit 1\n",
  83. expectedErr: "shutdown failed",
  84. }}
  85. for _, tt := range tests {
  86. t.Run(tt.description, func(t *testing.T) {
  87. defer logger.Recover(func(err error) {
  88. got := err.Error()
  89. if !strings.Contains(got, tt.expectedErr) {
  90. t.Errorf("got %q, want %q to be contained in error", got, tt.expectedErr)
  91. }
  92. })
  93. tmpDir := t.TempDir()
  94. rbeBootstrapCmd := filepath.Join(tmpDir, bootstrapCmd)
  95. if err := ioutil.WriteFile(rbeBootstrapCmd, []byte(tt.bootstrapProgram), 0755); err != nil {
  96. t.Fatalf("failed to create a fake bootstrap command file %s: %v", rbeBootstrapCmd, err)
  97. }
  98. env := &Environment{}
  99. env.Set("USE_RBE", "true")
  100. env.Set("OUT_DIR", tmpDir)
  101. env.Set("RBE_DIR", tmpDir)
  102. config := Config{&configImpl{
  103. environ: env,
  104. }}
  105. rbeMetricsFilename := filepath.Join(tmpDir, rbeMetricsPBFilename)
  106. DumpRBEMetrics(ctx, config, rbeMetricsFilename)
  107. t.Errorf("got nil, expecting %q as a failure", tt.expectedErr)
  108. })
  109. }
  110. }
  111. var rbeBootstrapProgram = fmt.Sprintf("#!/bin/bash\necho 1 > $RBE_output_dir/%s\n", rbeMetricsPBFilename)