1
+ package gorjun
2
+
3
+ import (
4
+ "fmt"
5
+ "io/ioutil"
6
+ "testing"
7
+ "encoding/json"
8
+ "net/http"
9
+ "github.com/stretchr/testify/assert"
10
+ "strconv"
11
+ "sort"
12
+ "time"
13
+ "math/rand"
14
+ )
15
+
16
+ func TestListUserFiles (t * testing.T ) {
17
+ g := NewGorjunServer ()
18
+ err := g .AuthenticateUser ()
19
+ if err != nil {
20
+ t .Errorf ("Authnetication failure: %v" , err )
21
+ }
22
+
23
+ d1 := []byte ("This is a test file\n " )
24
+ ioutil .WriteFile ("/tmp/libgorjun-test" , d1 , 0644 )
25
+ id , err := g .Upload ("/tmp/libgorjun-test" , "raw" )
26
+ if err != nil {
27
+ t .Errorf ("Failed to upload: %v" , err )
28
+ }
29
+ fmt .Printf ("File ID: %s" , id )
30
+
31
+ flist , err := g .ListUserFiles ()
32
+ if err != nil {
33
+ t .Errorf ("Failed to retrieve user files: %v" , err )
34
+ }
35
+ if len (flist ) <= 0 {
36
+ t .Errorf ("Resulting array is empty" )
37
+ }
38
+ }
39
+
40
+ func TestUploadRaw (t * testing.T ) {
41
+ g := NewGorjunServer ()
42
+ err := g .AuthenticateUser ()
43
+ if err != nil {
44
+ t .Errorf ("Authnetication failure: %v" , err )
45
+ }
46
+ d1 := []byte ("This is a test file\n " )
47
+ ioutil .WriteFile ("/tmp/libgorjun-test" , d1 , 0644 )
48
+ id , err := g .Upload ("/tmp/libgorjun-test" ,"raw" )
49
+ if err != nil {
50
+ t .Errorf ("Failed to upload: %v" , err )
51
+ }
52
+ fmt .Printf ("File ID: %s" , id )
53
+ }
54
+
55
+ func TestGetFileByName (t * testing.T ) {
56
+ g := NewGorjunServer ()
57
+ file , err := g .GetFileByName ("libgorjun-test" ,"raw" )
58
+ if err != nil {
59
+ t .Errorf ("Failed to get file by name: %s" , err )
60
+ }
61
+ fmt .Printf ("File: %+v\n " , file )
62
+ }
63
+
64
+ func TestRemoveFile (t * testing.T ) {
65
+ g := NewGorjunServer ()
66
+ err := g .AuthenticateUser ()
67
+ if err != nil {
68
+ t .Errorf ("Authnetication failure: %v" , err )
69
+ }
70
+ err = g .RemoveFile ("libgorjun-test" ,"raw" )
71
+ if err != nil {
72
+ t .Errorf ("Failed to remove file: %v" , err )
73
+ }
74
+ }
75
+
76
+ func TestRemoveTemplate (t * testing.T ) {
77
+ g := NewGorjunServer ()
78
+ err := g .AuthenticateUser ()
79
+ if err != nil {
80
+ t .Errorf ("Authnetication failure: %v" , err )
81
+ }
82
+ id , err := g .Upload ("data/abdysamat-apache-subutai-template_4.0.0_amd64.tar.gz" ,"template" )
83
+ if err != nil {
84
+ t .Errorf ("Failed to upload: %v" , err )
85
+ }
86
+ fmt .Printf ("Template uploaded successfully, id : %s\n " , id )
87
+ //err = g.RemoveFileByID(id,"template")
88
+ //if err != nil {
89
+ // t.Errorf("Failed to remove file: %v", err)
90
+ //}
91
+ fmt .Printf ("Template removed successfully, id : %s\n " , id )
92
+ }
93
+
94
+
95
+ //TestGorjunServer_CheckTemplatesSignatureExist will check signatures of
96
+ //templates, all templates should have more than zero signatures
97
+ func TestGorjunServer_CheckTemplatesSignatureExist (t * testing.T ) {
98
+ g := NewGorjunServer ();
99
+ resp , err := http .Get (fmt .Sprintf ("http://%s/kurjun/rest/template/list" , g .Hostname ))
100
+ data , err := ioutil .ReadAll (resp .Body )
101
+ resp .Body .Close ()
102
+ if err != nil {
103
+ fmt .Errorf ("Failed to read body from %s: %v" , g .Hostname , err )
104
+ }
105
+ var templates []GorjunFile
106
+ err = json .Unmarshal (data , & templates )
107
+ for _ ,template := range templates {
108
+ fmt .Printf ("ID of templates is %s\n " ,template .ID )
109
+ resp , err := http .Get (fmt .Sprintf ("http://%s/kurjun/rest/template/info?id=%s" , g .Hostname ,template .ID ))
110
+ data , err := ioutil .ReadAll (resp .Body )
111
+ resp .Body .Close ()
112
+ if err != nil {
113
+ fmt .Errorf ("Failed to read body from %s: %v" , g .Hostname , err )
114
+ }
115
+ var templateInfo []GorjunFile
116
+ err = json .Unmarshal (data , & templateInfo )
117
+ fmt .Printf ("Len of signatture is %d\n " ,len (templateInfo [0 ].Signature ))
118
+ assert .NotEqual (t , len (templateInfo [0 ].Signature ), 0 , "Template with ID = %s should have signature\n " ,template .ID )
119
+ }
120
+ }
121
+
122
+ func Shuffle (a []string ) {
123
+ for i := range a {
124
+ j := rand .Intn (i + 1 )
125
+ a [i ], a [j ] = a [j ], a [i ]
126
+ }
127
+ }
128
+ //TestGorjunServer_GetLatestTemplateByVersion will upload templates
129
+ //with different version in random order, info rest should return latest by version
130
+ //if several version exits it should return by date
131
+ func TestGorjunServer_GetLatestTemplateByVersion (t * testing.T ) {
132
+ g := NewGorjunServer ()
133
+ err := g .AuthenticateUser ()
134
+ if err != nil {
135
+ t .Errorf ("Authnetication failure: %v" , err )
136
+ }
137
+ var dates []int
138
+ templateVersions := []string {"0.1.6" , "0.1.7" , "0.1.9" , "0.1.10" ,"0.1.11" }
139
+ rand .Seed (time .Now ().UnixNano ())
140
+ Shuffle (templateVersions )
141
+
142
+ for _ , version := range templateVersions {
143
+ id , err := g .Upload ("data/nginx-subutai-template_" + version + "_amd64.tar.gz" ,"template" )
144
+ if err != nil {
145
+ t .Errorf ("Failed to upload: %v" , err )
146
+ }
147
+ fmt .Printf ("Template uploaded successfully, id : %s\n " , id )
148
+
149
+ resp , err := http .Get (fmt .Sprintf ("http://%s/kurjun/rest/template/info?id=%s" , g .Hostname ,id ))
150
+ data , err := ioutil .ReadAll (resp .Body )
151
+ resp .Body .Close ()
152
+ if err != nil {
153
+ fmt .Errorf ("Failed to read body from %s: %v" , g .Hostname , err )
154
+ }
155
+ var template []GorjunFile
156
+ err = json .Unmarshal (data , & template )
157
+ timestamp , err := strconv .Atoi (template [0 ].Timestamp )
158
+ dates = append (dates , timestamp )
159
+ time .Sleep (100 * time .Millisecond )
160
+ }
161
+ resp , err := http .Get (fmt .Sprintf ("http://%s/kurjun/rest/template/info?name=%s" , g .Hostname ,"nginx" ))
162
+ data , err := ioutil .ReadAll (resp .Body )
163
+ resp .Body .Close ()
164
+ if err != nil {
165
+ fmt .Errorf ("Failed to read body from %s: %v" , g .Hostname , err )
166
+ }
167
+ var template []GorjunFile
168
+ err = json .Unmarshal (data , & template )
169
+ assert .Equal (t ,"0.1.11" ,template [0 ].Version )
170
+ }
171
+
172
+ //TestGorjunServer_GetLatestRaw will upload raw
173
+ //files , info rest should return by date
174
+ func TestGorjunServer_GetLatestRaw (t * testing.T ) {
175
+ g := NewGorjunServer ()
176
+ err := g .AuthenticateUser ()
177
+ if err != nil {
178
+ t .Errorf ("Authnetication failure: %v" , err )
179
+ }
180
+ var dates []int
181
+ rawNumber := 10
182
+
183
+ for i := 1 ; i <= rawNumber ; i ++ {
184
+ id , err := g .Upload ("data/abdysamat-apache-subutai-template_4.0.0_amd64.tar.gz" ,"raw" )
185
+ if err != nil {
186
+ t .Errorf ("Failed to upload: %v" , err )
187
+ }
188
+ fmt .Printf ("Raw uploaded successfully, id : %s\n " , id )
189
+
190
+ resp , err := http .Get (fmt .Sprintf ("http://%s/kurjun/rest/raw/info?id=%s" , g .Hostname ,id ))
191
+ data , err := ioutil .ReadAll (resp .Body )
192
+ resp .Body .Close ()
193
+ if err != nil {
194
+ fmt .Errorf ("Failed to read body from %s: %v" , g .Hostname , err )
195
+ }
196
+ var template []GorjunFile
197
+ err = json .Unmarshal (data , & template )
198
+ timestamp , err := strconv .Atoi (template [0 ].Timestamp )
199
+ dates = append (dates , timestamp )
200
+ time .Sleep (101 * time .Millisecond )
201
+ }
202
+ sort .Ints (dates )
203
+ resp , err := http .Get (fmt .Sprintf ("http://%s/kurjun/rest/raw/info?name=%s" , g .Hostname ,"abdysamat-apache" ))
204
+ data , err := ioutil .ReadAll (resp .Body )
205
+ resp .Body .Close ()
206
+ if err != nil {
207
+ fmt .Errorf ("Failed to read body from %s: %v" , g .Hostname , err )
208
+ }
209
+ var template []GorjunFile
210
+ err = json .Unmarshal (data , & template )
211
+ timestamp , err := strconv .Atoi (template [0 ].Timestamp )
212
+ fmt .Println (dates )
213
+ fmt .Println (timestamp )
214
+ fmt .Println (dates [rawNumber - 1 ])
215
+ assert .Equal (t ,timestamp ,dates [rawNumber - 1 ])
216
+ }
217
+
218
+ //TestGorjunServer_SameTemplateUpload will upload
219
+ //same template twice, old template should deleted
220
+ func TestGorjunServer_SameTemplateUpload (t * testing.T ) {
221
+ g := NewGorjunServer ()
222
+ templateVersions := []string {"0.1.6" , "0.1.7" , "0.1.9" , "0.1.10" ,"0.1.11" }
223
+ for _ , version := range templateVersions {
224
+ resp , _ := http .Get (fmt .Sprintf ("http://%s/kurjun/rest/template/info?name=%s&version=%s" , g .Hostname ,"nginx" ,version ))
225
+ if resp .StatusCode != http .StatusOK {
226
+ fmt .Println ("Test can't be run because templates should uploaded" )
227
+ return
228
+ }
229
+ }
230
+ TestGorjunServer_GetLatestTemplateByVersion (t )
231
+ resp , err := http .Get (fmt .Sprintf ("http://%s/kurjun/rest/template/list" , g .Hostname ))
232
+ data , err := ioutil .ReadAll (resp .Body )
233
+ resp .Body .Close ()
234
+ if err != nil {
235
+ fmt .Errorf ("Failed to read body from %s: %v" , g .Hostname , err )
236
+ }
237
+ var templateList []GorjunFile
238
+ err = json .Unmarshal (data , & templateList )
239
+
240
+ m := make (map [string ]int )
241
+
242
+ for _ , template := range templateList {
243
+ s := template .Owner [0 ] + template .Name + template .Version
244
+ m [s ]++
245
+ assert .NotEqual (t , m [s ], 2 , "Same template exist twice" ,template .ID )
246
+ }
247
+ }
0 commit comments