forked from mlafeldt/chef-runner
/
resolver_test.go
105 lines (77 loc) · 2.36 KB
/
resolver_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package resolver_test
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/mlafeldt/chef-runner/exec"
. "github.com/mlafeldt/chef-runner/resolver"
"github.com/mlafeldt/chef-runner/util"
"github.com/stretchr/testify/assert"
)
const CookbookPath = "test-cookbooks"
var lastCmd []string
func init() {
exec.SetRunnerFunc(func(args []string) error {
lastCmd = args
return nil
})
}
func TestAutoResolve_Berkshelf(t *testing.T) {
lastCmd = []string{}
util.InTestDir(func() {
ioutil.WriteFile("Berksfile", []byte{}, 0644)
os.MkdirAll(CookbookPath, 0755)
AutoResolve(CookbookPath)
})
assert.Equal(t, []string{"ruby", "-e"}, lastCmd[:2])
assert.True(t, strings.Contains(lastCmd[2], `require "berkshelf"`))
assert.True(t, strings.Contains(lastCmd[2],
fmt.Sprintf(`.vendor("%s")`, CookbookPath)))
}
func TestAutoResolve_Librarian(t *testing.T) {
lastCmd = []string{}
util.InTestDir(func() {
ioutil.WriteFile("Cheffile", []byte{}, 0644)
os.MkdirAll(CookbookPath, 0755)
assert.NoError(t, AutoResolve(CookbookPath))
})
assert.Equal(t, []string{"librarian-chef", "install", "--path", CookbookPath}, lastCmd)
}
func TestAutoResolve_Dir(t *testing.T) {
lastCmd = []string{}
util.InTestDir(func() {
ioutil.WriteFile("metadata.rb", []byte(`name "cats"`), 0644)
assert.NoError(t, AutoResolve(CookbookPath))
})
assert.Equal(t, []string{"rsync", "--archive", "--delete", "--compress",
"metadata.rb", CookbookPath + "/cats"}, lastCmd)
}
func TestAutoResolve_DirUpdate(t *testing.T) {
lastCmd = []string{}
util.InTestDir(func() {
ioutil.WriteFile("metadata.rb", []byte(`name "cats"`), 0644)
ioutil.WriteFile("Berksfile", []byte{}, 0644)
os.MkdirAll(CookbookPath, 0755)
assert.NoError(t, AutoResolve(CookbookPath))
})
assert.Equal(t, []string{"rsync", "--archive", "--delete", "--compress",
"metadata.rb", CookbookPath + "/cats"}, lastCmd)
}
func TestAutoResolve_NoCookbooks(t *testing.T) {
lastCmd = []string{}
util.InTestDir(func() {
assert.EqualError(t, AutoResolve(CookbookPath),
"cookbooks could not be found")
})
assert.Equal(t, []string{}, lastCmd)
}
func TestResolve_Librarian(t *testing.T) {
lastCmd = []string{}
util.InTestDir(func() {
os.MkdirAll(CookbookPath, 0755)
assert.NoError(t, Resolve("librarian", CookbookPath))
})
assert.Equal(t, []string{"librarian-chef", "install", "--path", CookbookPath}, lastCmd)
}