1/*
2Copyright 2018 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package storage
18
19import (
20	"github.com/onsi/ginkgo"
21	"k8s.io/api/core/v1"
22	clientset "k8s.io/client-go/kubernetes"
23	v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
24	"k8s.io/kubernetes/test/e2e/framework"
25	e2enode "k8s.io/kubernetes/test/e2e/framework/node"
26	e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
27	"k8s.io/kubernetes/test/e2e/storage/utils"
28)
29
30var _ = utils.SIGDescribe("Volume limits", func() {
31	var (
32		c clientset.Interface
33	)
34	f := framework.NewDefaultFramework("volume-limits-on-node")
35	ginkgo.BeforeEach(func() {
36		e2eskipper.SkipUnlessProviderIs("aws", "gce", "gke")
37		// If CSIMigration is enabled, then the limits should be on CSINodes, not Nodes, and another test checks this
38		e2eskipper.SkipIfCSIMigrationEnabled()
39		c = f.ClientSet
40		framework.ExpectNoError(framework.WaitForAllNodesSchedulable(c, framework.TestContext.NodeSchedulableTimeout))
41	})
42
43	ginkgo.It("should verify that all nodes have volume limits", func() {
44		nodeList, err := e2enode.GetReadySchedulableNodes(f.ClientSet)
45		framework.ExpectNoError(err)
46		for _, node := range nodeList.Items {
47			volumeLimits := getVolumeLimit(&node)
48			if len(volumeLimits) == 0 {
49				framework.Failf("Expected volume limits to be set")
50			}
51		}
52	})
53})
54
55func getVolumeLimit(node *v1.Node) map[v1.ResourceName]int64 {
56	volumeLimits := map[v1.ResourceName]int64{}
57	nodeAllocatables := node.Status.Allocatable
58	for k, v := range nodeAllocatables {
59		if v1helper.IsAttachableVolumeResourceName(k) {
60			volumeLimits[k] = v.Value()
61		}
62	}
63	return volumeLimits
64}
65