Skip to content

Commit

Permalink
Generate mocks with mockgen
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Jul 16, 2019
1 parent e3e5e28 commit 95bd261
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mockgen:
go get github.com/golang/mock/gomock
go install github.com/golang/mock/mockgen
rm targets/target_mock_test.go
mockgen -source targets/targets.go -destination targets/target_mock_test.go -package targets

mockgen -source targets/targets.go -destination targets/targets_mock.go -package targets -self_package github.com/coveooss/credentials-sync/targets
mockgen -source credentials/sources.go -destination credentials/sources_mock.go -package credentials -self_package github.com/coveooss/credentials-sync/credentials
33 changes: 27 additions & 6 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

"gopkg.in/yaml.v2"

"github.com/coveooss/credentials-sync/credentials"
"github.com/coveooss/credentials-sync/targets"
"github.com/coveooss/credentials-sync/sync"
log "github.com/sirupsen/logrus"

Expand All @@ -32,14 +34,15 @@ var rootCmd = &cobra.Command{
Support Jenkins only for now.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
configuration = sync.NewConfiguration()
configurationDict := map[string]interface{}{}
sourcesConfiguration := &credentials.SourcesConfiguration{}
targetsConfiguration := &targets.Configuration{}
var (
err error
fileContent []byte
configurationDict = map[string]interface{}{}
configurationFile = viper.GetString("config")
err error
fileContent []byte
)

configurationFile := viper.GetString("config")

if configurationFile == "" {
return fmt.Errorf("A configuration file must be defined")
}
Expand Down Expand Up @@ -74,7 +77,25 @@ var rootCmd = &cobra.Command{
if err = yaml.Unmarshal(fileContent, configurationDict); err != nil {
return err
}
return mapstructure.Decode(configurationDict, configuration)

// Get the config
if err = mapstructure.Decode(configurationDict, configuration); err != nil {
return err
}

// Get sources from config
if err = mapstructure.Decode(configurationDict["sources"], sourcesConfiguration); err != nil {
return err
}
configuration.SetSources(sourcesConfiguration)

// Get targets from config
if err = mapstructure.Decode(configurationDict["targets"], targetsConfiguration); err != nil {
return err
}
configuration.SetTargets(targetsConfiguration)

return nil
},
}

Expand Down
6 changes: 6 additions & 0 deletions credentials/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ type SourcesConfiguration struct {
credentialsList []Credentials
}

type SourceCollection interface {
AllSources() []Source
Credentials() ([]Credentials, error)
ValidateConfiguration() bool
}

// AllSources returns all configured sources in a single list
func (sc *SourcesConfiguration) AllSources() []Source {
sources := []Source{}
Expand Down
142 changes: 142 additions & 0 deletions credentials/sources_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions sync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

// Configuration represents the parsed configuration file given to the application
type Configuration struct {
CredentialsToDelete []string `mapstructure:"credentials_to_delete"`
Sources *credentials.SourcesConfiguration `mapstructure:"sources"`
StopOnError bool `mapstructure:"stop_on_error"`
TargetParallelism int `mapstructure:"target_parallelism"`
Targets *targets.Configuration `mapstructure:"targets"`
CredentialsToDelete []string `mapstructure:"credentials_to_delete"`
Sources credentials.SourceCollection `mapstructure:"-"`
StopOnError bool `mapstructure:"stop_on_error"`
TargetParallelism int `mapstructure:"target_parallelism"`
Targets targets.TargetCollection `mapstructure:"-"`
}

// NewConfiguration creates a new configuration with default values
Expand All @@ -23,6 +23,14 @@ func NewConfiguration() *Configuration {
}
}

func (config *Configuration) SetSources(sources credentials.SourceCollection) {
config.Sources = sources
}

func (config *Configuration) SetTargets(targets targets.TargetCollection) {
config.Targets = targets
}

// Sync syncs credentials from the configured sources to the configured targets
func (config *Configuration) Sync() {
// Start reading credentials
Expand Down
38 changes: 38 additions & 0 deletions sync/target_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package sync

import (
"testing"

"github.com/coveooss/credentials-sync/credentials"
"github.com/coveooss/credentials-sync/targets"
"github.com/golang/mock/gomock"
)

func setSourceMock(t *testing.T, config *Configuration) *credentials.MockSource {
ctrl := gomock.NewController(t)
source := credentials.NewMockSource(ctrl)
sourceCollection := credentials.NewMockSourceCollection(ctrl)
sourceCollection.EXPECT().AllSources().Return([]credentials.Source{source}).AnyTimes()

config.SetSources(sourceCollection)
return source
}

func setTargetMock(t *testing.T, config *Configuration) *targets.MockTarget {
ctrl := gomock.NewController(t)
target := targets.NewMockTarget(ctrl)
targetCollection := targets.NewMockTargetCollection(ctrl)
targetCollection.EXPECT().AllTargets().Return([]targets.Target{target}).AnyTimes()

config.SetTargets(targetCollection)
return target
}

func TestDeleteListOfCredentials(t *testing.T) {
config := &Configuration{}
setSourceMock(t, config)
target := setTargetMock(t, config)
target.EXPECT().GetExistingCredentials().Return([]string{"test123"}).AnyTimes()

target.EXPECT().DeleteCredentials("test123").Return(nil)
}
11 changes: 1 addition & 10 deletions targets/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (jenkins *JenkinsTarget) UpdateCredentials(cred credentials.Credentials) er
if jenkinsCred == nil {
return fmt.Errorf("Unable to create jenkins credentials from %s", cred.GetID())
}
if jenkins.hasCredentials(cred) {
if HasCredential(jenkins, cred.GetID()) {
return jenkins.credentialsManager.Update(credentialsDomain, cred.GetID(), jenkinsCred)
}
return jenkins.credentialsManager.Add(credentialsDomain, jenkinsCred)
Expand All @@ -101,15 +101,6 @@ func (jenkins *JenkinsTarget) ValidateConfiguration() bool {
return true
}

func (jenkins *JenkinsTarget) hasCredentials(cred credentials.Credentials) bool {
for _, id := range jenkins.existingCredentials {
if cred.GetID() == id {
return true
}
}
return false
}

func toJenkinsCredential(creds credentials.Credentials) interface{} {
switch creds.(type) {
case *credentials.AmazonWebServicesCredentials:
Expand Down
16 changes: 16 additions & 0 deletions targets/jenkins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package targets

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestJenkinsToString(t *testing.T) {
jenkins := &JenkinsTarget{
Base: Base{Name: "targetName", Tags: map[string]string{"my_tag": "tag_value"}},
URL: "test.com",
}

assert.Equal(t, "targetName [Tags: my_tag=tag_value] (Jenkins) - test.com", jenkins.ToString())
}
5 changes: 5 additions & 0 deletions targets/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func HasCredential(target Target, id string) bool {
return false
}

type TargetCollection interface {
AllTargets() []Target
ValidateConfiguration() bool
}

// Configuration contains all configured targets
type Configuration struct {
JenkinsTargets []*JenkinsTarget `mapstructure:"jenkins"`
Expand Down
Loading

0 comments on commit 95bd261

Please sign in to comment.