Skip to content

Commit

Permalink
Testing logic for ssh
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
aerosouund committed Jun 29, 2024
1 parent d37b197 commit 8d12b8d
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cluster-provision/gocli/cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
64 changes: 64 additions & 0 deletions cluster-provision/gocli/cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
54 changes: 54 additions & 0 deletions cluster-provision/gocli/utils/mock/mock_ssh.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8d12b8d

Please sign in to comment.