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

     1  // Copyright 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  
    15  package gcp
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	pb "github.com/google/cloudprober/rds/proto"
    22  	serverconfigpb "github.com/google/cloudprober/rds/server/proto"
    23  )
    24  
    25  func testGCPConfig(t *testing.T, pc *serverconfigpb.Provider, projects []string, gceInstances bool, rtcConfig, pubsubTopic, apiVersion string, reEvalSec int) {
    26  	t.Helper()
    27  
    28  	if pc.GetId() != DefaultProviderID {
    29  		t.Errorf("pc.GetId()=%s, wanted=%s", pc.GetId(), DefaultProviderID)
    30  	}
    31  	c := pc.GetGcpConfig()
    32  
    33  	if !reflect.DeepEqual(c.GetProject(), projects) {
    34  		t.Errorf("Projects in GCP config=%v, wanted=%v", c.GetProject(), projects)
    35  	}
    36  
    37  	if c.GetApiVersion() != apiVersion {
    38  		t.Errorf("API verion in GCP config=%v, wanted=%v", c.GetApiVersion(), apiVersion)
    39  	}
    40  
    41  	if !gceInstances {
    42  		if c.GetGceInstances() != nil {
    43  			t.Errorf("c.GetGceInstances()=%v, wanted=nil", c.GetGceInstances())
    44  		}
    45  	} else {
    46  		if c.GetGceInstances() == nil {
    47  			t.Fatal("c.GetGceInstances() is nil, wanted=not-nil")
    48  		}
    49  		if c.GetGceInstances().GetReEvalSec() != int32(reEvalSec) {
    50  			t.Errorf("GCE instance reEvalSec=%d, wanted=%d", c.GetGceInstances().GetReEvalSec(), reEvalSec)
    51  		}
    52  	}
    53  
    54  	// Verify that RTC config is set correctly.
    55  	if rtcConfig == "" {
    56  		if c.GetRtcVariables() != nil {
    57  			t.Errorf("c.GetRtcVariables()=%v, wanted=nil", c.GetRtcVariables())
    58  		}
    59  	} else {
    60  		if c.GetRtcVariables() == nil {
    61  			t.Fatalf("c.GetRtcVariables()=nil, wanted=not-nil")
    62  		}
    63  		if c.GetRtcVariables().GetRtcConfig()[0].GetName() != rtcConfig {
    64  			t.Errorf("RTC config=%s, wanted=%s", c.GetRtcVariables().GetRtcConfig()[0].GetName(), rtcConfig)
    65  		}
    66  		if c.GetRtcVariables().GetRtcConfig()[0].GetReEvalSec() != int32(reEvalSec) {
    67  			t.Errorf("RTC config reEvalSec=%d, wanted=%d", c.GetRtcVariables().GetRtcConfig()[0].GetReEvalSec(), reEvalSec)
    68  		}
    69  	}
    70  
    71  	// Verify that Pub/Sub topic is set correctly.
    72  	if pubsubTopic == "" {
    73  		if c.GetPubsubMessages() != nil {
    74  			t.Errorf("c.GetPubsubMessages()=%v, wanted=nil", c.GetPubsubMessages())
    75  		}
    76  	} else {
    77  		if c.GetPubsubMessages() == nil {
    78  			t.Fatalf("c.GetRtcVariables()=nil, wanted=not-nil")
    79  		}
    80  		if c.GetPubsubMessages().GetSubscription()[0].GetTopicName() != pubsubTopic {
    81  			t.Errorf("Pubsub topic name=%s, wanted=%s", c.GetPubsubMessages().GetSubscription()[0].GetTopicName(), pubsubTopic)
    82  		}
    83  	}
    84  }
    85  
    86  func TestDefaultProviderConfig(t *testing.T) {
    87  	projects := []string{"p1", "p2"}
    88  	resTypes := map[string]string{
    89  		ResourceTypes.GCEInstances: "",
    90  	}
    91  	apiVersion := ""
    92  	c := DefaultProviderConfig(projects, resTypes, 10, apiVersion)
    93  	testGCPConfig(t, c, projects, true, "", "", apiVersion, 10)
    94  
    95  	// RTC and pub-sub
    96  	testRTCConfig := "rtc-config"
    97  	testPubsubTopic := "pubsub-topic"
    98  	apiVersion = "v1"
    99  	resTypes = map[string]string{
   100  		ResourceTypes.RTCVariables:   testRTCConfig,
   101  		ResourceTypes.PubsubMessages: testPubsubTopic,
   102  	}
   103  	c = DefaultProviderConfig(projects, resTypes, 10, apiVersion)
   104  	testGCPConfig(t, c, projects, false, testRTCConfig, testPubsubTopic, apiVersion, 10)
   105  
   106  	// GCE instances, RTC and pub-sub
   107  	resTypes = map[string]string{
   108  		ResourceTypes.GCEInstances:   "",
   109  		ResourceTypes.RTCVariables:   testRTCConfig,
   110  		ResourceTypes.PubsubMessages: testPubsubTopic,
   111  	}
   112  	c = DefaultProviderConfig(projects, resTypes, 10, apiVersion)
   113  	testGCPConfig(t, c, projects, true, testRTCConfig, testPubsubTopic, apiVersion, 10)
   114  }
   115  
   116  type dummyLister struct {
   117  	name string
   118  }
   119  
   120  func (dl *dummyLister) listResources(req *pb.ListResourcesRequest) ([]*pb.Resource, error) {
   121  	return []*pb.Resource{}, nil
   122  }
   123  
   124  func TestListersForResourcePath(t *testing.T) {
   125  	projects := []string{"p1", "p2"}
   126  	p := &Provider{
   127  		projects: projects,
   128  		listers:  make(map[string]map[string]lister),
   129  	}
   130  
   131  	for _, project := range projects {
   132  		p.listers[project] = map[string]lister{
   133  			ResourceTypes.GCEInstances: &dummyLister{
   134  				name: project,
   135  			},
   136  		}
   137  	}
   138  
   139  	testCases := []struct {
   140  		rp, listerName string
   141  		wantErr        bool
   142  	}{
   143  		{"gce_instances", projects[0], false},
   144  		{"gce_instances/", projects[0], false},
   145  		{"gce_instances/p1", "p1", false},
   146  		{"gce_instances/p2", "p2", false},
   147  		{"gce_instances/p3", "", true}, // unknown project
   148  		{"instances/p3", "", true},     // unknown resource_type
   149  	}
   150  
   151  	for _, tc := range testCases {
   152  		t.Run("testing_for_resource_path:"+tc.rp, func(t *testing.T) {
   153  			lr, err := p.listerForResourcePath(tc.rp)
   154  
   155  			if (err != nil) != tc.wantErr {
   156  				t.Errorf("err=%v, wantErr=%v", err, tc.wantErr)
   157  			}
   158  
   159  			if err != nil {
   160  				return
   161  			}
   162  
   163  			dlr := lr.(*dummyLister)
   164  			if dlr.name != tc.listerName {
   165  				t.Errorf("got lister=%s, wanted=%s", dlr.name, tc.listerName)
   166  			}
   167  		})
   168  	}
   169  
   170  }