Skip to content

Commit 931e9a7

Browse files
authored
Use fakeclient instead of envtest (#44)
1 parent 535c2cd commit 931e9a7

File tree

4 files changed

+9
-191
lines changed

4 files changed

+9
-191
lines changed

Makefile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
SOURCES := $(shell find . -name '*.go' -not -path "*/vendor/*" -not -path "*/.git/*")
22
.DEFAULT_GOAL := build
3-
ENVTEST_ASSETS_DIR = $(shell pwd)/testbin
4-
SHELL = /bin/bash
53

64
build: $(SOURCES) ## Build Test
75
go build -i -ldflags="-s -w" ./...
@@ -18,11 +16,7 @@ fmt: ## Run go fmt
1816
fmtcheck: ## Check go formatting
1917
@gofmt -l $(SOURCES) | grep ".*\.go"; if [ "$$?" = "0" ]; then exit 1; fi
2018

21-
export KUBEBUILDER_ASSETS := $(ENVTEST_ASSETS_DIR)/bin
2219
test: ## Run unit tests
23-
mkdir -p $(ENVTEST_ASSETS_DIR)
24-
test -f $(ENVTEST_ASSETS_DIR)/setup-envtest.sh || curl -sSLo $(ENVTEST_ASSETS_DIR)/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.6.3/hack/setup-envtest.sh
25-
. $(ENVTEST_ASSETS_DIR)/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR)
2620
@go test -race -covermode atomic -coverprofile cover.out ./...
2721

2822
vet: ## Run go vet

conditions/conditions_suite_test.go

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -15,101 +15,15 @@
1515
package conditions
1616

1717
import (
18-
"fmt"
19-
"os"
20-
"os/exec"
21-
"path/filepath"
2218
"testing"
2319

2420
. "github.com/onsi/ginkgo"
2521
. "github.com/onsi/gomega"
26-
apiv1 "github.com/operator-framework/api/pkg/operators/v1"
27-
"k8s.io/apimachinery/pkg/runtime"
28-
"k8s.io/apimachinery/pkg/util/rand"
29-
"k8s.io/client-go/rest"
30-
"sigs.k8s.io/controller-runtime/pkg/envtest"
22+
3123
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
32-
logf "sigs.k8s.io/controller-runtime/pkg/log"
33-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3424
)
3525

3626
func TestSource(t *testing.T) {
3727
RegisterFailHandler(Fail)
3828
RunSpecsWithDefaultAndCustomReporters(t, "Conditions Suite", []Reporter{printer.NewlineReporter{}, printer.NewProwReporter("Conditions Suite")})
3929
}
40-
41-
var testenv *envtest.Environment
42-
var cfg *rest.Config
43-
var sch = runtime.NewScheme()
44-
var err error
45-
var tempDir = fmt.Sprintf("%s_%d", "temp", rand.Int63nRange(0, 1000000))
46-
47-
const (
48-
olmYAMLURL = "https://github.com/operator-framework/operator-lifecycle-manager/releases/download/v0.17.0/olm.yaml"
49-
crdsYAMLURL = "https://github.com/operator-framework/operator-lifecycle-manager/releases/download/v0.17.0/crds.yaml"
50-
51-
// TODO: Remove this once OLM releases operator conditions CRD set
52-
condCRDYAML = "https://raw.githubusercontent.com/dinhxuanvu/operator-lifecycle-manager/create-operatorconditions-for-operator/deploy/chart/crds/0000_50_olm_00-operatorconditions.crd.yaml"
53-
)
54-
55-
var _ = BeforeSuite(func(done Done) {
56-
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
57-
58-
err = getOLMManifests()
59-
Expect(err).NotTo(HaveOccurred())
60-
// Add operator apiv1 to scheme
61-
err = apiv1.AddToScheme(sch)
62-
Expect(err).NotTo(HaveOccurred())
63-
64-
testenv = &envtest.Environment{}
65-
testenv.CRDInstallOptions = envtest.CRDInstallOptions{
66-
Paths: []string{tempDir},
67-
}
68-
69-
cfg, err = testenv.Start()
70-
Expect(err).NotTo(HaveOccurred())
71-
72-
close(done)
73-
}, 60)
74-
75-
var _ = AfterSuite(func() {
76-
// remove tmp folder
77-
os.RemoveAll(tempDir)
78-
Expect(err).NotTo(HaveOccurred())
79-
Expect(testenv.Stop()).To(Succeed())
80-
})
81-
82-
func getOLMManifests() error {
83-
// create a directory
84-
cmd := exec.Command("mkdir", tempDir)
85-
err := cmd.Run()
86-
if err != nil {
87-
return err
88-
}
89-
90-
// fetch manifests to install olm
91-
err = getYAML(filepath.Join(tempDir, "olm.yaml"), olmYAMLURL)
92-
if err != nil {
93-
return fmt.Errorf("error fetching olm.yaml %v", err)
94-
}
95-
96-
err = getYAML(filepath.Join(tempDir, "crds.yaml"), crdsYAMLURL)
97-
if err != nil {
98-
return fmt.Errorf("error fetching crds.yaml %v", err)
99-
}
100-
101-
err = getYAML(filepath.Join(tempDir, "operatorconditions.crd.yaml"), condCRDYAML)
102-
if err != nil {
103-
return fmt.Errorf("error fetching operator conditions crd %v", err)
104-
}
105-
return nil
106-
}
107-
108-
func getYAML(file, url string) error {
109-
cmd := exec.Command("curl", "-sSLo", file, url)
110-
err := cmd.Run()
111-
if err != nil {
112-
return err
113-
}
114-
return nil
115-
}

conditions/conditions_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ import (
2424
apiv1 "github.com/operator-framework/api/pkg/operators/v1"
2525
"k8s.io/apimachinery/pkg/api/meta"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/runtime"
2728
"k8s.io/apimachinery/pkg/types"
2829
kubeclock "k8s.io/apimachinery/pkg/util/clock"
2930
"sigs.k8s.io/controller-runtime/pkg/client"
31+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3032
)
3133

3234
const (
@@ -43,8 +45,10 @@ var _ = Describe("Condition", func() {
4345
var err error
4446

4547
BeforeEach(func() {
46-
cl, err = client.New(cfg, client.Options{})
48+
sch := runtime.NewScheme()
49+
err = apiv1.AddToScheme(sch)
4750
Expect(err).NotTo(HaveOccurred())
51+
cl = fake.NewFakeClientWithScheme(sch)
4852
})
4953

5054
Describe("NewCondition", func() {
@@ -102,8 +106,10 @@ var _ = Describe("Condition", func() {
102106
}
103107

104108
// create a new client
105-
cl, err = client.New(cfg, client.Options{Scheme: sch})
109+
sch := runtime.NewScheme()
110+
err = apiv1.AddToScheme(sch)
106111
Expect(err).NotTo(HaveOccurred())
112+
cl = fake.NewFakeClientWithScheme(sch)
107113

108114
// create an operator Condition resource
109115
err = cl.Create(ctx, operatorCond)

testbin/setup-envtest.sh

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)