-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
168 lines (139 loc) · 3.79 KB
/
client_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"testing"
"path"
"os"
"log"
"strings"
)
var jsonSample = `
{ "query": "ffmpeg", "results":
[{"description":"","is_official":false,"is_trusted":true,"name":"cellofellow/ffmpeg","star_count":1}
,{"description":"","is_official":false,"is_trusted":true,"name":"bfirsh/ffmpeg","star_count":0}
,{"description":"","is_official":false,"is_trusted":true,"name":"robd/aws-ffmpeg","star_count":0}
,{"description":"","is_official":false,"is_trusted":false,"name":"miovision/ffmpeg","star_count":0}
,{"description":"","is_official":false,"is_trusted":false,"name":"paulbrennan/ffmpeg","star_count":0}
,{"description":"","is_official":false,"is_trusted":false,"name":"cmark/ubuntu-ffmpeg","star_count":0}
]}
`
type MockWebClient struct {
}
func (mwc* MockWebClient) Get( url string ) []byte {
var rv []byte
if -1 != strings.Index( url, "index.docker.io" ) {
// Return the JSON
rv = []byte(jsonSample)
} else if -1 != strings.Index( url, "registry.hub" ) {
// Only if bfirsh/ffmpeg
if -1 != strings.Index( url, "bfirsh/ffmpeg" ) {
rv = []byte(FfmpegDockerfile)
} else {
rv = []byte("FROM ubuntu")
}
}
return rv
}
func getTestClient() *Client {
c := new(Client)
c.Http = new(MockWebClient)
c.LoadConfig( path.Join( ".", "test", "fixtures", DEFAULT_CONFIG_FILE ) )
// c.Verbose = true
LoadFfmpegDockerfile()
return c
}
func TestFullCircle( t *testing.T ) {
c := getTestClient()
c.Query( "ffmpeg" )
c.Annotate()
c.Filter( []string{ "quantal" } )
if !c.resultFound( "bfirsh/ffmpeg" ) {
t.Errorf( "Unable to filter for quantal" )
}
if c.resultFound( "miovision/ffmpeg" ) {
t.Errorf( "This should not pass" )
}
if c.resultFound( "nevergonna/happen" ) {
t.Errorf( "Hmm, we should not see this pass" )
}
}
func (c* Client) resultFound( key string ) bool {
found := false
for _, e := range c.Results {
if key == e.Name {
found = true
}
}
return found
}
// func TestFilters( t* testing.T ) {
// c := getTestClient()
// c.Query( "ffmpeg" )
// filters := []string{"libavcodec=src", "python"}
// c.Filter( filters )
// if !c.resultFound( "link/ffmpeg-built" ) {
// t.Errorf( "Unable to find ffmpeg library with python and libavcodec" )
// }
// }
func TestProcessFilter( t* testing.T ) {
td := ProcessFilter( "libavcodec" )
if td.Target != "libavcodec" &&
!td.Src &&
"" == td.Version {
t.Errorf( "Did not process filter correctly" )
}
td = ProcessFilter( "libavcodec:src" )
if td.Target != "libavcodec" &&
td.Src {
t.Errorf( "Did not process filter correctly" )
}
td = ProcessFilter( "libavcodec:2.2" )
if td.Target != "libavcodec" &&
!td.Src &&
"2.2" == td.Version {
t.Errorf( "Did not process filter correctly" )
}
}
var FfmpegDockerfile = ""
func makeFakeImages() []DockerImage {
rv := []DockerImage{}
image := new(DockerImage)
image.Name = "testing/test"
image.Dockerfile = FfmpegDockerfile
rv = append( rv, *image )
return rv
}
func LoadFfmpegDockerfile() {
file, _ := os.Open( path.Join( "test", "fixtures", "bfirst_ffmpeg_dockerfile.txt" ) )
if nil != file {
contents := make([]byte, 1024 )
_, err := file.Read( contents )
if nil == err {
FfmpegDockerfile = string(contents)
}
} else {
log.Fatal( "Cannot load test dockerfile" )
}
}
func TestFilter( t* testing.T ) {
c := getTestClient()
c.Images = makeFakeImages()
c.Filter( []string{ "quantal" } )
if !c.resultFound( "testing/test" ) {
t.Errorf( "Unable to filter for quantal" )
}
if c.resultFound( "nevergonna/happen" ) {
t.Errorf( "Hmm, we should not see this pass" )
}
}
// func (c* Client) Query() {
// var res DockerResults
// err := json.Unmarshal( jsonSample, res )
// if nil == err {
// c.Images = res.Results
// }
// }
// func (c* Client) Annotate() {
// for i, e := range c.Images {
// c.Images[i].Dockerfile = FfmpegDockerfile
// }
// }