-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdater_test.go
172 lines (143 loc) · 5.61 KB
/
updater_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
169
170
171
172
// Copyright (c) 2018-2022, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package updater
import (
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
func TestServer(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Updater")
}
var _ = Describe("Updater", func() {
Describe("FetchSpec", func() {
It("Should fetch the correct spec", func() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expect(r.URL.Path).To(Equal("/0.7.0/linux/amd64/release.json"))
spec, _ := os.ReadFile("testdata/0.7.0/linux/amd64/release.json")
fmt.Fprint(w, string(spec))
}))
defer ts.Close()
spec, err := FetchSpec(SourceRepo(ts.URL), Version("0.7.0"), OS("linux"), Arch("amd64"))
Expect(err).ToNot(HaveOccurred())
Expect(spec.BinaryPath).To(Equal("choria.bz2"))
Expect(spec.Sha256Hash).To(Equal("12a61f4e173fb3a11c05d6471f74728f76231b4a5fcd9667cef3af87a3ae4dc2"))
Expect(spec.BinaryURI.String()).To(Equal(fmt.Sprintf("%s/0.7.0/linux/amd64/choria.bz2", ts.URL)))
})
It("Should detect bad configs", func() {
_, err := FetchSpec()
Expect(err).To(MatchError("invalid updater configuration: no source repo given, please use SourceRepo()"))
})
})
Describe("Apply", func() {
It("Should download and apply the update", func() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
f, err := os.ReadFile(filepath.Join("testdata", r.URL.Path))
Expect(err).ToNot(HaveOccurred())
fmt.Fprint(w, string(f))
}))
defer ts.Close()
err := os.WriteFile("testdata/target", []byte("target file"), 0600)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("testdata/target")
err = Apply(
SourceRepo(ts.URL),
Version("0.7.0"),
OS("linux"),
Arch("amd64"),
TargetFile("testdata/target"),
Logger(log.New(io.Discard, "", 0)),
)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("testdata/target.backup")
swapped, err := os.ReadFile("testdata/target")
Expect(err).ToNot(HaveOccurred())
Expect(swapped).To(Equal([]byte("testing\n")))
backup, err := os.ReadFile("testdata/target.backup")
Expect(err).ToNot(HaveOccurred())
Expect(backup).To(Equal([]byte("target file")))
})
})
Describe("swapNew", func() {
It("Should detect rename errors", func() {
err := swapNew("/nonexisting.new", "/nonexisting.backup", &Config{TargetFile: "/nonexisting"})
Expect(err).To(MatchError("rename /nonexisting /nonexisting.old: no such file or directory"))
})
It("Should attempt to recover from renaming the new to the target and wrap the recovery error in a rollback error", func() {
err := os.WriteFile("testdata/source", []byte("old file"), 0600)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("testdata/source")
err = swapNew("/nonexisting", "/nonexisting", &Config{TargetFile: "testdata/source"})
err = RollbackError(err)
Expect(err).To(MatchError("rename /nonexisting testdata/source: no such file or directory"))
})
It("Should recover from rename errors by copying the backup data", func() {
err := os.WriteFile("testdata/source", []byte("old file"), 0600)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("testdata/source")
err = os.WriteFile("testdata/backup", []byte("backup file"), 0600)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("testdata/backup")
err = swapNew("testdata/new", "testdata/backup", &Config{TargetFile: "testdata/source"})
Expect(err).To(HaveOccurred())
recovered, err := os.ReadFile("testdata/source")
Expect(err).ToNot(HaveOccurred())
Expect(recovered).To(Equal([]byte("backup file")))
})
It("Should swap the file", func() {
err := os.WriteFile("testdata/source", []byte("old file"), 0600)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("testdata/source")
err = os.WriteFile("testdata/backup", []byte("backup file"), 0600)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("testdata/backup")
err = os.WriteFile("testdata/new", []byte("new file"), 0600)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("testdata/new")
err = swapNew("testdata/new", "testdata/backup", &Config{TargetFile: "testdata/source"})
Expect(err).ToNot(HaveOccurred())
swapped, err := os.ReadFile("testdata/source")
Expect(err).ToNot(HaveOccurred())
Expect(swapped).To(Equal([]byte("new file")))
})
})
Describe("backupTarget", func() {
It("Should detect missing targets", func() {
_, err := backupTarget(&Config{TargetFile: "/noexisting"})
Expect(err).To(MatchError("stat /noexisting: no such file or directory"))
})
It("Should create the backup", func() {
err := os.WriteFile("testdata/source", []byte("example data"), 0600)
Expect(err).ToNot(HaveOccurred())
defer os.Remove("testdata/source")
out, err := backupTarget(&Config{TargetFile: "testdata/source"})
Expect(err).ToNot(HaveOccurred())
defer os.Remove(out)
Expect(out).To(Equal("testdata/source.backup"))
src, err := os.ReadFile("testdata/source")
Expect(err).ToNot(HaveOccurred())
copy, err := os.ReadFile(out)
Expect(err).ToNot(HaveOccurred())
Expect(src).To(Equal(copy))
})
})
Describe("validateChecksum", func() {
It("Should correctly validate checksums", func() {
s := &Spec{
Sha256Hash: "f5b72762ab4080e712e266825400a63da7df57c31e485013d8f03070a631aee1",
}
Expect(validateChecksum("testdata/0.7.0/linux/amd64/choria.bz2", s)).To(BeTrue())
s.Sha256Hash = "fail"
Expect(validateChecksum("testdata/0.7.0/linux/amd64/choria.bz2", s)).To(BeFalse())
})
})
})