github.com/google/cloudprober@v0.11.3/rds/gcp/rtc_variables_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  	"reflect"
    19  	"sort"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/golang/protobuf/proto"
    24  	pb "github.com/google/cloudprober/rds/proto"
    25  	runtimeconfig "google.golang.org/api/runtimeconfig/v1beta1"
    26  )
    27  
    28  type testVar struct {
    29  	name       string
    30  	updateTime time.Time
    31  }
    32  
    33  func (tv *testVar) rtcVar() *runtimeconfig.Variable {
    34  	return &runtimeconfig.Variable{
    35  		Name:       tv.name,
    36  		UpdateTime: tv.updateTime.Format(time.RFC3339Nano),
    37  	}
    38  }
    39  
    40  func TestProcessVar(t *testing.T) {
    41  
    42  	// Valid variable
    43  	v1UpdateTime := time.Now().Add(-6 * time.Minute)
    44  	testVar := &runtimeconfig.Variable{
    45  		Name:       "config1/v1",
    46  		UpdateTime: v1UpdateTime.Format(time.RFC3339Nano),
    47  	}
    48  	got, err := processVar(testVar)
    49  	if err != nil {
    50  		t.Errorf("processVar: got error while processing the testVar (%v), err: %v", testVar, err)
    51  	}
    52  	if got.name != "v1" {
    53  		t.Errorf("processVar: got: %s, want: v1", got.name)
    54  	}
    55  	if !got.updateTime.Equal(v1UpdateTime) {
    56  		t.Errorf("processVars: got var[%s].updateTime: %s, want var[v1]: updateTime: %v", got.name, got.updateTime, v1UpdateTime)
    57  	}
    58  
    59  	// Invalid variable
    60  	testVar = &runtimeconfig.Variable{
    61  		Name:       "invalidname",
    62  		UpdateTime: v1UpdateTime.Format(time.RFC3339Nano),
    63  	}
    64  	got, err = processVar(testVar)
    65  	if err == nil {
    66  		t.Errorf("processVar: didn't get error for the invalid testVar: %v", testVar)
    67  	}
    68  
    69  }
    70  
    71  func compareResources(t *testing.T, resources []*pb.Resource, want []string) {
    72  	t.Helper()
    73  
    74  	var got []string
    75  	for _, res := range resources {
    76  		got = append(got, res.GetName())
    77  	}
    78  
    79  	sort.Strings(got)
    80  	sort.Strings(want)
    81  
    82  	if !reflect.DeepEqual(got, want) {
    83  		t.Errorf("RTC listResources: got=%v, want=%v", got, want)
    84  	}
    85  }
    86  
    87  func TestListRTCVars(t *testing.T) {
    88  	rvl := &rtcVariablesLister{
    89  		cache: make(map[string][]*rtcVar),
    90  	}
    91  	rvl.cache["c1"] = []*rtcVar{&rtcVar{"v1", time.Now().Add(-6 * time.Minute)}, &rtcVar{"v2", time.Now().Add(-1 * time.Minute)}}
    92  	rvl.cache["c2"] = []*rtcVar{&rtcVar{"v3", time.Now().Add(-1 * time.Minute)}}
    93  
    94  	// No filter
    95  	want := []string{"v1", "v2", "v3"}
    96  	resources, err := rvl.listResources(nil)
    97  	if err != nil {
    98  		t.Errorf("Got error while listing resources: %v", err)
    99  	}
   100  	compareResources(t, resources, want)
   101  
   102  	// Config c1, updated within 5m
   103  	want = []string{"v2"}
   104  	resources, err = rvl.listResources(&pb.ListResourcesRequest{
   105  		Filter: []*pb.Filter{
   106  			&pb.Filter{
   107  				Key:   proto.String("config_name"),
   108  				Value: proto.String("c1"),
   109  			},
   110  			&pb.Filter{
   111  				Key:   proto.String("updated_within"),
   112  				Value: proto.String("5m"),
   113  			},
   114  		},
   115  	})
   116  
   117  	if err != nil {
   118  		t.Errorf("Got error while listing resources: %v", err)
   119  	}
   120  	compareResources(t, resources, want)
   121  
   122  	// Config c1 and c2
   123  	want = []string{"v1", "v2", "v3"}
   124  	resources, _ = rvl.listResources(&pb.ListResourcesRequest{
   125  		Filter: []*pb.Filter{
   126  			&pb.Filter{
   127  				Key:   proto.String("config_name"),
   128  				Value: proto.String("c"),
   129  			},
   130  		},
   131  	})
   132  
   133  	if err != nil {
   134  		t.Errorf("Got error while listing resources: %v", err)
   135  	}
   136  	compareResources(t, resources, want)
   137  }