github.com/GoogleCloudPlatform/testgrid@v0.0.174/config/cache_test.go (about) 1 /* 2 Copyright 2021 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 config 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 "cloud.google.com/go/storage" 25 "github.com/golang/protobuf/proto" 26 "github.com/google/go-cmp/cmp" 27 "google.golang.org/protobuf/testing/protocmp" 28 29 configpb "github.com/GoogleCloudPlatform/testgrid/pb/config" 30 "github.com/GoogleCloudPlatform/testgrid/util/gcs" 31 "github.com/GoogleCloudPlatform/testgrid/util/gcs/fake" 32 ) 33 34 func Test_ReadGCS(t *testing.T) { 35 expectedConfig := &configpb.Configuration{ 36 Dashboards: []*configpb.Dashboard{ 37 { 38 Name: "Example", 39 }, 40 }, 41 } 42 expectedBytes, err := proto.Marshal(expectedConfig) 43 if err != nil { 44 t.Fatalf("Can't marshal expectations: %v", err) 45 } 46 47 now := time.Now() 48 49 cases := []struct { 50 name string 51 currentCache map[string]Config 52 remoteData []byte 53 remoteLastModified time.Time 54 remoteGeneration int64 55 }{ 56 { 57 name: "read on fresh cache", 58 currentCache: map[string]Config{}, 59 remoteData: expectedBytes, 60 remoteLastModified: now, 61 remoteGeneration: 1, 62 }, 63 { 64 name: "don't read if too recent", 65 currentCache: map[string]Config{ 66 "gs://example": { 67 proto: expectedConfig, 68 attrs: &storage.ReaderObjectAttrs{ 69 LastModified: now, 70 Generation: 1, 71 }, 72 lastFetch: now, 73 }, 74 }, 75 remoteData: []byte{1, 2, 3}, 76 remoteLastModified: now, 77 remoteGeneration: 1, 78 }, 79 { 80 name: "read on stale cache", 81 currentCache: map[string]Config{ 82 "gs://example": { 83 proto: &configpb.Configuration{ 84 Dashboards: []*configpb.Dashboard{ 85 { 86 Name: "stale", 87 }, 88 }, 89 }, 90 lastFetch: now.Add(-1 * time.Hour), 91 }, 92 }, 93 remoteData: expectedBytes, 94 remoteLastModified: now, 95 remoteGeneration: 1, 96 }, 97 } 98 99 for _, test := range cases { 100 t.Run(test.name, func(t *testing.T) { 101 cache = test.currentCache 102 client := fake.Client{ 103 Opener: fake.Opener{ 104 Paths: map[gcs.Path]fake.Object{}, 105 }, 106 } 107 expectedAttrs := &storage.ReaderObjectAttrs{ 108 LastModified: test.remoteLastModified, 109 Generation: test.remoteGeneration, 110 } 111 112 client.Opener.Paths[mustPath("gs://example")] = fake.Object{ 113 Data: string(test.remoteData), 114 Attrs: expectedAttrs, 115 } 116 result, attrs, err := ReadGCS(context.Background(), &client, mustPath("gs://example")) 117 if err != nil { 118 t.Errorf("Unexpected error %v", err) 119 } 120 if diff := cmp.Diff(expectedAttrs, attrs, protocmp.Transform()); diff != "" { 121 t.Errorf("Attributes differed (-want, +got): %s", diff) 122 } 123 if diff := cmp.Diff(expectedConfig, result, protocmp.Transform()); diff != "" { 124 t.Errorf("Config differed (-want, +got): %s ", diff) 125 } 126 127 }) 128 } 129 } 130 131 func mustPath(s string) gcs.Path { 132 p, err := gcs.NewPath(s) 133 if err != nil { 134 panic(err) 135 } 136 return *p 137 }