github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-control-plane/pkg/cache/v3/snapshot_test.go (about) 1 // Copyright 2018 Envoyproxy 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 cache_test 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 22 "github.com/hxx258456/ccgo/go-control-plane/pkg/cache/types" 23 "github.com/hxx258456/ccgo/go-control-plane/pkg/cache/v3" 24 rsrc "github.com/hxx258456/ccgo/go-control-plane/pkg/resource/v3" 25 "github.com/hxx258456/ccgo/go-control-plane/pkg/test/resource/v3" 26 ) 27 28 func TestSnapshotConsistent(t *testing.T) { 29 if err := snapshot.Consistent(); err != nil { 30 t.Errorf("got inconsistent snapshot for %#v", snapshot) 31 } 32 33 if snap, _ := cache.NewSnapshot(version, map[rsrc.Type][]types.Resource{ 34 rsrc.EndpointType: {testEndpoint}, 35 }); snap.Consistent() == nil { 36 t.Errorf("got consistent snapshot %#v", snap) 37 } 38 39 if snap, _ := cache.NewSnapshot(version, map[rsrc.Type][]types.Resource{ 40 rsrc.EndpointType: {resource.MakeEndpoint("missing", 8080)}, 41 rsrc.ClusterType: {testCluster}, 42 }); snap.Consistent() == nil { 43 t.Errorf("got consistent snapshot %#v", snap) 44 } 45 46 if snap, _ := cache.NewSnapshot(version, map[rsrc.Type][]types.Resource{ 47 rsrc.ListenerType: {testListener}}, 48 ); snap.Consistent() == nil { 49 t.Errorf("got consistent snapshot %#v", snap) 50 } 51 52 if snap, _ := cache.NewSnapshot(version, map[rsrc.Type][]types.Resource{ 53 rsrc.RouteType: {resource.MakeRoute("test", clusterName)}, 54 rsrc.ListenerType: {testListener}, 55 }); snap.Consistent() == nil { 56 t.Errorf("got consistent snapshot %#v", snap) 57 } 58 } 59 60 func TestSnapshotGetters(t *testing.T) { 61 var nilsnap *cache.Snapshot 62 if out := nilsnap.GetResources(rsrc.EndpointType); out != nil { 63 t.Errorf("got non-empty resources for nil snapshot: %#v", out) 64 } 65 if out := nilsnap.Consistent(); out == nil { 66 t.Errorf("nil snapshot should be inconsistent") 67 } 68 if out := nilsnap.GetVersion(rsrc.EndpointType); out != "" { 69 t.Errorf("got non-empty version for nil snapshot: %#v", out) 70 } 71 if out := snapshot.GetResources("not a type"); out != nil { 72 t.Errorf("got non-empty resources for unknown type: %#v", out) 73 } 74 if out := snapshot.GetVersion("not a type"); out != "" { 75 t.Errorf("got non-empty version for unknown type: %#v", out) 76 } 77 } 78 79 func TestNewSnapshotBadType(t *testing.T) { 80 snap, err := cache.NewSnapshot(version, map[rsrc.Type][]types.Resource{ 81 "random.type": nil, 82 }) 83 84 // Should receive an error from an unknown type 85 assert.Error(t, err) 86 assert.Equal(t, cache.Snapshot{}, snap) 87 }