github.com/argoproj/argo-cd/v3@v3.2.1/util/metrics/kubectl/util_test.go (about) 1 package kubectl 2 3 import ( 4 "net/url" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 // Test_resolveK8sRequestVerb is adapted from: https://github.com/argoproj/pkg/blob/f5a0a066030558f089fa645dc6546ddc5917bad5/kubeclientmetrics/metric_test.go 12 func Test_resolveK8sRequestVerb(t *testing.T) { 13 testData := []struct { 14 testName string 15 method string 16 url string 17 expected string 18 }{ 19 { 20 testName: "Pod LIST", 21 method: "GET", 22 url: "https://127.0.0.1/api/v1/namespaces/default/pods", 23 expected: "List", 24 }, 25 { 26 testName: "Pod Cluster LIST", 27 method: "GET", 28 url: "https://127.0.0.1/api/v1/pods", 29 expected: "List", 30 }, 31 { 32 testName: "Pod GET", 33 method: "GET", 34 url: "https://127.0.0.1/api/v1/namespaces/default/pods/pod-name-123456", 35 expected: "Get", 36 }, 37 { 38 testName: "Namespace LIST", 39 method: "GET", 40 url: "https://127.0.0.1/api/v1/namespaces", 41 expected: "List", 42 }, 43 { 44 testName: "Namespace GET", 45 method: "GET", 46 url: "https://127.0.0.1/api/v1/namespaces/default", 47 expected: "Get", 48 }, 49 { 50 testName: "ReplicaSet LIST", 51 method: "GET", 52 url: "https://127.0.0.1/apis/extensions/v1beta1/namespaces/default/replicasets", 53 expected: "List", 54 }, 55 { 56 testName: "ReplicaSet Cluster LIST", 57 method: "GET", 58 url: "https://127.0.0.1/apis/apps/v1/replicasets", 59 expected: "List", 60 }, 61 { 62 testName: "ReplicaSet GET", 63 method: "GET", 64 url: "https://127.0.0.1/apis/extensions/v1beta1/namespaces/default/replicasets/rs-abc123", 65 expected: "Get", 66 }, 67 { 68 testName: "VirtualService LIST", 69 method: "GET", 70 url: "https://127.0.0.1/apis/networking.istio.io/v1alpha3/namespaces/default/virtualservices", 71 expected: "List", 72 }, 73 { 74 testName: "VirtualService GET", 75 method: "GET", 76 url: "https://127.0.0.1/apis/networking.istio.io/v1alpha3/namespaces/default/virtualservices/virtual-service", 77 expected: "Get", 78 }, 79 { 80 testName: "ClusterRole LIST", 81 method: "GET", 82 url: "https://127.0.0.1/apis/rbac.authorization.k8s.io/v1/clusterroles", 83 expected: "List", 84 }, 85 { 86 testName: "ClusterRole Get", 87 method: "GET", 88 url: "https://127.0.0.1/apis/rbac.authorization.k8s.io/v1/clusterroles/argo-rollouts-clusterrole", 89 expected: "Get", 90 }, 91 { 92 testName: "CRD List", 93 method: "GET", 94 url: "https://127.0.0.1/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions", 95 expected: "List", 96 }, 97 { 98 testName: "CRD Get", 99 method: "GET", 100 url: "https://127.0.0.1/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/dummies.argoproj.io", 101 expected: "Get", 102 }, 103 { 104 testName: "Resource With Periods Get", 105 method: "GET", 106 url: "https://127.0.0.1/apis/argoproj.io/v1alpha1/namespaces/argocd/applications/my-cluster.cluster.k8s.local", 107 expected: "Get", 108 }, 109 { 110 testName: "Watch cluster resources", 111 method: "GET", 112 url: "https://127.0.0.1/api/v1/namespaces?resourceVersion=343003&watch=true", 113 expected: "Watch", 114 }, 115 { 116 testName: "Watch single cluster resource", 117 method: "GET", 118 url: "https://127.0.0.1/api/v1/namespaces?fieldSelector=metadata.name%3Ddefault&resourceVersion=0&watch=true", 119 expected: "Watch", 120 }, 121 { 122 testName: "Watch namespace resources", 123 method: "GET", 124 url: "https://127.0.0.1/api/v1/namespaces/kube-system/serviceaccounts?resourceVersion=343091&watch=true", 125 expected: "Watch", 126 }, 127 { 128 testName: "Watch single namespace resource", 129 method: "GET", 130 url: "https://127.0.0.1/api/v1/namespaces/kube-system/serviceaccounts?fieldSelector=metadata.name%3Ddefault&resourceVersion=0&watch=true", 131 expected: "Watch", 132 }, 133 { 134 testName: "Create single namespace resource", 135 method: "POST", 136 url: "https://127.0.0.1/api/v1/namespaces", 137 expected: "Create", 138 }, 139 { 140 testName: "Delete single namespace resource", 141 method: "DELETE", 142 url: "https://127.0.0.1/api/v1/namespaces/test", 143 expected: "Delete", 144 }, 145 { 146 testName: "Patch single namespace resource", 147 method: "PATCH", 148 url: "https://127.0.0.1/api/v1/namespaces/test", 149 expected: "Patch", 150 }, 151 { 152 testName: "Update single namespace resource", 153 method: "PUT", 154 url: "https://127.0.0.1/api/v1/namespaces/test", 155 expected: "Update", 156 }, 157 } 158 159 for _, td := range testData { 160 t.Run(td.testName, func(t *testing.T) { 161 u, err := url.Parse(td.url) 162 require.NoError(t, err) 163 info := resolveK8sRequestVerb(*u, td.method) 164 assert.Equal(t, td.expected, info) 165 }) 166 } 167 }