github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/googlecloudbuild/client/fake/fake.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package fake
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  
    23  	"cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb"
    24  	"google.golang.org/protobuf/types/known/timestamppb"
    25  
    26  	"k8s.io/apimachinery/pkg/util/sets"
    27  	cloudbuild "sigs.k8s.io/prow/pkg/googlecloudbuild/client"
    28  )
    29  
    30  // ensure FakeClient implements cloudbuild.Operator
    31  var _ cloudbuild.Operator = (*FakeClient)(nil)
    32  
    33  type FakeClient struct {
    34  	// builds in map{project: map{id: *Build}}
    35  	Builds map[string]map[string]*cloudbuildpb.Build
    36  	Err    error
    37  }
    38  
    39  func NewFakeClient() *FakeClient {
    40  	return &FakeClient{
    41  		Builds: make(map[string]map[string]*cloudbuildpb.Build),
    42  	}
    43  }
    44  
    45  func (fc *FakeClient) GetBuild(ctx context.Context, project, id string) (*cloudbuildpb.Build, error) {
    46  	if fc.Err != nil {
    47  		return nil, fc.Err
    48  	}
    49  	if bldsInProj, ok := fc.Builds[project]; ok {
    50  		if bld, ok := bldsInProj[id]; ok {
    51  			return bld, nil
    52  		}
    53  	}
    54  	return nil, errors.New("not found")
    55  }
    56  
    57  func (fc *FakeClient) ListBuildsByTag(ctx context.Context, project string, tags []string) ([]*cloudbuildpb.Build, error) {
    58  	if fc.Err != nil {
    59  		return nil, fc.Err
    60  	}
    61  	var res []*cloudbuildpb.Build
    62  	bldsInProj, ok := fc.Builds[project]
    63  	if !ok {
    64  		return nil, nil
    65  	}
    66  	for _, bld := range bldsInProj {
    67  		bld := bld
    68  		if sets.New[string](bld.Tags...).HasAll(tags...) {
    69  			res = append(res, bld)
    70  		}
    71  	}
    72  	return res, nil
    73  }
    74  
    75  // Figure out whether wait here or wait by caller
    76  func (fc *FakeClient) CreateBuild(ctx context.Context, project string, bld *cloudbuildpb.Build) (*cloudbuildpb.Build, error) {
    77  	if fc.Err != nil {
    78  		return nil, fc.Err
    79  	}
    80  	if len(bld.Id) == 0 {
    81  		return nil, errors.New("build Id cannot be empty")
    82  	}
    83  	if _, ok := fc.Builds[project]; !ok {
    84  		fc.Builds[project] = make(map[string]*cloudbuildpb.Build)
    85  	}
    86  	if _, ok := fc.Builds[project][bld.Id]; ok {
    87  		return nil, errors.New("build already exist")
    88  	}
    89  	// The input is modified here. It's good for now since these fields should not be relied
    90  	// on after the build was created.
    91  	bld.Status = cloudbuildpb.Build_QUEUED
    92  	bld.StartTime = timestamppb.Now()
    93  	fc.Builds[project][bld.Id] = bld
    94  	return nil, nil
    95  }
    96  
    97  func (fc *FakeClient) CancelBuild(ctx context.Context, project, id string) (*cloudbuildpb.Build, error) {
    98  	if fc.Err != nil {
    99  		return nil, fc.Err
   100  	}
   101  	bld, err := fc.GetBuild(ctx, project, id)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  	bld.FinishTime = timestamppb.Now()
   106  	bld.Status = cloudbuildpb.Build_CANCELLED
   107  	return nil, nil
   108  }