Skip to content

Commit ab2decb

Browse files
christoph-zededaOhmSpectator
authored andcommitted
collect-info: enhance test to extract data
check that the tarball is not corrupted and has correct data Signed-off-by: Christoph Ostarek <christoph@zededa.com>
1 parent c5c2894 commit ab2decb

File tree

1 file changed

+69
-10
lines changed

1 file changed

+69
-10
lines changed

pkg/debug/collect-info-test/collect-info_test.go

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
package main
55

66
import (
7+
"archive/tar"
8+
"bytes"
9+
"compress/gzip"
710
"context"
811
"errors"
12+
"io"
913
"net/http"
1014
"os"
1115
"os/exec"
@@ -16,17 +20,23 @@ import (
1620
"testing"
1721
)
1822

19-
func listenHTTP() (*http.Request, error) {
23+
func listenHTTP() (*http.Request, io.Reader, error) {
2024
var serverShutdown sync.Mutex
2125
var s *http.Server
2226
sm := http.NewServeMux()
2327
var req *http.Request
28+
var body bytes.Buffer
2429
var bodyCloseErr error
30+
var ioCopyErr error
2531
var shutdownErr error
2632

2733
sm.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
2834
req = r
2935

36+
if req.Body != nil {
37+
_, ioCopyErr = io.Copy(&body, req.Body)
38+
}
39+
3040
http.Error(w, "Bye", http.StatusOK)
3141

3242
defer func() {
@@ -51,13 +61,16 @@ func listenHTTP() (*http.Request, error) {
5161

5262
serverShutdown.Lock()
5363
if bodyCloseErr != nil {
54-
return nil, bodyCloseErr
64+
return nil, nil, bodyCloseErr
5565
}
5666
if shutdownErr != nil {
57-
return nil, shutdownErr
67+
return nil, nil, shutdownErr
68+
}
69+
if ioCopyErr != nil {
70+
return nil, nil, ioCopyErr
5871
}
5972

60-
return req, nil
73+
return req, &body, nil
6174
}
6275

6376
func cleanup(t *testing.T) {
@@ -69,15 +82,18 @@ func cleanup(t *testing.T) {
6982
}
7083
}
7184

72-
func runCollectInfo(t *testing.T, uploadServer string, cmdEnv []string, httpListener func() (*http.Request, error)) *http.Request {
85+
func runCollectInfo(t *testing.T, uploadServer string, cmdEnv []string, httpListener func() (*http.Request, io.Reader, error)) (*http.Request, io.Reader) {
7386
var cmd *exec.Cmd
7487
reqChan := make(chan *http.Request)
88+
bodyChan := make(chan io.Reader)
89+
7590
go func() {
76-
req, err := httpListener()
91+
req, body, err := httpListener()
7792
if err != nil {
7893
t.Logf("error from http server: %+v", err)
7994
}
8095
reqChan <- req
96+
bodyChan <- body
8197

8298
if cmd == nil {
8399
return
@@ -88,12 +104,16 @@ func runCollectInfo(t *testing.T, uploadServer string, cmdEnv []string, httpList
88104
}
89105
}()
90106

91-
for _, dir := range []string{"/proc/self", "/persist/status"} {
107+
for _, dir := range []string{"/proc/self", "/persist/status", "/persist/tmp", "/run"} {
92108
err := os.MkdirAll(filepath.Join("/out", dir), 0700)
93109
if err != nil {
94110
t.Fatal(err)
95111
}
96112
}
113+
err := os.WriteFile("/out/run/eve-hv-type", []byte("gotest"), 0600)
114+
if err != nil {
115+
t.Fatalf("could not write /out/run/eve-hv-type: %v", err)
116+
}
97117

98118
for _, fileContent := range []struct {
99119
file string
@@ -128,24 +148,63 @@ func runCollectInfo(t *testing.T, uploadServer string, cmdEnv []string, httpList
128148
_ = cmd.Run()
129149

130150
req := <-reqChan
151+
body := <-bodyChan
131152

132-
return req
153+
return req, body
133154
}
134155

135156
// TestCollectInfoUpload creates an http server and calls collect-info.sh to upload the tarball to it
136157
func TestCollectInfoUpload(t *testing.T) {
137158
defer cleanup(t)
138159

160+
var eveHvType string
161+
139162
uploadServer := "http://localhost:8080"
140163
cmdEnv := []string{"AUTHORIZATION=Bearer Vm0weGQxSXhiRmRXV0d4V1YwZDRWRmxyVm5kVmJGcHlWV3RLVUZWVU1Eaz0="}
141164

142-
req := runCollectInfo(t, uploadServer, cmdEnv, listenHTTP)
165+
req, body := runCollectInfo(t, uploadServer, cmdEnv, listenHTTP)
143166
t.Logf("req: %+v", req)
144167

145168
if req.Header["Authorization"][0] != "Bearer Vm0weGQxSXhiRmRXV0d4V1YwZDRWRmxyVm5kVmJGcHlWV3RLVUZWVU1Eaz0=" {
146169
t.Fatalf("authorization header wrong or not set - was %+v", req.Header["Authorization"])
147170
}
148171

172+
if req.Body == nil {
173+
t.Fatalf("request body empty")
174+
}
175+
176+
gz, err := gzip.NewReader(body)
177+
if err != nil {
178+
t.Fatalf("could not initialize gzip reader: %+v", err)
179+
}
180+
defer gz.Close()
181+
182+
tr := tar.NewReader(gz)
183+
184+
for {
185+
hdr, err := tr.Next()
186+
if err == io.EOF {
187+
break
188+
}
189+
if err != nil {
190+
t.Fatalf("next tar header failed: %v", err)
191+
}
192+
193+
t.Logf("- %s\n", hdr.Name)
194+
if filepath.Base(hdr.Name) == "eve-hv-type" {
195+
bs, err := io.ReadAll(tr)
196+
if err != nil {
197+
t.Fatalf("could not read from uploaded tar file: %v", err)
198+
}
199+
200+
eveHvType = string(bs)
201+
}
202+
}
203+
204+
if eveHvType != "gotest" {
205+
t.Fatalf("wrong eve-hv-type file content in collect-info tar file; expected 'gotest', got '%s'", eveHvType)
206+
}
207+
149208
if !strings.Contains(req.URL.Path, "123456") {
150209
t.Fatalf("http file path does not contain uuid; req: %+v", req)
151210
}
@@ -167,7 +226,7 @@ func TestCollectInfoFailingUpload(t *testing.T) {
167226
uploadServer := "http://localhost:8080"
168227
cmdEnv := []string{"AUTHORIZATION=Bearer Vm0weGQxSXhiRmRXV0d4V1YwZDRWRmxyVm5kVmJGcHlWV3RLVUZWVU1Eaz0="}
169228

170-
req := runCollectInfo(t, uploadServer, cmdEnv, func() (*http.Request, error) { return nil, nil })
229+
req, _ := runCollectInfo(t, uploadServer, cmdEnv, func() (*http.Request, io.Reader, error) { return nil, nil, nil })
171230
t.Logf("req: %+v", req)
172231

173232
dirEntries, err := os.ReadDir("/out/persist/eve-info")

0 commit comments

Comments
 (0)