github.com/cilium/cilium@v1.16.2/pkg/datapath/linux/modules/modules_linux_privileged_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  //go:build linux
     5  
     6  package modules
     7  
     8  import (
     9  	"context"
    10  	"testing"
    11  
    12  	"github.com/cilium/hive/cell"
    13  	"github.com/cilium/hive/hivetest"
    14  
    15  	"github.com/cilium/cilium/pkg/hive"
    16  	"github.com/cilium/cilium/pkg/testutils"
    17  )
    18  
    19  func TestFindOrLoadModules(t *testing.T) {
    20  	testutils.PrivilegedTest(t)
    21  
    22  	testCases := []struct {
    23  		modulesToFind []string
    24  		expectedErr   bool
    25  	}{
    26  		{
    27  			modulesToFind: []string{"bridge"},
    28  			expectedErr:   false,
    29  		},
    30  		{
    31  			modulesToFind: []string{"foo", "bar"},
    32  			expectedErr:   true,
    33  		},
    34  	}
    35  
    36  	var manager *Manager
    37  
    38  	hive := hive.New(
    39  		Cell,
    40  		cell.Invoke(func(mgr *Manager) {
    41  			manager = mgr
    42  		}),
    43  	)
    44  
    45  	tlog := hivetest.Logger(t)
    46  	if err := hive.Start(tlog, context.Background()); err != nil {
    47  		t.Fatalf("failed to start: %s", err)
    48  	}
    49  
    50  	for _, tc := range testCases {
    51  		err := manager.FindOrLoadModules(tc.modulesToFind...)
    52  		if tc.expectedErr && err == nil {
    53  			t.Fatal("expected error from FindOrLoadModules but none found")
    54  		}
    55  		if !tc.expectedErr && err != nil {
    56  			t.Fatalf("FindOrLoadModules failed with unexpected error: %s", err)
    57  		}
    58  	}
    59  
    60  	if err := hive.Stop(tlog, context.Background()); err != nil {
    61  		t.Fatalf("failed to stop: %s", err)
    62  	}
    63  }