go.undefinedlabs.com/scopeagent@v0.4.2/agent/agent_test.go (about) 1 package agent 2 3 import ( 4 "fmt" 5 "reflect" 6 "sync" 7 "testing" 8 "time" 9 10 "go.undefinedlabs.com/scopeagent/env" 11 "go.undefinedlabs.com/scopeagent/tags" 12 ) 13 14 func TestDsnParser(t *testing.T) { 15 dsnValues := [][]string{ 16 {"https://4432432432432423@shared.scope.dev", "4432432432432423", "https://shared.scope.dev"}, 17 {"http://4432432432432423@shared.scope.dev", "4432432432432423", "http://shared.scope.dev"}, 18 {"https://4432432432432423:ignored@shared.scope.dev", "4432432432432423", "https://shared.scope.dev"}, 19 {"https://4432432432432423:ignored@shared.scope.dev/custom/path", "4432432432432423", "https://shared.scope.dev/custom/path"}, 20 {"https://4432432432432423:ignored@scope.dev", "4432432432432423", "https://scope.dev"}, 21 22 {"4432432432432423@shared.scope.dev", "", "4432432432432423@shared.scope.dev"}, 23 {"noise", "", "noise"}, 24 } 25 26 for i := 0; i < len(dsnValues); i++ { 27 dsnValue := dsnValues[i] 28 t.Run(dsnValue[0], func(st *testing.T) { 29 apiKey, apiEndpoint, err := parseDSN(dsnValue[0]) 30 if apiKey != dsnValue[1] || apiEndpoint != dsnValue[2] { 31 if err != nil { 32 st.Error(err) 33 } else { 34 fmt.Println(dsnValue, apiKey, apiEndpoint) 35 } 36 st.FailNow() 37 } 38 }) 39 } 40 } 41 42 func TestWithConfigurationKeys(t *testing.T) { 43 myKeys := []string{"ConfigKey01", "ConfigKey02", "ConfigKey03"} 44 45 agent, err := NewAgent(WithApiKey("123"), WithConfigurationKeys(myKeys)) 46 if err != nil { 47 t.Fatal(err) 48 } 49 agent.Stop() 50 51 if agentKeys, ok := agent.metadata[tags.ConfigurationKeys]; ok { 52 if !reflect.DeepEqual(myKeys, agentKeys) { 53 t.Fatal("the configuration keys array are different") 54 } 55 } else { 56 t.Fatal("agent configuration keys can't be found") 57 } 58 } 59 60 func TestWithConfiguration(t *testing.T) { 61 myKeys := []string{"ConfigKey01", "ConfigKey02", "ConfigKey03"} 62 myConfiguration := map[string]interface{}{ 63 myKeys[0]: 101, 64 myKeys[1]: "Value 2", 65 myKeys[2]: true, 66 } 67 68 agent, err := NewAgent(WithApiKey("123"), WithConfiguration(myConfiguration)) 69 if err != nil { 70 t.Fatal(err) 71 } 72 agent.Stop() 73 74 if agentKeys, ok := agent.metadata[tags.ConfigurationKeys]; ok { 75 if !sameElements(myKeys, agentKeys.([]string)) { 76 t.Fatal("the configuration keys array are different", agentKeys, myKeys) 77 } 78 } else { 79 t.Fatal("agent configuration keys can't be found") 80 } 81 82 for k, v := range myConfiguration { 83 if mV, ok := agent.metadata[k]; ok { 84 if !reflect.DeepEqual(v, mV) { 85 t.Fatal("the configuration values are different") 86 } 87 } else { 88 t.Fatal("the configuration maps are different") 89 } 90 } 91 } 92 93 func sameElements(a, b []string) bool { 94 if len(a) != len(b) { 95 return false 96 } 97 for _, v := range a { 98 found := false 99 for _, v2 := range b { 100 found = found || v == v2 101 } 102 if !found { 103 return false 104 } 105 } 106 return true 107 } 108 109 func TestTildeExpandRaceMetadata(t *testing.T) { 110 env.ScopeSourceRoot.Value = "~/scope" 111 agent, err := NewAgent(WithApiKey("123"), WithTestingModeEnabled()) 112 if err != nil { 113 t.Fatal(err) 114 } 115 <-time.After(5 * time.Second) 116 agent.Stop() 117 } 118 119 var a *Agent 120 121 func BenchmarkNewAgent(b *testing.B) { 122 for i := 0; i < b.N; i++ { 123 var err error 124 a, err = NewAgent(WithTestingModeEnabled(), 125 WithHandlePanicAsFail(), 126 WithRetriesOnFail(3), 127 WithSetGlobalTracer()) 128 if err != nil { 129 b.Fatal(err) 130 } 131 span := a.Tracer().StartSpan("Test") 132 span.SetTag("span.kind", "test") 133 span.SetTag("test.name", "BenchNewAgent") 134 span.SetTag("test.suite", "root") 135 span.SetTag("test.status", tags.TestStatus_PASS) 136 span.SetBaggageItem("trace.kind", "test") 137 span.Finish() 138 once = sync.Once{} 139 a.Stop() 140 } 141 }