1// Copyright (c) 2017 Uber Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package server
16
17import (
18	"context"
19	"fmt"
20	"testing"
21
22	"github.com/opentracing/opentracing-go"
23	"github.com/stretchr/testify/assert"
24	"github.com/stretchr/testify/require"
25
26	"github.com/uber/jaeger-client-go"
27	"github.com/uber/jaeger-client-go/crossdock/common"
28	"github.com/uber/jaeger-client-go/crossdock/log"
29	"github.com/uber/jaeger-client-go/crossdock/thrift/tracetest"
30)
31
32func TestServerJSON(t *testing.T) {
33	tracer, tCloser := jaeger.NewTracer(
34		"crossdock",
35		jaeger.NewConstSampler(false),
36		jaeger.NewNullReporter())
37	defer tCloser.Close()
38
39	s := &Server{HostPortHTTP: "127.0.0.1:0", Tracer: tracer}
40	err := s.Start()
41	require.NoError(t, err)
42	defer s.Close()
43
44	req := tracetest.NewStartTraceRequest()
45	req.Sampled = true
46	req.Baggage = "Zoidberg"
47	req.Downstream = &tracetest.Downstream{
48		ServiceName: "go",
49		Host:        "localhost",
50		Port:        s.GetPortHTTP(),
51		Transport:   tracetest.Transport_HTTP,
52		Downstream: &tracetest.Downstream{
53			ServiceName: "go",
54			Host:        "localhost",
55			Port:        s.GetPortHTTP(),
56			Transport:   tracetest.Transport_HTTP,
57		},
58	}
59
60	url := fmt.Sprintf("http://%s/start_trace", s.HostPortHTTP)
61	result, err := common.PostJSON(context.Background(), url, req)
62
63	require.NoError(t, err)
64	log.Printf("response=%+v", &result)
65}
66
67func TestObserveSpan(t *testing.T) {
68	tracer, tCloser := jaeger.NewTracer(
69		"crossdock",
70		jaeger.NewConstSampler(true),
71		jaeger.NewNullReporter())
72	defer tCloser.Close()
73
74	_, err := observeSpan(context.Background(), tracer)
75	assert.Error(t, err)
76
77	span := tracer.StartSpan("hi")
78	span.SetBaggageItem(BaggageKey, "xyz")
79	ctx := opentracing.ContextWithSpan(context.Background(), span)
80
81	s, err := observeSpan(ctx, tracer)
82	assert.NoError(t, err)
83	assert.True(t, s.Sampled)
84	traceID := span.Context().(jaeger.SpanContext).TraceID().String()
85	assert.Equal(t, traceID, s.TraceId)
86	assert.Equal(t, "xyz", s.Baggage)
87}
88