123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- // +build linux
- /*
- Copyright 2014 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package emptydir
- import (
- "os"
- "path/filepath"
- "testing"
- "k8s.io/utils/mount"
- v1 "k8s.io/api/core/v1"
- "k8s.io/apimachinery/pkg/api/resource"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/types"
- utiltesting "k8s.io/client-go/util/testing"
- "k8s.io/kubernetes/pkg/volume"
- volumetest "k8s.io/kubernetes/pkg/volume/testing"
- "k8s.io/kubernetes/pkg/volume/util"
- )
- // Construct an instance of a plugin, by name.
- func makePluginUnderTest(t *testing.T, plugName, basePath string) volume.VolumePlugin {
- plugMgr := volume.VolumePluginMgr{}
- plugMgr.InitPlugins(ProbeVolumePlugins(), nil /* prober */, volumetest.NewFakeVolumeHost(t, basePath, nil, nil))
- plug, err := plugMgr.FindPluginByName(plugName)
- if err != nil {
- t.Errorf("Can't find the plugin by name")
- }
- return plug
- }
- func TestCanSupport(t *testing.T) {
- tmpDir, err := utiltesting.MkTmpdir("emptydirTest")
- if err != nil {
- t.Fatalf("can't make a temp dir: %v", err)
- }
- defer os.RemoveAll(tmpDir)
- plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", tmpDir)
- if plug.GetPluginName() != "kubernetes.io/empty-dir" {
- t.Errorf("Wrong name: %s", plug.GetPluginName())
- }
- if !plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}}}) {
- t.Errorf("Expected true")
- }
- if plug.CanSupport(&volume.Spec{Volume: &v1.Volume{VolumeSource: v1.VolumeSource{}}}) {
- t.Errorf("Expected false")
- }
- }
- type fakeMountDetector struct {
- medium v1.StorageMedium
- isMount bool
- }
- func (fake *fakeMountDetector) GetMountMedium(path string) (v1.StorageMedium, bool, error) {
- return fake.medium, fake.isMount, nil
- }
- func TestPluginEmptyRootContext(t *testing.T) {
- doTestPlugin(t, pluginTestConfig{
- medium: v1.StorageMediumDefault,
- expectedSetupMounts: 0,
- expectedTeardownMounts: 0})
- }
- func TestPluginHugetlbfs(t *testing.T) {
- doTestPlugin(t, pluginTestConfig{
- medium: v1.StorageMediumHugePages,
- expectedSetupMounts: 1,
- expectedTeardownMounts: 0,
- shouldBeMountedBeforeTeardown: true,
- })
- }
- type pluginTestConfig struct {
- medium v1.StorageMedium
- idempotent bool
- expectedSetupMounts int
- shouldBeMountedBeforeTeardown bool
- expectedTeardownMounts int
- }
- // doTestPlugin sets up a volume and tears it back down.
- func doTestPlugin(t *testing.T, config pluginTestConfig) {
- basePath, err := utiltesting.MkTmpdir("emptydir_volume_test")
- if err != nil {
- t.Fatalf("can't make a temp rootdir: %v", err)
- }
- defer os.RemoveAll(basePath)
- var (
- volumePath = filepath.Join(basePath, "pods/poduid/volumes/kubernetes.io~empty-dir/test-volume")
- metadataDir = filepath.Join(basePath, "pods/poduid/plugins/kubernetes.io~empty-dir/test-volume")
- plug = makePluginUnderTest(t, "kubernetes.io/empty-dir", basePath)
- volumeName = "test-volume"
- spec = &v1.Volume{
- Name: volumeName,
- VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{Medium: config.medium}},
- }
- physicalMounter = mount.NewFakeMounter(nil)
- mountDetector = fakeMountDetector{}
- pod = &v1.Pod{
- ObjectMeta: metav1.ObjectMeta{
- UID: types.UID("poduid"),
- },
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceName("hugepages-2Mi"): resource.MustParse("100Mi"),
- },
- },
- },
- },
- },
- }
- )
- if config.idempotent {
- physicalMounter.MountPoints = []mount.MountPoint{
- {
- Path: volumePath,
- },
- }
- util.SetReady(metadataDir)
- }
- mounter, err := plug.(*emptyDirPlugin).newMounterInternal(volume.NewSpecFromVolume(spec),
- pod,
- physicalMounter,
- &mountDetector,
- volume.VolumeOptions{})
- if err != nil {
- t.Errorf("Failed to make a new Mounter: %v", err)
- }
- if mounter == nil {
- t.Errorf("Got a nil Mounter")
- }
- volPath := mounter.GetPath()
- if volPath != volumePath {
- t.Errorf("Got unexpected path: %s", volPath)
- }
- if err := mounter.SetUp(volume.MounterArgs{}); err != nil {
- t.Errorf("Expected success, got: %v", err)
- }
- // Stat the directory and check the permission bits
- fileinfo, err := os.Stat(volPath)
- if !config.idempotent {
- if err != nil {
- if os.IsNotExist(err) {
- t.Errorf("SetUp() failed, volume path not created: %s", volPath)
- } else {
- t.Errorf("SetUp() failed: %v", err)
- }
- }
- if e, a := perm, fileinfo.Mode().Perm(); e != a {
- t.Errorf("Unexpected file mode for %v: expected: %v, got: %v", volPath, e, a)
- }
- } else if err == nil {
- // If this test is for idempotency and we were able
- // to stat the volume path, it's an error.
- t.Errorf("Volume directory was created unexpectedly")
- }
- log := physicalMounter.GetLog()
- // Check the number of mounts performed during setup
- if e, a := config.expectedSetupMounts, len(log); e != a {
- t.Errorf("Expected %v physicalMounter calls during setup, got %v", e, a)
- } else if config.expectedSetupMounts == 1 &&
- (log[0].Action != mount.FakeActionMount || (log[0].FSType != "tmpfs" && log[0].FSType != "hugetlbfs")) {
- t.Errorf("Unexpected physicalMounter action during setup: %#v", log[0])
- }
- physicalMounter.ResetLog()
- // Make an unmounter for the volume
- teardownMedium := v1.StorageMediumDefault
- if config.medium == v1.StorageMediumMemory {
- teardownMedium = v1.StorageMediumMemory
- }
- unmounterMountDetector := &fakeMountDetector{medium: teardownMedium, isMount: config.shouldBeMountedBeforeTeardown}
- unmounter, err := plug.(*emptyDirPlugin).newUnmounterInternal(volumeName, types.UID("poduid"), physicalMounter, unmounterMountDetector)
- if err != nil {
- t.Errorf("Failed to make a new Unmounter: %v", err)
- }
- if unmounter == nil {
- t.Errorf("Got a nil Unmounter")
- }
- // Tear down the volume
- if err := unmounter.TearDown(); err != nil {
- t.Errorf("Expected success, got: %v", err)
- }
- if _, err := os.Stat(volPath); err == nil {
- t.Errorf("TearDown() failed, volume path still exists: %s", volPath)
- } else if !os.IsNotExist(err) {
- t.Errorf("TearDown() failed: %v", err)
- }
- log = physicalMounter.GetLog()
- // Check the number of physicalMounter calls during tardown
- if e, a := config.expectedTeardownMounts, len(log); e != a {
- t.Errorf("Expected %v physicalMounter calls during teardown, got %v", e, a)
- } else if config.expectedTeardownMounts == 1 && log[0].Action != mount.FakeActionUnmount {
- t.Errorf("Unexpected physicalMounter action during teardown: %#v", log[0])
- }
- physicalMounter.ResetLog()
- }
- func TestPluginBackCompat(t *testing.T) {
- basePath, err := utiltesting.MkTmpdir("emptydirTest")
- if err != nil {
- t.Fatalf("can't make a temp dir: %v", err)
- }
- defer os.RemoveAll(basePath)
- plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", basePath)
- spec := &v1.Volume{
- Name: "vol1",
- }
- pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
- mounter, err := plug.NewMounter(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
- if err != nil {
- t.Errorf("Failed to make a new Mounter: %v", err)
- }
- if mounter == nil {
- t.Fatalf("Got a nil Mounter")
- }
- volPath := mounter.GetPath()
- if volPath != filepath.Join(basePath, "pods/poduid/volumes/kubernetes.io~empty-dir/vol1") {
- t.Errorf("Got unexpected path: %s", volPath)
- }
- }
- // TestMetrics tests that MetricProvider methods return sane values.
- func TestMetrics(t *testing.T) {
- // Create an empty temp directory for the volume
- tmpDir, err := utiltesting.MkTmpdir("empty_dir_test")
- if err != nil {
- t.Fatalf("Can't make a tmp dir: %v", err)
- }
- defer os.RemoveAll(tmpDir)
- plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", tmpDir)
- spec := &v1.Volume{
- Name: "vol1",
- }
- pod := &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: types.UID("poduid")}}
- mounter, err := plug.NewMounter(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
- if err != nil {
- t.Errorf("Failed to make a new Mounter: %v", err)
- }
- if mounter == nil {
- t.Fatalf("Got a nil Mounter")
- }
- // Need to create the subdirectory
- os.MkdirAll(mounter.GetPath(), 0755)
- expectedEmptyDirUsage, err := volumetest.FindEmptyDirectoryUsageOnTmpfs()
- if err != nil {
- t.Errorf("Unexpected error finding expected empty directory usage on tmpfs: %v", err)
- }
- // TODO(pwittroc): Move this into a reusable testing utility
- metrics, err := mounter.GetMetrics()
- if err != nil {
- t.Errorf("Unexpected error when calling GetMetrics %v", err)
- }
- if e, a := expectedEmptyDirUsage.Value(), metrics.Used.Value(); e != a {
- t.Errorf("Unexpected value for empty directory; expected %v, got %v", e, a)
- }
- if metrics.Capacity.Value() <= 0 {
- t.Errorf("Expected Capacity to be greater than 0")
- }
- if metrics.Available.Value() <= 0 {
- t.Errorf("Expected Available to be greater than 0")
- }
- }
- func TestGetHugePagesMountOptions(t *testing.T) {
- testCases := map[string]struct {
- pod *v1.Pod
- shouldFail bool
- expectedResult string
- }{
- "testWithProperValues": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceName("hugepages-2Mi"): resource.MustParse("100Mi"),
- },
- },
- },
- },
- },
- },
- shouldFail: false,
- expectedResult: "pagesize=2Mi",
- },
- "testWithProperValuesAndDifferentPageSize": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceName("hugepages-1Gi"): resource.MustParse("2Gi"),
- },
- },
- },
- {
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceName("hugepages-1Gi"): resource.MustParse("4Gi"),
- },
- },
- },
- },
- },
- },
- shouldFail: false,
- expectedResult: "pagesize=1Gi",
- },
- "InitContainerAndContainerHasProperValues": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- InitContainers: []v1.Container{
- {
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceName("hugepages-1Gi"): resource.MustParse("2Gi"),
- },
- },
- },
- {
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceName("hugepages-1Gi"): resource.MustParse("4Gi"),
- },
- },
- },
- },
- },
- },
- shouldFail: false,
- expectedResult: "pagesize=1Gi",
- },
- "InitContainerAndContainerHasDifferentPageSizes": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- InitContainers: []v1.Container{
- {
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceName("hugepages-2Mi"): resource.MustParse("2Gi"),
- },
- },
- },
- {
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceName("hugepages-1Gi"): resource.MustParse("4Gi"),
- },
- },
- },
- },
- },
- },
- shouldFail: true,
- expectedResult: "",
- },
- "ContainersWithMultiplePageSizes": {
- pod: &v1.Pod{
- Spec: v1.PodSpec{
- Containers: []v1.Container{
- {
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceName("hugepages-1Gi"): resource.MustParse("2Gi"),
- },
- },
- },
- {
- Resources: v1.ResourceRequirements{
- Requests: v1.ResourceList{
- v1.ResourceName("hugepages-2Mi"): resource.MustParse("100Mi"),
- },
- },
- },
- },
- },
- },
- shouldFail: true,
- expectedResult: "",
- },
- "PodWithNoHugePagesRequest": {
- pod: &v1.Pod{},
- shouldFail: true,
- expectedResult: "",
- },
- }
- for testCaseName, testCase := range testCases {
- value, err := getPageSizeMountOptionFromPod(testCase.pod)
- if testCase.shouldFail && err == nil {
- t.Errorf("Expected an error in %v", testCaseName)
- } else if !testCase.shouldFail && err != nil {
- t.Errorf("Unexpected error in %v, got %v", testCaseName, err)
- } else if testCase.expectedResult != value {
- t.Errorf("Unexpected mountOptions for Pod. Expected %v, got %v", testCase.expectedResult, value)
- }
- }
- }
|