Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2e test #241

Merged
merged 17 commits into from
Jul 22, 2024
21 changes: 21 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: E2E
on:
pull_request:
branches:
- main
jobs:
create-cluster:
name: Create Kind cluster
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install GO
uses: actions/setup-go@v4
with:
go-version-file: go.mod
cache: false
- name: Create kind cluster
uses: helm/[email protected]
- run: kind get clusters
- run: go test -v -race ./pkg/cache/... -tags="e2e"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ velero-crds:

.PHONY: test
test: envtest ## run tests
@KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(GOPATH)/bin -p path)" go test $$(go list ./... | grep -v /e2e) -v
@KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(GOPATH)/bin -p path)" go test $$(go list ./... | grep -v /e2e) -v -tags="cache"

.PHONY: lint
lint: $(PRE) ## run linters
Expand Down
70 changes: 70 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//go:build cache

package cache

import (
"context"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

func TestCache(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Test cache")
}

var _ = Describe("Resource cache", Ordered, func() {
Context("Resource cache", func() {
const (
resourceName = "default"
namespace = "default"
key = "key"
)
rce := &ResourceCacheEntry{}
pod := v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: namespace,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "test",
Image: "test",
},
},
},
}
cache := NewCache[*ResourceCacheEntry](context.Background(), time.Second)

It("check cache", func() {
res, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&pod)
Expect(err).ToNot(HaveOccurred())
unstructuredPod := unstructured.Unstructured{Object: res}
Expect(rce.SetSHA(unstructuredPod, ApplySHA)).ToNot(HaveOccurred())
Expect(rce.SetSHA(unstructuredPod, ManifestSHA)).ToNot(HaveOccurred())
Expect(rce.SetSHA(unstructuredPod, ServerSHA)).ToNot(HaveOccurred())

cache.Set(key, rce)
cachedResource, ok := cache.Get(key)
Expect(ok).To(BeTrue())
Expect(cachedResource).To(Equal(rce))
// should expire and clean applySHA and manifestSHA
time.Sleep(1 * time.Second)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to get rid of these sleep calls? Looks like potential race condition and it will make tests run longer.

cachedResource, ok = cache.Get(key)
Expect(ok).To(BeTrue())
Expect(cachedResource.applySHA).Should(BeNil())
Expect(cachedResource.manifestSHA).Should(BeNil())
Expect(cachedResource.serverSHA).ShouldNot(BeNil())
})

})

})
55 changes: 55 additions & 0 deletions pkg/cache/resource_cache_entry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build cache

package cache

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

var _ = Describe("Resource cache entry", Ordered, func() {
Context("Resource cache entry", func() {
const (
resourceName = "default"
namespace = "default"
)
rce := ResourceCacheEntry{}
pod := v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: namespace,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "test",
Image: "test",
},
},
},
}

It("check ResourceCacheEntry", func() {
res, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&pod)
Expect(err).ToNot(HaveOccurred())
unstructuredPod := unstructured.Unstructured{Object: res}
Expect(rce.SetSHA(unstructuredPod, ApplySHA)).ToNot(HaveOccurred())
Expect(rce.SetSHA(unstructuredPod, ManifestSHA)).ToNot(HaveOccurred())
Expect(rce.SetSHA(unstructuredPod, ServerSHA)).ToNot(HaveOccurred())

Expect(rce.RequiresApply("test")).Should(BeTrue())
Expect(rce.RequiresApply("U33NQLAAPDEC5RDDKQ2KUHCUHIQUOC4PLMCQ5QVBYZ53B6V5UI5A====")).Should(BeFalse())

rce.Expire()
Expect(rce.applySHA).Should(BeNil())
Expect(rce.manifestSHA).Should(BeNil())
Expect(rce.serverSHA).ShouldNot(BeNil())
})

})

})
Loading
Loading