github.com/cilium/cilium@v1.16.2/pkg/hubble/parser/seven/parser_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Hubble
     3  
     4  package seven
     5  
     6  import (
     7  	"io"
     8  	"net/http"
     9  	"net/url"
    10  	"testing"
    11  
    12  	"github.com/sirupsen/logrus"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  
    16  	flowpb "github.com/cilium/cilium/api/v1/flow"
    17  	"github.com/cilium/cilium/pkg/hubble/testutils"
    18  	"github.com/cilium/cilium/pkg/labels"
    19  	"github.com/cilium/cilium/pkg/proxy/accesslog"
    20  	"github.com/cilium/cilium/pkg/u8proto"
    21  )
    22  
    23  var log *logrus.Logger
    24  
    25  var (
    26  	fakeTimestamp = "2006-01-02T15:04:05.999999999Z"
    27  	fakeNodeInfo  = accesslog.NodeAddressInfo{
    28  		IPv4: "192.168.1.100",
    29  		IPv6: " fd01::a",
    30  	}
    31  	fakeSourceEndpoint = accesslog.EndpointInfo{
    32  		ID:       1234,
    33  		IPv4:     "10.16.32.10",
    34  		IPv6:     "f00d::a10:0:0:abcd",
    35  		Identity: 9876,
    36  		Labels:   labels.ParseLabelArray("k1=v1", "k2=v2"),
    37  	}
    38  	fakeDestinationEndpoint = accesslog.EndpointInfo{
    39  		ID:       4321,
    40  		IPv4:     "10.16.32.20",
    41  		IPv6:     "f00d::a10:0:0:1234",
    42  		Port:     80,
    43  		Identity: 6789,
    44  		Labels:   labels.ParseLabelArray("k3=v3", "k4=v4"),
    45  	}
    46  )
    47  
    48  func init() {
    49  	log = logrus.New()
    50  	log.SetOutput(io.Discard)
    51  }
    52  
    53  func BenchmarkL7Decode(b *testing.B) {
    54  	requestPath, err := url.Parse("http://myhost/some/path")
    55  	require.NoError(b, err)
    56  	lr := &accesslog.LogRecord{
    57  		Type:                accesslog.TypeResponse,
    58  		Timestamp:           fakeTimestamp,
    59  		NodeAddressInfo:     fakeNodeInfo,
    60  		ObservationPoint:    accesslog.Ingress,
    61  		SourceEndpoint:      fakeDestinationEndpoint,
    62  		DestinationEndpoint: fakeSourceEndpoint,
    63  		IPVersion:           accesslog.VersionIPv4,
    64  		Verdict:             accesslog.VerdictForwarded,
    65  		TransportProtocol:   accesslog.TransportProtocol(u8proto.TCP),
    66  		ServiceInfo:         nil,
    67  		DropReason:          nil,
    68  		HTTP: &accesslog.LogRecordHTTP{
    69  			Code:     404,
    70  			Method:   "POST",
    71  			URL:      requestPath,
    72  			Protocol: "HTTP/1.1",
    73  			Headers: http.Header{
    74  				"Host":        {"myhost"},
    75  				"Traceparent": {"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"},
    76  			},
    77  		},
    78  	}
    79  	lr.SourceEndpoint.Port = 80
    80  	lr.DestinationEndpoint.Port = 56789
    81  
    82  	dnsGetter := &testutils.NoopDNSGetter
    83  	ipGetter := &testutils.NoopIPGetter
    84  	serviceGetter := &testutils.NoopServiceGetter
    85  	endpointGetter := &testutils.NoopEndpointGetter
    86  
    87  	parser, err := New(log, dnsGetter, ipGetter, serviceGetter, endpointGetter)
    88  	require.NoError(b, err)
    89  
    90  	f := &flowpb.Flow{}
    91  	b.ReportAllocs()
    92  	b.ResetTimer()
    93  	for i := 0; i < b.N; i++ {
    94  		_ = parser.Decode(lr, f)
    95  	}
    96  }
    97  
    98  func Test_decodeVerdict(t *testing.T) {
    99  	assert.Equal(t, flowpb.Verdict_FORWARDED, decodeVerdict(accesslog.VerdictForwarded))
   100  	assert.Equal(t, flowpb.Verdict_DROPPED, decodeVerdict(accesslog.VerdictDenied))
   101  	assert.Equal(t, flowpb.Verdict_ERROR, decodeVerdict(accesslog.VerdictError))
   102  	assert.Equal(t, flowpb.Verdict_REDIRECTED, decodeVerdict(accesslog.VerdictRedirected))
   103  	assert.Equal(t, flowpb.Verdict_VERDICT_UNKNOWN, decodeVerdict("bad"))
   104  }