Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcscottiii committed Jul 25, 2016
1 parent 1b3ed48 commit a9eaa8d
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 132 deletions.
18 changes: 18 additions & 0 deletions lib/common/entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package common

import "github.com/opencontrol/compliance-masonry/tools/constants"

// Entry is a generic holder for handling the specific location and revision of a resource.
type Entry struct {
URL string `yaml:"url"`
Revision string `yaml:"revision"`
Path string `yaml:"path"`
}

// GetConfigFile is a getter for the config file name. Will return DefaultConfigYaml value if none has been set.
func (e Entry) GetConfigFile() string {
if e.Path == "" {
return constants.DefaultConfigYaml
}
return e.Path
}
19 changes: 19 additions & 0 deletions lib/common/entry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package common

import (
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/extensions/table"
"github.com/stretchr/testify/assert"
"github.com/opencontrol/compliance-masonry/tools/constants"
)

var _ = Describe("Entry", func() {
Describe("Retrieving the config file", func() {
table.DescribeTable("GetConfigFile", func(e Entry, expectedPath string) {
assert.Equal(GinkgoT(), e.GetConfigFile(), expectedPath)
},
table.Entry("Empty / new base struct to return default", Entry{}, constants.DefaultConfigYaml),
table.Entry("overriden config file path", Entry{Path: "samplepath"}, "samplepath"),
)
})
})
3 changes: 1 addition & 2 deletions lib/components/versions/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/blang/semver"
"github.com/opencontrol/compliance-masonry/config"
"github.com/opencontrol/compliance-masonry/lib"
"github.com/opencontrol/compliance-masonry/lib/common"
"github.com/opencontrol/compliance-masonry/lib/components"
Expand Down Expand Up @@ -288,7 +287,7 @@ var componentTestErrors = []componentTestError{

// Check for version that is unsupported
{filepath.Join("..", "..", "..", "fixtures", "component_fixtures", "common", "EC2UnsupportedVersion"),
config.ErrUnknownSchemaVersion},
common.ErrUnknownSchemaVersion},

// Check for the case when someone says they are using a certain version (2.0) but it actually is not
{filepath.Join("..", "..", "..", "fixtures", "component_fixtures", "common", "EC2_InvalidFieldTypeForVersion2_0"),
Expand Down
21 changes: 3 additions & 18 deletions lib/opencontrol/resources/entry.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
package resources

import (
"github.com/opencontrol/compliance-masonry/tools/constants"
"github.com/opencontrol/compliance-masonry/tools/vcs"
"github.com/opencontrol/compliance-masonry/lib/common"
)

// Entry is a generic holder for handling the specific location and revision of a resource.
type Entry struct {
URL string `yaml:"url"`
Revision string `yaml:"revision"`
Path string `yaml:"path"`
}

// GetConfigFile is a getter for the config file name. Will return DefaultConfigYaml value if none has been set.
func (e Entry) GetConfigFile() string {
if e.Path == "" {
return constants.DefaultConfigYaml
}
return e.Path
}

// EntryDownloader is a generic interface for how to download entries.
type EntryDownloader interface {
DownloadEntry(Entry, string) error
DownloadEntry(common.Entry, string) error
}

// NewVCSDownloader is a constructor for downloading entries using VCS methods.
Expand All @@ -35,7 +20,7 @@ type vcsEntryDownloader struct {
}

// DownloadEntry is a implementation for downloading entries using VCS methods.
func (v vcsEntryDownloader) DownloadEntry(entry Entry, destination string) error {
func (v vcsEntryDownloader) DownloadEntry(entry common.Entry, destination string) error {
err := v.manager.Clone(entry.URL, entry.Revision, destination)
if err != nil {
return err
Expand Down
21 changes: 7 additions & 14 deletions lib/opencontrol/resources/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,29 @@ import (
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/extensions/table"
"github.com/stretchr/testify/assert"
"github.com/opencontrol/compliance-masonry/tools/constants"
"github.com/opencontrol/compliance-masonry/tools/vcs/mocks"
"errors"
"errors"
"github.com/opencontrol/compliance-masonry/lib/common"
)

var _ = Describe("Entry", func() {
Describe("Retrieving the config file", func(){
table.DescribeTable("GetConfigFile", func(e Entry, expectedPath string) {
assert.Equal(GinkgoT(), e.GetConfigFile(), expectedPath)
},
table.Entry("Empty / new base struct to return default", Entry{}, constants.DefaultConfigYaml),
table.Entry("overriden config file path", Entry{Path: "samplepath"}, "samplepath"),
)
})
var _ = Describe("Downloader", func() {

Describe("Constructing a new VCEntrySDownloader", func() {
It("should return a downloader of type VCSEntryDownloader", func() {
downloader := NewVCSDownloader()
assert.IsType(GinkgoT(), vcsEntryDownloader{}, downloader)
})
})
Describe("Downloading Entry from VCS", func(){
table.DescribeTable("DownloadEntry", func(e Entry, err error) {
table.DescribeTable("DownloadEntry", func(e common.Entry, err error) {
m := new(mocks.RepoManager)
m.On("Clone", e.URL, e.Revision, ".").Return(err)
v := vcsEntryDownloader{m}
v.DownloadEntry(e, ".")
m.AssertExpectations(GinkgoT())
},
table.Entry("No error returned", Entry{}, nil),
table.Entry("An error returned", Entry{}, errors.New("an error")),
table.Entry("No error returned", common.Entry{}, nil),
table.Entry("An error returned", common.Entry{}, errors.New("an error")),
)
})
})
5 changes: 3 additions & 2 deletions lib/opencontrol/resources/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"path/filepath"
"github.com/opencontrol/compliance-masonry/lib/opencontrol/versions/base"
"github.com/opencontrol/compliance-masonry/lib/opencontrol/versions"
"github.com/opencontrol/compliance-masonry/lib/common"
)

// ResourceGetter is an interface for how to get and place local and remote resources.
type Getter interface {
GetLocalResources(source string, resources []string, destination string, subfolder string, recursively bool, worker *base.Worker, resourceType constants.ResourceType) error
GetRemoteResources(destination string, subfolder string, worker *base.Worker, entries []Entry) error
GetRemoteResources(destination string, subfolder string, worker *base.Worker, entries []common.Entry) error
}

func NewVCSAndLocalGetter() Getter {
Expand Down Expand Up @@ -56,7 +57,7 @@ func (g vcsAndLocalFSGetter) GetLocalResources(source string, resources []string
}

// GetRemoteResources is the implementation that uses VCS to get remote resources.
func (g vcsAndLocalFSGetter) GetRemoteResources(destination string, subfolder string, worker *base.Worker, entries []Entry) error {
func (g vcsAndLocalFSGetter) GetRemoteResources(destination string, subfolder string, worker *base.Worker, entries []common.Entry) error {
tempResourcesDir, err := worker.FSUtil.TempDir("", "opencontrol-resources")
if err != nil {
return err
Expand Down
52 changes: 26 additions & 26 deletions lib/opencontrol/resources/getter_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package resources_test
package resources

import (
. "github.com/opencontrol/compliance-masonry/config/common/resources"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
"github.com/opencontrol/compliance-masonry/config/common"
"github.com/opencontrol/compliance-masonry/config/common/mocks"
"github.com/onsi/ginkgo/extensions/table"
"github.com/opencontrol/compliance-masonry/lib/opencontrol/versions/base"
"github.com/opencontrol/compliance-masonry/tools/constants"
basemocks "github.com/opencontrol/compliance-masonry/lib/opencontrol/versions/base/mocks"
fsmocks "github.com/opencontrol/compliance-masonry/tools/fs/mocks"
"github.com/opencontrol/compliance-masonry/tools/mapset"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/vektra/errors"
"github.com/opencontrol/compliance-masonry/lib/common"
)

var _ = Describe("ResourceGetter", func() {
Expand All @@ -21,46 +21,46 @@ var _ = Describe("ResourceGetter", func() {
var (
resMap mapset.MapSet
)
DescribeTable("", func(recursively bool, initMap bool, resources []string, mkdirsError, copyError, copyAllError, expectedError error) {
getter := VCSAndLocalFSGetter{}
table.DescribeTable("", func(recursively bool, initMap bool, resources []string, mkdirsError, copyError, copyAllError, expectedError error) {
getter := NewVCSAndLocalGetter()
fsUtil := new(fsmocks.Util)
fsUtil.On("Mkdirs", mock.AnythingOfType("string")).Return(mkdirsError)
fsUtil.On("Copy", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(copyError)
fsUtil.On("CopyAll", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(copyAllError)
if initMap {
resMap = mapset.Init()
}
worker := new(common.ConfigWorker)
worker := new(base.Worker)
worker.ResourceMap = resMap
worker.FSUtil = fsUtil
err := getter.GetLocalResources("", resources, "dest", "subfolder", recursively, worker, constants.Standards)
assert.Equal(GinkgoT(), expectedError, err)
},
Entry("Bad input to reserve", false, true, []string{""}, nil, nil, nil, mapset.ErrEmptyInput),
Entry("Successful recursive copy", true, true, []string{"res"}, nil, nil, nil, nil),
Entry("Successful single copy", false, true, []string{"res"}, nil, nil, nil, nil),
Entry("Failure of single copy", false, true, []string{"res"}, nil, errors.New("single copy fail"), nil, errors.New("single copy fail")),
Entry("Mkdirs", false, true, []string{"res"}, errors.New("mkdirs error"), nil, nil, errors.New("mkdirs error")),
table.Entry("Bad input to reserve", false, true, []string{""}, nil, nil, nil, mapset.ErrEmptyInput),
table.Entry("Successful recursive copy", true, true, []string{"res"}, nil, nil, nil, nil),
table.Entry("Successful single copy", false, true, []string{"res"}, nil, nil, nil, nil),
table.Entry("Failure of single copy", false, true, []string{"res"}, nil, errors.New("single copy fail"), nil, errors.New("single copy fail")),
table.Entry("Mkdirs", false, true, []string{"res"}, errors.New("mkdirs error"), nil, nil, errors.New("mkdirs error")),
)
})
Describe("GetRemoteResources", func() {
DescribeTable("", func(downloadEntryError, tempDirError, openAndReadFileError, getResourcesError, parseV1_0_0Error, expectedError error) {
table.DescribeTable("", func(downloadEntryError, tempDirError, openAndReadFileError, getResourcesError, parseV1_0_0Error, expectedError error) {
entries := []common.Entry{
{
Path: "",
},
}
getter := VCSAndLocalFSGetter{}
worker := new(common.ConfigWorker)
downloader := new(mocks.EntryDownloader)
getter := vcsAndLocalFSGetter{}
worker := new(base.Worker)
downloader := new(basemocks.EntryDownloader)
downloader.On("DownloadEntry", entries[0], mock.AnythingOfType("string")).Return(downloadEntryError)
worker.Downloader = downloader
getter.Downloader = downloader
fsUtil := new(fsmocks.Util)
fsUtil.On("TempDir", "", "opencontrol-resources").Return("sometempdir", tempDirError)
data := []byte("schema_version: 1.0.0")
fsUtil.On("OpenAndReadFile", mock.AnythingOfType("string")).Return(data, openAndReadFileError)
parser := new(mocks.SchemaParser)
schema := new(mocks.BaseSchema)
parser := new(basemocks.SchemaParser)
schema := new(basemocks.BaseSchema)
schema.On("GetResources", mock.AnythingOfType("string"), mock.AnythingOfType("string"), worker).Return(getResourcesError)
parser.On("ParseV1_0_0", data).Return(schema, parseV1_0_0Error)
worker.Parser = parser
Expand All @@ -69,12 +69,12 @@ var _ = Describe("ResourceGetter", func() {
assert.Equal(GinkgoT(), expectedError, err)

},
Entry("success", nil, nil, nil, nil, nil, nil),
Entry("fail to get resources", nil, nil, nil, errors.New("error getting resources"), nil, errors.New("error getting resources")),
Entry("fail to parse config", nil, nil, nil, nil, errors.New("error parsing"), errors.New("error parsing")),
Entry("fail to open and read file", nil, nil, errors.New("error reading file"), nil, nil, errors.New("error reading file")),
Entry("fail to download repo", errors.New("error downloading entry"), nil, nil, nil, nil, errors.New("error downloading entry")),
Entry("fail to create temp dir", nil, errors.New("error creating tempdir"), nil, nil, nil, errors.New("error creating tempdir")),
table.Entry("success", nil, nil, nil, nil, nil, nil),
table.Entry("fail to get resources", nil, nil, nil, errors.New("error getting resources"), nil, errors.New("error getting resources")),
table.Entry("fail to parse config", nil, nil, nil, nil, errors.New("error parsing"), errors.New("error parsing")),
table.Entry("fail to open and read file", nil, nil, errors.New("error reading file"), nil, nil, errors.New("error reading file")),
table.Entry("fail to download repo", errors.New("error downloading entry"), nil, nil, nil, nil, errors.New("error downloading entry")),
table.Entry("fail to create temp dir", nil, errors.New("error creating tempdir"), nil, nil, nil, errors.New("error creating tempdir")),
)
})
})
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package mocks

import "github.com/stretchr/testify/mock"

import "github.com/opencontrol/compliance-masonry/config/common"
import "github.com/opencontrol/compliance-masonry/tools/constants"

type ResourceGetter struct {
import (
"github.com/opencontrol/compliance-masonry/tools/constants"
"github.com/opencontrol/compliance-masonry/lib/opencontrol/versions/base"
"github.com/stretchr/testify/mock"
"github.com/opencontrol/compliance-masonry/lib/common"
)

type Getter struct {
mock.Mock
}

// GetLocalResources provides a mock function with given fields: source, resources, destination, subfolder, recursively, worker, resourceType
func (_m *ResourceGetter) GetLocalResources(source string, resources []string, destination string, subfolder string, recursively bool, worker *common.ConfigWorker, resourceType constants.ResourceType) error {
func (_m *Getter) GetLocalResources(source string, resources []string, destination string, subfolder string, recursively bool, worker *base.Worker, resourceType constants.ResourceType) error {
ret := _m.Called(source, resources, destination, subfolder, recursively, worker, resourceType)

var r0 error
if rf, ok := ret.Get(0).(func(string, []string, string, string, bool, *common.ConfigWorker, constants.ResourceType) error); ok {
if rf, ok := ret.Get(0).(func(string, []string, string, string, bool, *base.Worker, constants.ResourceType) error); ok {
r0 = rf(source, resources, destination, subfolder, recursively, worker, resourceType)
} else {
r0 = ret.Error(0)
Expand All @@ -24,11 +26,11 @@ func (_m *ResourceGetter) GetLocalResources(source string, resources []string, d
}

// GetRemoteResources provides a mock function with given fields: destination, subfolder, worker, entries
func (_m *ResourceGetter) GetRemoteResources(destination string, subfolder string, worker *common.ConfigWorker, entries []common.Entry) error {
func (_m *Getter) GetRemoteResources(destination string, subfolder string, worker *base.Worker, entries []common.Entry) error {
ret := _m.Called(destination, subfolder, worker, entries)

var r0 error
if rf, ok := ret.Get(0).(func(string, string, *common.ConfigWorker, []common.Entry) error); ok {
if rf, ok := ret.Get(0).(func(string, string, *base.Worker, []common.Entry) error); ok {
r0 = rf(destination, subfolder, worker, entries)
} else {
r0 = ret.Error(0)
Expand Down
2 changes: 1 addition & 1 deletion lib/opencontrol/resources/resources_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resources_test
package resources

import (
. "github.com/onsi/ginkgo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/opencontrol/compliance-masonry/tools/constants"
"gopkg.in/yaml.v2"
"log"
"github.com/opencontrol/compliance-masonry/lib/common"
)

const (
Expand All @@ -29,9 +30,9 @@ type Schema struct {

// Dependencies contains all the dependencies for the system
type Dependencies struct {
Certifications []resources.Entry `yaml:"certifications"`
Systems []resources.Entry `yaml:",flow"`
Standards []resources.Entry `yaml:",flow"`
Certifications []common.Entry `yaml:"certifications"`
Systems []common.Entry `yaml:",flow"`
Standards []common.Entry `yaml:",flow"`
}

// Metadata contains metadata about the system.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
"github.com/stretchr/testify/assert"
"github.com/opencontrol/compliance-masonry/config/common"
"github.com/opencontrol/compliance-masonry/config/common/resources"
"github.com/opencontrol/compliance-masonry/config/common/resources/mocks"
"errors"

"errors"
"github.com/opencontrol/compliance-masonry/tools/constants"
"github.com/opencontrol/compliance-masonry/lib/opencontrol/versions/base"
"github.com/opencontrol/compliance-masonry/lib/opencontrol/resources"
"github.com/opencontrol/compliance-masonry/lib/opencontrol/resources/mocks"
"github.com/opencontrol/compliance-masonry/lib/common"
)

var _ = Describe("Schema", func() {
Expand Down Expand Up @@ -50,8 +52,8 @@ dependencies:
revision: master
`),
Schema{
resourceGetter: resources.VCSAndLocalFSGetter{},
Base: common.Base{SchemaVersion: "1.0.0"},
resourceGetter: resources.NewVCSAndLocalGetter(),
Base: base.Base{SchemaVersion: "1.0.0"},
Name: "test",
Meta: Metadata{
Description: "A system to test parsing",
Expand Down Expand Up @@ -117,18 +119,18 @@ dependencies:

Describe("Getting resources", func(){
var (
getter *mocks.ResourceGetter
getter *mocks.Getter
dependentStandards, dependentCertifications, dependentComponents []common.Entry
certifications, standards, components []string
worker *common.ConfigWorker
worker *base.Worker
dependencies Dependencies
destination = "."
expectedError error
s Schema
)
BeforeEach(func(){
getter = new(mocks.ResourceGetter)
worker = new(common.ConfigWorker)
getter = new(mocks.Getter)
worker = new(base.Worker)
dependencies = Dependencies{Certifications: dependentCertifications, Systems: dependentComponents, Standards: dependentStandards}
s = Schema{resourceGetter: getter, Dependencies: dependencies, Certifications: certifications, Standards: standards, Components: components}
})
Expand Down

0 comments on commit a9eaa8d

Please sign in to comment.