Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#11356 from dlipovetsky/public-test…
Browse files Browse the repository at this point in the history
…-builder-package

🌱 Make `internal/test/builder` package public (as `util/test/builder`)
  • Loading branch information
k8s-ci-robot authored Oct 31, 2024
2 parents 16232aa + febc6a9 commit 93f03e6
Show file tree
Hide file tree
Showing 67 changed files with 448 additions and 361 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,9 @@ generate-manifests-core: $(CONTROLLER_GEN) $(KUSTOMIZE) ## Generate manifests e.
output:crd:dir=./cmd/clusterctl/config/crd/bases
$(KUSTOMIZE) build $(CLUSTERCTL_MANIFEST_DIR)/crd > $(CLUSTERCTL_MANIFEST_DIR)/manifest/clusterctl-api.yaml
$(CONTROLLER_GEN) \
paths=./internal/test/builder/... \
paths=./util/test/builder/... \
crd:crdVersions=v1 \
output:crd:dir=./internal/test/builder/crd
output:crd:dir=./util/test/builder/crd

.PHONY: generate-manifests-kubeadm-bootstrap
generate-manifests-kubeadm-bootstrap: $(CONTROLLER_GEN) ## Generate manifests e.g. CRD, RBAC etc. for kubeadm bootstrap
Expand Down Expand Up @@ -398,7 +398,7 @@ generate-go-deepcopy-core: $(CONTROLLER_GEN) ## Generate deepcopy go code for co
paths=./$(EXP_DIR)/runtime/hooks/api/... \
paths=./internal/runtime/test/... \
paths=./cmd/clusterctl/... \
paths=./internal/test/builder/...
paths=./util/test/builder/...

.PHONY: generate-go-deepcopy-kubeadm-bootstrap
generate-go-deepcopy-kubeadm-bootstrap: $(CONTROLLER_GEN) ## Generate deepcopy go code for kubeadm bootstrap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util/test/builder"
)

func TestKubeadmConfigReconciler(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ import (
"sigs.k8s.io/cluster-api/controllers/clustercache"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/certs"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/secret"
"sigs.k8s.io/cluster-api/util/test/builder"
utilyaml "sigs.k8s.io/cluster-api/util/yaml"
)

Expand Down Expand Up @@ -213,7 +213,8 @@ func TestKubeadmConfigReconciler_TestSecretOwnerReferenceReconciliation(t *testi
Name: machine.Name,
UID: machine.UID,
Controller: ptr.To(true),
}})
},
})
g.Expect(myclient.Update(ctx, actual)).To(Succeed())

_, err = k.Reconcile(ctx, request)
Expand Down
6 changes: 2 additions & 4 deletions cmd/clusterctl/client/cluster/objectgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util/test/builder"
)

func TestObjectGraph_getDiscoveryTypeMetaList(t *testing.T) {
Expand Down Expand Up @@ -1043,7 +1043,6 @@ var objectGraphsTests = []struct {
},
want: wantGraph{
nodes: map[string]wantGraphItem{

"infrastructure.cluster.x-k8s.io/v1beta1, Kind=GenericInfrastructureMachineTemplate, ns1/shared": {
owners: []string{
"cluster.x-k8s.io/v1beta1, Kind=Cluster, ns1/cluster1",
Expand Down Expand Up @@ -1638,7 +1637,6 @@ var objectGraphsTests = []struct {
// We need to deduplicate objects here as the clusterclasses share objects and
// setting up the test server panics if we try to create it with duplicate objects.
return deduplicateObjects(objs)

}(),
},
want: wantGraph{
Expand Down Expand Up @@ -1807,7 +1805,7 @@ func TestObjectGraph_DiscoveryByNamespace(t *testing.T) {
namespace string
objs []client.Object
}
var tests = []struct {
tests := []struct {
name string
args args
want wantGraph
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/internal/test/fake_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import (
fakeinfrastructure "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test/providers/infrastructure"
addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/test/builder"
)

type FakeCluster struct {
Expand Down
5 changes: 3 additions & 2 deletions controlplane/kubeadm/internal/controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import (
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/contract"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/internal/util/ssa"
"sigs.k8s.io/cluster-api/internal/webhooks"
"sigs.k8s.io/cluster-api/util"
Expand All @@ -65,6 +64,7 @@ import (
"sigs.k8s.io/cluster-api/util/kubeconfig"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/secret"
"sigs.k8s.io/cluster-api/util/test/builder"
)

func TestClusterToKubeadmControlPlane(t *testing.T) {
Expand All @@ -85,7 +85,8 @@ func TestClusterToKubeadmControlPlane(t *testing.T) {
{
NamespacedName: client.ObjectKey{
Namespace: cluster.Spec.ControlPlaneRef.Namespace,
Name: cluster.Spec.ControlPlaneRef.Name},
Name: cluster.Spec.ControlPlaneRef.Name,
},
},
}

Expand Down
8 changes: 5 additions & 3 deletions controlplane/kubeadm/internal/controllers/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/internal/util/ssa"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/collections"
"sigs.k8s.io/cluster-api/util/test/builder"
)

const UpdatedVersion string = "v1.17.4"
const Host string = "nodomain.example.com"
const (
UpdatedVersion string = "v1.17.4"
Host string = "nodomain.example.com"
)

func TestKubeadmControlPlaneReconciler_RolloutStrategy_ScaleUp(t *testing.T) {
setup := func(t *testing.T, g *WithT) *corev1.Namespace {
Expand Down
2 changes: 1 addition & 1 deletion docs/book/src/developer/core/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ easily readable, well documented and consistent across the code base.

In light of continuing improving our practice around this ambitious goal, we are starting to introduce a shared set of:

- Builders (`sigs.k8s.io/cluster-api/internal/test/builder`), allowing to create test objects in a simple and consistent way.
- Builders (`sigs.k8s.io/cluster-api/util/test/builder`), allowing to create test objects in a simple and consistent way.
- Matchers (`sigs.k8s.io/controller-runtime/pkg/envtest/komega`), improving how we write test assertions.

Each contribution in growing this set of utilities or their adoption across the codebase is more than welcome!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ import (
"sigs.k8s.io/cluster-api/controllers/external"
externalfake "sigs.k8s.io/cluster-api/controllers/external/fake"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/internal/util/ssa"
"sigs.k8s.io/cluster-api/util/kubeconfig"
"sigs.k8s.io/cluster-api/util/labels/format"
"sigs.k8s.io/cluster-api/util/test/builder"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion exp/internal/controllers/machinepool_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ import (
"sigs.k8s.io/cluster-api/controllers/external"
externalfake "sigs.k8s.io/cluster-api/controllers/external/fake"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/test/builder"
)

func TestMachinePoolFinalizer(t *testing.T) {
Expand Down
11 changes: 7 additions & 4 deletions exp/topology/desiredstate/desired_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ import (
"sigs.k8s.io/cluster-api/internal/contract"
"sigs.k8s.io/cluster-api/internal/hooks"
fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/internal/topology/clustershim"
"sigs.k8s.io/cluster-api/internal/topology/names"
"sigs.k8s.io/cluster-api/internal/topology/ownerrefs"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/test/builder"
)

var (
Expand Down Expand Up @@ -1381,7 +1381,8 @@ func TestComputeMachineDeployment(t *testing.T) {
MachineHealthCheck: &clusterv1.MachineHealthCheckClass{
UnhealthyConditions: unhealthyConditions,
NodeStartupTimeout: &metav1.Duration{
Duration: time.Duration(1)},
Duration: time.Duration(1),
},
},
},
},
Expand Down Expand Up @@ -2906,7 +2907,8 @@ func Test_computeMachineHealthCheck(t *testing.T) {
},
},
NodeStartupTimeout: &metav1.Duration{
Duration: time.Duration(1)},
Duration: time.Duration(1),
},
}
selector := &metav1.LabelSelector{MatchLabels: map[string]string{
"foo": "bar",
Expand Down Expand Up @@ -2950,7 +2952,8 @@ func Test_computeMachineHealthCheck(t *testing.T) {
},
},
NodeStartupTimeout: &metav1.Duration{
Duration: time.Duration(1)},
Duration: time.Duration(1),
},
},
}

Expand Down
2 changes: 1 addition & 1 deletion exp/topology/scope/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"k8s.io/utils/ptr"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util/test/builder"
)

func TestIsControlPlaneMachineHealthCheckEnabled(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion exp/topology/scope/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util/test/builder"
)

func TestMDUpgrading(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import (
"sigs.k8s.io/cluster-api/controllers/external"
externalfake "sigs.k8s.io/cluster-api/controllers/external/fake"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/test/builder"
)

func TestClusterReconcilePhases(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/cluster/cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import (
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/conditions"
conditionsv1beta2 "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/test/builder"
)

const (
Expand Down
40 changes: 29 additions & 11 deletions internal/controllers/clusterclass/clusterclass_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/feature"
fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util/test/builder"
)

func TestClusterClassReconciler_reconcile(t *testing.T) {
Expand Down Expand Up @@ -211,6 +211,7 @@ func assertStatusVariables(actualClusterClass *clusterv1.ClusterClass) error {
}
return nil
}

func assertInfrastructureClusterTemplate(ctx context.Context, actualClusterClass *clusterv1.ClusterClass, ns *corev1.Namespace) error {
// Assert the infrastructure cluster template has the correct owner reference.
actualInfraClusterTemplate := builder.InfrastructureClusterTemplate("", "").Build()
Expand Down Expand Up @@ -538,7 +539,9 @@ func TestReconciler_reconcileVariables(t *testing.T) {
Name: "patch1",
External: &clusterv1.ExternalPatchDefinition{
DiscoverVariablesExtension: ptr.To("variables-one"),
}}}).
},
},
}).
Build(),
patchResponse: &runtimehooksv1.DiscoverVariablesResponse{
CommonResponse: runtimehooksv1.CommonResponse{
Expand Down Expand Up @@ -754,7 +757,9 @@ func TestReconciler_reconcileVariables(t *testing.T) {
Name: "patch1",
External: &clusterv1.ExternalPatchDefinition{
DiscoverVariablesExtension: ptr.To("variables-one"),
}}}).
},
},
}).
Build(),
patchResponse: &runtimehooksv1.DiscoverVariablesResponse{
CommonResponse: runtimehooksv1.CommonResponse{
Expand Down Expand Up @@ -792,7 +797,9 @@ func TestReconciler_reconcileVariables(t *testing.T) {
Name: "patch1",
External: &clusterv1.ExternalPatchDefinition{
DiscoverVariablesExtension: ptr.To("variables-one"),
}}}).
},
},
}).
Build(),
patchResponse: &runtimehooksv1.DiscoverVariablesResponse{
CommonResponse: runtimehooksv1.CommonResponse{
Expand Down Expand Up @@ -965,7 +972,9 @@ func TestReconciler_reconcileVariables(t *testing.T) {
Name: "patch1",
External: &clusterv1.ExternalPatchDefinition{
DiscoverVariablesExtension: ptr.To("variables-one"),
}}}).
},
},
}).
Build(),
patchResponse: &runtimehooksv1.DiscoverVariablesResponse{
CommonResponse: runtimehooksv1.CommonResponse{
Expand Down Expand Up @@ -1011,7 +1020,9 @@ func TestReconciler_reconcileVariables(t *testing.T) {
Name: "patch1",
External: &clusterv1.ExternalPatchDefinition{
DiscoverVariablesExtension: ptr.To("variables-one"),
}}}).
},
},
}).
Build(),
patchResponse: &runtimehooksv1.DiscoverVariablesResponse{
CommonResponse: runtimehooksv1.CommonResponse{
Expand Down Expand Up @@ -1062,7 +1073,9 @@ func TestReconciler_reconcileVariables(t *testing.T) {
Name: "patch1",
External: &clusterv1.ExternalPatchDefinition{
DiscoverVariablesExtension: ptr.To("variables-one"),
}}}).
},
},
}).
Build(),
patchResponse: &runtimehooksv1.DiscoverVariablesResponse{
CommonResponse: runtimehooksv1.CommonResponse{
Expand Down Expand Up @@ -1137,7 +1150,9 @@ func TestReconciler_reconcileVariables(t *testing.T) {
Name: "patch1",
External: &clusterv1.ExternalPatchDefinition{
DiscoverVariablesExtension: ptr.To("variables-one"),
}}}).
},
},
}).
Build(),
patchResponse: &runtimehooksv1.DiscoverVariablesResponse{
CommonResponse: runtimehooksv1.CommonResponse{
Expand Down Expand Up @@ -1258,18 +1273,21 @@ func TestReconciler_extensionConfigToClusterClass(t *testing.T) {
// These ClusterClasses will be reconciled as they both reference the passed ExtensionConfig `runtime1`.
onePatchClusterClass := builder.ClusterClass(metav1.NamespaceDefault, "cc1").
WithPatches([]clusterv1.ClusterClassPatch{
{External: &clusterv1.ExternalPatchDefinition{DiscoverVariablesExtension: ptr.To("discover-variables.runtime1")}}}).
{External: &clusterv1.ExternalPatchDefinition{DiscoverVariablesExtension: ptr.To("discover-variables.runtime1")}},
}).
Build()
twoPatchClusterClass := builder.ClusterClass(metav1.NamespaceDefault, "cc2").
WithPatches([]clusterv1.ClusterClassPatch{
{External: &clusterv1.ExternalPatchDefinition{DiscoverVariablesExtension: ptr.To("discover-variables.runtime1")}},
{External: &clusterv1.ExternalPatchDefinition{DiscoverVariablesExtension: ptr.To("discover-variables.runtime2")}}}).
{External: &clusterv1.ExternalPatchDefinition{DiscoverVariablesExtension: ptr.To("discover-variables.runtime2")}},
}).
Build()

// This ClusterClasses will not be reconciled as it does not reference the passed ExtensionConfig `runtime1`.
notReconciledClusterClass := builder.ClusterClass(metav1.NamespaceDefault, "cc3").
WithPatches([]clusterv1.ClusterClassPatch{
{External: &clusterv1.ExternalPatchDefinition{DiscoverVariablesExtension: ptr.To("discover-variables.other-runtime-class")}}}).
{External: &clusterv1.ExternalPatchDefinition{DiscoverVariablesExtension: ptr.To("discover-variables.other-runtime-class")}},
}).
Build()

t.Run("test", func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ import (
"sigs.k8s.io/cluster-api/api/v1beta1/index"
"sigs.k8s.io/cluster-api/controllers/clustercache"
"sigs.k8s.io/cluster-api/controllers/remote"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/internal/topology/ownerrefs"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/kubeconfig"
"sigs.k8s.io/cluster-api/util/test/builder"
)

func TestReconcileNode(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/external"
externalfake "sigs.k8s.io/cluster-api/controllers/external/fake"
"sigs.k8s.io/cluster-api/internal/test/builder"
"sigs.k8s.io/cluster-api/util/test/builder"
)

func init() {
Expand Down
Loading

0 comments on commit 93f03e6

Please sign in to comment.