github.com/google/cloudprober@v0.11.3/targets/lameduck/lameduck_test.go (about)

     1  // Copyright 2017-2019 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  // TODO(manugarg): Add more tests after a bit of refactoring.
    15  
    16  package lameduck
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"reflect"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/golang/protobuf/proto"
    26  	rdspb "github.com/google/cloudprober/rds/proto"
    27  	"github.com/google/cloudprober/targets/endpoint"
    28  )
    29  
    30  type mockLDLister struct {
    31  	list []string
    32  }
    33  
    34  func (mldl *mockLDLister) ListEndpoints() []endpoint.Endpoint {
    35  	return endpoint.EndpointsFromNames(mldl.list)
    36  }
    37  
    38  func TestDefaultLister(t *testing.T) {
    39  	list1 := []string{"list1"}
    40  
    41  	// Initialize default lister with the given lister
    42  	InitDefaultLister(nil, &mockLDLister{list1}, nil)
    43  	lister, err := GetDefaultLister()
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  	gotList := endpoint.NamesFromEndpoints(lister.ListEndpoints())
    48  	if !reflect.DeepEqual(gotList, list1) {
    49  		t.Errorf("Default lister retured: %v, expected: %v", gotList, list1)
    50  	}
    51  
    52  	list2 := []string{"list2"}
    53  	// Initialize default lister with the given lister. This time it should have
    54  	// no impact as default lister is already initialized.
    55  	InitDefaultLister(nil, &mockLDLister{list2}, nil)
    56  	lister, err = GetDefaultLister()
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	gotList = endpoint.NamesFromEndpoints(lister.ListEndpoints())
    61  	if !reflect.DeepEqual(gotList, list1) {
    62  		t.Errorf("Default lister retured: %v, expected: %v", gotList, list1)
    63  	}
    64  }
    65  
    66  func TestList(t *testing.T) {
    67  	li := lister{
    68  		listResourcesFunc: funcListResources,
    69  		project:           "test-project",
    70  		rtcConfig:         "lame-duck-targets",
    71  		pubsubTopic:       "lame-duck-targets",
    72  	}
    73  	if err := li.initClients(); err != nil {
    74  		t.Errorf("lister.initClients(): %v", err)
    75  	}
    76  
    77  	names := endpoint.NamesFromEndpoints(li.ListEndpoints())
    78  	wantNames := []string{"v1", "v2", "m1", "m2"}
    79  
    80  	if !reflect.DeepEqual(names, wantNames) {
    81  		t.Errorf("lister.List(): got=%v, want=%v", names, wantNames)
    82  	}
    83  }
    84  
    85  // We use funcListResources to create RDS clients for testing purpose.
    86  func funcListResources(ctx context.Context, in *rdspb.ListResourcesRequest) (*rdspb.ListResourcesResponse, error) {
    87  	path := in.GetResourcePath()
    88  	resType := strings.SplitN(path, "/", 2)[0]
    89  	var resources []*rdspb.Resource
    90  
    91  	switch resType {
    92  	case "rtc_variables":
    93  		resources = []*rdspb.Resource{
    94  			{
    95  				Name: proto.String("v1"),
    96  			},
    97  			{
    98  				Name: proto.String("v2"),
    99  			},
   100  		}
   101  	case "pubsub_messages":
   102  		resources = []*rdspb.Resource{
   103  			{
   104  				Name: proto.String("m1"),
   105  			},
   106  			{
   107  				Name: proto.String("m2"),
   108  			},
   109  		}
   110  	default:
   111  		return nil, errors.New("unsupported resource_type")
   112  	}
   113  
   114  	return &rdspb.ListResourcesResponse{Resources: resources}, nil
   115  }