github.com/gocrane/crane@v0.11.0/pkg/utils/expression_prom_deafult_test.go (about) 1 package utils 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestGetPodNameReg(t *testing.T) { 9 test := struct { 10 description string 11 name string 12 kind string 13 expect string 14 }{ 15 description: "GetPodNameReg", 16 name: "test", 17 kind: "Deployment", 18 expect: "^test-[a-z0-9]+-[a-z0-9]{5}$", 19 } 20 21 requests := GetPodNameReg(test.name, test.kind) 22 if requests != test.expect { 23 t.Errorf("expect requests %s actual requests %s", test.expect, requests) 24 } 25 } 26 27 func TestGetCustomerExpression(t *testing.T) { 28 test := struct { 29 description string 30 name string 31 labels []string 32 expect string 33 }{ 34 description: "GetCustomerExpression", 35 name: "test", 36 labels: []string{ 37 "container=\"test\"", 38 "namespace=\"default\"", 39 }, 40 expect: "sum(test{container=\"test\",namespace=\"default\"})", 41 } 42 43 requests := GetCustomerExpression(test.name, strings.Join(test.labels, ",")) 44 if requests != test.expect { 45 t.Errorf("expect requests %s actual requests %s", test.expect, requests) 46 } 47 } 48 49 func TestGetWorkloadCpuUsageExpression(t *testing.T) { 50 test := struct { 51 description string 52 namespace string 53 name string 54 kind string 55 expect string 56 }{ 57 description: "GetWorkloadCpuUsageExpression", 58 namespace: "default", 59 name: "test", 60 kind: "Deployment", 61 expect: "sum(irate(container_cpu_usage_seconds_total{namespace=\"default\",pod=~\"^test-[a-z0-9]+-[a-z0-9]{5}$\",container!=\"\"}[3m]))", 62 } 63 64 requests := GetWorkloadCpuUsageExpression(test.namespace, test.name, test.kind) 65 if requests != test.expect { 66 t.Errorf("expect requests %s actual requests %s", test.expect, requests) 67 } 68 } 69 70 func TestGetWorkloadMemUsageExpression(t *testing.T) { 71 test := struct { 72 description string 73 namespace string 74 name string 75 kind string 76 expect string 77 }{ 78 description: "GetWorkloadMemUsageExpression", 79 namespace: "default", 80 name: "test", 81 kind: "Deployment", 82 expect: "sum(container_memory_working_set_bytes{namespace=\"default\",pod=~\"^test-[a-z0-9]+-[a-z0-9]{5}$\",container!=\"\"})", 83 } 84 85 requests := GetWorkloadMemUsageExpression(test.namespace, test.name, test.kind) 86 if requests != test.expect { 87 t.Errorf("expect requests %s actual requests %s", test.expect, requests) 88 } 89 } 90 91 func TestGetContainerCpuUsageExpression(t *testing.T) { 92 test := struct { 93 description string 94 namespace string 95 name string 96 kind string 97 nameContainer string 98 expect string 99 }{ 100 description: "GetContainerCpuUsageExpression", 101 namespace: "default", 102 name: "test", 103 kind: "Deployment", 104 nameContainer: "test", 105 expect: "irate(container_cpu_usage_seconds_total{container!=\"POD\",namespace=\"default\",pod=~\"^test-[a-z0-9]+-[a-z0-9]{5}$\",container=\"test\"}[3m])", 106 } 107 108 requests := GetContainerCpuUsageExpression(test.namespace, test.name, test.kind, test.nameContainer) 109 if requests != test.expect { 110 t.Errorf("expect requests %s actual requests %s", test.expect, requests) 111 } 112 } 113 114 func TestGetContainerMemUsageExpression(t *testing.T) { 115 test := struct { 116 description string 117 namespace string 118 name string 119 kind string 120 nameContainer string 121 expect string 122 }{ 123 description: "GetContainerMemUsageExpression", 124 namespace: "default", 125 name: "test", 126 kind: "Deployment", 127 nameContainer: "test", 128 expect: "container_memory_working_set_bytes{container!=\"POD\",namespace=\"default\",pod=~\"^test-[a-z0-9]+-[a-z0-9]{5}$\",container=\"test\"}", 129 } 130 131 requests := GetContainerMemUsageExpression(test.namespace, test.name, test.kind, test.nameContainer) 132 if requests != test.expect { 133 t.Errorf("expect requests %s actual requests %s", test.expect, requests) 134 } 135 } 136 137 func TestGetPodCpuUsageExpression(t *testing.T) { 138 test := struct { 139 description string 140 namespace string 141 name string 142 expect string 143 }{ 144 description: "GetPodCpuUsageExpression", 145 namespace: "default", 146 name: "test-pod-001", 147 expect: "sum(irate(container_cpu_usage_seconds_total{container!=\"POD\",namespace=\"default\",pod=\"test-pod-001\"}[3m]))", 148 } 149 150 requests := GetPodCpuUsageExpression(test.namespace, test.name) 151 if requests != test.expect { 152 t.Errorf("expect requests %s actual requests %s", test.expect, requests) 153 } 154 } 155 156 func TestGetPodMemUsageExpression(t *testing.T) { 157 test := struct { 158 description string 159 namespace string 160 name string 161 expect string 162 }{ 163 description: "GetPodMemUsageExpression", 164 namespace: "default", 165 name: "test-pod-001", 166 expect: "sum(container_memory_working_set_bytes{container!=\"POD\",namespace=\"default\",pod=\"test-pod-001\"})", 167 } 168 169 requests := GetPodMemUsageExpression(test.namespace, test.name) 170 if requests != test.expect { 171 t.Errorf("expect requests %s actual requests %s", test.expect, requests) 172 } 173 } 174 175 func TestGetNodeCpuUsageExpression(t *testing.T) { 176 test := struct { 177 description string 178 node string 179 expect string 180 }{ 181 description: "GetNodeCpuUsageExpression", 182 node: "test-node-001", 183 expect: "sum(count(node_cpu_seconds_total{mode=\"idle\",instance=~\"(test-node-001)(:\\\\d+)?\"}) by (mode, cpu)) - sum(irate(node_cpu_seconds_total{mode=\"idle\",instance=~\"(test-node-001)(:\\\\d+)?\"}[3m]))", 184 } 185 186 requests := GetNodeCpuUsageExpression(test.node) 187 if requests != test.expect { 188 t.Errorf("expect requests %s actual requests %s", test.expect, requests) 189 } 190 } 191 192 func TestGetNodeMemUsageExpression(t *testing.T) { 193 test := struct { 194 description string 195 node string 196 expect string 197 }{ 198 description: "GetNodeMemUsageExpression", 199 node: "test-node-001", 200 expect: "sum(node_memory_MemTotal_bytes{instance=~\"(test-node-001)(:\\\\d+)?\"} - node_memory_MemAvailable_bytes{instance=~\"(test-node-001)(:\\\\d+)?\"})", 201 } 202 203 requests := GetNodeMemUsageExpression(test.node) 204 if requests != test.expect { 205 t.Errorf("expect requests %s actual requests %s", test.expect, requests) 206 } 207 }