...

Source file src/go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/internal/semconvutil/netconv_test.go

Documentation: go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp/internal/semconvutil

     1  // Code created by gotmpl. DO NOT MODIFY.
     2  // source: internal/shared/semconvutil/netconv_test.go.tmpl
     3  
     4  // Copyright The OpenTelemetry Authors
     5  // SPDX-License-Identifier: Apache-2.0
     6  
     7  package semconvutil
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  
    14  	"go.opentelemetry.io/otel/attribute"
    15  )
    16  
    17  const (
    18  	addr = "127.0.0.1"
    19  	port = 1834
    20  )
    21  
    22  func TestNetTransport(t *testing.T) {
    23  	transports := map[string]attribute.KeyValue{
    24  		"tcp":        attribute.String("net.transport", "ip_tcp"),
    25  		"tcp4":       attribute.String("net.transport", "ip_tcp"),
    26  		"tcp6":       attribute.String("net.transport", "ip_tcp"),
    27  		"udp":        attribute.String("net.transport", "ip_udp"),
    28  		"udp4":       attribute.String("net.transport", "ip_udp"),
    29  		"udp6":       attribute.String("net.transport", "ip_udp"),
    30  		"unix":       attribute.String("net.transport", "inproc"),
    31  		"unixgram":   attribute.String("net.transport", "inproc"),
    32  		"unixpacket": attribute.String("net.transport", "inproc"),
    33  		"ip:1":       attribute.String("net.transport", "other"),
    34  		"ip:icmp":    attribute.String("net.transport", "other"),
    35  		"ip4:proto":  attribute.String("net.transport", "other"),
    36  		"ip6:proto":  attribute.String("net.transport", "other"),
    37  	}
    38  
    39  	for network, want := range transports {
    40  		assert.Equal(t, want, NetTransport(network))
    41  	}
    42  }
    43  
    44  func TestNetHost(t *testing.T) {
    45  	testAddrs(t, []addrTest{
    46  		{address: "", expected: nil},
    47  		{address: "192.0.0.1", expected: []attribute.KeyValue{
    48  			nc.HostName("192.0.0.1"),
    49  		}},
    50  		{address: "192.0.0.1:9090", expected: []attribute.KeyValue{
    51  			nc.HostName("192.0.0.1"),
    52  			nc.HostPort(9090),
    53  		}},
    54  	}, nc.Host)
    55  }
    56  
    57  func TestNetHostName(t *testing.T) {
    58  	expected := attribute.Key("net.host.name").String(addr)
    59  	assert.Equal(t, expected, nc.HostName(addr))
    60  }
    61  
    62  func TestNetHostPort(t *testing.T) {
    63  	expected := attribute.Key("net.host.port").Int(port)
    64  	assert.Equal(t, expected, nc.HostPort(port))
    65  }
    66  
    67  func TestNetPeer(t *testing.T) {
    68  	testAddrs(t, []addrTest{
    69  		{address: "", expected: nil},
    70  		{address: "example.com", expected: []attribute.KeyValue{
    71  			nc.PeerName("example.com"),
    72  		}},
    73  		{address: "/tmp/file", expected: []attribute.KeyValue{
    74  			nc.PeerName("/tmp/file"),
    75  		}},
    76  		{address: "192.0.0.1", expected: []attribute.KeyValue{
    77  			nc.PeerName("192.0.0.1"),
    78  		}},
    79  		{address: ":9090", expected: nil},
    80  		{address: "192.0.0.1:9090", expected: []attribute.KeyValue{
    81  			nc.PeerName("192.0.0.1"),
    82  			nc.PeerPort(9090),
    83  		}},
    84  	}, nc.Peer)
    85  }
    86  
    87  func TestNetPeerName(t *testing.T) {
    88  	expected := attribute.Key("net.peer.name").String(addr)
    89  	assert.Equal(t, expected, nc.PeerName(addr))
    90  }
    91  
    92  func TestNetPeerPort(t *testing.T) {
    93  	expected := attribute.Key("net.peer.port").Int(port)
    94  	assert.Equal(t, expected, nc.PeerPort(port))
    95  }
    96  
    97  func TestNetSockPeerName(t *testing.T) {
    98  	expected := attribute.Key("net.sock.peer.addr").String(addr)
    99  	assert.Equal(t, expected, nc.SockPeerAddr(addr))
   100  }
   101  
   102  func TestNetSockPeerPort(t *testing.T) {
   103  	expected := attribute.Key("net.sock.peer.port").Int(port)
   104  	assert.Equal(t, expected, nc.SockPeerPort(port))
   105  }
   106  
   107  func TestNetFamily(t *testing.T) {
   108  	tests := []struct {
   109  		network string
   110  		address string
   111  		expect  string
   112  	}{
   113  		{"", "", ""},
   114  		{"unix", "", "unix"},
   115  		{"unix", "gibberish", "unix"},
   116  		{"unixgram", "", "unix"},
   117  		{"unixgram", "gibberish", "unix"},
   118  		{"unixpacket", "gibberish", "unix"},
   119  		{"tcp", "123.0.2.8", "inet"},
   120  		{"tcp", "gibberish", ""},
   121  		{"", "123.0.2.8", "inet"},
   122  		{"", "gibberish", ""},
   123  		{"tcp", "fe80::1", "inet6"},
   124  		{"", "fe80::1", "inet6"},
   125  	}
   126  
   127  	for _, test := range tests {
   128  		got := family(test.network, test.address)
   129  		assert.Equal(t, test.expect, got, test.network+"/"+test.address)
   130  	}
   131  }
   132  
   133  func TestSplitHostPort(t *testing.T) {
   134  	tests := []struct {
   135  		hostport string
   136  		host     string
   137  		port     int
   138  	}{
   139  		{"", "", -1},
   140  		{":8080", "", 8080},
   141  		{"127.0.0.1", "127.0.0.1", -1},
   142  		{"www.example.com", "www.example.com", -1},
   143  		{"127.0.0.1%25en0", "127.0.0.1%25en0", -1},
   144  		{"[]", "", -1}, // Ensure this doesn't panic.
   145  		{"[fe80::1", "", -1},
   146  		{"[fe80::1]", "fe80::1", -1},
   147  		{"[fe80::1%25en0]", "fe80::1%25en0", -1},
   148  		{"[fe80::1]:8080", "fe80::1", 8080},
   149  		{"[fe80::1]::", "", -1}, // Too many colons.
   150  		{"127.0.0.1:", "127.0.0.1", -1},
   151  		{"127.0.0.1:port", "127.0.0.1", -1},
   152  		{"127.0.0.1:8080", "127.0.0.1", 8080},
   153  		{"www.example.com:8080", "www.example.com", 8080},
   154  		{"127.0.0.1%25en0:8080", "127.0.0.1%25en0", 8080},
   155  	}
   156  
   157  	for _, test := range tests {
   158  		h, p := splitHostPort(test.hostport)
   159  		assert.Equal(t, test.host, h, test.hostport)
   160  		assert.Equal(t, test.port, p, test.hostport)
   161  	}
   162  }
   163  
   164  type addrTest struct {
   165  	address  string
   166  	expected []attribute.KeyValue
   167  }
   168  
   169  func testAddrs(t *testing.T, tests []addrTest, f func(string) []attribute.KeyValue) {
   170  	t.Helper()
   171  
   172  	for _, test := range tests {
   173  		got := f(test.address)
   174  		assert.Equal(t, cap(test.expected), cap(got), "slice capacity")
   175  		assert.ElementsMatch(t, test.expected, got, test.address)
   176  	}
   177  }
   178  
   179  func TestNetProtocol(t *testing.T) {
   180  	type testCase struct {
   181  		name, version string
   182  	}
   183  	tests := map[string]testCase{
   184  		"HTTP/1.0":        {name: "http", version: "1.0"},
   185  		"HTTP/1.1":        {name: "http", version: "1.1"},
   186  		"HTTP/2":          {name: "http", version: "2"},
   187  		"HTTP/3":          {name: "http", version: "3"},
   188  		"SPDY":            {name: "spdy"},
   189  		"SPDY/2":          {name: "spdy", version: "2"},
   190  		"QUIC":            {name: "quic"},
   191  		"unknown/proto/2": {name: "unknown", version: "proto/2"},
   192  		"other":           {name: "other"},
   193  	}
   194  
   195  	for proto, want := range tests {
   196  		name, version := netProtocol(proto)
   197  		assert.Equal(t, want.name, name)
   198  		assert.Equal(t, want.version, version)
   199  	}
   200  }
   201  

View as plain text