-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add different levels of tests for example plugin
- Loading branch information
James C. Scott
committed
Oct 4, 2016
1 parent
0bd1b9d
commit ee6b64c
Showing
4 changed files
with
128 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,103 @@ | ||
package main | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
. "github.com/onsi/gomega/gbytes" | ||
. "github.com/onsi/gomega/gexec" | ||
"github.com/opencontrol/compliance-masonry/lib/common" | ||
"github.com/opencontrol/compliance-masonry/lib/common/mocks" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
func TestSimpleDatNoData(t *testing.T) { | ||
// Test the case when there is no data | ||
var _ = Describe("Exampleplugin", func() { | ||
// example of unit test with mocks. | ||
Describe("simpleDataExtract", func() { | ||
Context("When there is no data for the standard-control combo", func() { | ||
It("should print 'no data'", func() { | ||
// create mock workspace | ||
ws := new(mocks.Workspace) | ||
ws.On("GetAllVerificationsWith", "NIST-800-53", "CM-2").Return(common.Verifications{}) | ||
// test function expecting "no data" | ||
p := plugin{ws} | ||
data := simpleDataExtract(p) | ||
Expect(data).To(Equal("no data")) | ||
}) | ||
}) | ||
Context("When there is data for the standard-control combo", func() { | ||
It("should print the 'IMPLEMENTED', given that's expected", func() { | ||
// create mock workspace | ||
ws := new(mocks.Workspace) | ||
satisfies := new(mocks.Satisfies) | ||
satisfies.On("GetImplementationStatus").Return("IMPLEMENTED") | ||
ws.On("GetAllVerificationsWith", "NIST-800-53", "CM-2").Return( | ||
common.Verifications{common.Verification{SatisfiesData: satisfies}}) | ||
// test function expecting "IMPLEMENTED" | ||
p := plugin{ws} | ||
data := simpleDataExtract(p) | ||
Expect(data).To(Equal("IMPLEMENTED")) | ||
assert.Equal(GinkgoT(), data, "IMPLEMENTED") | ||
}) | ||
}) | ||
}) | ||
// Example of reading the standard output. | ||
Describe("run", func() { | ||
Context("When running it on data in a workspace", func() { | ||
It("should find the data and print it out to standard out", func() { | ||
wsPath := filepath.Join("..", "fixtures", "opencontrol_fixtures") | ||
certPath := filepath.Join("..", "..", "fixtures", "opencontrol_fixtures", "certifications", "LATO.yaml") | ||
buffer := NewBuffer() | ||
run(wsPath, certPath, buffer) | ||
Expect(buffer).To(Say("partial")) | ||
}) | ||
}) | ||
}) | ||
// Example of Running Masonry (with "get") and then the Plugin executables. | ||
Describe("running the executable", func() { | ||
BeforeEach(func() { | ||
cleanupOpencontrolWorkspace() | ||
}) | ||
Context("when running the executable", func() { | ||
It("should build and run with two arguments", func() { | ||
masonry := Masonry("--verbose", "get") | ||
Eventually(masonry).Should(Exit(0)) | ||
p := Plugin(filepath.Join("opencontrols"), filepath.Join("opencontrols", "certifications")) | ||
Eventually(p).Should(Exit(0)) | ||
// Should match the implementation status of CloudFormation in aws-compliance, which is | ||
// "none" | ||
Eventually(p.Out.Contents()).Should(ContainSubstring("none")) | ||
}) | ||
}) | ||
AfterEach(func() { | ||
cleanupOpencontrolWorkspace() | ||
CleanupBuildArtifacts() | ||
}) | ||
}) | ||
}) | ||
|
||
// create mock workspace | ||
ws := new(mocks.Workspace) | ||
ws.On("GetAllVerificationsWith", "standard", "control").Return(common.Verifications{}) | ||
// test function expecting "no data" | ||
p := plugin{ws} | ||
data := simpleDataExtract(p) | ||
assert.Equal(t, data, "no data") | ||
func cleanupOpencontrolWorkspace() { | ||
os.RemoveAll("opencontrols") | ||
} | ||
|
||
func TestSimpleDataWithData(t *testing.T) { | ||
// Test the case when there is data. | ||
func Masonry(args ...string) *Session { | ||
path, err := Build("github.com/opencontrol/compliance-masonry") | ||
Expect(err).NotTo(HaveOccurred()) | ||
return createCommand(path, args...) | ||
} | ||
func Plugin(args ...string) *Session { | ||
path, err := Build("github.com/opencontrol/compliance-masonry/exampleplugin") | ||
Expect(err).NotTo(HaveOccurred()) | ||
return createCommand(path, args...) | ||
} | ||
|
||
// create mock workspace | ||
ws := new(mocks.Workspace) | ||
satisfies := new(mocks.Satisfies) | ||
satisfies.On("GetImplementationStatus").Return("IMPLEMENTED") | ||
ws.On("GetAllVerificationsWith", "standard", "control").Return(common.Verifications{common.Verification{SatisfiesData: satisfies}}) | ||
// test function expecting "IMPLEMENTED" | ||
p := plugin{ws} | ||
data := simpleDataExtract(p) | ||
assert.Equal(t, data, "IMPLEMENTED") | ||
func createCommand(cmdPath string, args ...string) *Session { | ||
cmd := exec.Command(cmdPath, args...) | ||
session, err := Start(cmd, GinkgoWriter, GinkgoWriter) | ||
session.Wait(20 * time.Second) | ||
Expect(err).NotTo(HaveOccurred()) | ||
return session | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"testing" | ||
) | ||
|
||
func TestExampleplugin(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Exampleplugin Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
schema_version: "1.0.0" | ||
name: test-opencontrol-yaml | ||
metadata: | ||
description: "This opencontrol YAML is just used for showing various levels of testing. For more details refer to example_test.go" | ||
dependencies: | ||
standards: | ||
- url: https://github.com/opencontrol/NIST-800-53-Standards | ||
revision: master | ||
certifications: | ||
- url: https://github.com/opencontrol/FedRAMP-Certifications | ||
revision: master | ||
systems: | ||
- url: https://github.com/opencontrol/aws-compliance | ||
revision: master |