package tests

import (
	"rbd_exporter/rbdfactory"
	mock "rbd_exporter/mytypes/mock"
	"testing"

	"github.com/ceph/go-ceph/rbd"
	gomock "go.uber.org/mock/gomock"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)


func TestRBDFactory(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	MockIOContexter := mock.NewMockIOContexter(mockCtrl)
	MockRBDImager := mock.NewMockRBDImager(mockCtrl)

	rbdName := "test-rbd"
	expectedImageInfo := &rbd.ImageInfo{
		Size: 1488,
	}

	MockIOContexter.EXPECT().GetImage(rbdName).Return(MockRBDImager)
	MockRBDImager.EXPECT().Open().Return(nil)
	MockRBDImager.EXPECT().Close().Return(nil)
	MockRBDImager.EXPECT().Stat().Return(expectedImageInfo,nil)


	result,err := rbdfactory.RBDFacroty(MockIOContexter,rbdName)

	require.NoError(t, err)
	assert.Equal(t, rbdName, result.Name)
	assert.Equal(t, expectedImageInfo.Size, result.Size)
}


func TestPoolFactory(t *testing.T) {
	mockCtrl := gomock.NewController(t)
	defer mockCtrl.Finish()

	mockCephConn := mock.NewMockCephConnector(mockCtrl)
	mockIOCtx := mock.NewMockIOContexter(mockCtrl)
	mockImage := mock.NewMockRBDImager(mockCtrl)


	poolName := "test-pool"
	imageNames := []string{"rbd1", "rbd2"}
	expectedImageInfo := &rbd.ImageInfo{Size: 1024}

	mockCephConn.EXPECT().OpenIOContext(poolName).Return(mockIOCtx,nil)
	mockIOCtx.EXPECT().GetImageNames().Return(imageNames,nil)
	mockIOCtx.EXPECT().Destroy()

	for _, imageName := range imageNames {
		mockIOCtx.EXPECT().GetImage(imageName).Return(mockImage)
		mockImage.EXPECT().Open().Return(nil)
		mockImage.EXPECT().Close().Return(nil)
		mockImage.EXPECT().Stat().Return(expectedImageInfo, nil)
	}

	result, err := rbdfactory.PoolFactory(mockCephConn, poolName)

	require.NoError(t, err)
	assert.Equal(t, poolName, result.Name)
	assert.True(t, result.HasRBD)
	assert.Len(t, result.RBDlist, 2)
	assert.Equal(t, "rbd1", result.RBDlist[0].GetName())
	assert.Equal(t, "rbd2", result.RBDlist[1].GetName())
}