-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_test.go
88 lines (74 loc) · 2.57 KB
/
db_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
// (c) Siemens AG 2023
//
// SPDX-License-Identifier: MIT
package ieddata
import (
"os"
"time"
"github.com/ory/dockertest/v3/docker"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/thediveo/fdooze"
. "github.com/thediveo/success"
)
var _ = Describe("IED app engine database", func() {
BeforeEach(func() {
if os.Getuid() != 0 {
Skip("needs root")
}
goodfds := Filedescriptors()
DeferCleanup(func() {
pool.Client.HTTPClient.CloseIdleConnections()
// There's a whale watcher in the background needing to wind, so we
// give it a chance to get scheduled and run its course.
Eventually(Filedescriptors).WithTimeout(2 * time.Second).WithPolling(250 * time.Millisecond).
ShouldNot(HaveLeakedFds(goodfds))
})
})
It("sanitizes database names", func() {
Expect(sanitize("abcxyz.0-9")).To(Equal("abcxyz.0-9"))
Expect(sanitize("../abc?foo=bar")).To(Equal("__abc_foo_bar"))
})
It("fails for invalid container PID", func() {
Expect(OpenInPID("foo.db", 0)).Error().To(MatchError(MatchRegexp(`invalid mount namespace reference`)))
})
It("fails for missing IE runtime container", func() {
// now that we have a fake edge core container running for the whole
// suite we need to move it out of the way for just this test...
Expect(pool.Client.RenameContainer(docker.RenameContainerOptions{
ID: fakecore.Container.ID,
Name: "off-" + EdgeIotCoreContainerName,
})).To(Succeed())
defer func() {
Expect(pool.Client.RenameContainer(docker.RenameContainerOptions{
ID: fakecore.Container.ID,
Name: EdgeIotCoreContainerName,
})).To(Succeed())
}()
Expect(Open("foo.db")).Error().To(MatchError(MatchRegexp(`no .* runtime container`)))
})
It("fails for missing/invalid IED app engine database", func() {
Expect(Open("foo.db")).Error().To(MatchError(ContainSubstring("/root/data")))
Expect(Open("not.a.db")).Error().To(MatchError(ContainSubstring("unable to open database")))
})
It("accesses the app engine database", func() {
var db *AppEngineDB
Eventually(func() error {
var err error
db, err = Open("platformbox.db")
return err
}).WithTimeout(10 * time.Second).WithPolling(250 * time.Millisecond).
Should(Succeed())
defer db.Close()
rows := Successful(db.Query("SELECT deviceKey, deviceValue from device"))
defer rows.Close()
m := map[string]string{}
for rows.Next() {
var key, value string
Expect(rows.Scan(&key, &value)).To(Succeed())
m[key] = value
}
Expect(m).To(HaveKeyWithValue("deviceName", "iedx12345"))
Expect(m).To(HaveKeyWithValue("ownerEmail", "foo.bar@example.com"))
})
})