From 7c4656af17bcba87fee353cd245584e2f8db7e35 Mon Sep 17 00:00:00 2001 From: arbulu89 Date: Thu, 17 Mar 2022 17:45:59 +0100 Subject: [PATCH] Add tests to the BuildCatalog function --- runner/runner_test.go | 70 ++++++++++++++++++++++++++++++++++++-- test/fixtures/catalog.json | 32 +++++++++++++++++ 2 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/catalog.json diff --git a/runner/runner_test.go b/runner/runner_test.go index 1079fc9..491e0ee 100644 --- a/runner/runner_test.go +++ b/runner/runner_test.go @@ -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 ( @@ -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) @@ -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", @@ -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", diff --git a/test/fixtures/catalog.json b/test/fixtures/catalog.json new file mode 100644 index 0000000..83eeb4d --- /dev/null +++ b/test/fixtures/catalog.json @@ -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" + } + ] + } +}