|
1 | 1 | package metrics |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/assert" |
@@ -145,5 +146,62 @@ func TestBuildResourceURI(t *testing.T) { |
145 | 146 | } |
146 | 147 | } |
147 | 148 | }) |
| 149 | + |
| 150 | + t.Run("provider extraction from metricNamespaceArray", func(t *testing.T) { |
| 151 | + ub := &urlBuilder{ |
| 152 | + DefaultSubscription: strPtr("default-sub"), |
| 153 | + MetricNamespace: strPtr("provider1/service1"), |
| 154 | + ResourceGroup: strPtr("rg"), |
| 155 | + ResourceName: strPtr("rn1/rn2/rn3"), |
| 156 | + } |
| 157 | + expectedProvider := "provider1" |
| 158 | + |
| 159 | + uri, err := ub.buildResourceURI() |
| 160 | + if err != nil { |
| 161 | + t.Errorf("Unexpected error: %v", err) |
| 162 | + } |
| 163 | + if uri == nil { |
| 164 | + t.Fatalf("Expected non-nil uri") |
| 165 | + } |
| 166 | + if !strings.Contains(*uri, expectedProvider) { |
| 167 | + t.Errorf("Expected provider %v in uri %v", expectedProvider, *uri) |
| 168 | + } |
| 169 | + }) |
| 170 | + |
| 171 | + t.Run("when metricNamespace is not in the correct format", func(t *testing.T) { |
| 172 | + ub := &urlBuilder{ |
| 173 | + DefaultSubscription: strPtr("default-sub"), |
| 174 | + MetricNamespace: strPtr("invalidformat"), |
| 175 | + } |
| 176 | + |
| 177 | + _, err := ub.buildResourceURI() |
| 178 | + if err == nil || err.Error() != "metricNamespace is not in the correct format" { |
| 179 | + t.Errorf("Expected error: metricNamespace is not in the correct format") |
| 180 | + } |
| 181 | + }) |
| 182 | + |
| 183 | + t.Run("when resourceNameArray index out of range", func(t *testing.T) { |
| 184 | + ub := &urlBuilder{ |
| 185 | + DefaultSubscription: strPtr("default-sub"), |
| 186 | + MetricNamespace: strPtr("provider1/service1"), |
| 187 | + ResourceName: strPtr("rn1/rn2/rn3"), |
| 188 | + } |
| 189 | + |
| 190 | + _, err := ub.buildResourceURI() |
| 191 | + if err != nil { |
| 192 | + t.Errorf("Unexpected error: %v", err) |
| 193 | + } |
| 194 | + |
| 195 | + ub = &urlBuilder{ |
| 196 | + DefaultSubscription: strPtr("default-sub"), |
| 197 | + MetricNamespace: strPtr("provider1/service1/service2"), |
| 198 | + ResourceName: strPtr(""), |
| 199 | + } |
| 200 | + |
| 201 | + _, err = ub.buildResourceURI() |
| 202 | + if err == nil || err.Error() != "resourceNameArray does not have enough elements" { |
| 203 | + t.Errorf("Expected error: resourceNameArray does not have enough elements") |
| 204 | + } |
| 205 | + }) |
148 | 206 | }) |
149 | 207 | } |
0 commit comments