...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/clientconfig/config_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/clientconfig

     1  // Copyright 2022 Google LLC
     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 clientconfig_test
    16  
    17  import (
    18  	"regexp"
    19  	"testing"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/dcl/clientconfig"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp"
    23  
    24  	"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    27  )
    28  
    29  func TestSetUserAgentWithBlueprintAttribution(t *testing.T) {
    30  	kind := "Test1Foo"
    31  	apiVersion := "test1.cnrm.cloud.google.com/v1alpha1"
    32  	bp := "test-blueprint"
    33  	dclConfig := dcl.NewConfig(dcl.WithUserAgent(gcp.KCCUserAgent))
    34  	tests := []struct {
    35  		name           string
    36  		obj            metav1.Object
    37  		dclConfig      dcl.Config
    38  		userAgentRegex string
    39  	}{
    40  		{
    41  			name: "resource with no blueprint attribution annotation",
    42  			obj: &unstructured.Unstructured{
    43  				Object: map[string]interface{}{
    44  					"kind":       kind,
    45  					"apiVersion": apiVersion,
    46  					"metadata": map[string]interface{}{
    47  						"name":      "foo-example",
    48  						"namespace": "test-system",
    49  					},
    50  				},
    51  			},
    52  			userAgentRegex: "kcc/controller-manager DeclarativeClientLib([0-9A-Za-z.:/_-]+)",
    53  		},
    54  		{
    55  			name: "resource with blueprint attribution annotation set",
    56  			obj: &unstructured.Unstructured{
    57  				Object: map[string]interface{}{
    58  					"kind":       kind,
    59  					"apiVersion": apiVersion,
    60  					"metadata": map[string]interface{}{
    61  						"name":      "foo-example",
    62  						"namespace": "test-system",
    63  						"annotations": map[string]interface{}{
    64  							"cnrm.cloud.google.com/blueprint": bp,
    65  						},
    66  					},
    67  				},
    68  			},
    69  			userAgentRegex: "kcc/controller-manager blueprints/test-blueprint DeclarativeClientLib([0-9A-Za-z.:/_-]+)",
    70  		},
    71  	}
    72  
    73  	for _, tc := range tests {
    74  		tc := tc
    75  		t.Run(tc.name, func(t *testing.T) {
    76  			newConfig := clientconfig.SetUserAgentWithBlueprintAttribution(dclConfig, tc.obj)
    77  			actualUserAgent := newConfig.UserAgent()
    78  			expr, err := regexp.Compile(tc.userAgentRegex)
    79  			if err != nil {
    80  				t.Fatalf("unexpected error: %v", err)
    81  			}
    82  			if !expr.MatchString(actualUserAgent) {
    83  				t.Fatalf("the actual user agent %v doesn't match %v regex expression", actualUserAgent, tc.userAgentRegex)
    84  			}
    85  		})
    86  	}
    87  }
    88  

View as plain text