-
Notifications
You must be signed in to change notification settings - Fork 28
/
icon_test.go
131 lines (111 loc) · 3.11 KB
/
icon_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package draft
//go:generate statik -f -p statik -src=./assets
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/emicklei/dot"
)
func TestCopyFileTo(t *testing.T) {
dir, err := ioutil.TempDir("", "icons")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
if err := os.Mkdir(filepath.Join(dir, "default"), os.ModePerm); err != nil {
t.Fatal(err)
}
tests := []struct {
src string
dst string
}{
{"/icons/waf.png", fmt.Sprintf("%s/default/waf.png", dir)},
{"/icons/fun.png", fmt.Sprintf("%s/default/fun.png", dir)},
{"/icons/kub.png", fmt.Sprintf("%s/default/kub.png", dir)},
}
for _, tt := range tests {
t.Run(tt.src, func(t *testing.T) {
if err := copyFileTo(tt.src, tt.dst); err != nil {
t.Fatal(err)
}
if !fileExists(tt.dst) {
t.Errorf("file [%v] does not exists", tt.dst)
}
})
}
}
func TestIconPath(t *testing.T) {
dir, err := ioutil.TempDir("", "icons")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
tests := []struct {
provider string
kind string
want string
}{
{"aws", kindFirewall, fmt.Sprintf("%s/default/waf.png", dir)},
{"aws", kindFunction, fmt.Sprintf("%s/default/fun.png", dir)},
{"google", kindRDB, fmt.Sprintf("%s/default/rdb.png", dir)},
{"google", kindCache, fmt.Sprintf("%s/default/mem.png", dir)},
{"azure", kindDNS, fmt.Sprintf("%s/default/dns.png", dir)},
{"azure", kindCDN, fmt.Sprintf("%s/default/cdn.png", dir)},
}
cfg := NewConfig(IconsPath(dir))
for _, tt := range tests {
t.Run(tt.kind, func(t *testing.T) {
got := iconPath(cfg, Component{Kind: tt.kind})
if got != tt.want {
t.Errorf("got [%v] want [%v]", got, tt.want)
}
})
}
}
func TestIconFigure(t *testing.T) {
dir, err := ioutil.TempDir("", "icons")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
tests := []struct {
provider string
kind string
find string
want bool
}{
{"aws", kindFirewall, fmt.Sprintf(`src="%s/default/waf.png"`, dir), true},
{"aws", kindFunction, fmt.Sprintf(`src="%s/default/fun.png"`, dir), true},
{"google", kindRDB, fmt.Sprintf(`src="%s/default/rdb.png"`, dir), true},
{"google", kindCache, fmt.Sprintf(`src="%s/default/mem.png"`, dir), true},
{"azure", kindDNS, fmt.Sprintf(`src="%s/default/dns.png"`, dir), true},
{"azure", kindCDN, fmt.Sprintf(`src="%s/default/cdn.png"`, dir), true},
}
cfg := NewConfig(IconsPath(dir))
gfx := dot.NewGraph(dot.Directed)
for _, tt := range tests {
com := Component{Kind: tt.kind}
t.Run(tt.find, func(t *testing.T) {
n := render(cfg, com, gfx)
x := fmt.Sprintf("%v", n.AttributesMap.Value("label"))
if got := strings.Contains(x, tt.find); got != tt.want {
t.Errorf("got [%v] want [%v] : %s", got, tt.want, x)
}
})
}
}
// remove tabs and newlines and spaces
func flatten(s string) string {
return strings.Replace((strings.Replace(s, "\n", "", -1)), "\t", "", -1)
}
func verify(src, kv string) bool {
r := regexp.MustCompile(`n1\[(.*)\]`)
if m := r.FindAllStringSubmatch(src, -1); len(m) > 0 {
return strings.Contains(m[0][1], kv)
}
return false
}