github.com/castai/kvisor@v1.7.1-0.20240516114728-b3572a2607b5/cmd/controller/state/kubelinter/controller_test.go (about)

     1  package kubelinter
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	castaipb "github.com/castai/kvisor/api/v1/runtime"
     8  	"github.com/castai/kvisor/cmd/controller/kube"
     9  	"github.com/castai/kvisor/pkg/logging"
    10  	"github.com/samber/lo"
    11  	"github.com/stretchr/testify/require"
    12  	"google.golang.org/grpc"
    13  	corev1 "k8s.io/api/core/v1"
    14  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    15  )
    16  
    17  func TestSubscriber(t *testing.T) {
    18  	log := logging.NewTestLog()
    19  
    20  	t.Run("sends linter checks", func(t *testing.T) {
    21  		r := require.New(t)
    22  		castaiClient := &mockCastaiClient{}
    23  
    24  		linter, err := New(lo.Keys(LinterRuleMap))
    25  		r.NoError(err)
    26  
    27  		ctrl := &Controller{
    28  			client: castaiClient,
    29  			linter: linter,
    30  			delta:  newDeltaState(),
    31  			log:    log,
    32  		}
    33  
    34  		objects := []kube.Object{
    35  			&corev1.Pod{
    36  				TypeMeta: metav1.TypeMeta{
    37  					Kind:       "Pod",
    38  					APIVersion: "v1",
    39  				},
    40  				ObjectMeta: metav1.ObjectMeta{
    41  					Name: "test_pod",
    42  				},
    43  			},
    44  		}
    45  		ctx := context.Background()
    46  		r.NoError(ctrl.lintObjects(ctx, objects))
    47  		r.Len(castaiClient.reports, 1)
    48  	})
    49  }
    50  
    51  type mockCastaiClient struct {
    52  	reports []*castaipb.KubeLinterReport
    53  }
    54  
    55  func (m *mockCastaiClient) KubeLinterReportIngest(ctx context.Context, in *castaipb.KubeLinterReport, opts ...grpc.CallOption) (*castaipb.KubeLinterReportIngestResponse, error) {
    56  	m.reports = append(m.reports, in)
    57  	return &castaipb.KubeLinterReportIngestResponse{}, nil
    58  }