From 8d12b8d5812fee4e66a7fddd151f21fb8e88ceb2 Mon Sep 17 00:00:00 2001 From: aerosouund Date: Sat, 29 Jun 2024 16:19:26 +0300 Subject: [PATCH] Testing logic for ssh Introduce a testing package based on mock that tests the provisionNode and provisionK8sOptions methods and include the logic to run this package in the main test suite of the cmd package. put tests in the same package as the logic Signed-off-by: aerosouund --- cluster-provision/gocli/cmd/cmd_suite_test.go | 4 +- cluster-provision/gocli/cmd/run_test.go | 64 +++++++++++++++++++ .../gocli/utils/mock/mock_ssh.go | 54 ++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 cluster-provision/gocli/cmd/run_test.go create mode 100644 cluster-provision/gocli/utils/mock/mock_ssh.go diff --git a/cluster-provision/gocli/cmd/cmd_suite_test.go b/cluster-provision/gocli/cmd/cmd_suite_test.go index 318fefe60d..8e3505857f 100644 --- a/cluster-provision/gocli/cmd/cmd_suite_test.go +++ b/cluster-provision/gocli/cmd/cmd_suite_test.go @@ -1,13 +1,15 @@ -package cmd_test +package cmd import ( "testing" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "github.com/stretchr/testify/suite" ) func TestCmd(t *testing.T) { + suite.Run(t, new(TestSuite)) RegisterFailHandler(Fail) RunSpecs(t, "Provision Manager Suite") } diff --git a/cluster-provision/gocli/cmd/run_test.go b/cluster-provision/gocli/cmd/run_test.go new file mode 100644 index 0000000000..4c6d7dd5b3 --- /dev/null +++ b/cluster-provision/gocli/cmd/run_test.go @@ -0,0 +1,64 @@ +package cmd + +import ( + "fmt" + + "github.com/stretchr/testify/suite" + "go.uber.org/mock/gomock" + nodesconfig "kubevirt.io/kubevirtci/cluster-provision/gocli/cmd/config" + kubevirtcimocks "kubevirt.io/kubevirtci/cluster-provision/gocli/utils/mock" +) + +type TestSuite struct { + suite.Suite + sshClient *kubevirtcimocks.MockSSHClient +} + +func (ts *TestSuite) SetupTest() { + ts.sshClient = kubevirtcimocks.NewMockSSHClient(gomock.NewController(ts.T())) +} + +func (ts *TestSuite) TearDownTest() { + ts.sshClient = nil +} + +func (ts *TestSuite) TestProvisionNode() { + n := nodesconfig.NewNodeLinuxConfig(1, "k8s-1.30", "", "512M", "", false, true, true, true, true, true) + cmds := []string{ + "mkdir -p /var/lib/etcd", + "test -d /var/lib/etcd", + fmt.Sprintf("mount -t tmpfs -o size=%s tmpfs /var/lib/etcd", n.EtcdSize), + "df -h /var/lib/etcd", + "/scripts/realtime.sh", + "-s -- --vendor 8086:2668 < /scripts/bind_device_to_vfio.sh", + "-s -- --vendor 8086:2415 < /scripts/bind_device_to_vfio.sh", + "touch /home/vagrant/single_stack", + "touch /home/vagrant/enable_audit", + "/scripts/psa.sh", + "/scripts/node01.sh", + } + + for _, cmd := range cmds { + ts.sshClient.EXPECT().SSH(cmd, true).Return("", nil) + } + + err := provisionNode(ts.sshClient, n) + ts.NoError(err) +} + +func (ts *TestSuite) TestProvisionNodeK8sOpts() { + n := nodesconfig.NewNodeK8sConfig(true, true, true, true, true, true) + cmds := []string{ + "/scripts/rook-ceph.sh", + "/scripts/nfs-csi.sh", + "/scripts/istio.sh", + "-s -- --alertmanager true --grafana true < /scripts/prometheus.sh", + } + + for _, cmd := range cmds { + ts.sshClient.EXPECT().SSH(cmd, true).Return("", nil) + } + + err := provisionK8sOptions(ts.sshClient, n) + ts.NoError(err) +} diff --git a/cluster-provision/gocli/utils/mock/mock_ssh.go b/cluster-provision/gocli/utils/mock/mock_ssh.go new file mode 100644 index 0000000000..4055379b39 --- /dev/null +++ b/cluster-provision/gocli/utils/mock/mock_ssh.go @@ -0,0 +1,54 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: utils/ssh/ssh.go +// +// Generated by this command: +// +// mockgen -source=utils/ssh/ssh.go -destination=utils/mock/mock_ssh.go -package=kubevirtcimocks +// + +// Package kubevirtcimocks is a generated GoMock package. +package kubevirtcimocks + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockSSHClient is a mock of SSHClient interface. +type MockSSHClient struct { + ctrl *gomock.Controller + recorder *MockSSHClientMockRecorder +} + +// MockSSHClientMockRecorder is the mock recorder for MockSSHClient. +type MockSSHClientMockRecorder struct { + mock *MockSSHClient +} + +// NewMockSSHClient creates a new mock instance. +func NewMockSSHClient(ctrl *gomock.Controller) *MockSSHClient { + mock := &MockSSHClient{ctrl: ctrl} + mock.recorder = &MockSSHClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockSSHClient) EXPECT() *MockSSHClientMockRecorder { + return m.recorder +} + +// SSH mocks base method. +func (m *MockSSHClient) SSH(cmd string, stdout bool) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SSH", cmd, stdout) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// SSH indicates an expected call of SSH. +func (mr *MockSSHClientMockRecorder) SSH(cmd, stdout any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SSH", reflect.TypeOf((*MockSSHClient)(nil).SSH), cmd, stdout) +}