github.com/google/cloudprober@v0.11.3/rds/gcp/pubsub_test.go (about)

     1  // Copyright 2018 The Cloudprober 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 gcp
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/golang/protobuf/proto"
    22  	pb "github.com/google/cloudprober/rds/proto"
    23  )
    24  
    25  func TestListMessages(t *testing.T) {
    26  	lister := &pubsubMsgsLister{
    27  		cache: make(map[string]map[string]time.Time),
    28  	}
    29  	lister.cache["s1"] = map[string]time.Time{
    30  		"m1": time.Now().Add(-6 * time.Minute),
    31  		"m2": time.Now().Add(-1 * time.Minute),
    32  	}
    33  	lister.cache["s2"] = map[string]time.Time{
    34  		"m3": time.Now().Add(-1 * time.Minute),
    35  	}
    36  
    37  	// No filter
    38  	want := []string{"m1", "m2", "m3"}
    39  	resources, err := lister.listResources(nil)
    40  	if err != nil {
    41  		t.Errorf("Got error while listing resources: %v", err)
    42  	}
    43  	compareResources(t, resources, want)
    44  
    45  	// Subscription s1, updated within 5m
    46  	want = []string{"m2"}
    47  	resources, err = lister.listResources(&pb.ListResourcesRequest{
    48  		Filter: []*pb.Filter{
    49  			&pb.Filter{
    50  				Key:   proto.String("subscription"),
    51  				Value: proto.String("s1"),
    52  			},
    53  			&pb.Filter{
    54  				Key:   proto.String("updated_within"),
    55  				Value: proto.String("5m"),
    56  			},
    57  		},
    58  	})
    59  
    60  	if err != nil {
    61  		t.Errorf("Got error while listing resources: %v", err)
    62  	}
    63  	compareResources(t, resources, want)
    64  
    65  	// Subscription s1 and s2
    66  	want = []string{"m1", "m2", "m3"}
    67  	resources, _ = lister.listResources(&pb.ListResourcesRequest{
    68  		Filter: []*pb.Filter{
    69  			&pb.Filter{
    70  				Key:   proto.String("subscription"),
    71  				Value: proto.String("s"),
    72  			},
    73  		},
    74  	})
    75  
    76  	if err != nil {
    77  		t.Errorf("Got error while listing resources: %v", err)
    78  	}
    79  	compareResources(t, resources, want)
    80  }