1package tracing
2
3// Copyright 2018 Microsoft Corporation
4//
5//  Licensed under the Apache License, Version 2.0 (the "License");
6//  you may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at
8//
9//      http://www.apache.org/licenses/LICENSE-2.0
10//
11//  Unless required by applicable law or agreed to in writing, software
12//  distributed under the License is distributed on an "AS IS" BASIS,
13//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14//  See the License for the specific language governing permissions and
15//  limitations under the License.
16
17import (
18	"context"
19	"fmt"
20	"net/http"
21	"os"
22	"reflect"
23	"testing"
24
25	"contrib.go.opencensus.io/exporter/ocagent"
26	"go.opencensus.io/stats/view"
27	"go.opencensus.io/trace"
28)
29
30func TestNoTracingByDefault(t *testing.T) {
31	if expected, got := false, IsEnabled(); expected != got {
32		t.Fatalf("By default expected %t, got %t", expected, got)
33	}
34
35	if sampler == nil {
36		t.Fatal("By default expected non nil sampler")
37	}
38
39	if Transport.GetStartOptions(&http.Request{}).Sampler == nil {
40		t.Fatalf("By default expected configured Sampler to be non-nil")
41	}
42
43	for n := range views {
44		v := view.Find(n)
45		if v != nil {
46			t.Fatalf("By default expected no registered views, found %s", v.Name)
47		}
48	}
49}
50
51func TestEnableTracing(t *testing.T) {
52	err := Enable()
53
54	if err != nil {
55		t.Fatalf("Enable failed, got error %v", err)
56	}
57	if !IsEnabled() {
58		t.Fatalf("Enable failed, IsEnabled() is %t", IsEnabled())
59	}
60	if sampler != nil {
61		t.Fatalf("Enable failed, expected nil sampler, got %v", sampler)
62	}
63
64	if Transport.GetStartOptions(&http.Request{}).Sampler != nil {
65		t.Fatalf("Enable failed, expected Transport.GetStartOptions.Sampler to be nil")
66	}
67
68	for n, v := range views {
69		fv := view.Find(n)
70		if fv == nil || !reflect.DeepEqual(v, fv) {
71			t.Fatalf("Enable failed, view %s was not registered", n)
72		}
73	}
74}
75
76func TestTracingByEnv(t *testing.T) {
77	os.Setenv("AZURE_SDK_TRACING_ENABLED", "")
78	enableFromEnv()
79	if !IsEnabled() {
80		t.Fatalf("Enable failed, IsEnabled() is %t", IsEnabled())
81	}
82	if sampler != nil {
83		t.Fatalf("Enable failed, expected nil sampler, got %v", sampler)
84	}
85
86	if Transport.GetStartOptions(&http.Request{}).Sampler != nil {
87		t.Fatalf("Enable failed, expected Transport.GetStartOptions.Sampler to be nil")
88	}
89
90	for n, v := range views {
91		fv := view.Find(n)
92		if fv == nil || !reflect.DeepEqual(v, fv) {
93			t.Fatalf("Enable failed, view %s was not registered", n)
94		}
95	}
96}
97
98func TestEnableTracingWithAIError(t *testing.T) {
99	agentEndpoint := fmt.Sprintf("%s:%d", ocagent.DefaultAgentHost, ocagent.DefaultAgentPort)
100	err := EnableWithAIForwarding(agentEndpoint)
101	if !IsEnabled() {
102		t.Fatalf("Enable failed, IsEnabled() is %t", IsEnabled())
103	}
104	if sampler != nil {
105		t.Fatalf("Enable failed, expected nil sampler, got %v", sampler)
106	}
107
108	if Transport.GetStartOptions(&http.Request{}).Sampler != nil {
109		t.Fatalf("Enable failed, expected Transport.GetStartOptions.Sampler to be nil")
110	}
111
112	for n, v := range views {
113		fv := view.Find(n)
114		if fv == nil || !reflect.DeepEqual(v, fv) {
115			t.Fatalf("Enable failed, view %s was not registered", n)
116		}
117	}
118
119	if err == nil {
120		t.Fatal("Expected error on no agent running, got nil")
121	}
122}
123
124func TestDisableTracing(t *testing.T) {
125	Enable()
126	Disable()
127	if expected, got := false, IsEnabled(); expected != got {
128		t.Fatalf("By default expected %t, got %t", expected, got)
129	}
130
131	if sampler == nil {
132		t.Fatal("By default expected non nil sampler")
133	}
134
135	if Transport.GetStartOptions(&http.Request{}).Sampler == nil {
136		t.Fatalf("By default expected configured Sampler to be non-nil")
137	}
138
139	for n := range views {
140		v := view.Find(n)
141		if v != nil {
142			t.Fatalf("By default expected no registered views, found %s", v.Name)
143		}
144	}
145}
146
147func TestStartSpan(t *testing.T) {
148	ctx := StartSpan(context.Background(), "testSpan")
149	defer EndSpan(ctx, 200, nil)
150
151	span := trace.FromContext(ctx)
152	if span == nil {
153		t.Fatal("StartSpan failed, expected non-nil span")
154	}
155}
156