github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/collector/default_test.go (about)

     1  package collector
     2  
     3  import (
     4  	"testing"
     5  
     6  	"go.aporeto.io/enforcerd/trireme-lib/policy"
     7  )
     8  
     9  func TestStatsUserHash(t *testing.T) {
    10  	type args struct {
    11  		userRecord *UserRecord
    12  		hash       string
    13  	}
    14  	tests := []struct {
    15  		name    string
    16  		args    args
    17  		wantErr bool
    18  	}{
    19  		{
    20  			name: "Test_StatsUserHash1",
    21  			args: args{
    22  				userRecord: &UserRecord{
    23  					Namespace: "/_apotests",
    24  					Claims: []string{
    25  						"CN=b01a6042-437-allow",
    26  						"O=b01a6042-437-allow",
    27  					},
    28  				},
    29  				hash: "14815208496714115169",
    30  			},
    31  			wantErr: false,
    32  		},
    33  		{
    34  			name: "Test_StatsUserHash2",
    35  			args: args{
    36  				userRecord: &UserRecord{
    37  					Namespace: "/_apotests",
    38  					Claims: []string{
    39  						"CN=apotests-master-staging2 Root CA",
    40  						"O=_apotests/b01a6042-437c-44ab-a17f-e14f6f915b87",
    41  						"OU=aporeto-enforcerd",
    42  					},
    43  				},
    44  				hash: "3750309273572959404",
    45  			},
    46  			wantErr: false,
    47  		},
    48  	}
    49  	for _, tt := range tests {
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			if err := StatsUserHash(tt.args.userRecord); (err != nil) != tt.wantErr {
    52  				t.Errorf("StatsUserHash() error = %v, wantErr %v", err, tt.wantErr)
    53  			}
    54  			if tt.args.hash != tt.args.userRecord.ID {
    55  				t.Errorf("Wanted %s but got %s", tt.args.hash, tt.args.userRecord.ID)
    56  			}
    57  		})
    58  	}
    59  }
    60  
    61  func TestStatsFlowHash(t *testing.T) {
    62  	type args struct {
    63  		r *FlowRecord
    64  	}
    65  	tests := []struct {
    66  		name            string
    67  		args            args
    68  		wantFlowhash    uint64
    69  		wantContenthash uint64
    70  	}{
    71  		{
    72  			name: "basic hash",
    73  			args: args{
    74  				r: &FlowRecord{
    75  					ContextID:             "context",
    76  					Namespace:             "ns",
    77  					Source:                EndPoint{},
    78  					Destination:           EndPoint{},
    79  					Tags:                  []string{"tag=val"},
    80  					DropReason:            "none",
    81  					PolicyID:              "default",
    82  					ObservedPolicyID:      "default",
    83  					ServiceType:           policy.ServiceL3,
    84  					ServiceID:             "svc",
    85  					Count:                 1,
    86  					Action:                policy.Accept,
    87  					ObservedAction:        policy.Accept,
    88  					ObservedActionType:    policy.ObserveContinue,
    89  					L4Protocol:            7,
    90  					SourceController:      "src-controller",
    91  					DestinationController: "dst-controller",
    92  					RuleName:              "1",
    93  				},
    94  			},
    95  			wantFlowhash:    11145182160106660097,
    96  			wantContenthash: 5951126184511352450,
    97  		},
    98  	}
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			gotFlowhash, gotContenthash := StatsFlowHash(tt.args.r)
   102  			if gotFlowhash != tt.wantFlowhash {
   103  				t.Errorf("StatsFlowHash() gotFlowhash = %v, want %v", gotFlowhash, tt.wantFlowhash)
   104  			}
   105  			if gotContenthash != tt.wantContenthash {
   106  				t.Errorf("StatsFlowHash() gotContenthash = %v, want %v", gotContenthash, tt.wantContenthash)
   107  			}
   108  
   109  			gothash := StatsFlowContentHash(tt.args.r)
   110  			if gothash != tt.wantContenthash {
   111  				t.Errorf("StatsFlowHash() gothash = %v, want %v", gothash, tt.wantContenthash)
   112  			}
   113  		})
   114  	}
   115  }