OSDN Git Service

Create ossClient.go (#574)
[bytom/vapor.git] / vendor / github.com / aliyun / aliyun-oss-go-sdk / oss / livechannel_test.go
1 package oss
2
3 import (
4         "fmt"
5         "strings"
6         "time"
7
8         . "gopkg.in/check.v1"
9 )
10
11 type OssBucketLiveChannelSuite struct {
12         client *Client
13         bucket *Bucket
14 }
15
16 var _ = Suite(&OssBucketLiveChannelSuite{})
17
18 // SetUpSuite Run once when the suite starts running
19 func (s *OssBucketLiveChannelSuite) SetUpSuite(c *C) {
20         client, err := New(endpoint, accessID, accessKey)
21         c.Assert(err, IsNil)
22         s.client = client
23
24         err = s.client.CreateBucket(bucketName)
25         c.Assert(err, IsNil)
26         time.Sleep(5 * time.Second)
27
28         bucket, err := s.client.Bucket(bucketName)
29         c.Assert(err, IsNil)
30         s.bucket = bucket
31
32         testLogger.Println("test livechannel started...")
33 }
34
35 // TearDownSuite Run once after all tests or benchmarks
36 func (s *OssBucketLiveChannelSuite) TearDownSuite(c *C) {
37         marker := ""
38         for {
39                 result, err := s.bucket.ListLiveChannel(Marker(marker))
40                 c.Assert(err, IsNil)
41
42                 for _, channel := range result.LiveChannel {
43                         err := s.bucket.DeleteLiveChannel(channel.Name)
44                         c.Assert(err, IsNil)
45                 }
46
47                 if result.IsTruncated {
48                         marker = result.NextMarker
49                 } else {
50                         break
51                 }
52         }
53
54         testLogger.Println("test livechannel done...")
55 }
56
57 // SetUpTest Run before each test or benchmark starts
58 func (s *OssBucketLiveChannelSuite) SetUpTest(c *C) {
59
60 }
61
62 // TearDownTest Run after each test or benchmark runs.
63 func (s *OssBucketLiveChannelSuite) TearDownTest(c *C) {
64
65 }
66
67 // TestCreateLiveChannel
68 func (s *OssBucketLiveChannelSuite) TestCreateLiveChannel(c *C) {
69         channelName := "test-create-channel"
70         playlistName := "test-create-channel.m3u8"
71
72         target := LiveChannelTarget{
73                 PlaylistName: playlistName,
74                 Type:         "HLS",
75         }
76
77         config := LiveChannelConfiguration{
78                 Description: "livechannel for test",
79                 Status:      "enabled",
80                 Target:      target,
81         }
82
83         result, err := s.bucket.CreateLiveChannel(channelName, config)
84         c.Assert(err, IsNil)
85
86         playURL := getPlayURL(bucketName, channelName, playlistName)
87         publishURL := getPublishURL(bucketName, channelName)
88         c.Assert(result.PlayUrls[0], Equals, playURL)
89         c.Assert(result.PublishUrls[0], Equals, publishURL)
90
91         err = s.bucket.DeleteLiveChannel(channelName)
92         c.Assert(err, IsNil)
93
94         invalidType := RandStr(4)
95         invalidTarget := LiveChannelTarget{
96                 PlaylistName: playlistName,
97                 Type:         invalidType,
98         }
99
100         invalidConfig := LiveChannelConfiguration{
101                 Description: "livechannel for test",
102                 Status:      "enabled",
103                 Target:      invalidTarget,
104         }
105
106         _, err = s.bucket.CreateLiveChannel(channelName, invalidConfig)
107         c.Assert(err, NotNil)
108 }
109
110 // TestDeleteLiveChannel
111 func (s *OssBucketLiveChannelSuite) TestDeleteLiveChannel(c *C) {
112         channelName := "test-delete-channel"
113
114         target := LiveChannelTarget{
115                 Type: "HLS",
116         }
117
118         config := LiveChannelConfiguration{
119                 Target: target,
120         }
121
122         _, err := s.bucket.CreateLiveChannel(channelName, config)
123         c.Assert(err, IsNil)
124
125         err = s.bucket.DeleteLiveChannel(channelName)
126         c.Assert(err, IsNil)
127
128         emptyChannelName := ""
129         err = s.bucket.DeleteLiveChannel(emptyChannelName)
130         c.Assert(err, NotNil)
131
132         longChannelName := RandStr(65)
133         err = s.bucket.DeleteLiveChannel(longChannelName)
134         c.Assert(err, NotNil)
135
136         config, err = s.bucket.GetLiveChannelInfo(channelName)
137         c.Assert(err, NotNil)
138 }
139
140 // TestGetLiveChannelInfo
141 func (s *OssBucketLiveChannelSuite) TestGetLiveChannelInfo(c *C) {
142         channelName := "test-get-channel-status"
143
144         _, err := s.bucket.GetLiveChannelInfo(channelName)
145         c.Assert(err, NotNil)
146
147         createCfg := LiveChannelConfiguration{
148                 Target: LiveChannelTarget{
149                         Type:         "HLS",
150                         FragDuration: 10,
151                         FragCount:    4,
152                         PlaylistName: "test-get-channel-status.m3u8",
153                 },
154         }
155
156         _, err = s.bucket.CreateLiveChannel(channelName, createCfg)
157         c.Assert(err, IsNil)
158
159         getCfg, err := s.bucket.GetLiveChannelInfo(channelName)
160         c.Assert(err, IsNil)
161         c.Assert("enabled", Equals, getCfg.Status) //The default value is enabled
162         c.Assert("HLS", Equals, getCfg.Target.Type)
163         c.Assert(10, Equals, getCfg.Target.FragDuration)
164         c.Assert(4, Equals, getCfg.Target.FragCount)
165         c.Assert("test-get-channel-status.m3u8", Equals, getCfg.Target.PlaylistName)
166
167         err = s.bucket.DeleteLiveChannel(channelName)
168         c.Assert(err, IsNil)
169 }
170
171 // TestPutLiveChannelStatus
172 func (s *OssBucketLiveChannelSuite) TestPutLiveChannelStatus(c *C) {
173         channelName := "test-put-channel-status"
174
175         config := LiveChannelConfiguration{
176                 Status: "disabled",
177                 Target: LiveChannelTarget{
178                         Type: "HLS",
179                 },
180         }
181
182         _, err := s.bucket.CreateLiveChannel(channelName, config)
183         c.Assert(err, IsNil)
184         getCfg, err := s.bucket.GetLiveChannelInfo(channelName)
185         c.Assert(err, IsNil)
186         c.Assert("disabled", Equals, getCfg.Status)
187
188         err = s.bucket.PutLiveChannelStatus(channelName, "enabled")
189         c.Assert(err, IsNil)
190         getCfg, err = s.bucket.GetLiveChannelInfo(channelName)
191         c.Assert(err, IsNil)
192         c.Assert("enabled", Equals, getCfg.Status)
193
194         err = s.bucket.PutLiveChannelStatus(channelName, "disabled")
195         c.Assert(err, IsNil)
196         getCfg, err = s.bucket.GetLiveChannelInfo(channelName)
197         c.Assert(err, IsNil)
198         c.Assert("disabled", Equals, getCfg.Status)
199
200         invalidStatus := RandLowStr(9)
201         err = s.bucket.PutLiveChannelStatus(channelName, invalidStatus)
202         c.Assert(err, NotNil)
203
204         err = s.bucket.DeleteLiveChannel(channelName)
205         c.Assert(err, IsNil)
206 }
207
208 // TestGetLiveChannelHistory
209 func (s *OssBucketLiveChannelSuite) TestGetLiveChannelHistory(c *C) {
210         channelName := "test-get-channel-history"
211
212         _, err := s.bucket.GetLiveChannelHistory(channelName)
213         c.Assert(err, NotNil)
214
215         config := LiveChannelConfiguration{
216                 Target: LiveChannelTarget{
217                         Type: "HLS",
218                 },
219         }
220
221         _, err = s.bucket.CreateLiveChannel(channelName, config)
222         c.Assert(err, IsNil)
223
224         history, err := s.bucket.GetLiveChannelHistory(channelName)
225         c.Assert(err, IsNil)
226         c.Assert(len(history.Record), Equals, 0)
227
228         err = s.bucket.DeleteLiveChannel(channelName)
229         c.Assert(err, IsNil)
230 }
231
232 // TestGetLiveChannelStat
233 func (s *OssBucketLiveChannelSuite) TestGetLiveChannelStat(c *C) {
234         channelName := "test-get-channel-stat"
235
236         _, err := s.bucket.GetLiveChannelStat(channelName)
237         c.Assert(err, NotNil)
238
239         config := LiveChannelConfiguration{
240                 Target: LiveChannelTarget{
241                         Type: "HLS",
242                 },
243         }
244
245         _, err = s.bucket.CreateLiveChannel(channelName, config)
246         c.Assert(err, IsNil)
247
248         stat, err := s.bucket.GetLiveChannelStat(channelName)
249         c.Assert(err, IsNil)
250         c.Assert(stat.Status, Equals, "Idle")
251
252         err = s.bucket.DeleteLiveChannel(channelName)
253         c.Assert(err, IsNil)
254 }
255
256 // TestPostVodPlaylist
257 func (s *OssBucketLiveChannelSuite) TestPostVodPlaylist(c *C) {
258         channelName := "test-post-vod-playlist"
259         playlistName := "test-post-vod-playlist.m3u8"
260
261         config := LiveChannelConfiguration{
262                 Target: LiveChannelTarget{
263                         Type: "HLS",
264                 },
265         }
266
267         _, err := s.bucket.CreateLiveChannel(channelName, config)
268         c.Assert(err, IsNil)
269
270         endTime := time.Now().Add(-1 * time.Minute)
271         startTime := endTime.Add(-60 * time.Minute)
272
273         err = s.bucket.PostVodPlaylist(channelName, playlistName, startTime, endTime)
274         c.Assert(err, NotNil)
275
276         err = s.bucket.DeleteLiveChannel(channelName)
277         c.Assert(err, IsNil)
278 }
279
280 // TestPostVodPlaylist
281 func (s *OssBucketLiveChannelSuite) TestGetVodPlaylist(c *C) {
282         channelName := "test-get-vod-playlist"
283
284         config := LiveChannelConfiguration{
285                 Target: LiveChannelTarget{
286                         Type: "HLS",
287                 },
288         }
289
290         _, err := s.bucket.CreateLiveChannel(channelName, config)
291         c.Assert(err, IsNil)
292
293         endTime := time.Now().Add(-1 * time.Minute)
294         startTime := endTime.Add(-60 * time.Minute)
295
296         _, err = s.bucket.GetVodPlaylist(channelName, startTime, endTime)
297         c.Assert(err, NotNil)
298
299         err = s.bucket.DeleteLiveChannel(channelName)
300         c.Assert(err, IsNil)
301 }
302
303 // TestListLiveChannel
304 func (s *OssBucketLiveChannelSuite) TestListLiveChannel(c *C) {
305         result, err := s.bucket.ListLiveChannel()
306         c.Assert(err, IsNil)
307         ok := compareListResult(result, "", "", "", 100, false, 0)
308         c.Assert(ok, Equals, true)
309
310         prefix := "test-list-channel"
311         for i := 0; i < 200; i++ {
312                 channelName := fmt.Sprintf("%s-%03d", prefix, i)
313
314                 config := LiveChannelConfiguration{
315                         Target: LiveChannelTarget{
316                                 Type: "HLS",
317                         },
318                 }
319
320                 _, err := s.bucket.CreateLiveChannel(channelName, config)
321                 c.Assert(err, IsNil)
322         }
323
324         result, err = s.bucket.ListLiveChannel()
325         c.Assert(err, IsNil)
326         nextMarker := fmt.Sprintf("%s-099", prefix)
327         ok = compareListResult(result, "", "", nextMarker, 100, true, 100)
328         c.Assert(ok, Equals, true)
329
330         randPrefix := RandStr(5)
331         result, err = s.bucket.ListLiveChannel(Prefix(randPrefix))
332         c.Assert(err, IsNil)
333         ok = compareListResult(result, randPrefix, "", "", 100, false, 0)
334         c.Assert(ok, Equals, true)
335
336         marker := fmt.Sprintf("%s-100", prefix)
337         result, err = s.bucket.ListLiveChannel(Prefix(prefix), Marker(marker))
338         c.Assert(err, IsNil)
339         ok = compareListResult(result, prefix, marker, "", 100, false, 99)
340         c.Assert(ok, Equals, true)
341
342         maxKeys := 1000
343         result, err = s.bucket.ListLiveChannel(MaxKeys(maxKeys))
344         c.Assert(err, IsNil)
345         ok = compareListResult(result, "", "", "", maxKeys, false, 200)
346
347         invalidMaxKeys := -1
348         result, err = s.bucket.ListLiveChannel(MaxKeys(invalidMaxKeys))
349         c.Assert(err, NotNil)
350
351         for i := 0; i < 200; i++ {
352                 channelName := fmt.Sprintf("%s-%03d", prefix, i)
353                 err := s.bucket.DeleteLiveChannel(channelName)
354                 c.Assert(err, IsNil)
355         }
356 }
357
358 // TestSignRtmpURL
359 func (s *OssBucketLiveChannelSuite) TestSignRtmpURL(c *C) {
360         channelName := "test-sign-rtmp-url"
361         playlistName := "test-sign-rtmp-url.m3u8"
362
363         config := LiveChannelConfiguration{
364                 Target: LiveChannelTarget{
365                         Type:         "HLS",
366                         PlaylistName: playlistName,
367                 },
368         }
369
370         _, err := s.bucket.CreateLiveChannel(channelName, config)
371         c.Assert(err, IsNil)
372
373         expires := int64(3600)
374         signedRtmpURL, err := s.bucket.SignRtmpURL(channelName, playlistName, expires)
375         c.Assert(err, IsNil)
376         playURL := getPublishURL(s.bucket.BucketName, channelName)
377         hasPrefix := strings.HasPrefix(signedRtmpURL, playURL)
378         c.Assert(hasPrefix, Equals, true)
379
380         invalidExpires := int64(-1)
381         signedRtmpURL, err = s.bucket.SignRtmpURL(channelName, playlistName, invalidExpires)
382         c.Assert(err, NotNil)
383
384         err = s.bucket.DeleteLiveChannel(channelName)
385         c.Assert(err, IsNil)
386 }
387
388 // private
389 // getPlayURL Get the play url of the live channel
390 func getPlayURL(bucketName, channelName, playlistName string) string {
391         host := ""
392         useHTTPS := false
393         if strings.Contains(endpoint, "https://") {
394                 host = endpoint[8:]
395                 useHTTPS = true
396         } else if strings.Contains(endpoint, "http://") {
397                 host = endpoint[7:]
398         } else {
399                 host = endpoint
400         }
401
402         if useHTTPS {
403                 return fmt.Sprintf("https://%s.%s/%s/%s", bucketName, host, channelName, playlistName)
404         }
405         return fmt.Sprintf("http://%s.%s/%s/%s", bucketName, host, channelName, playlistName)
406 }
407
408 // getPublistURL Get the push url of the live stream channel
409 func getPublishURL(bucketName, channelName string) string {
410         host := ""
411         if strings.Contains(endpoint, "https://") {
412                 host = endpoint[8:]
413         } else if strings.Contains(endpoint, "http://") {
414                 host = endpoint[7:]
415         } else {
416                 host = endpoint
417         }
418
419         return fmt.Sprintf("rtmp://%s.%s/live/%s", bucketName, host, channelName)
420 }
421
422 func compareListResult(result ListLiveChannelResult, prefix, marker, nextMarker string, maxKey int, isTruncated bool, count int) bool {
423         if result.Prefix != prefix || result.Marker != marker || result.NextMarker != nextMarker || result.MaxKeys != maxKey || result.IsTruncated != isTruncated || len(result.LiveChannel) != count {
424                 return false
425         }
426
427         return true
428 }