-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion_detection_test.go
144 lines (126 loc) · 3.5 KB
/
motion_detection_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
package axisevents
import (
"context"
"fmt"
"io/ioutil"
"log"
"mime"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
)
const (
USERNAME = "AXISEVENTS_USERNAME"
PASSWORD = "AXISEVENTS_PASSWORD"
ADDRESS = "AXISEVENTS_ADDRESS"
)
var (
username = os.Getenv(USERNAME)
password = os.Getenv(PASSWORD)
address = os.Getenv(ADDRESS)
testServer = "https://172.25.125.218:58674/"
// go test -run=TestSendVideoClipLongRunning -timeout 100s --httptest.serve=:58674
)
var device = Device{
Address: address,
Username: username,
Password: password,
}
func TestMotionDetectionRecordingOnSdCardLongRunning(t *testing.T) {
if testing.Short() {
t.Skip("RecordingOnSdCard tests on a real camera")
}
verifyEnvVariables(t)
err := NewMotionDetectionHandler("newtest", true, 4).
Record().
Video().
SdCard().
ExecuteOn(context.TODO(), device)
if err != nil {
t.Error("Failed to toggle motion detection on device: ", err)
}
}
func TestHttpNotificationLongRunning(t *testing.T) {
if testing.Short() {
t.Skip("HttpNotification tests on a real camera")
}
verifyEnvVariables(t)
// url will be http://192.168.1.106?Message=testing&source=livingroom
err := NewMotionDetectionHandler("httpNotifications", true, 4).
HttpNotification().
Notify("testing", "source=livingroom").
HTTP(testServer, "", "", "", "", "", "", "").
ExecuteOn(context.TODO(), device)
if err != nil {
t.Error("Failed to set http notification: ", err)
}
}
// TestSendVideoClipLongRunning tests on a real camera
func TestSendVideoClipLongRunning(t *testing.T) {
if testing.Short() {
t.Skip("SendVideoClip tests on a real camera")
}
verifyEnvVariables(t)
ts := startTestServer(t)
defer ts.Close()
t.Logf("TestServer: %s", ts.URL)
err := NewMotionDetectionHandler(
"sendVideoClipTest", true, 4).
VideoClip().
Send("video-test-%y-%m-%d_%H-%M-%S-%f_#s.mkv", "hej=hopp&danke=gut", "", 1000, 1000).
HTTPS(
testServer, false, "", "", "", "", "", "", "").
ExecuteOn(context.Background(), device)
if err != nil {
t.Error("Failed to set SendVideoClip: ", err)
} else {
time.Sleep(time.Second * 100)
}
}
func startTestServer(t *testing.T) *httptest.Server {
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading body %s", err)
} else {
log.Printf("URL: %s", r.URL.String())
log.Printf("Headers: %v", r.Header)
_, params, err := mime.ParseMediaType(r.Header.Get("Content-Disposition"))
if err != nil {
log.Printf("Failed to get filename: %s", err)
}
filename, ok := params["filename"] // set to "foo.png"
if !ok {
t.Error("Expected to get filename")
}
now := time.Now()
expectedPrefix := fmt.Sprintf("video-test-%d-%d-%d", now.Year(), int(now.Month()), now.Day())
if !strings.HasPrefix(filename, expectedPrefix) {
t.Errorf("Expected file to start with %s but was %s", expectedPrefix, filename)
}
n, err := ioutil.Discard.Write(b)
if err != nil {
t.Errorf("Failed to save file %s", err)
}
if n != len(b) {
t.Errorf("Expected %d to be save but was %d", len(b), n)
}
}
fmt.Fprintln(w, "Hello, client")
}))
return ts
}
func verifyEnvVariables(t *testing.T) {
if len(device.Address) == 0 {
t.Fatalf("You need to set env variable %s", ADDRESS)
}
if len(device.Username) == 0 {
t.Fatalf("You need to set env variable %s", USERNAME)
}
if len(device.Password) == 0 {
t.Fatalf("You need to set env variable %s", PASSWORD)
}
}