|
| 1 | +package prometheusmetrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/coder/coder/v2/agent/proto" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAnnotatedMetric_Is(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + am1 := &annotatedMetric{ |
| 15 | + Stats_Metric: &proto.Stats_Metric{ |
| 16 | + Name: "met", |
| 17 | + Type: proto.Stats_Metric_COUNTER, |
| 18 | + Value: 1, |
| 19 | + Labels: []*proto.Stats_Metric_Label{ |
| 20 | + {Name: "rarity", Value: "blue moon"}, |
| 21 | + {Name: "certainty", Value: "yes"}, |
| 22 | + }, |
| 23 | + }, |
| 24 | + username: "spike", |
| 25 | + workspaceName: "work", |
| 26 | + agentName: "janus", |
| 27 | + templateName: "tempe", |
| 28 | + expiryDate: time.Now(), |
| 29 | + } |
| 30 | + for _, tc := range []struct { |
| 31 | + name string |
| 32 | + req updateRequest |
| 33 | + m *proto.Stats_Metric |
| 34 | + is bool |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "OK", |
| 38 | + req: updateRequest{ |
| 39 | + username: "spike", |
| 40 | + workspaceName: "work", |
| 41 | + agentName: "janus", |
| 42 | + templateName: "tempe", |
| 43 | + metrics: nil, |
| 44 | + timestamp: time.Now().Add(-5 * time.Second), |
| 45 | + }, |
| 46 | + m: &proto.Stats_Metric{ |
| 47 | + Name: "met", |
| 48 | + Type: proto.Stats_Metric_COUNTER, |
| 49 | + Value: 2, |
| 50 | + Labels: []*proto.Stats_Metric_Label{ |
| 51 | + {Name: "rarity", Value: "blue moon"}, |
| 52 | + {Name: "certainty", Value: "yes"}, |
| 53 | + }, |
| 54 | + }, |
| 55 | + is: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "missingLabel", |
| 59 | + req: updateRequest{ |
| 60 | + username: "spike", |
| 61 | + workspaceName: "work", |
| 62 | + agentName: "janus", |
| 63 | + templateName: "tempe", |
| 64 | + metrics: nil, |
| 65 | + timestamp: time.Now().Add(-5 * time.Second), |
| 66 | + }, |
| 67 | + m: &proto.Stats_Metric{ |
| 68 | + Name: "met", |
| 69 | + Type: proto.Stats_Metric_COUNTER, |
| 70 | + Value: 2, |
| 71 | + Labels: []*proto.Stats_Metric_Label{ |
| 72 | + {Name: "certainty", Value: "yes"}, |
| 73 | + }, |
| 74 | + }, |
| 75 | + is: false, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "wrongLabelValue", |
| 79 | + req: updateRequest{ |
| 80 | + username: "spike", |
| 81 | + workspaceName: "work", |
| 82 | + agentName: "janus", |
| 83 | + templateName: "tempe", |
| 84 | + metrics: nil, |
| 85 | + timestamp: time.Now().Add(-5 * time.Second), |
| 86 | + }, |
| 87 | + m: &proto.Stats_Metric{ |
| 88 | + Name: "met", |
| 89 | + Type: proto.Stats_Metric_COUNTER, |
| 90 | + Value: 2, |
| 91 | + Labels: []*proto.Stats_Metric_Label{ |
| 92 | + {Name: "rarity", Value: "blue moon"}, |
| 93 | + {Name: "certainty", Value: "inshallah"}, |
| 94 | + }, |
| 95 | + }, |
| 96 | + is: false, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "wrongMetricName", |
| 100 | + req: updateRequest{ |
| 101 | + username: "spike", |
| 102 | + workspaceName: "work", |
| 103 | + agentName: "janus", |
| 104 | + templateName: "tempe", |
| 105 | + metrics: nil, |
| 106 | + timestamp: time.Now().Add(-5 * time.Second), |
| 107 | + }, |
| 108 | + m: &proto.Stats_Metric{ |
| 109 | + Name: "cub", |
| 110 | + Type: proto.Stats_Metric_COUNTER, |
| 111 | + Value: 2, |
| 112 | + Labels: []*proto.Stats_Metric_Label{ |
| 113 | + {Name: "rarity", Value: "blue moon"}, |
| 114 | + {Name: "certainty", Value: "yes"}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + is: false, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "wrongUsername", |
| 121 | + req: updateRequest{ |
| 122 | + username: "steve", |
| 123 | + workspaceName: "work", |
| 124 | + agentName: "janus", |
| 125 | + templateName: "tempe", |
| 126 | + metrics: nil, |
| 127 | + timestamp: time.Now().Add(-5 * time.Second), |
| 128 | + }, |
| 129 | + m: &proto.Stats_Metric{ |
| 130 | + Name: "met", |
| 131 | + Type: proto.Stats_Metric_COUNTER, |
| 132 | + Value: 2, |
| 133 | + Labels: []*proto.Stats_Metric_Label{ |
| 134 | + {Name: "rarity", Value: "blue moon"}, |
| 135 | + {Name: "certainty", Value: "yes"}, |
| 136 | + }, |
| 137 | + }, |
| 138 | + is: false, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "wrongWorkspaceName", |
| 142 | + req: updateRequest{ |
| 143 | + username: "spike", |
| 144 | + workspaceName: "play", |
| 145 | + agentName: "janus", |
| 146 | + templateName: "tempe", |
| 147 | + metrics: nil, |
| 148 | + timestamp: time.Now().Add(-5 * time.Second), |
| 149 | + }, |
| 150 | + m: &proto.Stats_Metric{ |
| 151 | + Name: "met", |
| 152 | + Type: proto.Stats_Metric_COUNTER, |
| 153 | + Value: 2, |
| 154 | + Labels: []*proto.Stats_Metric_Label{ |
| 155 | + {Name: "rarity", Value: "blue moon"}, |
| 156 | + {Name: "certainty", Value: "yes"}, |
| 157 | + }, |
| 158 | + }, |
| 159 | + is: false, |
| 160 | + }, |
| 161 | + { |
| 162 | + name: "wrongAgentName", |
| 163 | + req: updateRequest{ |
| 164 | + username: "spike", |
| 165 | + workspaceName: "work", |
| 166 | + agentName: "bond", |
| 167 | + templateName: "tempe", |
| 168 | + metrics: nil, |
| 169 | + timestamp: time.Now().Add(-5 * time.Second), |
| 170 | + }, |
| 171 | + m: &proto.Stats_Metric{ |
| 172 | + Name: "met", |
| 173 | + Type: proto.Stats_Metric_COUNTER, |
| 174 | + Value: 2, |
| 175 | + Labels: []*proto.Stats_Metric_Label{ |
| 176 | + {Name: "rarity", Value: "blue moon"}, |
| 177 | + {Name: "certainty", Value: "yes"}, |
| 178 | + }, |
| 179 | + }, |
| 180 | + is: false, |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "wrongTemplateName", |
| 184 | + req: updateRequest{ |
| 185 | + username: "spike", |
| 186 | + workspaceName: "work", |
| 187 | + agentName: "janus", |
| 188 | + templateName: "phoenix", |
| 189 | + metrics: nil, |
| 190 | + timestamp: time.Now().Add(-5 * time.Second), |
| 191 | + }, |
| 192 | + m: &proto.Stats_Metric{ |
| 193 | + Name: "met", |
| 194 | + Type: proto.Stats_Metric_COUNTER, |
| 195 | + Value: 2, |
| 196 | + Labels: []*proto.Stats_Metric_Label{ |
| 197 | + {Name: "rarity", Value: "blue moon"}, |
| 198 | + {Name: "certainty", Value: "yes"}, |
| 199 | + }, |
| 200 | + }, |
| 201 | + is: false, |
| 202 | + }, |
| 203 | + } { |
| 204 | + tc := tc |
| 205 | + t.Run(tc.name, func(t *testing.T) { |
| 206 | + t.Parallel() |
| 207 | + require.Equal(t, tc.is, am1.is(tc.req, tc.m)) |
| 208 | + }) |
| 209 | + } |
| 210 | +} |
0 commit comments