...
1
2
3
4
5
6
7
8
9
10
11
12
13
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