MCPcopy
hub / github.com/grpc/grpc-go / setupEnv

Function setupEnv

stats/opentelemetry/csm/observability_test.go:51–89  ·  view source on GitHub ↗

setupEnv configures the environment for CSM Observability Testing. It sets the bootstrap env var to a bootstrap file with a nodeID provided. It sets CSM Env Vars as well, and mocks the resource detector's returned attribute set to simulate the environment. It registers a cleanup function on the prov

(t *testing.T, resourceDetectorEmissions map[string]string, meshID, csmCanonicalServiceName, csmWorkloadName string)

Source from the content-addressed store, hash-verified

49// simulate the environment. It registers a cleanup function on the provided t
50// to restore the environment to its original state.
51func setupEnv(t *testing.T, resourceDetectorEmissions map[string]string, meshID, csmCanonicalServiceName, csmWorkloadName string) {
52 oldCSMMeshID, csmMeshIDPresent := os.LookupEnv("CSM_MESH_ID")
53 oldCSMCanonicalServiceName, csmCanonicalServiceNamePresent := os.LookupEnv("CSM_CANONICAL_SERVICE_NAME")
54 oldCSMWorkloadName, csmWorkloadNamePresent := os.LookupEnv("CSM_WORKLOAD_NAME")
55 os.Setenv("CSM_MESH_ID", meshID)
56 os.Setenv("CSM_CANONICAL_SERVICE_NAME", csmCanonicalServiceName)
57 os.Setenv("CSM_WORKLOAD_NAME", csmWorkloadName)
58
59 var attributes []attribute.KeyValue
60 for k, v := range resourceDetectorEmissions {
61 attributes = append(attributes, attribute.String(k, v))
62 }
63 // Return the attributes configured as part of the test in place
64 // of reading from resource.
65 attrSet := attribute.NewSet(attributes...)
66 origGetAttrSet := getAttrSetFromResourceDetector
67 getAttrSetFromResourceDetector = func(context.Context) *attribute.Set {
68 return &attrSet
69 }
70 t.Cleanup(func() {
71 if csmMeshIDPresent {
72 os.Setenv("CSM_MESH_ID", oldCSMMeshID)
73 } else {
74 os.Unsetenv("CSM_MESH_ID")
75 }
76 if csmCanonicalServiceNamePresent {
77 os.Setenv("CSM_CANONICAL_SERVICE_NAME", oldCSMCanonicalServiceName)
78 } else {
79 os.Unsetenv("CSM_CANONICAL_SERVICE_NAME")
80 }
81 if csmWorkloadNamePresent {
82 os.Setenv("CSM_WORKLOAD_NAME", oldCSMWorkloadName)
83 } else {
84 os.Unsetenv("CSM_WORKLOAD_NAME")
85 }
86
87 getAttrSetFromResourceDetector = origGetAttrSet
88 })
89}
90
91// TestCSMPluginOptionUnary tests the CSM Plugin Option and labels. It
92// configures the environment for the CSM Plugin Option to read from. It then

Callers 2

Calls 1

StringMethod · 0.65

Tested by

no test coverage detected