Skip to content

EMCECS/operator-sdk-mock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mock functionality for the operator SDK

Build Status

Usage

This module provides an interface over the Opererator SDK to allow writing mock tests by use of gomock.

Handler

Create a handler that accesses the operator sdk functions via the sdk operator SDK implementation.

import (
    "github.com/operator-framework/operator-sdk/pkg/sdk"
    sdkIf "github.com/bakito/operator-sdk-mock/pkg/sdk"
)

type Handler struct {
    sdk sdkIf.SdkInterface
}

func (h *Handler) Handle(ctx context.Context, event sdk.Event) error {
    h.sdkInterface.Update(event.Object)
    return nil
}

Testing

See the examples.

import (
    "testing"

    "github.com/golang/mock/gomock"
    "github.com/operator-framework/operator-sdk/pkg/sdk"
    mock "github.com/bakito/operator-sdk-mock/pkg/sdk/mock_sdk"
)

func Test_Handle_Delete(t *testing.T) {

    ctrl := gomock.NewController(t)
    defer ctrl.Finish()

    mockSdk := mock.NewMockSdkInterface(ctrl)

    handler := Handler{
        sdk:       mockSdk,
    }

    pp := &v1.ProjectProvisioning{}

    mockSdk.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(namespace string, into sdk.Object, opts sdk.ListOption) {
        ...
    })

    mockSdk.EXPECT().Delete(ns)

    handler.Handle(nil, sdk.Event{
        Deleted: true,
        Object:  pp,
    })
}

Generate mock

mockgen -source=pkg/sdk/interface.go -destination=pkg/sdk/mock_sdk/interface_mock.go 

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages