Skip to content

Commit 5e4225f

Browse files
fix test
1 parent 7fae6cf commit 5e4225f

4 files changed

Lines changed: 115 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ index_build_*/
3333

3434
*.bak
3535
.DS_Store
36+
.cache/
3637
config/crd/bases/vpa-v1-crd.yaml
3738
Listeners
3839
.tool-versions

Makefile

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ GOBIN=$(shell go env GOBIN)
4747
endif
4848

4949
BUILD_DATETIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
50+
GOCACHE ?= $(shell pwd)/.cache/go-build
51+
export GOCACHE
5052

5153
## Location to install dependencies to
5254
LOCALBIN ?= $(shell pwd)/bin
@@ -65,7 +67,21 @@ all: manager
6567
# Run tests
6668
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
6769
test: generate fmt vet manifests envtest
68-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn go test ./... -coverprofile cover.out
70+
@assets="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)"; \
71+
echo "mode: set" > cover.out; \
72+
for pkg in $$(go list ./...); do \
73+
gocache=$$(mktemp -d); \
74+
has_tests=$$(go list -f '{{if or .TestGoFiles .XTestGoFiles}}yes{{end}}' "$$pkg"); \
75+
if [ -n "$$has_tests" ]; then \
76+
profile=$$(mktemp); \
77+
KUBEBUILDER_ASSETS="$$assets" GOCACHE="$$gocache" GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn go test "$$pkg" -coverprofile "$$profile" || { rm -rf "$$gocache" "$$profile"; exit 1; }; \
78+
tail -n +2 "$$profile" >> cover.out; \
79+
rm -f "$$profile"; \
80+
else \
81+
KUBEBUILDER_ASSETS="$$assets" GOCACHE="$$gocache" GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn go test "$$pkg" || { rm -rf "$$gocache"; exit 1; }; \
82+
fi; \
83+
rm -rf "$$gocache"; \
84+
done
6985

7086
test-ginkgo: generate fmt vet manifests envtest
7187
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" GOLANG_PROTOBUF_REGISTRATION_CONFLICT=warn go test ./controllers/ -v -ginkgo.v
@@ -302,4 +318,4 @@ redhat-certificated-image-push: ## Push the bundle image.
302318
.PHONY: generate-metricsdocs
303319
generate-metricsdocs:
304320
mkdir -p $(shell pwd)/docs/monitoring
305-
go run -ldflags="${LDFLAGS}" ./pkg/monitoring/metricsdocs > docs/monitoring/metrics.md
321+
go run -ldflags="${LDFLAGS}" ./pkg/monitoring/metricsdocs > docs/monitoring/metrics.md

controllers/suite_test.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
package controllers
1919

2020
import (
21+
"os"
22+
"os/exec"
2123
"path/filepath"
24+
"strings"
2225
"testing"
2326
"time"
2427

@@ -48,6 +51,7 @@ var sourceReconciler *SourceReconciler
4851
var sinkReconciler *SinkReconciler
4952
var funcReconciler *FunctionReconciler
5053
var testEnv *envtest.Environment
54+
var testVpaCRDDir string
5155

5256
func TestAPIs(t *testing.T) {
5357
RegisterFailHandler(Fail)
@@ -59,12 +63,17 @@ var _ = BeforeSuite(func(done Done) {
5963
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
6064

6165
By("bootstrapping test environment")
66+
vpaCRDDir, err := vpaCRDDirectory()
67+
Expect(err).ToNot(HaveOccurred())
68+
testVpaCRDDir = vpaCRDDir
6269
testEnv = &envtest.Environment{
63-
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")},
70+
CRDDirectoryPaths: []string{
71+
filepath.Join("..", "config", "crd", "bases"),
72+
vpaCRDDir,
73+
},
6474
ErrorIfCRDPathMissing: true,
6575
}
6676

67-
var err error
6877
cfg, err = testEnv.Start()
6978
Expect(err).ToNot(HaveOccurred())
7079
Expect(cfg).ToNot(BeNil())
@@ -137,10 +146,39 @@ var _ = BeforeSuite(func(done Done) {
137146
// kube-apiserver to return
138147
err := testEnv.Stop()
139148
Expect(err).ToNot(HaveOccurred())
149+
if testVpaCRDDir != "" {
150+
Expect(os.RemoveAll(testVpaCRDDir)).To(Succeed())
151+
}
140152
}()
141153

142154
k8sClient = k8sManager.GetClient()
143155
Expect(k8sClient).ToNot(BeNil())
144156

145157
close(done)
146158
}, 60)
159+
160+
func vpaCRDDirectory() (string, error) {
161+
cmd := exec.Command("go", "list", "-m", "-f", "{{.Dir}}", "k8s.io/autoscaler/vertical-pod-autoscaler")
162+
output, err := cmd.Output()
163+
if err != nil {
164+
return "", err
165+
}
166+
167+
sourcePath := filepath.Join(strings.TrimSpace(string(output)), "deploy", "vpa-v1-crd-gen.yaml")
168+
crdContents, err := os.ReadFile(sourcePath)
169+
if err != nil {
170+
return "", err
171+
}
172+
173+
tempDir, err := os.MkdirTemp("", "function-mesh-vpa-crd-*")
174+
if err != nil {
175+
return "", err
176+
}
177+
178+
if err := os.WriteFile(filepath.Join(tempDir, filepath.Base(sourcePath)), crdContents, 0o600); err != nil {
179+
_ = os.RemoveAll(tempDir)
180+
return "", err
181+
}
182+
183+
return tempDir, nil
184+
}

license_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919

2020
import (
2121
"os"
22+
"os/exec"
2223
"path/filepath"
2324
"regexp"
2425
"strings"
@@ -75,6 +76,8 @@ var skip = map[string]bool{
7576
}
7677

7778
func TestLicense(t *testing.T) {
79+
trackedFiles, trackedDirs := gitTrackedPaths()
80+
7881
err := filepath.Walk(".", func(path string, fi os.FileInfo, err error) error {
7982
if skip[path] {
8083
return nil
@@ -84,6 +87,25 @@ func TestLicense(t *testing.T) {
8487
return err
8588
}
8689

90+
cleanPath := filepath.Clean(path)
91+
if fi.IsDir() {
92+
if cleanPath == "vendor" || cleanPath == ".git" {
93+
return filepath.SkipDir
94+
}
95+
if len(trackedDirs) > 0 && cleanPath != "." {
96+
if _, ok := trackedDirs[cleanPath]; !ok {
97+
return filepath.SkipDir
98+
}
99+
}
100+
return nil
101+
}
102+
103+
if len(trackedFiles) > 0 {
104+
if _, ok := trackedFiles[cleanPath]; !ok {
105+
return nil
106+
}
107+
}
108+
87109
switch filepath.Ext(path) {
88110
case ".go":
89111
if strings.Contains(path, "zz_generated") {
@@ -133,3 +155,37 @@ func TestLicense(t *testing.T) {
133155
t.Fatal(err)
134156
}
135157
}
158+
159+
func gitTrackedPaths() (map[string]struct{}, map[string]struct{}) {
160+
cmd := exec.Command("git", "ls-files", "-z")
161+
output, err := cmd.Output()
162+
if err != nil {
163+
return nil, nil
164+
}
165+
166+
files := make(map[string]struct{})
167+
dirs := map[string]struct{}{
168+
".": {},
169+
}
170+
171+
for _, file := range strings.Split(string(output), "\x00") {
172+
if file == "" {
173+
continue
174+
}
175+
176+
cleanFile := filepath.Clean(file)
177+
files[cleanFile] = struct{}{}
178+
179+
dir := filepath.Dir(cleanFile)
180+
for dir != "." && dir != string(filepath.Separator) {
181+
dirs[dir] = struct{}{}
182+
next := filepath.Dir(dir)
183+
if next == dir {
184+
break
185+
}
186+
dir = next
187+
}
188+
}
189+
190+
return files, dirs
191+
}

0 commit comments

Comments
 (0)