Skip to content

Commit 38ff340

Browse files
authored
feat(e2e): add e2e go test for support-bundler (#1265)
1 parent 04f69b3 commit 38ff340

File tree

10 files changed

+388
-30
lines changed

10 files changed

+388
-30
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,27 @@ jobs:
222222
- run: chmod +x bin/support-bundle
223223
- run: make support-bundle-e2e-test
224224

225+
validate-supportbundle-e2e-go-test:
226+
runs-on: ubuntu-latest
227+
needs: compile-supportbundle
228+
steps:
229+
- uses: actions/setup-go@v4
230+
with:
231+
go-version: "1.19"
232+
- name: setup env
233+
run: |
234+
echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV
235+
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
236+
shell: bash
237+
- uses: actions/checkout@v3
238+
- name: Download support bundle binary
239+
uses: actions/download-artifact@v3
240+
with:
241+
name: support-bundle
242+
path: bin/
243+
- run: chmod +x bin/support-bundle
244+
- run: make support-bundle-e2e-go-test
245+
225246
compile-collect:
226247
runs-on: ubuntu-latest
227248
steps:

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ endef
3636

3737
BUILDFLAGS = -tags "netgo containers_image_ostree_stub exclude_graphdriver_devicemapper exclude_graphdriver_btrfs containers_image_openpgp" -installsuffix netgo
3838
BUILDPATHS = ./pkg/... ./cmd/... ./internal/...
39+
E2EPATHS = ./test/e2e/...
3940
TESTFLAGS ?= -v -coverprofile cover.out
4041

4142
.DEFAULT: all
@@ -71,6 +72,14 @@ run-examples:
7172
support-bundle-e2e-test:
7273
./test/validate-support-bundle-e2e.sh
7374

75+
.PHONY: support-bundle-e2e-go-test
76+
support-bundle-e2e-go-test:
77+
if [ -n $(RUN) ]; then \
78+
go test ${BUILDFLAGS} ${E2EPATHS} -v -run $(RUN); \
79+
else \
80+
go test ${BUILDFLAGS} ${E2EPATHS} -v; \
81+
fi
82+
7483
# Build all binaries in parallel ( -j )
7584
build:
7685
@echo "Build cli binaries"

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ require (
7272
github.com/sirupsen/logrus v1.9.3 // indirect
7373
github.com/sylabs/sif/v2 v2.11.1 // indirect
7474
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
75+
github.com/vladimirvivien/gexe v0.2.0 // indirect
7576
go.opentelemetry.io/otel/metric v1.16.0 // indirect
7677
go.opentelemetry.io/otel/trace v1.16.0 // indirect
7778
go.uber.org/atomic v1.10.0 // indirect
7879
go.uber.org/multierr v1.11.0 // indirect
7980
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
8081
golang.org/x/mod v0.10.0 // indirect
8182
golang.org/x/tools v0.9.1 // indirect
83+
sigs.k8s.io/e2e-framework v0.2.0 // indirect
8284
)
8385

8486
require (

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,8 @@ github.com/vbatts/tar-split v0.11.3 h1:hLFqsOLQ1SsppQNTMpkpPXClLDfC2A3Zgy9OUU+RV
810810
github.com/vbatts/tar-split v0.11.3/go.mod h1:9QlHN18E+fEH7RdG+QAJJcuya3rqT7eXSTY7wGrAokY=
811811
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
812812
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
813+
github.com/vladimirvivien/gexe v0.2.0 h1:nbdAQ6vbZ+ZNsolCgSVb9Fno60kzSuvtzVh6Ytqi/xY=
814+
github.com/vladimirvivien/gexe v0.2.0/go.mod h1:LHQL00w/7gDUKIak24n801ABp8C+ni6eBht9vGVst8w=
813815
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
814816
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
815817
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
@@ -1490,6 +1492,8 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
14901492
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
14911493
sigs.k8s.io/controller-runtime v0.15.1 h1:9UvgKD4ZJGcj24vefUFgZFP3xej/3igL9BsOUTb/+4c=
14921494
sigs.k8s.io/controller-runtime v0.15.1/go.mod h1:7ngYvp1MLT+9GeZ+6lH3LOlcHkp/+tzA/fmHa4iq9kk=
1495+
sigs.k8s.io/e2e-framework v0.2.0 h1:gD6AWWAHFcHibI69E9TgkNFhh0mVwWtRCHy2RU057jQ=
1496+
sigs.k8s.io/e2e-framework v0.2.0/go.mod h1:E6JXj/V4PIlb95jsn2WrNKG+Shb45xaaI7C0+BH4PL8=
14931497
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
14941498
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
14951499
sigs.k8s.io/kustomize/api v0.13.2 h1:kejWfLeJhUsTGioDoFNJET5LQe/ajzXhJGYoU+pJsiA=
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package e2e
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"os"
9+
"os/exec"
10+
"strings"
11+
"testing"
12+
13+
"github.com/replicatedhq/troubleshoot/pkg/convert"
14+
appsv1 "k8s.io/api/apps/v1"
15+
corev1 "k8s.io/api/core/v1"
16+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17+
"sigs.k8s.io/e2e-framework/pkg/envconf"
18+
"sigs.k8s.io/e2e-framework/pkg/features"
19+
)
20+
21+
func TestPendingPod(t *testing.T) {
22+
supportBundleName := "pod-deployment"
23+
deploymentName := "test-pending-deployment"
24+
containerName := "curl"
25+
feature := features.New("Pending Pod Test").
26+
Setup(func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
27+
deployment := newDeployment(c.Namespace(), deploymentName, 1, containerName)
28+
client, err := c.NewClient()
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
if err = client.Resources().Create(ctx, deployment); err != nil {
33+
t.Fatal(err)
34+
}
35+
36+
return ctx
37+
}).
38+
Assess("check support bundle catch pending pod", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
39+
var out bytes.Buffer
40+
var results []*convert.Result
41+
42+
tarPath := fmt.Sprintf("%s.tar.gz", supportBundleName)
43+
targetFile := fmt.Sprintf("%s/analysis.json", supportBundleName)
44+
45+
cmd := exec.Command("../../../bin/support-bundle", "spec/pod.yaml", "--interactive=false", fmt.Sprintf("-o=%s", supportBundleName))
46+
cmd.Stdout = &out
47+
err := cmd.Run()
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
52+
analysisJSON, err := readFileFromTar(tarPath, targetFile)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
57+
err = json.Unmarshal(analysisJSON, &results)
58+
if err != nil {
59+
t.Fatal(err)
60+
}
61+
62+
for _, result := range results {
63+
if strings.Contains(result.Insight.Detail, deploymentName) {
64+
return ctx
65+
}
66+
}
67+
68+
t.Fatal("Pending pod not found")
69+
defer func() {
70+
err := os.Remove(fmt.Sprintf("%s.tar.gz", supportBundleName))
71+
if err != nil {
72+
t.Fatal("Error remove file:", err)
73+
}
74+
}()
75+
return ctx
76+
}).Feature()
77+
testenv.Test(t, feature)
78+
}
79+
80+
func newDeployment(namespace string, name string, replicas int32, containerName string) *appsv1.Deployment {
81+
labels := map[string]string{"app": "pending-test"}
82+
return &appsv1.Deployment{
83+
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
84+
Spec: appsv1.DeploymentSpec{
85+
Replicas: &replicas,
86+
Selector: &metav1.LabelSelector{
87+
MatchLabels: labels,
88+
},
89+
Template: corev1.PodTemplateSpec{
90+
ObjectMeta: metav1.ObjectMeta{Labels: labels},
91+
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: containerName, Image: "nginx", Command: []string{"wge", "-O", "/work-dir/index.html", "https://www.wikipedia.org"}}}},
92+
},
93+
},
94+
}
95+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 TestClusterResources(t *testing.T) {
17+
tests := []struct {
18+
paths []string
19+
expectType string
20+
}{
21+
{
22+
paths: []string{
23+
"clusterroles.json",
24+
"volumeattachments.json",
25+
"nodes.json",
26+
"pvs.json",
27+
"pod-disruption-budgets-errors.json",
28+
"resources.json",
29+
"cronjobs-errors.json",
30+
"custom-resource-definitions.json",
31+
"groups.json",
32+
"priorityclasses.json",
33+
"namespaces.json",
34+
"clusterrolebindings.json",
35+
"storage-classes.json",
36+
},
37+
expectType: "file",
38+
},
39+
{
40+
paths: []string{
41+
"limitranges",
42+
"daemonsets",
43+
"deployments",
44+
"pvcs",
45+
"leases",
46+
"auth-cani-list",
47+
"services",
48+
"roles",
49+
"events",
50+
"rolebindings",
51+
"statefulsets-errors.json",
52+
"jobs",
53+
"serviceaccounts",
54+
"configmaps",
55+
"statefulsets",
56+
"endpoints",
57+
"network-policy",
58+
"resource-quota",
59+
"ingress",
60+
"pods",
61+
},
62+
expectType: "folder",
63+
},
64+
}
65+
66+
feature := features.New("Cluster Resouces Test").
67+
Assess("check support bundle catch cluster resouces", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
68+
var out bytes.Buffer
69+
supportBundleName := "cluster-resources"
70+
tarPath := fmt.Sprintf("%s.tar.gz", supportBundleName)
71+
targetFolder := fmt.Sprintf("%s/cluster-resources/", supportBundleName)
72+
cmd := exec.Command("../../../bin/support-bundle", "spec/clusterResources.yaml", "--interactive=false", fmt.Sprintf("-o=%s", supportBundleName))
73+
cmd.Stdout = &out
74+
err := cmd.Run()
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
79+
defer func() {
80+
err := os.Remove(fmt.Sprintf("%s.tar.gz", supportBundleName))
81+
if err != nil {
82+
t.Fatal("Error remove file:", err)
83+
}
84+
}()
85+
86+
files, folders, err := readFilesAndFoldersFromTar(tarPath, targetFolder)
87+
88+
if err != nil {
89+
t.Fatal(err)
90+
}
91+
92+
for _, test := range tests {
93+
if test.expectType == "file" {
94+
for _, path := range test.paths {
95+
if !slices.Contains(files, path) {
96+
t.Fatalf("Expected file %s not found", path)
97+
}
98+
}
99+
} else if test.expectType == "folder" {
100+
for _, path := range test.paths {
101+
if !slices.Contains(folders, path) {
102+
t.Fatalf("Expected folder %s not found", path)
103+
}
104+
}
105+
}
106+
}
107+
108+
return ctx
109+
}).Feature()
110+
testenv.Test(t, feature)
111+
}

0 commit comments

Comments
 (0)