|
| 1 | +package outputs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + "syscall" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + |
| 15 | + "github.com/camptocamp/prometheus-puppetdb-sd/internal/config" |
| 16 | +) |
| 17 | + |
| 18 | +var expectedHttpOutputs = []string{ |
| 19 | + ` |
| 20 | +[ |
| 21 | + { |
| 22 | + "targets": [ |
| 23 | + "server-1.example.com:9100" |
| 24 | + ], |
| 25 | + "labels": { |
| 26 | + "certname": "server-1.example.com", |
| 27 | + "environment": "production", |
| 28 | + "team": "team-1" |
| 29 | + } |
| 30 | + }, |
| 31 | + { |
| 32 | + "targets": [ |
| 33 | + "server-2.example.com:9100" |
| 34 | + ], |
| 35 | + "labels": { |
| 36 | + "certname": "server-2.example.com", |
| 37 | + "environment": "development", |
| 38 | + "team": "team-1" |
| 39 | + } |
| 40 | + }, |
| 41 | + { |
| 42 | + "targets": [ |
| 43 | + "server-1.example.com:9117" |
| 44 | + ], |
| 45 | + "labels": { |
| 46 | + "certname": "server-1.example.com", |
| 47 | + "environment": "production", |
| 48 | + "team": "team-2" |
| 49 | + } |
| 50 | + } |
| 51 | +] |
| 52 | +`, |
| 53 | + ` |
| 54 | +[ |
| 55 | + { |
| 56 | + "targets": [ |
| 57 | + "server-1.example.com:9100" |
| 58 | + ], |
| 59 | + "labels": { |
| 60 | + "certname": "server-1.example.com", |
| 61 | + "environment": "production", |
| 62 | + "team": "team-1" |
| 63 | + } |
| 64 | + }, |
| 65 | + { |
| 66 | + "targets": [ |
| 67 | + "server-2.example.com:9100" |
| 68 | + ], |
| 69 | + "labels": { |
| 70 | + "certname": "server-2.example.com", |
| 71 | + "environment": "development", |
| 72 | + "team": "team-2" |
| 73 | + } |
| 74 | + } |
| 75 | +] |
| 76 | + `, |
| 77 | +} |
| 78 | + |
| 79 | +func TestHttpOutput(t *testing.T) { |
| 80 | + ctx, cancelFunc := context.WithCancel(context.Background()) |
| 81 | + defer cancelFunc() |
| 82 | + |
| 83 | + cfg := config.OutputConfig{ |
| 84 | + Http: config.HttpOutputConfig{ |
| 85 | + Port: 8000, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + o, err := setupHttpOutput(&cfg) |
| 90 | + |
| 91 | + url := fmt.Sprintf("http://localhost:%d", cfg.Http.Port) |
| 92 | + |
| 93 | + assert.Nil(t, err) |
| 94 | + |
| 95 | + resp, err := http.Get(url) |
| 96 | + if err != nil { |
| 97 | + assert.Fail(t, "Failed to request HTTP server", err) |
| 98 | + } |
| 99 | + |
| 100 | + assert.Equal(t, 404, resp.StatusCode) |
| 101 | + |
| 102 | + for i := range scrapeConfigs { |
| 103 | + err := o.Write(ctx, scrapeConfigs[i]) |
| 104 | + if err != nil { |
| 105 | + assert.FailNow(t, "Failed to write output", err.Error()) |
| 106 | + } |
| 107 | + |
| 108 | + resp, err := http.Get(url) |
| 109 | + if err != nil { |
| 110 | + assert.Fail(t, "Failed to request HTTP server", err) |
| 111 | + } |
| 112 | + |
| 113 | + assert.Equal(t, 200, resp.StatusCode) |
| 114 | + assert.Equal(t, "application/json", resp.Header.Get("Content-Type")) |
| 115 | + |
| 116 | + output, err := io.ReadAll(resp.Body) |
| 117 | + if err != nil { |
| 118 | + assert.Fail(t, "Failed to read HTTP response", err) |
| 119 | + } |
| 120 | + |
| 121 | + expectedOutput := expectedHttpOutputs[i] |
| 122 | + |
| 123 | + assert.Equal(t, strings.TrimSpace(expectedOutput), strings.TrimSpace(string(output))) |
| 124 | + } |
| 125 | + |
| 126 | + err = o.Close(ctx) |
| 127 | + if err != nil { |
| 128 | + assert.FailNow(t, "Failed to close output", err.Error()) |
| 129 | + } |
| 130 | + |
| 131 | + _, err = http.Get(url) |
| 132 | + assert.True(t, errors.Is(err, syscall.ECONNREFUSED)) |
| 133 | +} |
0 commit comments