metadata_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package consul
  4. import (
  5. "context"
  6. "fmt"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "github.com/hashicorp/consul/api"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. func TestConsulHappyPath(t *testing.T) {
  15. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  16. if r.URL.String() == "/v1/agent/self" {
  17. fmt.Fprintln(w, `{
  18. "Config": {
  19. "Datacenter":"dc1",
  20. "NodeName":"hostname",
  21. "NodeID": "00000000-0000-0000-0000-000000000000"
  22. },
  23. "Meta": {
  24. "test": "test",
  25. "environment": "prod"
  26. }
  27. }`)
  28. }
  29. }))
  30. defer ts.Close()
  31. config := api.DefaultConfig()
  32. config.Address = ts.URL
  33. allowedLabels := map[string]any{
  34. "test": nil,
  35. }
  36. client, err := api.NewClient(config)
  37. require.NoError(t, err)
  38. provider := NewProvider(client, allowedLabels)
  39. meta, err := provider.Metadata(context.TODO())
  40. assert.NoError(t, err)
  41. assert.Equal(t, "hostname", meta.Hostname)
  42. assert.Equal(t, "dc1", meta.Datacenter)
  43. assert.Equal(t, "00000000-0000-0000-0000-000000000000", meta.NodeID)
  44. assert.Equal(t, map[string]string{"test": "test"}, meta.HostMetadata)
  45. }