Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
efortin committed Jan 26, 2021
1 parent f1d82f9 commit b886202
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 56 deletions.
3 changes: 1 addition & 2 deletions internal/ansible/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ type Play struct {
func (play *Play) AllTags() (tags *utils.Set) {
tags = utils.NewSet()
for _, role := range play.Roles {
tags = tags.Concat(role.AllTags().List())

tags.Concat(role.AllTags().List())
}
tags.Concat(play.Tags.List())
return
Expand Down
29 changes: 13 additions & 16 deletions internal/ansible/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Playbook struct {
func (playbook *Playbook) AllTags() (tags *utils.Set) {
tags = utils.NewSet()
for _, play := range playbook.Plays {
fmt.Println("tagsplayyy are", play.AllTags().List())
tags.Concat(play.AllTags().List())
}
return
Expand All @@ -34,7 +35,7 @@ func (playbook *Playbook) RelativePath() string {
}

// Unmarshall a playbook from file
func (p *playbooks) unmarshallFromPath(playbookPath string) (playbook *Playbook, err error) {
func (p *playbooks) unmarshallFromPath(playbookPath string, rootPath string) (playbook *Playbook, err error) {
// Try to check playbook content
binData, err := ioutil.ReadFile(playbookPath)

Expand All @@ -59,6 +60,12 @@ func (p *playbooks) unmarshallFromPath(playbookPath string) (playbook *Playbook,
log.Debug("No play found inside the playbook: ", playbookPath)
return
}

for _, play := range playbook.Plays {
for _, role := range play.Roles {
role.LoadFromPath(rootPath)
}
}
return
}

Expand Down Expand Up @@ -86,37 +93,27 @@ func (p *playbooks) LoadFromPath(rootPath string) (result []Playbook, err error)
}

// Try to check playbook content
playbook, err := Playbooks.unmarshallFromPath(osPathname)
playbook, err := Playbooks.unmarshallFromPath(osPathname, rootPath)

if err != nil {
return nil
}

// Browse Role Tags
for _, play := range playbook.Plays {

allTags.Concat(play.AllTags().List())
for _, role := range play.Roles {
err := role.LoadFromPath(rootPath)
if err != nil {
log.Warn("Cannot load role: ", utils.WrapRed(err.Error()))
} else {
allTags.Concat(role.AllTags().List())
}
}
}

playbook.absolutePath = osPathname
playbook.rootPath = &rootPath

result = append(result, *playbook)
log.Debug("Available tags for playbook", utils.WrapGrey(osPathname), " are: ", playbook.AllTags().List())
return nil
},
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
return godirwalk.SkipNode
},
Unsorted: true,
})

for _, play := range result {
allTags.Concat(play.AllTags().List())
}
return
}
33 changes: 16 additions & 17 deletions internal/ansible/playbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ansible

import (
"github.com/ca-gip/dploy/internal/utils"
"github.com/go-test/deep"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"testing"
Expand Down Expand Up @@ -40,19 +39,9 @@ func Test(t *testing.T) {
assert.NotEmpty(t, plays)

//deep.CompareUnexportedFields = false
if diff := deep.Equal(plays[0], expectedValidPlaybook1); diff != nil {
t.Error(diff)
}
assert.Equal(t, plays[0], expectedValidPlaybook1)

// Not so deep ?
if diff := deep.Equal(plays[0].Roles[0].Tags.List(), expectedValidPlaybook1.Roles[0].Tags.List()); diff != nil {
t.Error(diff)
}
utils.DeepEqual(t, plays[0], expectedValidPlaybook1)
utils.DeepEqual(t, plays[0].Roles[0].Tags.List(), expectedValidPlaybook1.Roles[0].Tags.List())

if diff := deep.Equal(plays[0].Roles[0].Tags.List(), expectedValidPlaybook1.Roles[0].Tags.List()); diff != nil {
t.Error(diff)
}
})

t.Run("with two different play should be different deep.Equals", func(t *testing.T) {
Expand All @@ -78,10 +67,20 @@ func Test(t *testing.T) {
},
Tags: *utils.NewSetFromSlice("right", "Right"),
}
// Not so deep ?
if diff := deep.Equal(left, right); len(diff) != 0 {
t.Error(diff)
}
utils.NotDeepEqual(t, left, right)
})

t.Run("with a task and task tag should return all task ( play, role, task's tags)", func(t *testing.T) {

playbooks, err := Playbooks.LoadFromPath(utils.ProjectMultiLevelPath)
assert.Nil(t, err)
assert.NotNil(t, playbooks)
assert.Len(t, playbooks, 1)
assert.NotEmpty(t, playbooks)
assert.NotEmpty(t, playbooks[0].AllTags())
assert.Contains(t, playbooks[0].AllTags().List(), "playtag1")
assert.Contains(t, playbooks[0].AllTags().List(), "test1-tag")

})

}
7 changes: 5 additions & 2 deletions internal/ansible/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ func (role *Role) AllTags() (tags *utils.Set) {
tags = utils.NewSet()
for _, task := range role.Tasks {
tags.Concat(task.Tags.List())
log.Debug("tasktag !", task.Tags, "role:", task.Name)

}
log.Trace("tags:::", tags.List())
log.Debug("tags:::", tags.List())
tags.Concat(role.Tags.List())
return
}
Expand Down Expand Up @@ -61,12 +63,13 @@ func (role *Role) LoadFromPath(rootPath string) (err error) {
if err != nil {
log.Warn("Error during role parsing: ", utils.WrapRed(osPathname), ". More info in trace level.")
log.Trace("Err:", err.Error())
return nil
}

for _, task := range roleTasks.Tasks {
role.Tasks = append(role.Tasks, task)
}
log.Trace("Available tags for role ", utils.WrapGrey(osPathname), " are: ", role.AllTags().List())
log.Debug("Available tags for role ", utils.WrapGrey(osPathname), " are: ", role.AllTags().List())
return nil
},
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
Expand Down
22 changes: 14 additions & 8 deletions internal/ansible/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ansible

import (
"github.com/ca-gip/dploy/internal/utils"
"github.com/go-test/deep"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"testing"
Expand All @@ -11,7 +10,7 @@ import (
var simpleRole = Role{
Name: "existing-role-1",
Tasks: nil,
Tags: utils.Set{},
Tags: *utils.NewSetFromSlice("role-tags-1"),
}

func init() {
Expand All @@ -25,9 +24,7 @@ func TestRole_ReadRoleTasks(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, simpleRole.Tasks)
assert.NotEmpty(t, simpleRole.Tasks)
if diff := deep.Equal([]string{"test1-tag", "test2-tag"}, simpleRole.AllTags().List()); len(diff) != 0 {
t.Error(diff)
}
utils.DeepEqual(t, []string{"test1-tag", "test2-tag"}, simpleRole.AllTags().List())
})

t.Run("with a valid path and simple data should have good tags", func(t *testing.T) {
Expand All @@ -36,8 +33,17 @@ func TestRole_ReadRoleTasks(t *testing.T) {
assert.NotNil(t, simpleRole.Tasks)
assert.NotEmpty(t, simpleRole.Tasks)

if diff := deep.Equal([]string{"test1-tag", "test2-tag"}, simpleRole.AllTags().List()); len(diff) != 0 {
t.Error(diff)
}
utils.DeepEqual(t, []string{"test-tag", "test2-tag"}, simpleRole.AllTags().List())
})
}

func TestRole_AllTags(t *testing.T) {
t.Run("with a valid path and simple data should have good tags", func(t *testing.T) {
err := simpleRole.LoadFromPath(utils.ProjectMultiLevelPath)
assert.Nil(t, err)
assert.NotNil(t, simpleRole.Tasks)
assert.NotEmpty(t, simpleRole.Tasks)

utils.DeepEqual(t, []string{"test1-tag", "test2-tag"}, simpleRole.AllTags().List())
})
}
4 changes: 2 additions & 2 deletions internal/ansible/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestTask(t *testing.T) {
Tags: *utils.NewSetFromSlice("tasktag2"),
}

utils.DeepNotEqual(t, left, right)
utils.NotDeepEqual(t, left, right)
})

t.Run("with two different task name should fail", func(t *testing.T) {
Expand All @@ -58,7 +58,7 @@ func TestTask(t *testing.T) {
Tags: *utils.NewSetFromSlice("tasktag1"),
}

utils.DeepNotEqual(t, left, right)
utils.NotDeepEqual(t, left, right)
})

t.Run("without name should have tags", func(t *testing.T) {
Expand Down
75 changes: 75 additions & 0 deletions internal/utils/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package utils

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestSet_Concat(t *testing.T) {

t.Run("with 2 lists should contains 2 items ordered", func(t *testing.T) {
left := NewSetFromSlice("l1")
right := NewSetFromSlice("r1")
concat := left.Concat(right.List())

assert.NotNil(t, concat)
assert.NotEmpty(t, concat.List())
DeepEqual(t, []string{"l1", "r1"}, concat.List())
})

t.Run("with 2 lists unordered should contains 2 items ordered", func(t *testing.T) {
left := NewSetFromSlice("l1")
right := NewSetFromSlice("r1")
concat := right.Concat(left.List())

assert.NotNil(t, concat)
assert.NotEmpty(t, concat.List())
DeepEqual(t, []string{"l1", "r1"}, concat.List())
})

t.Run("with 2 lists should be ordered", func(t *testing.T) {
left := NewSetFromSlice("l1")
right := NewSetFromSlice("r1")
concat := right.Concat(left.List())

assert.NotNil(t, concat)
assert.NotEmpty(t, concat.List())
DeepEqual(t, []string{"l1", "r1"}, concat.List())
})

t.Run("with 3 lists should be ordered", func(t *testing.T) {
left := NewSetFromSlice("l1")
right := NewSetFromSlice("r1")
center := NewSetFromSlice("c1")
right.Concat(left.List())
right.Concat(center.List())

assert.NotNil(t, right)
assert.NotEmpty(t, right.List())
DeepEqual(t, []string{"c1", "l1", "r1"}, right.List())
})

}

func TestSet_Add(t *testing.T) {

t.Run("with 1 item should be ordered", func(t *testing.T) {
actual := NewSet()
actual.Add("s1")

assert.NotNil(t, actual)
assert.NotEmpty(t, actual.List())
DeepEqual(t, []string{"s1"}, actual.List())
})

t.Run("with 3 item should be ordered", func(t *testing.T) {
actual := NewSet()
actual.Add("s1")
actual.Add("s2")
actual.Add("s3")

assert.NotNil(t, actual)
assert.NotEmpty(t, actual.List())
DeepEqual(t, []string{"s1", "s2", "s3"}, actual.List())
})
}
2 changes: 1 addition & 1 deletion internal/utils/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func DeepEqual(t *testing.T, expected interface{}, actual interface{}) {
}
}

func DeepNotEqual(t *testing.T, expected interface{}, actual interface{}) {
func NotDeepEqual(t *testing.T, expected interface{}, actual interface{}) {
if eq := reflect.DeepEqual(expected, actual); eq {
t.Errorf("\nStruct are both \n\t%+v", expected)
}
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

func main() {

log.SetLevel(log.DebugLevel)
home, _ := os.UserHomeDir()
path := fmt.Sprintf("%s/%s", home, "Projects/ansible-mock")
k8s := ansible.Projects.LoadFromPath(path)
Expand Down
14 changes: 7 additions & 7 deletions test/projectMultiLevel/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
roles:
- { role: existing-role-1, tags: [ "existing-role", "role-1" ] }
tags: playtag1

- hosts: unexisting-group
gather_facts: yes
serial: 100%
roles:
- { role: missingRole, tags: [ "missing-role", "role-2" ] }
tags: playtag2
#
#- hosts: unexisting-group
# gather_facts: yes
# serial: 100%
# roles:
# - { role: missingRole, tags: [ "missing-role", "role-2" ] }
# tags: playtag2
2 changes: 1 addition & 1 deletion test/projectSimpleLevel/test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
- hosts: worker
gather_facts: yes
roles:
- { role: existing-role-1, tags: [ "existing-role", "role-1" ] }
- { role: existing-role-1, tags: [ "existing-role", "role-1", "role-tags-1" ] }
tags: playtag1

- hosts: unexisting-group
Expand Down

0 comments on commit b886202

Please sign in to comment.