diff --git a/pkg/daemon/ceph/config/pod.go b/pkg/daemon/ceph/config/pod.go index 3aa48421b..f57e2e80b 100644 --- a/pkg/daemon/ceph/config/pod.go +++ b/pkg/daemon/ceph/config/pod.go @@ -19,17 +19,18 @@ package config import "k8s.io/api/core/v1" const ( - defaultConfigMountName = "ceph-default-config-dir" + // DefaultConfigMountName is the name of the volume mount used to mount the default Ceph config + DefaultConfigMountName = "ceph-default-config-dir" ) // DefaultConfigVolume returns an empty volume used to store Ceph's config at the default path // in containers. func DefaultConfigVolume() v1.Volume { - return v1.Volume{Name: defaultConfigMountName, + return v1.Volume{Name: DefaultConfigMountName, VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}} } // DefaultConfigMount returns a volume mount to Ceph's default config path. func DefaultConfigMount() v1.VolumeMount { - return v1.VolumeMount{Name: defaultConfigMountName, MountPath: DefaultConfigDir} + return v1.VolumeMount{Name: DefaultConfigMountName, MountPath: DefaultConfigDir} } diff --git a/pkg/operator/ceph/cluster/mon/spec_test.go b/pkg/operator/ceph/cluster/mon/spec_test.go index 577a81dd9..4ef20bc73 100644 --- a/pkg/operator/ceph/cluster/mon/spec_test.go +++ b/pkg/operator/ceph/cluster/mon/spec_test.go @@ -81,15 +81,12 @@ func testPodSpec(t *testing.T, dataDir string) { assert.Equal(t, "a", pod.Name) assert.Equal(t, v1.RestartPolicyAlways, pod.Spec.RestartPolicy) assert.Equal(t, 3, len(pod.Spec.Volumes)) - assert.Equal(t, "rook-data", pod.Spec.Volumes[0].Name) - assert.Equal(t, k8sutil.ConfigOverrideName, pod.Spec.Volumes[1].Name) - assert.Equal(t, cephconfig.DefaultConfigVolume(), pod.Spec.Volumes[2]) + assert.Nil(t, testop.VolumeExists("rook-data", pod.Spec.Volumes)) + assert.Nil(t, testop.VolumeExists(k8sutil.ConfigOverrideName, pod.Spec.Volumes)) if dataDir == "" { - assert.NotNil(t, pod.Spec.Volumes[0].EmptyDir) - assert.Nil(t, pod.Spec.Volumes[0].HostPath) + assert.Nil(t, testop.VolumeIsEmptyDir(k8sutil.DataDirVolume, pod.Spec.Volumes)) } else { - assert.Nil(t, pod.Spec.Volumes[0].EmptyDir) - assert.Equal(t, dataDir, pod.Spec.Volumes[0].HostPath.Path) + assert.Nil(t, testop.VolumeIsHostPath(k8sutil.DataDirVolume, dataDir, pod.Spec.Volumes)) } assert.Equal(t, "a", pod.ObjectMeta.Name) @@ -101,7 +98,8 @@ func testPodSpec(t *testing.T, dataDir string) { assert.Equal(t, "rook/rook:myversion", cont.Image) assert.Equal(t, 0, len(cont.Ports)) assert.Equal(t, 3, len(cont.VolumeMounts)) - assert.Equal(t, cephconfig.DefaultConfigMount(), cont.VolumeMounts[1]) + assert.Nil(t, testop.VolumeMountExists(cephconfig.DefaultConfigMountName, cont.VolumeMounts)) + assert.Nil(t, testop.VolumeMountExists(k8sutil.ConfigOverrideName, cont.VolumeMounts)) assert.Equal(t, 7, len(cont.Env)) assert.False(t, *cont.SecurityContext.Privileged) logCommandWithArgs("confg init", cont.Command, cont.Args) @@ -118,7 +116,7 @@ func testPodSpec(t *testing.T, dataDir string) { assert.Equal(t, "rook/rook:myversion", cont.Image) assert.Equal(t, 0, len(cont.Ports)) assert.Equal(t, 2, len(cont.VolumeMounts)) - assert.Equal(t, cephconfig.DefaultConfigMount(), cont.VolumeMounts[1]) + assert.Nil(t, testop.VolumeMountExists(cephconfig.DefaultConfigMountName, cont.VolumeMounts)) assert.Equal(t, 0, len(cont.Env)) assert.False(t, *cont.SecurityContext.Privileged) logCommandWithArgs("monmap init", cont.Command, cont.Args) @@ -135,7 +133,7 @@ func testPodSpec(t *testing.T, dataDir string) { assert.Equal(t, "rook/rook:myversion", cont.Image) assert.Equal(t, 0, len(cont.Ports)) assert.Equal(t, 2, len(cont.VolumeMounts)) - assert.Equal(t, cephconfig.DefaultConfigMount(), cont.VolumeMounts[1]) + assert.Nil(t, testop.VolumeMountExists(cephconfig.DefaultConfigMountName, cont.VolumeMounts)) assert.Equal(t, 0, len(cont.Env)) assert.False(t, *cont.SecurityContext.Privileged) logCommandWithArgs("mon fs init", cont.Command, cont.Args) @@ -154,10 +152,11 @@ func testPodSpec(t *testing.T, dataDir string) { assert.Equal(t, cont.Ports[0].ContainerPort, int32(6790)) assert.Equal(t, cont.Ports[0].Protocol, v1.ProtocolTCP) assert.Equal(t, 2, len(cont.VolumeMounts)) - assert.Equal(t, cephconfig.DefaultConfigMount(), cont.VolumeMounts[1]) + assert.Nil(t, testop.VolumeMountExists(cephconfig.DefaultConfigMountName, cont.VolumeMounts)) assert.Equal(t, 0, len(cont.Env)) assert.False(t, *cont.SecurityContext.Privileged) logCommandWithArgs("main mon daemon", cont.Command, cont.Args) + assert.Equal(t, 1, len(cont.Command)) assert.Equal(t, "/usr/bin/ceph-mon", cont.Command[0]) assert.Equal(t, "--foreground", cont.Args[0]) @@ -165,6 +164,17 @@ func testPodSpec(t *testing.T, dataDir string) { assert.Equal(t, "2.4.6.1:6790", cont.Args[2]) testCephMonCommonArgs(t, c, name, cont, 3) + // Verify that all the mounts have volumes and that there are no extraneous volumes + volsMountsTestDef := testop.VolumesAndMountsTestDefinition{ + VolumesSpec: &testop.VolumesSpec{Moniker: "mon pod volumes", Volumes: pod.Spec.Volumes}, + MountsSpecItems: []*testop.MountsSpec{ + {Moniker: "mon config init mounts", Mounts: pod.Spec.InitContainers[0].VolumeMounts}, + {Moniker: "mon monmap init mounts", Mounts: pod.Spec.InitContainers[1].VolumeMounts}, + {Moniker: "mon fs init mounts", Mounts: pod.Spec.InitContainers[2].VolumeMounts}, + {Moniker: "mon daemon mounts", Mounts: pod.Spec.Containers[0].VolumeMounts}}, + } + volsMountsTestDef.TestMountsMatchVolumes(t) + assert.Equal(t, "100", cont.Resources.Limits.Cpu().String()) assert.Equal(t, "1337", cont.Resources.Requests.Memory().String()) } diff --git a/pkg/operator/test/volumes.go b/pkg/operator/test/volumes.go new file mode 100644 index 000000000..929e1f45e --- /dev/null +++ b/pkg/operator/test/volumes.go @@ -0,0 +1,210 @@ +package test + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "k8s.io/api/core/v1" +) + +/* + * Simple Volume and VolumeMount Tests + */ + +// VolumeExists returns a descriptive error if the volume does not exist. +func VolumeExists(volumeName string, volumes []v1.Volume) error { + _, err := getVolume(volumeName, volumes) + return err +} + +// VolumeIsEmptyDir returns a descriptive error if the volume does not exist or is not an empty dir +func VolumeIsEmptyDir(volumeName string, volumes []v1.Volume) error { + volume, err := getVolume(volumeName, volumes) + if err == nil && volume.VolumeSource.EmptyDir == nil { + return fmt.Errorf("volume %s is not EmptyDir: %s", volumeName, HumanReadableVolume(volume)) + } + if err == nil && volume.VolumeSource.HostPath != nil { + return fmt.Errorf("volume %s is both EmptyDir and HostPath: %s", volumeName, HumanReadableVolume(volume)) + } + return err +} + +// VolumeIsHostPath returns a descriptive error if the volume does not exist, is not a HostPath +// volume, or if the volume's path is not as expected. +func VolumeIsHostPath(volumeName, path string, volumes []v1.Volume) error { + volume, err := getVolume(volumeName, volumes) + if err == nil && volume.VolumeSource.HostPath == nil { + return fmt.Errorf("volume %s is not HostPath: %s", volumeName, HumanReadableVolume(volume)) + } + if err == nil && volume.VolumeSource.HostPath.Path != path { + return fmt.Errorf("volume %s is HostPath but has wrong path: %s", volumeName, HumanReadableVolume(volume)) + } + if err == nil && volume.VolumeSource.EmptyDir != nil { + return fmt.Errorf("volume %s is both HostPath and EmptyDir: %s", volumeName, HumanReadableVolume(volume)) + } + return err +} + +// VolumeMountExists returns returns a descriptive error if the volume mount does not exist. +func VolumeMountExists(mountName string, mounts []v1.VolumeMount) error { + _, err := getMount(mountName, mounts) + return err +} + +/* + * Human-readable representations of Volumes and VolumeMounts + */ + +// HumanReadableVolumes returns a string representation of a list of Kubernetes volumes which is +// more compact and human readable than the default string go prints. +func HumanReadableVolumes(volumes []v1.Volume) string { + stringVols := []string{} + for _, volume := range volumes { + stringVols = append(stringVols, HumanReadableVolume(&volume)) + } + return fmt.Sprintf("%v", stringVols) +} + +// HumanReadableVolume returns a string representation of a Kubernetes volume which is more compact +// and human readable than the default string go prints. +func HumanReadableVolume(v *v1.Volume) string { + sourceString := "something went wrong parsing the volume source in HumanReadableVolume()" + if v.VolumeSource.EmptyDir != nil { + sourceString = "EmptyDir" + } else if v.VolumeSource.HostPath != nil { + sourceString = v.VolumeSource.HostPath.Path + } else { + // If can't convert the vol source to something human readable, just output something useful + sourceString = fmt.Sprintf("%v", v.VolumeSource) + } + return fmt.Sprintf("{%s : %s}", v.Name, sourceString) +} + +// HumanReadableVolumeMounts returns a string representation of a list of Kubernetes volume mounts which +// is more compact and human readable than the default string go prints. +func HumanReadableVolumeMounts(mounts []v1.VolumeMount) string { + stringMounts := []string{} + for _, mount := range mounts { + stringMounts = append(stringMounts, HumanReadableVolumeMount(&mount)) + } + return fmt.Sprintf("%v", stringMounts) +} + +// HumanReadableVolumeMount returns a string representation of a Kubernetes volume mount which is more +// compact and human readable than the default string go prints. +func HumanReadableVolumeMount(m *v1.VolumeMount) string { + return fmt.Sprintf("{%s : %s}", m.Name, m.MountPath) +} + +/* + * Fully-implemented test for matching Volumes and VolumeMounts + */ + +// VolumesSpec is a struct which includes a list of Kubernetes volumes as well as additional +// metadata about the volume list for better identification during tests. +type VolumesSpec struct { + // Moniker is a name given to the list to help identify it + Moniker string + Volumes []v1.Volume +} + +// MountsSpec is a struct which includes a list of Kubernetes volume mounts as well as additional +// metadata about the volume mount list for better identification during tests. +type MountsSpec struct { + // Moniker is a name given to the list to help identify it + Moniker string + Mounts []v1.VolumeMount +} + +// VolumesAndMountsTestDefinition defines which volumes and mounts to test and what those values +// should be. The test is intended to be defined with VolumesSpec defined as a pod's volumes, and +// the list of MountsSpec items defined as the volume mounts from every container in the pod. +type VolumesAndMountsTestDefinition struct { + VolumesSpec *VolumesSpec + MountsSpecItems []*MountsSpec +} + +// TestMountsMatchVolumes tests two things: +// (1) That each volume mount in each every MountsSpec has a corresponding volume to source it +// in the VolumesSpec +// (2) That there are no extraneous volumes defined in the VolumesSpec that do not have a +// corresponding volume mount in any of the MountsSpec items +func (d *VolumesAndMountsTestDefinition) TestMountsMatchVolumes(t *testing.T) { + // Run a test for each MountsSpec item to verify that all the volume mounts within each item + // have a corresponding volume in VolumesSpec to source it + for _, mountsSpec := range d.MountsSpecItems { + t.Run( + fmt.Sprintf("%s mounts match volumes in %s", mountsSpec.Moniker, d.VolumesSpec.Moniker), + func(t *testing.T) { + for _, mount := range mountsSpec.Mounts { + assert.Nil(t, VolumeExists(mount.Name, d.VolumesSpec.Volumes), + "%s volume mount %s does not have a corresponding %s volume to source it: %s", + mountsSpec.Moniker, HumanReadableVolumeMount(&mount), + d.VolumesSpec.Moniker, HumanReadableVolumes(d.VolumesSpec.Volumes)) + } + }, + ) + } + + // Test that each volume in VolumesSpec has a usage in at least one of the volume mounts in at + // least one of the MountsSpecs items + t.Run( + fmt.Sprintf("No extraneous %s volumes exist", d.VolumesSpec.Moniker), + func(t *testing.T) { + for _, volume := range d.VolumesSpec.Volumes { + assert.Nil(t, mountExistsInMountsSpecItems(volume.Name, d.MountsSpecItems), + "%s volume %s is not used by any volume mount: %v", + d.VolumesSpec.Moniker, HumanReadableVolume(&volume), + humanReadableMountsSpecItems(d.MountsSpecItems)) + } + }, + ) +} + +/* + * Helpers + */ + +func mountExistsInMountsSpecItems(name string, mountsSpecItems []*MountsSpec) error { + for _, mountsSpec := range mountsSpecItems { + if err := VolumeMountExists(name, mountsSpec.Mounts); err == nil { + return nil // successfully found that the mount exists at least once + } + } + return fmt.Errorf("") // not in any mounts; calling func will output a better error +} + +func humanReadableMountsSpecItems(mountsSpecItems []*MountsSpec) (humanReadable string) { + for _, mountsSpec := range mountsSpecItems { + humanReadable = fmt.Sprintf("%s%s - %s\n", humanReadable, + mountsSpec.Moniker, HumanReadableVolumeMounts(mountsSpec.Mounts), + ) + } + return +} + +const ( + volNotFound = "Volume Was Not Found by getVolume()" + mntNotFound = "VolumeMount Was Not Found by getMount()" +) + +func getVolume(volumeName string, volumes []v1.Volume) (*v1.Volume, error) { + for _, volume := range volumes { + if volume.Name == volumeName { + return &volume, nil + } + } + return &v1.Volume{Name: volNotFound}, + fmt.Errorf("volume %s does not exist in %s", volumeName, HumanReadableVolumes(volumes)) +} + +func getMount(mountName string, mounts []v1.VolumeMount) (*v1.VolumeMount, error) { + for _, mount := range mounts { + if mount.Name == mountName { + return &mount, nil + } + } + return &v1.VolumeMount{Name: mntNotFound}, + fmt.Errorf("volume mount %s does not exist in %s", mountName, HumanReadableVolumeMounts(mounts)) +} diff --git a/pkg/operator/test/volumes_test.go b/pkg/operator/test/volumes_test.go new file mode 100644 index 000000000..eb1d7c5a8 --- /dev/null +++ b/pkg/operator/test/volumes_test.go @@ -0,0 +1,120 @@ +package test + +import ( + "testing" + + "k8s.io/api/core/v1" +) + +func TestVolumeExists(t *testing.T) { + vols := []v1.Volume{{Name: "a"}, {Name: "b"}, {Name: "d"}} + type args struct { + volumeName string + volumes []v1.Volume + } + tests := []struct { + name string + args args + wantErr bool + }{ + {"exists", args{"d", vols}, false}, + {"dne", args{"c", vols}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := VolumeExists(tt.args.volumeName, tt.args.volumes); (err != nil) != tt.wantErr { + t.Errorf("VolumeExists() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func vols(v ...v1.Volume) []v1.Volume { + return v +} +func TestVolumeIsEmptyDir(t *testing.T) { + emptyVolume := v1.Volume{Name: "e", VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}} + emptyAndHostVolume := v1.Volume{Name: "e&hp", VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}} + emptyAndHostVolume.VolumeSource.HostPath = &v1.HostPathVolumeSource{Path: "/dev/sdx"} + hostVolume := v1.Volume{Name: "h", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/dev/vdh"}}} + type args struct { + volumeName string + volumes []v1.Volume + } + tests := []struct { + name string + args args + wantErr bool + }{ + {"is EmptyDir", args{"e", vols(emptyVolume)}, false}, + {"is HostPath", args{"h", vols(hostVolume)}, true}, + {"EmptyDir and HostPath", args{"e&hp", vols(emptyAndHostVolume)}, true}, + {"not found", args{"e", vols(hostVolume)}, true}, + {"many ; ok", args{"e", vols(emptyVolume, hostVolume, emptyAndHostVolume)}, false}, + {"many ; nf", args{"e", vols(hostVolume, emptyAndHostVolume)}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := VolumeIsEmptyDir(tt.args.volumeName, tt.args.volumes); (err != nil) != tt.wantErr { + t.Errorf("VolumeIsEmptyDir() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestVolumeIsHostPath(t *testing.T) { + emptyVolume := v1.Volume{Name: "e", VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}} + emptyAndHostVolume := v1.Volume{Name: "e&hp", VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}} + emptyAndHostVolume.VolumeSource.HostPath = &v1.HostPathVolumeSource{Path: "/dev/sdx"} + hostVolume := v1.Volume{Name: "h", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/dev/vdh"}}} + type args struct { + volumeName string + path string + volumes []v1.Volume + } + tests := []struct { + name string + args args + wantErr bool + }{ + {"is EmptyDir", args{"e", "/dev/sdx", vols(emptyVolume)}, true}, + {"is HostPath", args{"h", "/dev/vdh", vols(hostVolume)}, false}, + {"wrong HostPath", args{"h", "/dev/sdx", vols(hostVolume)}, true}, + {"EmptyDir and HostPath", args{"e&hp", "/dev/sdx", vols(emptyAndHostVolume)}, true}, + {"not found", args{"e", "/dev/sdx", vols(hostVolume)}, true}, + {"many ; ok", args{"h", "/dev/vdh", vols(emptyVolume, hostVolume, emptyAndHostVolume)}, false}, + {"many ; nf", args{"h", "/dev/vdh", vols(emptyVolume, emptyAndHostVolume)}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := VolumeIsHostPath(tt.args.volumeName, tt.args.path, tt.args.volumes); (err != nil) != tt.wantErr { + t.Errorf("VolumeIsHostPath() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestVolumeMountExists(t *testing.T) { + mounts := []v1.VolumeMount{{Name: "a"}, {Name: "b"}, {Name: "d"}} + type args struct { + mountName string + mounts []v1.VolumeMount + } + tests := []struct { + name string + args args + wantErr bool + }{ + {"exists", args{"d", mounts}, false}, + {"dne", args{"c", mounts}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := VolumeMountExists(tt.args.mountName, tt.args.mounts); (err != nil) != tt.wantErr { + t.Errorf("VolumeMountExists() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +// Don't test human readables since they aren't critical to function