|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestIntegration(t *testing.T) { |
| 13 | + const host = "http://localhost:9011" |
| 14 | + go main() |
| 15 | + time.Sleep(time.Second * 1) // Giving some time to start the server |
| 16 | + |
| 17 | + c := http.Client{} |
| 18 | + |
| 19 | + req, _ := http.NewRequest("POST", host+"/transaction", nil) |
| 20 | + |
| 21 | + _, err := c.Do(req) |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("request to /transaction failed %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + req, _ = http.NewRequest("POST", host+"/return", nil) |
| 27 | + |
| 28 | + _, err = c.Do(req) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("request to /transaction failed %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + req, _ = http.NewRequest("GET", "http://localhost:2121/metrics", nil) |
| 34 | + |
| 35 | + resp, err := c.Do(req) |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("request to localhost:2121/metrics failed %v", err) |
| 38 | + } |
| 39 | + |
| 40 | + body, _ := io.ReadAll(resp.Body) |
| 41 | + |
| 42 | + strBody := string(body) |
| 43 | + |
| 44 | + assert.Equal(t, http.StatusOK, resp.StatusCode, "TEST[%d], Failed.\n%s") |
| 45 | + |
| 46 | + assert.Contains(t, strBody, `product_stock{otel_scope_name="using-metrics",otel_scope_version="v0.1.0"} 50`) |
| 47 | + assert.Contains(t, strBody, `total_credit_day_sale{otel_scope_name="using-metrics",otel_scope_version="v0.1.0",sale_type="credit"} 1000`) |
| 48 | + assert.Contains(t, strBody, `total_credit_day_sale{otel_scope_name="using-metrics",otel_scope_version="v0.1.0",sale_type="credit_return"} -1000`) |
| 49 | + assert.Contains(t, strBody, `transaction_success_total{otel_scope_name="using-metrics",otel_scope_version="v0.1.0"} 1`) |
| 50 | + assert.Contains(t, strBody, "transaction_time") |
| 51 | +} |
0 commit comments