-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
replace_test.go
74 lines (56 loc) · 1.66 KB
/
replace_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
package file
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/guumaster/hostctl/pkg/parser"
"github.com/guumaster/hostctl/pkg/types"
)
func TestFile_ReplaceProfile(t *testing.T) {
mem := createBasicFS(t)
f, err := mem.Open("/tmp/etc/hosts")
assert.NoError(t, err)
t.Run("Replace", func(t *testing.T) {
m, err := NewWithFs(f.Name(), mem)
assert.NoError(t, err)
r := strings.NewReader(`4.4.4.4 replaced.loc`)
p, err := parser.ParseProfile(r)
assert.NoError(t, err)
p.Name = "profile1"
p.Status = types.Enabled
err = m.ReplaceProfile(p)
assert.NoError(t, err)
replaced, err := m.GetProfile("profile1")
assert.NoError(t, err)
hosts, err := replaced.GetHostNames("4.4.4.4")
assert.NoError(t, err)
assert.Equal(t, hosts, []string{"replaced.loc"})
})
t.Run("Replace new", func(t *testing.T) {
m, err := NewWithFs(f.Name(), mem)
assert.NoError(t, err)
r := strings.NewReader(`4.4.4.4 replaced.loc`)
p, err := parser.ParseProfile(r)
assert.NoError(t, err)
p.Name = "awesome"
p.Status = types.Enabled
err = m.ReplaceProfile(p)
assert.NoError(t, err)
added, err := m.GetProfile("awesome")
assert.NoError(t, err)
hosts, err := added.GetHostNames("4.4.4.4")
assert.NoError(t, err)
assert.Equal(t, hosts, []string{"replaced.loc"})
})
t.Run("Replace unknown", func(t *testing.T) {
m, err := NewWithFs(f.Name(), mem)
assert.NoError(t, err)
r := strings.NewReader(`4.4.4.4 replaced.loc`)
p, err := parser.ParseProfile(r)
assert.NoError(t, err)
p.Name = types.Default
p.Status = types.Enabled
err = m.ReplaceProfile(p)
assert.EqualError(t, err, types.ErrDefaultProfile.Error())
})
}