|
| 1 | +// +build integration |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "net/http" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/docker/docker/api/types" |
| 14 | + "github.com/docker/docker/api/types/container" |
| 15 | + "github.com/docker/docker/client" |
| 16 | + "github.com/docker/go-connections/nat" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + |
| 19 | + "code.uber.internal/infra/jaeger/model/ui" |
| 20 | + |
| 21 | + "golang.org/x/net/context" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + imageName = "standalone" |
| 26 | + |
| 27 | + host = "0.0.0.0" |
| 28 | + queryPort = "16686" |
| 29 | + queryHostPort = host + ":" + queryPort |
| 30 | + queryURL = "http://" + queryHostPort |
| 31 | + |
| 32 | + getServicesURL = queryURL + "/api/services" |
| 33 | + getTraceURL = queryURL + "/api/traces?service=jaeger-query&tag=jaeger-debug-id:debug" |
| 34 | +) |
| 35 | + |
| 36 | +var ( |
| 37 | + httpClient = &http.Client{ |
| 38 | + Timeout: time.Second, |
| 39 | + } |
| 40 | +) |
| 41 | + |
| 42 | +func TestStandalone(t *testing.T) { |
| 43 | + ctx := context.Background() |
| 44 | + cli, err := client.NewEnvClient() |
| 45 | + if err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + |
| 49 | + exposedPorts, portBindings, _ := nat.ParsePortSpecs([]string{ |
| 50 | + fmt.Sprintf("%s:%s", queryHostPort, queryPort), |
| 51 | + }) |
| 52 | + |
| 53 | + resp, err := cli.ContainerCreate( |
| 54 | + ctx, |
| 55 | + &container.Config{Image: imageName, ExposedPorts: exposedPorts}, |
| 56 | + &container.HostConfig{PortBindings: portBindings}, |
| 57 | + nil, |
| 58 | + "", |
| 59 | + ) |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + |
| 64 | + if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + defer func() { |
| 68 | + if err := cli.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force: true}); err != nil { |
| 69 | + t.Fatalf("cannot remove container: %s", err) |
| 70 | + } |
| 71 | + }() |
| 72 | + |
| 73 | + // Check if the query service is available |
| 74 | + if err := healthCheck(); err != nil { |
| 75 | + t.Fatal(err) |
| 76 | + } |
| 77 | + |
| 78 | + createTrace(t) |
| 79 | + getAPITrace(t) |
| 80 | +} |
| 81 | + |
| 82 | +func createTrace(t *testing.T) { |
| 83 | + req, err := http.NewRequest("GET", getServicesURL, nil) |
| 84 | + require.NoError(t, err) |
| 85 | + req.Header.Add("jaeger-debug-id", "debug") |
| 86 | + |
| 87 | + resp, err := httpClient.Do(req) |
| 88 | + require.NoError(t, err) |
| 89 | + resp.Body.Close() |
| 90 | +} |
| 91 | + |
| 92 | +type response struct { |
| 93 | + Data []*ui.Trace `json:"data"` |
| 94 | +} |
| 95 | + |
| 96 | +func getAPITrace(t *testing.T) { |
| 97 | + req, err := http.NewRequest("GET", getTraceURL, nil) |
| 98 | + require.NoError(t, err) |
| 99 | + |
| 100 | + var queryResponse response |
| 101 | + |
| 102 | + for i := 0; i < 20; i++ { |
| 103 | + resp, err := httpClient.Do(req) |
| 104 | + require.NoError(t, err) |
| 105 | + |
| 106 | + body, _ := ioutil.ReadAll(resp.Body) |
| 107 | + |
| 108 | + err = json.Unmarshal(body, &queryResponse) |
| 109 | + require.NoError(t, err) |
| 110 | + resp.Body.Close() |
| 111 | + |
| 112 | + if len(queryResponse.Data) == 1 { |
| 113 | + break |
| 114 | + } |
| 115 | + time.Sleep(time.Second) |
| 116 | + } |
| 117 | + require.Len(t, queryResponse.Data, 1) |
| 118 | + require.Len(t, queryResponse.Data[0].Spans, 1) |
| 119 | +} |
| 120 | + |
| 121 | +func healthCheck() error { |
| 122 | + for i := 0; i < 100; i++ { |
| 123 | + _, err := http.Get(queryURL) |
| 124 | + if err == nil { |
| 125 | + return nil |
| 126 | + } |
| 127 | + time.Sleep(100 * time.Millisecond) |
| 128 | + } |
| 129 | + return fmt.Errorf("query service is not ready") |
| 130 | +} |
0 commit comments