go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/internal/config/fakecfgclient.go (about)

     1  // Copyright 2022 The LUCI Authors.
     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 config
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/common/errors"
    21  	luciconfig "go.chromium.org/luci/config"
    22  )
    23  
    24  var chromiumRevision = "deadbeef"
    25  var chromiumBuildbucketCfg = `
    26  	buckets {
    27  		name: "try"
    28  		swarming {
    29  			task_template_canary_percentage { value: 10 }
    30  			builders {
    31  				name: "linux"
    32  				dimensions: "os:Linux"
    33  				exe {
    34  					cipd_version: "refs/heads/main"
    35  					cipd_package: "infra/recipe_bundle"
    36  					cmd: ["luciexe"]
    37  				}
    38  				swarming_host: "swarming.example.com"
    39  				task_template_canary_percentage {
    40  					value: 10
    41  				}
    42  			}
    43  		}
    44  	}
    45  	buckets {
    46  		name: "master.tryserver.chromium.linux"
    47  	}
    48  	buckets {
    49  		name: "master.tryserver.chromium.win"
    50  	}
    51  `
    52  var dartRevision = "deadbeef"
    53  var dartBuildbucketCfg = `
    54  	buckets {
    55  		name: "try"
    56  		swarming {
    57  			builders {
    58  				name: "linux"
    59  				dimensions: "pool:Dart.LUCI"
    60  				exe {
    61  					cipd_version: "refs/heads/main"
    62  					cipd_package: "infra/recipe_bundle"
    63  					cmd: ["luciexe"]
    64  				}
    65  			}
    66  		}
    67  	}
    68  `
    69  var v8Revision = ""
    70  var v8BuildbucketCfg = `
    71  	buckets {
    72  		name: "master.tryserver.v8"
    73  	}
    74  `
    75  
    76  // fakeCfgClient mocks the luciconfig.Interface.
    77  type fakeCfgClient struct {
    78  	luciconfig.Interface
    79  }
    80  
    81  func (*fakeCfgClient) GetConfig(ctx context.Context, configSet luciconfig.Set, path string, metaOnly bool) (*luciconfig.Config, error) {
    82  	switch configSet {
    83  	case "projects/chromium":
    84  		return &luciconfig.Config{
    85  			Meta: luciconfig.Meta{
    86  				ConfigSet: "projects/chromium",
    87  				Path:      "fake-cr-buildbucket.cfg",
    88  				Revision:  chromiumRevision,
    89  			},
    90  			Content: chromiumBuildbucketCfg,
    91  		}, nil
    92  	case "projects/dart":
    93  		if dartBuildbucketCfg == "error" {
    94  			return nil, errors.New("internal server error")
    95  		}
    96  		return &luciconfig.Config{
    97  			Meta: luciconfig.Meta{
    98  				ConfigSet: "projects/dart",
    99  				Path:      "fake-cr-buildbucket.cfg",
   100  				Revision:  dartRevision,
   101  			},
   102  			Content: dartBuildbucketCfg,
   103  		}, nil
   104  	case "projects/v8":
   105  		return &luciconfig.Config{
   106  			Meta: luciconfig.Meta{
   107  				ConfigSet: "projects/v8",
   108  				Path:      "fake-cr-buildbucket.cfg",
   109  				Revision:  v8Revision,
   110  			},
   111  			Content: v8BuildbucketCfg,
   112  		}, nil
   113  	default:
   114  		return nil, nil
   115  	}
   116  }
   117  
   118  func (*fakeCfgClient) GetProjectConfigs(ctx context.Context, path string, metaOnly bool) ([]luciconfig.Config, error) {
   119  	if path != "${appid}.cfg" {
   120  		return nil, errors.New("not found")
   121  	}
   122  	var configsToReturn []luciconfig.Config
   123  	if chromiumBuildbucketCfg != "" {
   124  		configsToReturn = append(configsToReturn, luciconfig.Config{
   125  			Meta: luciconfig.Meta{
   126  				ConfigSet: "projects/chromium",
   127  				Path:      "fake-cr-buildbucket.cfg",
   128  				Revision:  chromiumRevision,
   129  			},
   130  		})
   131  	}
   132  	if dartBuildbucketCfg != "" {
   133  		configsToReturn = append(configsToReturn, luciconfig.Config{
   134  			Meta: luciconfig.Meta{
   135  				ConfigSet: "projects/dart",
   136  				Path:      "fake-cr-buildbucket.cfg",
   137  				Revision:  dartRevision,
   138  			},
   139  		})
   140  	}
   141  	if v8BuildbucketCfg != "" {
   142  		configsToReturn = append(configsToReturn, luciconfig.Config{
   143  			Meta: luciconfig.Meta{
   144  				ConfigSet: "projects/v8",
   145  				Path:      "fake-cr-buildbucket.cfg",
   146  				Revision:  v8Revision,
   147  			},
   148  		})
   149  	}
   150  	return configsToReturn, nil
   151  }
   152  
   153  func (*fakeCfgClient) Close() error {
   154  	return nil
   155  }