Skip to content

Commit 40eef3c

Browse files
authored
feat(e2e): add local host support bundle test (#1680)
1 parent d25aa7d commit 40eef3c

File tree

6 files changed

+378
-0
lines changed

6 files changed

+378
-0
lines changed

.github/workflows/build-test-deploy.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ jobs:
248248
name: support-bundle
249249
path: bin/
250250
- run: chmod +x bin/support-bundle
251+
- name: Download preflight binary
252+
uses: actions/download-artifact@v4
253+
with:
254+
name: preflight
255+
path: bin/
256+
- run: chmod +x bin/preflight
251257
- run: make support-bundle-e2e-go-test
252258

253259
compile-collect:
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package e2e
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"slices"
10+
"strings"
11+
"testing"
12+
13+
"sigs.k8s.io/e2e-framework/pkg/envconf"
14+
"sigs.k8s.io/e2e-framework/pkg/features"
15+
)
16+
17+
func TestHostLocalCollector(t *testing.T) {
18+
tests := []struct {
19+
paths []string
20+
notExpectedPaths []string
21+
expectType string
22+
}{
23+
{
24+
paths: []string{
25+
"cpu.json",
26+
},
27+
notExpectedPaths: []string{
28+
"node_list.json",
29+
},
30+
expectType: "file",
31+
},
32+
}
33+
34+
feature := features.New("Preflight Host Local Collector").
35+
Assess("check preflight catch host local collector", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
36+
var out bytes.Buffer
37+
var errOut bytes.Buffer
38+
preflightName := "preflightbundle"
39+
cmd := exec.CommandContext(ctx, preflightBinary(), "spec/localHostCollectors.yaml", "--interactive=false")
40+
cmd.Stdout = &out
41+
cmd.Stderr = &errOut
42+
43+
err := cmd.Run()
44+
tarPath := GetFilename(errOut.String(), preflightName)
45+
if err != nil {
46+
if tarPath == "" {
47+
t.Error(err)
48+
}
49+
}
50+
51+
defer func() {
52+
err := os.Remove(tarPath)
53+
if err != nil {
54+
t.Error("Error remove file:", err)
55+
}
56+
}()
57+
58+
targetFile := fmt.Sprintf("%s/host-collectors/system/", strings.TrimSuffix(tarPath, ".tar.gz"))
59+
60+
files, _, err := readFilesAndFoldersFromTar(tarPath, targetFile)
61+
if err != nil {
62+
t.Error(err)
63+
}
64+
65+
for _, test := range tests {
66+
if test.expectType == "file" {
67+
for _, path := range test.paths {
68+
if !slices.Contains(files, path) {
69+
t.Errorf("Expected file %s not found in the tarball", path)
70+
}
71+
}
72+
for _, path := range test.notExpectedPaths {
73+
if slices.Contains(files, path) {
74+
t.Errorf("Unexpected file %s found in the tarball", path)
75+
}
76+
}
77+
}
78+
}
79+
return ctx
80+
}).Feature()
81+
testenv.Test(t, feature)
82+
}
83+
84+
func GetFilename(input, prefix string) string {
85+
// Split the input into words
86+
words := strings.Fields(input)
87+
// Iterate over each word to find the one starting with the prefix
88+
for _, word := range words {
89+
if strings.HasPrefix(word, prefix) {
90+
return word
91+
}
92+
}
93+
94+
// Return an empty string if no match is found
95+
return ""
96+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package e2e
2+
3+
import (
4+
"archive/tar"
5+
"bytes"
6+
"compress/gzip"
7+
"context"
8+
"flag"
9+
"fmt"
10+
"io"
11+
"os"
12+
"path/filepath"
13+
"strings"
14+
"testing"
15+
16+
"k8s.io/klog/v2"
17+
"sigs.k8s.io/e2e-framework/pkg/env"
18+
"sigs.k8s.io/e2e-framework/pkg/envconf"
19+
"sigs.k8s.io/e2e-framework/pkg/envfuncs"
20+
"sigs.k8s.io/e2e-framework/support/kind"
21+
)
22+
23+
var testenv env.Environment
24+
25+
const ClusterName = "kind-cluster"
26+
27+
func TestMain(m *testing.M) {
28+
// enable klog
29+
klog.InitFlags(nil)
30+
if os.Getenv("E2E_VERBOSE") == "1" {
31+
_ = flag.Set("v", "10")
32+
}
33+
34+
testenv = env.New()
35+
namespace := envconf.RandomName("default", 16)
36+
testenv.Setup(
37+
envfuncs.CreateCluster(kind.NewProvider(), ClusterName),
38+
envfuncs.CreateNamespace(namespace),
39+
)
40+
testenv.Finish(
41+
envfuncs.DeleteNamespace(namespace),
42+
envfuncs.DestroyCluster(ClusterName),
43+
)
44+
os.Exit(testenv.Run(m))
45+
}
46+
47+
func getClusterFromContext(t *testing.T, ctx context.Context, clusterName string) *kind.Cluster {
48+
provider, ok := envfuncs.GetClusterFromContext(ctx, clusterName)
49+
if !ok {
50+
t.Fatalf("Failed to extract kind cluster %s from context", clusterName)
51+
}
52+
cluster, ok := provider.(*kind.Cluster)
53+
if !ok {
54+
t.Fatalf("Failed to cast kind cluster %s from provider", clusterName)
55+
}
56+
57+
return cluster
58+
}
59+
60+
func readFilesAndFoldersFromTar(tarPath, targetFolder string) ([]string, []string, error) {
61+
file, err := os.Open(tarPath)
62+
if err != nil {
63+
return nil, nil, fmt.Errorf("Error opening file: %w", err)
64+
}
65+
defer file.Close()
66+
67+
gzipReader, err := gzip.NewReader(file)
68+
if err != nil {
69+
return nil, nil, fmt.Errorf("Error initializing gzip reader: %w", err)
70+
}
71+
defer gzipReader.Close()
72+
73+
tarReader := tar.NewReader(gzipReader)
74+
var files []string
75+
var folders []string
76+
77+
for {
78+
header, err := tarReader.Next()
79+
80+
if err == io.EOF {
81+
break
82+
}
83+
if err != nil {
84+
return nil, nil, fmt.Errorf("Error reading tar: %w", err)
85+
}
86+
87+
if strings.HasPrefix(header.Name, targetFolder) {
88+
relativePath, err := filepath.Rel(targetFolder, header.Name)
89+
if err != nil {
90+
return nil, nil, fmt.Errorf("Error getting relative path: %w", err)
91+
}
92+
if relativePath != "" {
93+
relativeDir := filepath.Dir(relativePath)
94+
if relativeDir != "." {
95+
parentDir := strings.Split(relativeDir, "/")[0]
96+
folders = append(folders, parentDir)
97+
} else {
98+
files = append(files, relativePath)
99+
}
100+
}
101+
}
102+
}
103+
104+
return files, folders, nil
105+
}
106+
107+
func readFileFromTar(tarPath, targetFile string) ([]byte, error) {
108+
file, err := os.Open(tarPath)
109+
if err != nil {
110+
return nil, fmt.Errorf("Error opening file: %w", err)
111+
}
112+
defer file.Close()
113+
114+
gzipReader, err := gzip.NewReader(file)
115+
if err != nil {
116+
return nil, fmt.Errorf("Error initializing gzip reader: %w", err)
117+
}
118+
defer gzipReader.Close()
119+
120+
tarReader := tar.NewReader(gzipReader)
121+
122+
for {
123+
header, err := tarReader.Next()
124+
125+
if err == io.EOF {
126+
break
127+
}
128+
if err != nil {
129+
return nil, fmt.Errorf("Error reading tar: %w", err)
130+
}
131+
132+
if header.Name == targetFile {
133+
buf := new(bytes.Buffer)
134+
_, err = io.Copy(buf, tarReader)
135+
if err != nil {
136+
return nil, fmt.Errorf("Error copying data: %w", err)
137+
}
138+
return buf.Bytes(), nil
139+
}
140+
}
141+
return nil, fmt.Errorf("File not found: %q", targetFile)
142+
}
143+
144+
func preflightBinary() string {
145+
return "../../../bin/preflight"
146+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: troubleshoot.sh/v1beta2
2+
kind: HostPreflight
3+
metadata:
4+
name: ec-cluster-preflight
5+
spec:
6+
collectors:
7+
- cpu: {}
8+
analyzers:
9+
- cpu:
10+
checkName: "Number of CPUs"
11+
outcomes:
12+
- pass:
13+
when: "count < 2"
14+
message: At least 2 CPU cores are required, and 4 CPU cores are recommended
15+
- warn:
16+
when: "count < 4"
17+
message: At least 4 CPU cores are recommended
18+
- pass:
19+
message: This server has at least 4 CPU cores
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package e2e
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"testing"
10+
11+
"golang.org/x/exp/slices"
12+
"sigs.k8s.io/e2e-framework/pkg/envconf"
13+
"sigs.k8s.io/e2e-framework/pkg/features"
14+
)
15+
16+
func TestHostLocalCollector(t *testing.T) {
17+
tests := []struct {
18+
paths []string
19+
notExpectedPaths []string
20+
expectType string
21+
}{
22+
{
23+
paths: []string{
24+
"cpu.json",
25+
"hostos_info.json",
26+
"ipv4Interfaces.json",
27+
"memory.json",
28+
},
29+
notExpectedPaths: []string{
30+
"node_list.json",
31+
},
32+
expectType: "file",
33+
},
34+
}
35+
feature := features.New("Host Local Collector").
36+
Assess("check support bundle catch host local collector", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
37+
var out bytes.Buffer
38+
supportbundleName := "host-local-collector"
39+
tarPath := fmt.Sprintf("%s.tar.gz", supportbundleName)
40+
targetFile := fmt.Sprintf("%s/host-collectors/system/", supportbundleName)
41+
cmd := exec.CommandContext(ctx, sbBinary(), "spec/localHostCollectors.yaml", "--interactive=false", fmt.Sprintf("-o=%s", supportbundleName))
42+
cmd.Stdout = &out
43+
err := cmd.Run()
44+
if err != nil {
45+
t.Error(err)
46+
}
47+
48+
defer func() {
49+
err := os.Remove(fmt.Sprintf("%s.tar.gz", supportbundleName))
50+
if err != nil {
51+
t.Error("Error remove file:", err)
52+
}
53+
}()
54+
55+
files, _, err := readFilesAndFoldersFromTar(tarPath, targetFile)
56+
if err != nil {
57+
t.Error(err)
58+
}
59+
60+
for _, test := range tests {
61+
if test.expectType == "file" {
62+
for _, path := range test.notExpectedPaths {
63+
if slices.Contains(files, path) {
64+
t.Fatalf("Unexpected file %s found", path)
65+
}
66+
}
67+
for _, path := range test.paths {
68+
if !slices.Contains(files, path) {
69+
t.Fatalf("Expected file %s not found", path)
70+
}
71+
}
72+
}
73+
}
74+
75+
return ctx
76+
}).Feature()
77+
testenv.Test(t, feature)
78+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: troubleshoot.sh/v1beta2
2+
kind: SupportBundle
3+
metadata:
4+
name: "remote-host-collectors"
5+
spec:
6+
hostCollectors:
7+
- ipv4Interfaces: {}
8+
- hostServices: {}
9+
- cpu: {}
10+
- hostOS: {}
11+
- memory: {}
12+
- blockDevices: {}
13+
- kernelConfigs: {}
14+
- copy:
15+
collectorName: etc-resolv
16+
path: /etc/resolv.conf
17+
- dns:
18+
collectorName: replicated-app-resolve
19+
hostnames:
20+
- replicated.app
21+
- diskUsage:
22+
collectorName: root-disk-usage
23+
path: /
24+
- diskUsage:
25+
collectorName: tmp
26+
path: /tmp
27+
- http:
28+
collectorName: get-replicated-app
29+
get:
30+
url: https://replicated.app
31+
- run:
32+
collectorName: uptime
33+
command: uptime

0 commit comments

Comments
 (0)