Skip to content

Commit

Permalink
Add tests to the BuildCatalog function
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Mar 17, 2022
1 parent 09195c4 commit 7c4656a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
70 changes: 67 additions & 3 deletions runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package runner
import (
"io/ioutil"
"os"
"os/exec"
"path"
"testing"

"github.com/stretchr/testify/suite"

"github.com/trento-project/runner/runner/mocks"
)

const (
Expand All @@ -15,15 +18,76 @@ const (

type RunnerTestCase struct {
suite.Suite
runnerService RunnerService
ansibleDir string
}

func TestRunnerTestCase(t *testing.T) {
suite.Run(t, new(RunnerTestCase))
}

func (suite *RunnerTestCase) SetupTest() {
tmpDir, _ := ioutil.TempDir(os.TempDir(), "trentotest")
runnerService, _ := NewRunnerService(&Config{AnsibleFolder: tmpDir})
suite.runnerService = runnerService
suite.ansibleDir = tmpDir
}


func (suite *RunnerTestCase) Test_BuildCatalog() {
suite.Equal(false, suite.runnerService.IsCatalogReady())

cmd := exec.Command("cp", "../test/fixtures/catalog.json", path.Join(suite.ansibleDir,"ansible"))

mockCommand := new(mocks.CustomCommand)
customExecCommand = mockCommand.Execute

mockCommand.On(
"Execute", "ansible-playbook", path.Join(suite.ansibleDir,"ansible/meta.yml")).Return(
cmd,
)

err := suite.runnerService.BuildCatalog()

expectedMap := map[string]*Catalog{
"azure": &Catalog{
Checks: []*CatalogCheck{
{
ID: "156F64",
Name:"1.1.1",
Group:"Corosync",
Description: "description azure",
Remediation: "remediation",
Implementation: "implementation",
Labels: "generic",
Premium: false,
},
},
},
"dev": &Catalog{
Checks: []*CatalogCheck{
{
ID: "156F64",
Name:"1.1.1",
Group:"Corosync",
Description: "description dev",
Remediation: "remediation",
Implementation: "implementation",
Labels: "generic",
Premium: false,
},
},
},
}

suite.NoError(err)
suite.Equal(true, suite.runnerService.IsCatalogReady())
suite.Equal(expectedMap, suite.runnerService.GetCatalog())
}

// TODO: This test could be improved to check the definitve ansible files structure
// once we have something fixed
func (suite *ApiTestCase) Test_CreateAnsibleFiles() {
func (suite *RunnerTestCase) Test_CreateAnsibleFiles() {
tmpDir, _ := ioutil.TempDir(os.TempDir(), "trentotest")
err := createAnsibleFiles(tmpDir)

Expand All @@ -33,7 +97,7 @@ func (suite *ApiTestCase) Test_CreateAnsibleFiles() {
os.RemoveAll(tmpDir)
}

func (suite *ApiTestCase) Test_NewAnsibleMetaRunner() {
func (suite *RunnerTestCase) Test_NewAnsibleMetaRunner() {

cfg := &Config{
ApiHost: "127.0.0.1",
Expand All @@ -56,7 +120,7 @@ func (suite *ApiTestCase) Test_NewAnsibleMetaRunner() {
suite.Equal(expectedMetaRunner, a)
}

func (suite *ApiTestCase) Test_NewAnsibleCheckRunner() {
func (suite *RunnerTestCase) Test_NewAnsibleCheckRunner() {

cfg := &Config{
ApiHost: "127.0.0.1",
Expand Down
32 changes: 32 additions & 0 deletions test/fixtures/catalog.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"azure":
{
"checks": [
{
"group": "Corosync",
"name": "1.1.1",
"implementation": "implementation",
"labels": "generic",
"remediation": "remediation",
"premium": false,
"id": "156F64",
"description": "description azure"
}
]
},
"dev":
{
"checks": [
{
"group": "Corosync",
"name": "1.1.1",
"implementation": "implementation",
"labels": "generic",
"remediation": "remediation",
"premium": false,
"id": "156F64",
"description": "description dev"
}
]
}
}

0 comments on commit 7c4656a

Please sign in to comment.