Skip to content

Commit

Permalink
E2E: Generate command (#723)
Browse files Browse the repository at this point in the history
* e2e: testing the generate command

Signed-off-by: lucas.bruno <lucas.bruno@zup.com.br>
  • Loading branch information
lucasbrunozup authored Oct 27, 2021
1 parent c1c754d commit 1888d99
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 16 deletions.
Empty file removed e2e/commands/generate/.gitkeep
Empty file.
28 changes: 28 additions & 0 deletions e2e/commands/generate/generate_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2021 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package generate_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestGenerate(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Command generate suite")

}
87 changes: 87 additions & 0 deletions e2e/commands/generate/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2021 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package generate_test

import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
"os"
"path/filepath"
"strings"

"github.com/ZupIT/horusec/internal/utils/testutil"
)

var _ = Describe("Run horusec CLI with generate argument", func() {
var (
outBuffer = gbytes.NewBuffer()
errBuffer = gbytes.NewBuffer()
session *gexec.Session
err error
flags map[string]string
configFilePath string
)

BeforeSuite(func() {
configFileName := "horusec-config-generate-test.json"
configFilePath = filepath.Join(os.TempDir(), configFileName)
// Add scape slashes when running on Windows.
configFilePath = strings.ReplaceAll(configFilePath, `\`, `\\`)
})

BeforeEach(func() {
flags = map[string]string{
"--config-file-path": configFilePath,
}
cmd := testutil.GinkgoGetHorusecCmdWithFlags(testutil.GenerateCmd, flags)
session, err = gexec.Start(cmd, outBuffer, errBuffer)
})

AfterEach(func() {
_ = outBuffer.Clear()
_ = errBuffer.Clear()
})

When("the horusec-config.json still doesn't exists", func() {
BeforeEach(func() {
_ = os.Remove(configFilePath)
})

It("execute command without error", func() {
Expect(err).ShouldNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
})

It("show success message and correct path of config file", func() {
Eventually(outBuffer).Should(gbytes.Say(`Horusec created file of configuration with success on path:`))
Eventually(outBuffer).Should(gbytes.Say(fmt.Sprintf("[%s]", configFilePath)))
})
})

When("the horusec-config.json already exists", func() {
It("execute command without error", func() {
Expect(err).ShouldNot(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
})

It("show message already exists", func() {
Eventually(outBuffer).Should(gbytes.Say(`Horusec configuration already exists on path:`))
Eventually(outBuffer).Should(gbytes.Say(fmt.Sprintf("[%s]", configFilePath)))
})
})
})
6 changes: 2 additions & 4 deletions e2e/commands/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package version_test

import (
"os/exec"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
Expand All @@ -33,9 +31,9 @@ var _ = Describe("Run horusec CLI with version argument", func() {
)

BeforeEach(func() {
binaryPath := testutil.GomegaBuildHorusecBinary()
cmd := testutil.GinkgoGetHorusecCmd(testutil.VersionCmd)
outBuffer = gbytes.NewBuffer()
session, err = gexec.Start(exec.Command(binaryPath, "version"), outBuffer, outBuffer)
session, err = gexec.Start(cmd, outBuffer, outBuffer)
})

It("execute command without error", func() {
Expand Down
12 changes: 1 addition & 11 deletions e2e/scan/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path"

"github.com/google/uuid"
Expand Down Expand Up @@ -125,7 +124,6 @@ var _ = Describe("Scan vulnerabilities example folder", func() {

for _, tt := range testcases {
It(tt.name, func() {
bin := testutil.GomegaBuildHorusecBinary()
output := path.Join(os.TempDir(), fmt.Sprintf("horusec-analysis-%s.json", uuid.New().String()))

flags := map[string]string{
Expand All @@ -134,15 +132,7 @@ var _ = Describe("Scan vulnerabilities example folder", func() {
"-O": output,
}

cmdArguments := []string{
"start",
}

for flag, value := range flags {
cmdArguments = append(cmdArguments, fmt.Sprintf("%s=%s", flag, value))
}

cmd := exec.Command(bin, cmdArguments...)
cmd := testutil.GinkgoGetHorusecCmdWithFlags(testutil.StartCmd, flags)

stdout := bytes.NewBufferString("")
stderr := bytes.NewBufferString("")
Expand Down
33 changes: 32 additions & 1 deletion internal/utils/testutil/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,34 @@ import (
"github.com/onsi/ginkgo"
)

func GomegaBuildHorusecBinary(customArgs ...string) string {
type HorusecCmd string

func (h HorusecCmd) String() string {
return string(h)
}

const (
VersionCmd HorusecCmd = "version"
StartCmd HorusecCmd = "start"
GenerateCmd HorusecCmd = "generate"
)

func GinkgoGetHorusecCmd(horusecCmd HorusecCmd) *exec.Cmd {
bin := ginkgoBuildHorusecBinary()
args := setLogLevelArgsToHorusecCmd(horusecCmd.String())
return exec.Command(bin, args...)
}

func GinkgoGetHorusecCmdWithFlags(cmdArg HorusecCmd, flags map[string]string) *exec.Cmd {
bin := ginkgoBuildHorusecBinary()
args := setLogLevelArgsToHorusecCmd(cmdArg.String())
for flag, value := range flags {
args = append(args, fmt.Sprintf("%s=%s", flag, value))
}
return exec.Command(bin, args...)
}

func ginkgoBuildHorusecBinary(customArgs ...string) string {
binary := filepath.Join(os.TempDir(), getBinaryNameBySystem())
args := []string{
"build",
Expand All @@ -40,6 +67,10 @@ func GomegaBuildHorusecBinary(customArgs ...string) string {
return binary
}

func setLogLevelArgsToHorusecCmd(horusecCmd ...string) []string {
return append(horusecCmd, fmt.Sprintf("%s=%s", "--log-level", "debug"))
}

func getBinaryNameBySystem() string {
binaryName := "horusec-e2e"
if runtime.GOOS == "windows" {
Expand Down

0 comments on commit 1888d99

Please sign in to comment.