mcc: add unit test for GetMachineConfigsForPool() in render_controller.go
@@ -7,6 +7,7 @@ import (
ignv2_2types "github.com/coreos/ignition/config/v2_2/types"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
+ ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common"
"github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned/fake"
informers "github.com/openshift/machine-config-operator/pkg/generated/informers/externalversions"
corev1 "k8s.io/api/core/v1"
@@ -65,12 +66,14 @@ func newMachineConfig(name string, labels map[string]string, osurl string, files
if labels == nil {
labels = map[string]string{}
}
+ ignCfg := ctrlcommon.NewIgnConfig()
+ ignCfg.Storage.Files = files
return &mcfgv1.MachineConfig{
TypeMeta: metav1.TypeMeta{APIVersion: mcfgv1.SchemeGroupVersion.String()},
ObjectMeta: metav1.ObjectMeta{Name: name, Labels: labels, UID: types.UID(utilrand.String(5))},
Spec: mcfgv1.MachineConfigSpec{
OSImageURL: osurl,
- Config: ignv2_2types.Config{Storage: ignv2_2types.Storage{Files: files}},
+ Config: ignCfg,
},
}
}
@@ -323,6 +326,43 @@ func TestDoNothing(t *testing.T) {
f.run(getKey(mcp, t))
}
+func TestGetMachineConfigsForPool(t *testing.T) {
+ masterPool := newMachineConfigPool("test-cluster-master", metav1.AddLabelToSelector(&metav1.LabelSelector{}, "node-role", "master"), "")
+ files := []ignv2_2types.File{{
+ Node: ignv2_2types.Node{
+ Path: "/dummy/0",
+ },
+ }, {
+ Node: ignv2_2types.Node{
+ Path: "/dummy/1",
+ },
+ }, {
+ Node: ignv2_2types.Node{
+ Path: "/dummy/2",
+ },
+ }}
+ mcs := []*mcfgv1.MachineConfig{
+ newMachineConfig("00-test-cluster-master", map[string]string{"node-role": "master"}, "dummy://", []ignv2_2types.File{files[0]}),
+ newMachineConfig("05-extra-master", map[string]string{"node-role": "master"}, "dummy://1", []ignv2_2types.File{files[1]}),
+ newMachineConfig("00-test-cluster-worker", map[string]string{"node-role": "worker"}, "dummy://2", []ignv2_2types.File{files[2]}),
+ }
+ masterConfigs, err := getMachineConfigsForPool(masterPool, mcs)
+ if err != nil {
+ t.Fatalf("expected no error, got: %v", err)
+ }
+ // check that only the master MCs were selected
+ if len(masterConfigs) != 2 {
+ t.Fatalf("expected to select 2 configs for pool master got: %v", len(masterConfigs))
+ }
+
+ // search for a worker config in an array of MCs with no worker configs
+ workerPool := newMachineConfigPool("test-cluster-worker", metav1.AddLabelToSelector(&metav1.LabelSelector{}, "node-role", "worker"), "")
+ _, err = getMachineConfigsForPool(workerPool, mcs[:2])
+ if err == nil {
+ t.Fatalf("expected error, no worker configs found")
+ }
+}
+
func getKey(config *mcfgv1.MachineConfigPool, t *testing.T) string {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(config)
if err != nil {
