github.com/GoogleCloudPlatform/testgrid@v0.0.174/pkg/api/v1/server_fake.go (about)

     1  /*
     2  Copyright 2022 The TestGrid 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 v1
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"fmt"
    23  	"io"
    24  	"io/ioutil"
    25  	"testing"
    26  	"time"
    27  
    28  	"cloud.google.com/go/storage"
    29  	"google.golang.org/protobuf/proto"
    30  
    31  	"github.com/GoogleCloudPlatform/testgrid/config"
    32  	pb "github.com/GoogleCloudPlatform/testgrid/pb/config"
    33  	statepb "github.com/GoogleCloudPlatform/testgrid/pb/state"
    34  	summarypb "github.com/GoogleCloudPlatform/testgrid/pb/summary"
    35  	"github.com/GoogleCloudPlatform/testgrid/util/gcs"
    36  )
    37  
    38  var (
    39  	serverDefaultBucket     = "gs://default"
    40  	serverSummaryPathPrefix = "summary"
    41  	serverTabPathPrefix     = "tabs"
    42  )
    43  
    44  func setupTestServer(t *testing.T, configurations map[string]*pb.Configuration, grids map[string]*statepb.Grid, summaries map[string]*summarypb.DashboardSummary) Server {
    45  	t.Helper()
    46  
    47  	fc := fakeClient{
    48  		Datastore: map[gcs.Path][]byte{},
    49  	}
    50  
    51  	config.InitCache()
    52  	for p, cfg := range configurations {
    53  		path, err := gcs.NewPath(p)
    54  		if err != nil {
    55  			t.Fatalf("setupTestServer() can't generate path: %v", err)
    56  		}
    57  
    58  		fc.Datastore[*path], err = proto.Marshal(cfg)
    59  		if err != nil {
    60  			t.Fatalf("Could not serialize proto: %v\n\nProto:\n%s", err, cfg.String())
    61  		}
    62  	}
    63  
    64  	for p, grid := range grids {
    65  		path, err := gcs.NewPath(p)
    66  		if err != nil {
    67  			t.Fatalf("setupTestServer() can't generate path: %v", err)
    68  		}
    69  
    70  		fc.Datastore[*path], err = gcs.MarshalGrid(grid)
    71  		if err != nil {
    72  			t.Fatalf("Could not serialize proto: %v\n\nProto:\n%s", err, grid.String())
    73  		}
    74  	}
    75  
    76  	for p, summary := range summaries {
    77  		path, err := gcs.NewPath(p)
    78  		if err != nil {
    79  			t.Fatalf("setupTestServer() can't generate path: %v", err)
    80  		}
    81  
    82  		sb, err := proto.Marshal(summary)
    83  		if err != nil {
    84  			t.Fatalf("Could not serialize proto: %v\n\nProto:\n%s", err, summary.String())
    85  		}
    86  
    87  		fc.Datastore[*path] = sb
    88  	}
    89  
    90  	return Server{
    91  		Client:            fc,
    92  		DefaultBucket:     serverDefaultBucket, // Needs test coverage
    93  		TabPathPrefix:     serverTabPathPrefix,
    94  		SummaryPathPrefix: serverSummaryPathPrefix,
    95  		Timeout:           10 * time.Second, // Needs test coverage
    96  	}
    97  }
    98  
    99  type fakeClient struct {
   100  	Datastore map[gcs.Path][]byte
   101  }
   102  
   103  func (f fakeClient) Open(ctx context.Context, path gcs.Path) (io.ReadCloser, *storage.ReaderObjectAttrs, error) {
   104  	data, exists := f.Datastore[path]
   105  	if !exists {
   106  		return nil, nil, fmt.Errorf("fake file %s does not exist", path.String())
   107  	}
   108  	return ioutil.NopCloser(bytes.NewReader(data)), &storage.ReaderObjectAttrs{}, nil
   109  }
   110  
   111  func (f fakeClient) Upload(ctx context.Context, path gcs.Path, bytes []byte, b bool, s string) (*storage.ObjectAttrs, error) {
   112  	panic("fakeClient Upload not implemented")
   113  }
   114  
   115  func (f fakeClient) Objects(ctx context.Context, prefix gcs.Path, delimiter, start string) gcs.Iterator {
   116  	panic("fakeClient Objects not implemented")
   117  }
   118  
   119  func (f fakeClient) Stat(ctx context.Context, prefix gcs.Path) (*storage.ObjectAttrs, error) {
   120  	panic("fakeClient Stat not implemented")
   121  }
   122  
   123  func (f fakeClient) Copy(ctx context.Context, from, to gcs.Path) (*storage.ObjectAttrs, error) {
   124  	panic("fakeClient Copy not implemented")
   125  }
   126  
   127  func (f fakeClient) If(read, write *storage.Conditions) gcs.ConditionalClient {
   128  	return f
   129  }