Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions build/test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ REL_VERSION ?=latest
DOCKER_REGISTRY ?=ghcr.io/radius-project/dev
ENVTEST_ASSETS_DIR=$(shell pwd)/bin
K8S_VERSION=1.30.*
ENVTEST_ARCH ?= $(shell go env GOHOSTARCH)
ENV_SETUP=$(GOBIN)/setup-envtest$(BINARY_EXT)

# Use gotestsum if available, otherwise use go test. We want to enable testing with just 'make test'
Expand All @@ -55,8 +56,8 @@ GOTEST_TOOL ?= gotestsum $(GOTESTSUM_OPTS) --
endif

.PHONY: test
test: test-get-envtools test-helm ## Runs unit tests, excluding kubernetes controller tests
KUBEBUILDER_ASSETS="$(shell $(ENV_SETUP) use -p path ${K8S_VERSION} --arch amd64)" CGO_ENABLED=1 $(GOTEST_TOOL) -v ./pkg/... $(GOTEST_OPTS)
test: test-get-envtools test-helm ## Runs Go tests
KUBEBUILDER_ASSETS="$(shell $(ENV_SETUP) use -p path ${K8S_VERSION} --arch $(ENVTEST_ARCH))" CGO_ENABLED=1 $(GOTEST_TOOL) -v ./pkg/... $(GOTEST_OPTS)

.PHONY: test-compile
test-compile: test-get-envtools ## Compiles all tests without running them
Expand All @@ -69,7 +70,7 @@ test-get-envtools:
$(call go-install-tool,$(ENV_SETUP),sigs.k8s.io/controller-runtime/tools/setup-envtest@release-0.20)
@echo "$(ARROW) Instructions:"
@echo "$(ARROW) Set environment variable KUBEBUILDER_ASSETS for tests."
@echo "$(ARROW) KUBEBUILDER_ASSETS=\"$(shell $(ENV_SETUP) use -p path ${K8S_VERSION} --arch amd64)\""
@echo "$(ARROW) KUBEBUILDER_ASSETS=\"$(shell $(ENV_SETUP) use -p path ${K8S_VERSION} --arch $(ENVTEST_ARCH))\""

.PHONY: test-validate-cli
test-validate-cli: ## Run cli integration tests
Expand Down
22 changes: 1 addition & 21 deletions pkg/controller/reconciler/deployment_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (

const (
deploymentTestWaitDuration = time.Second * 10
deploymentTestWaitInterval = time.Second * 1
deploymentTestWaitInterval = 200 * time.Millisecond
deploymentTestControllerDelayInterval = time.Millisecond * 100
)

Expand Down Expand Up @@ -546,18 +546,15 @@ func Test_DeploymentReconciler_RadiusDisabled_ThenRadiusDisabled(t *testing.T) {
func waitForStateWaiting(t *testing.T, client client.Client, name types.NamespacedName) *deploymentAnnotations {
ctx := testcontext.New(t)

logger := t
var annotations deploymentAnnotations
require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Logf("Fetching Deployment: %+v", name)
current := &appsv1.Deployment{}
err := client.Get(ctx, name, current)
require.NoError(t, err)

annotations, err = readAnnotations(current)
require.NoError(t, err)
assert.NotNil(t, annotations)
logger.Logf("Annotations.Status: %+v", annotations.Status)

if assert.NotNil(t, annotations.Status) && assert.Equal(t, deploymentPhraseWaiting, annotations.Status.Phrase) {
assert.Empty(t, annotations.Status.Operation)
Expand All @@ -570,18 +567,15 @@ func waitForStateWaiting(t *testing.T, client client.Client, name types.Namespac
func waitForStateUpdating(t *testing.T, client client.Client, name types.NamespacedName) *deploymentAnnotations {
ctx := testcontext.New(t)

logger := t
var annotations deploymentAnnotations
require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Logf("Fetching Deployment: %+v", name)
current := &appsv1.Deployment{}
err := client.Get(ctx, name, current)
require.NoError(t, err)

annotations, err = readAnnotations(current)
require.NoError(t, err)
assert.NotNil(t, annotations)
logger.Logf("Annotations.Status: %+v", annotations.Status)

if assert.NotNil(t, annotations.Status) && assert.Equal(t, deploymentPhraseUpdating, annotations.Status.Phrase) {
assert.NotEmpty(t, annotations.Status.Operation)
Expand All @@ -594,18 +588,15 @@ func waitForStateUpdating(t *testing.T, client client.Client, name types.Namespa
func waitForStateReady(t *testing.T, client client.Client, name types.NamespacedName) *deploymentAnnotations {
ctx := testcontext.New(t)

logger := t
var annotations deploymentAnnotations
require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Logf("Fetching Deployment: %+v", name)
current := &appsv1.Deployment{}
err := client.Get(ctx, name, current)
require.NoError(t, err)

annotations, err = readAnnotations(current)
require.NoError(t, err)
assert.NotNil(t, annotations)
logger.Logf("Annotations.Status: %+v", annotations.Status)

if assert.NotNil(t, annotations.Status) && assert.Equal(t, deploymentPhraseReady, annotations.Status.Phrase) {
assert.Empty(t, annotations.Status.Operation)
Expand All @@ -618,18 +609,15 @@ func waitForStateReady(t *testing.T, client client.Client, name types.Namespaced
func waitForStateDeleting(t *testing.T, client client.Client, name types.NamespacedName) *deploymentAnnotations {
ctx := testcontext.New(t)

logger := t
var annotations deploymentAnnotations
require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Logf("Fetching Deployment: %+v", name)
current := &appsv1.Deployment{}
err := client.Get(ctx, name, current)
require.NoError(t, err)

annotations, err = readAnnotations(current)
require.NoError(t, err)
assert.NotNil(t, annotations)
logger.Logf("Annotations.Status: %+v", annotations.Status)

if assert.NotNil(t, annotations.Status) && assert.Equal(t, deploymentPhraseDeleting, annotations.Status.Phrase) {
assert.NotEmpty(t, annotations.Status.Operation)
Expand All @@ -651,11 +639,8 @@ type expectedEvent struct {
// We can have multiple events as the result of the List function but we are only interested in the expected event.
func waitForEvent(t *testing.T, client client.Client, event expectedEvent) {
ctx := testcontext.New(t)
logger := t

require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Log("Fetching Events")

events := &corev1.EventList{}
err := client.List(ctx, events)
require.NoError(t, err)
Expand All @@ -675,15 +660,12 @@ func waitForEvent(t *testing.T, client client.Client, event expectedEvent) {
func waitForRadiusContainerDeleted(t *testing.T, client client.Client, name types.NamespacedName) *deploymentAnnotations {
ctx := testcontext.New(t)

logger := t
var annotations *deploymentAnnotations
require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Logf("Fetching Deployment: %+v", name)
current := &appsv1.Deployment{}
err := client.Get(ctx, name, current)
require.NoError(t, err)

logger.Logf("Annotations: %+v", current.Annotations)
assert.NotContains(t, current.Annotations, AnnotationRadiusStatus)
assert.NotContains(t, current.Annotations, AnnotationRadiusConfigurationHash)
}, deploymentTestWaitDuration, deploymentTestWaitInterval, "waiting for state to be Deleting")
Expand All @@ -694,9 +676,7 @@ func waitForRadiusContainerDeleted(t *testing.T, client client.Client, name type
func waitForDeploymentDeleted(t *testing.T, client client.Client, name types.NamespacedName) {
ctx := testcontext.New(t)

logger := t
require.Eventuallyf(t, func() bool {
logger.Logf("Fetching Deployment: %+v", name)
err := client.Get(ctx, name, &appsv1.Deployment{})
return apierrors.IsNotFound(err)
}, deploymentTestWaitDuration, deploymentTestWaitInterval, "waiting for deployment to be deleted")
Expand Down
17 changes: 2 additions & 15 deletions pkg/controller/reconciler/deploymentresource_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (

const (
DeploymentResourceTestWaitDuration = time.Second * 10
DeploymentResourceTestWaitInterval = time.Second * 1
DeploymentResourceTestWaitInterval = 200 * time.Millisecond
DeploymentResourceTestControllerDelayInterval = time.Millisecond * 100

TestDeploymentResourceNamespace = "deploymentresource-basic"
Expand Down Expand Up @@ -125,16 +125,13 @@ func Test_DeploymentResourceReconciler_Basic(t *testing.T) {
func waitForDeploymentResourceStateReady(t *testing.T, client k8sClient.Client, name types.NamespacedName) *radappiov1alpha3.DeploymentResourceStatus {
ctx := testcontext.New(t)

logger := t
status := &radappiov1alpha3.DeploymentResourceStatus{}
require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Logf("Fetching DeploymentResource: %+v", name)
current := &radappiov1alpha3.DeploymentResource{}
err := client.Get(ctx, name, current)
require.NoError(t, err)

status = &current.Status
logger.Logf("DeploymentResource.Status: %+v", current.Status)
if assert.Equal(t, radappiov1alpha3.DeploymentResourcePhraseReady, current.Status.Phrase) {
assert.Empty(t, current.Status.Operation)
}
Expand All @@ -146,16 +143,13 @@ func waitForDeploymentResourceStateReady(t *testing.T, client k8sClient.Client,
func waitForDeploymentResourceStateDeleting(t *testing.T, client k8sClient.Client, name types.NamespacedName, oldOperation *radappiov1alpha3.ResourceOperation) *radappiov1alpha3.DeploymentResourceStatus {
ctx := testcontext.New(t)

logger := t
status := &radappiov1alpha3.DeploymentResourceStatus{}
require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Logf("Fetching DeploymentResource: %+v", name)
current := &radappiov1alpha3.DeploymentResource{}
err := client.Get(ctx, name, current)
assert.NoError(t, err)

status = &current.Status
logger.Logf("DeploymentResource.Status: %+v", current.Status)
assert.Equal(t, status.ObservedGeneration, current.Generation, "Status is not updated")

if assert.Equal(t, radappiov1alpha3.DeploymentResourcePhraseDeleting, current.Status.Phrase) {
Expand All @@ -170,17 +164,10 @@ func waitForDeploymentResourceStateDeleting(t *testing.T, client k8sClient.Clien
func waitForDeploymentResourceDeleted(t *testing.T, client k8sClient.Client, name types.NamespacedName) {
ctx := testcontext.New(t)

logger := t
require.Eventuallyf(t, func() bool {
logger.Logf("Fetching DeploymentResource: %+v", name)
current := &radappiov1alpha3.DeploymentResource{}
err := client.Get(ctx, name, current)
if apierrors.IsNotFound(err) {
return true
}

logger.Logf("DeploymentResource.Status: %+v", current.Status)
return false
return apierrors.IsNotFound(err)

}, DeploymentResourceTestWaitDuration, DeploymentResourceTestWaitInterval, "DeploymentResource still exists")
}
20 changes: 2 additions & 18 deletions pkg/controller/reconciler/deploymenttemplate_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (

const (
deploymentTemplateTestWaitDuration = time.Second * 10
deploymentTemplateTestWaitInterval = time.Second * 1
deploymentTemplateTestWaitInterval = 200 * time.Millisecond
deploymentTemplateTestControllerDelayInterval = time.Millisecond * 100
)

Expand Down Expand Up @@ -770,10 +770,8 @@ func Test_DeploymentTemplateReconciler_OutputResources(t *testing.T) {
func waitForDeploymentTemplateStateUpdating(t *testing.T, client k8sclient.Client, name types.NamespacedName, oldOperation *radappiov1alpha3.ResourceOperation) *radappiov1alpha3.DeploymentTemplateStatus {
ctx := testcontext.New(t)

logger := t
status := &radappiov1alpha3.DeploymentTemplateStatus{}
require.EventuallyWithT(t, func(t *assert.CollectT) {
logger.Logf("Fetching DeploymentTemplate: %+v", name)
current := &radappiov1alpha3.DeploymentTemplate{
Status: radappiov1alpha3.DeploymentTemplateStatus{
Phrase: radappiov1alpha3.DeploymentTemplatePhrase(radappiov1alpha3.DeploymentResourcePhraseDeleting),
Expand All @@ -783,7 +781,6 @@ func waitForDeploymentTemplateStateUpdating(t *testing.T, client k8sclient.Clien
require.NoError(t, err)

status = &current.Status
logger.Logf("DeploymentTemplate.Status: %+v", current.Status)
assert.Equal(t, status.ObservedGeneration, current.Generation, "Status is not updated")

if assert.Equal(t, radappiov1alpha3.DeploymentTemplatePhraseUpdating, current.Status.Phrase) {
Expand All @@ -799,16 +796,13 @@ func waitForDeploymentTemplateStateUpdating(t *testing.T, client k8sclient.Clien
func waitForDeploymentTemplateStateReady(t *testing.T, client k8sclient.Client, name types.NamespacedName) *radappiov1alpha3.DeploymentTemplateStatus {
ctx := testcontext.New(t)

logger := t
status := &radappiov1alpha3.DeploymentTemplateStatus{}
require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Logf("Fetching DeploymentTemplate: %+v", name)
current := &radappiov1alpha3.DeploymentTemplate{}
err := client.Get(ctx, name, current)
require.NoError(t, err)

status = &current.Status
logger.Logf("DeploymentTemplate.Status: %+v", current.Status)
assert.Equal(t, status.ObservedGeneration, current.Generation, "Status is not updated")

if assert.Equal(t, radappiov1alpha3.DeploymentTemplatePhraseReady, current.Status.Phrase) {
Expand All @@ -822,16 +816,13 @@ func waitForDeploymentTemplateStateReady(t *testing.T, client k8sclient.Client,
func waitForDeploymentTemplateStateDeleting(t *testing.T, client k8sclient.Client, name types.NamespacedName) *radappiov1alpha3.DeploymentTemplateStatus {
ctx := testcontext.New(t)

logger := t
status := &radappiov1alpha3.DeploymentTemplateStatus{}
require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Logf("Fetching DeploymentTemplate: %+v", name)
current := &radappiov1alpha3.DeploymentTemplate{}
err := client.Get(ctx, name, current)
assert.NoError(t, err)

status = &current.Status
logger.Logf("DeploymentTemplate.Status: %+v", current.Status)
assert.Equal(t, status.ObservedGeneration, current.Generation, "Status is not updated")

assert.Equal(t, radappiov1alpha3.DeploymentTemplatePhraseDeleting, current.Status.Phrase)
Expand All @@ -843,17 +834,10 @@ func waitForDeploymentTemplateStateDeleting(t *testing.T, client k8sclient.Clien
func waitForDeploymentTemplateStateDeleted(t *testing.T, client k8sclient.Client, name types.NamespacedName) {
ctx := testcontext.New(t)

logger := t
require.Eventuallyf(t, func() bool {
logger.Logf("Fetching DeploymentTemplate: %+v", name)
current := &radappiov1alpha3.DeploymentTemplate{}
err := client.Get(ctx, name, current)
if apierrors.IsNotFound(err) {
return true
}

logger.Logf("DeploymentTemplate.Status: %+v", current.Status)
return false
return apierrors.IsNotFound(err)

}, deploymentTemplateTestWaitDuration, deploymentTemplateTestWaitInterval, "DeploymentTemplate still exists")
}
Expand Down
20 changes: 2 additions & 18 deletions pkg/controller/reconciler/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (

const (
recipeTestWaitDuration = time.Second * 10
recipeTestWaitInterval = time.Second * 1
recipeTestWaitInterval = 200 * time.Millisecond
recipeTestControllerDelayInterval = time.Millisecond * 100
)

Expand Down Expand Up @@ -69,16 +69,13 @@ func makeRecipe(name types.NamespacedName, resourceType string) *radappiov1alpha
func waitForRecipeStateUpdating(t *testing.T, client client.Client, name types.NamespacedName, oldOperation *radappiov1alpha3.ResourceOperation) *radappiov1alpha3.RecipeStatus {
ctx := testcontext.New(t)

logger := t
status := &radappiov1alpha3.RecipeStatus{}
require.EventuallyWithT(t, func(t *assert.CollectT) {
logger.Logf("Fetching Recipe: %+v", name)
current := &radappiov1alpha3.Recipe{}
err := client.Get(ctx, name, current)
require.NoError(t, err)

status = &current.Status
logger.Logf("Recipe.Status: %+v", current.Status)
assert.Equal(t, status.ObservedGeneration, current.Generation, "Status is not updated")

if assert.Equal(t, radappiov1alpha3.PhraseUpdating, current.Status.Phrase) {
Expand All @@ -94,16 +91,13 @@ func waitForRecipeStateUpdating(t *testing.T, client client.Client, name types.N
func waitForRecipeStateReady(t *testing.T, client client.Client, name types.NamespacedName) *radappiov1alpha3.RecipeStatus {
ctx := testcontext.New(t)

logger := t
status := &radappiov1alpha3.RecipeStatus{}
require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Logf("Fetching Recipe: %+v", name)
current := &radappiov1alpha3.Recipe{}
err := client.Get(ctx, name, current)
require.NoError(t, err)

status = &current.Status
logger.Logf("Recipe.Status: %+v", current.Status)
assert.Equal(t, status.ObservedGeneration, current.Generation, "Status is not updated")

if assert.Equal(t, radappiov1alpha3.PhraseReady, current.Status.Phrase) {
Expand All @@ -117,16 +111,13 @@ func waitForRecipeStateReady(t *testing.T, client client.Client, name types.Name
func waitForRecipeStateDeleting(t *testing.T, client client.Client, name types.NamespacedName, oldOperation *radappiov1alpha3.ResourceOperation) *radappiov1alpha3.RecipeStatus {
ctx := testcontext.New(t)

logger := t
status := &radappiov1alpha3.RecipeStatus{}
require.EventuallyWithTf(t, func(t *assert.CollectT) {
logger.Logf("Fetching Recipe: %+v", name)
current := &radappiov1alpha3.Recipe{}
err := client.Get(ctx, name, current)
assert.NoError(t, err)

status = &current.Status
logger.Logf("Recipe.Status: %+v", current.Status)
assert.Equal(t, status.ObservedGeneration, current.Generation, "Status is not updated")

if assert.Equal(t, radappiov1alpha3.PhraseDeleting, current.Status.Phrase) {
Expand All @@ -141,17 +132,10 @@ func waitForRecipeStateDeleting(t *testing.T, client client.Client, name types.N
func waitForRecipeDeleted(t *testing.T, client client.Client, name types.NamespacedName) {
ctx := testcontext.New(t)

logger := t
require.Eventuallyf(t, func() bool {
logger.Logf("Fetching Recipe: %+v", name)
current := &radappiov1alpha3.Recipe{}
err := client.Get(ctx, name, current)
if apierrors.IsNotFound(err) {
return true
}

logger.Logf("Recipe.Status: %+v", current.Status)
return false
return apierrors.IsNotFound(err)

}, recipeTestWaitDuration, recipeTestWaitInterval, "recipe still exists")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,21 @@ func Test_ResourceProvider_CascadingDelete(t *testing.T) {
deleteResourceProvider(server)

// The resource type should be gone now.
require.Eventually(t, func() bool {
response := server.MakeRequest(http.MethodGet, resourceTypeURL, nil)
return response.Raw.StatusCode == http.StatusNotFound
}, 10*time.Second, 100*time.Millisecond)

response := server.MakeRequest(http.MethodGet, resourceTypeURL, nil)
response.EqualsErrorCode(404, "NotFound")
require.Equal(t, resourceTypeID, response.Error.Error.Target)

// The location should be gone now.
require.Eventually(t, func() bool {
response := server.MakeRequest(http.MethodGet, locationURL, nil)
return response.Raw.StatusCode == http.StatusNotFound
}, 10*time.Second, 100*time.Millisecond)

response = server.MakeRequest(http.MethodGet, locationURL, nil)
response.EqualsErrorCode(404, "NotFound")
require.Equal(t, locationID, response.Error.Error.Target)
Expand Down
Loading