-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Guilherme Cassolato <[email protected]>
- Loading branch information
1 parent
5445f17
commit e78b382
Showing
10 changed files
with
209 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Setting SHELL to bash allows bash commands to be executed by recipes. | ||
# Options are set to exit when a recipe line exits non-zero or a piped command fails. | ||
SHELL = /usr/bin/env bash -o pipefail | ||
.SHELLFLAGS = -ec | ||
|
||
MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST))) | ||
PROJECT_PATH := $(patsubst %/,%,$(dir $(MKFILE_PATH))) | ||
|
||
##@ General | ||
|
||
# The help target prints out all targets with their descriptions organized | ||
# beneath their categories. The categories are represented by '##@' and the | ||
# target descriptions by '##'. The awk commands is responsible for reading the | ||
# entire set of makefiles included in this invocation, looking for lines of the | ||
# file as xyz: ## something, and then pretty-format the target and help. Then, | ||
# if there's a line with ##@ something, that gets pretty-printed as a category. | ||
# More info on the usage of ANSI control characters for terminal formatting: | ||
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters | ||
# More info on the awk command: | ||
# http://linuxcommand.org/lc3_adv_awk.php | ||
|
||
help: ## Display this help. | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-30s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) | ||
|
||
.PHONY: project-path | ||
project-path: ## Print the project path. | ||
@echo $(PROJECT_PATH) | ||
|
||
##@ Tools | ||
|
||
# go-install-tool will 'go install' any package $2 and install it to $1. | ||
define go-install-tool | ||
@[ -f $(1) ] || { \ | ||
set -e ;\ | ||
TMP_DIR=$$(mktemp -d) ;\ | ||
cd $$TMP_DIR ;\ | ||
go mod init tmp ;\ | ||
echo "Downloading $(2)" ;\ | ||
GOBIN=$(PROJECT_PATH)/bin go install $(2) ;\ | ||
rm -rf $$TMP_DIR ;\ | ||
} | ||
endef | ||
|
||
KIND = $(PROJECT_PATH)/bin/kind | ||
KIND_VERSION = v0.23.0 | ||
$(KIND): | ||
$(call go-install-tool,$(KIND),sigs.k8s.io/kind@$(KIND_VERSION)) | ||
|
||
.PHONY: kind | ||
kind: $(KIND) ## Download kind locally if necessary. | ||
|
||
##@ Development | ||
|
||
.PHONY: test | ||
test: ## Run tests. | ||
$(MAKE) test-unit | ||
$(MAKE) test-integration | ||
|
||
.PHONY: test-unit | ||
test-unit: ## Run unit tests. | ||
go test -tags=unit -v ./... | ||
|
||
.PHONY: test-integration | ||
test-integration: kind ## Run integration tests. | ||
$(KIND) create cluster --name test-integration | ||
go test -tags=integration -v ./... | ||
$(KIND) delete cluster --name test-integration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:build integration | ||
|
||
package controller | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestStartControllerManaged(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
testManager, err := buildStartableTestManager() | ||
if err != nil { | ||
t.Errorf("expected no error when building test manager, got %s", err.Error()) | ||
} | ||
c := NewController(ManagedBy(testManager)) | ||
go func() { | ||
if err := c.Start(ctx); err != nil { | ||
t.Errorf("expected no error when starting manager, got %s", err.Error()) | ||
cancel() | ||
} | ||
}() | ||
time.Sleep(3 * time.Second) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//go:build integration | ||
|
||
package controller | ||
|
||
import ( | ||
"k8s.io/client-go/rest" | ||
"k8s.io/utils/ptr" | ||
|
||
ctrlruntimeenvtest "sigs.k8s.io/controller-runtime/pkg/envtest" | ||
ctrlruntimemanager "sigs.k8s.io/controller-runtime/pkg/manager" | ||
) | ||
|
||
var ( | ||
testConfig *rest.Config | ||
testEnv *ctrlruntimeenvtest.Environment | ||
testEnvStarted bool | ||
) | ||
|
||
// startTestEnv starts the test environment if it hasn't been started yet | ||
// It requires a Kubernetes cluster running in the context | ||
func startTestEnv() { | ||
if testEnvStarted { | ||
return | ||
} | ||
testEnv = &ctrlruntimeenvtest.Environment{ | ||
Scheme: testScheme, | ||
UseExistingCluster: ptr.To(true), | ||
} | ||
var err error | ||
testConfig, err = testEnv.Start() | ||
if err != nil { | ||
panic(err) | ||
} | ||
testEnvStarted = true | ||
} | ||
|
||
// buildStartableTestManager builds a startable manager for testing | ||
func buildStartableTestManager() (ctrlruntimemanager.Manager, error) { | ||
startTestEnv() // lazy loading the environment and config | ||
|
||
return ctrlruntimemanager.New(testConfig, ctrlruntimemanager.Options{ | ||
Logger: testLogger, | ||
Scheme: testScheme, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.