From 7a5412a259cc374fad8f688430e343dad554ad17 Mon Sep 17 00:00:00 2001 From: derekwaynecarr Date: Fri, 3 Jun 2016 15:21:52 -0400 Subject: [PATCH] Move quota usage testing for loadbalancers into unit tests --- pkg/quota/evaluator/core/services_test.go | 88 +++++++++++ test/e2e/resource_quota.go | 178 ---------------------- 2 files changed, 88 insertions(+), 178 deletions(-) create mode 100644 pkg/quota/evaluator/core/services_test.go diff --git a/pkg/quota/evaluator/core/services_test.go b/pkg/quota/evaluator/core/services_test.go new file mode 100644 index 00000000000..8affd4cfffa --- /dev/null +++ b/pkg/quota/evaluator/core/services_test.go @@ -0,0 +1,88 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +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 core + +import ( + "testing" + + "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/resource" + "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake" + "k8s.io/kubernetes/pkg/quota" +) + +func TestServiceEvaluatorMatchesResources(t *testing.T) { + kubeClient := fake.NewSimpleClientset() + evaluator := NewServiceEvaluator(kubeClient) + expected := quota.ToSet([]api.ResourceName{ + api.ResourceServices, + api.ResourceServicesNodePorts, + api.ResourceServicesLoadBalancers, + }) + actual := quota.ToSet(evaluator.MatchesResources()) + if !expected.Equal(actual) { + t.Errorf("expected: %v, actual: %v", expected, actual) + } +} + +func TestServiceEvaluatorUsage(t *testing.T) { + kubeClient := fake.NewSimpleClientset() + evaluator := NewServiceEvaluator(kubeClient) + testCases := map[string]struct { + service *api.Service + usage api.ResourceList + }{ + "loadbalancer": { + service: &api.Service{ + Spec: api.ServiceSpec{ + Type: api.ServiceTypeLoadBalancer, + }, + }, + usage: api.ResourceList{ + api.ResourceServicesLoadBalancers: resource.MustParse("1"), + api.ResourceServices: resource.MustParse("1"), + }, + }, + "clusterip": { + service: &api.Service{ + Spec: api.ServiceSpec{ + Type: api.ServiceTypeClusterIP, + }, + }, + usage: api.ResourceList{ + api.ResourceServices: resource.MustParse("1"), + }, + }, + "nodeports": { + service: &api.Service{ + Spec: api.ServiceSpec{ + Type: api.ServiceTypeNodePort, + }, + }, + usage: api.ResourceList{ + api.ResourceServices: resource.MustParse("1"), + api.ResourceServicesNodePorts: resource.MustParse("1"), + }, + }, + } + for testName, testCase := range testCases { + actual := evaluator.Usage(testCase.service) + if !quota.Equals(testCase.usage, actual) { + t.Errorf("%s expected: %v, actual: %v", testName, testCase.usage, actual) + } + } +} diff --git a/test/e2e/resource_quota.go b/test/e2e/resource_quota.go index f75f7492dcd..24b860ba4f1 100644 --- a/test/e2e/resource_quota.go +++ b/test/e2e/resource_quota.go @@ -132,184 +132,6 @@ var _ = framework.KubeDescribe("ResourceQuota", func() { Expect(err).NotTo(HaveOccurred()) }) - It("should create a ResourceQuota and capture the life of a nodePort service.", func() { - By("Creating a ResourceQuota") - quotaName := "test-quota" - resourceQuota := newTestResourceQuota(quotaName) - resourceQuota, err := createResourceQuota(f.Client, f.Namespace.Name, resourceQuota) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status is calculated") - usedResources := api.ResourceList{} - usedResources[api.ResourceQuotas] = resource.MustParse("1") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - - By("Creating a NodePort type Service") - service := newTestServiceForQuota("test-service", api.ServiceTypeNodePort) - service, err = f.Client.Services(f.Namespace.Name).Create(service) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status captures service creation") - usedResources = api.ResourceList{} - usedResources[api.ResourceQuotas] = resource.MustParse("1") - usedResources[api.ResourceServices] = resource.MustParse("1") - usedResources[api.ResourceServicesNodePorts] = resource.MustParse("1") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - - By("Deleting a Service") - err = f.Client.Services(f.Namespace.Name).Delete(service.Name) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status released usage") - usedResources[api.ResourceServices] = resource.MustParse("0") - usedResources[api.ResourceServicesNodePorts] = resource.MustParse("0") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - }) - - It("should create a ResourceQuota and capture the life of a nodePort service updated to clusterIP.", func() { - By("Creating a ResourceQuota") - quotaName := "test-quota" - resourceQuota := newTestResourceQuota(quotaName) - resourceQuota, err := createResourceQuota(f.Client, f.Namespace.Name, resourceQuota) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status is calculated") - usedResources := api.ResourceList{} - usedResources[api.ResourceQuotas] = resource.MustParse("1") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - - By("Creating a NodePort type Service") - service := newTestServiceForQuota("test-service", api.ServiceTypeNodePort) - service, err = f.Client.Services(f.Namespace.Name).Create(service) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status captures service creation") - usedResources = api.ResourceList{} - usedResources[api.ResourceQuotas] = resource.MustParse("1") - usedResources[api.ResourceServices] = resource.MustParse("1") - usedResources[api.ResourceServicesNodePorts] = resource.MustParse("1") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - - By("Updating the service type to clusterIP") - service.Spec.Type = api.ServiceTypeClusterIP - service.Spec.Ports[0].NodePort = 0 - _, err = f.Client.Services(f.Namespace.Name).Update(service) - Expect(err).NotTo(HaveOccurred()) - - By("Checking resource quota status capture service update") - usedResources = api.ResourceList{} - usedResources[api.ResourceQuotas] = resource.MustParse("1") - usedResources[api.ResourceServices] = resource.MustParse("1") - usedResources[api.ResourceServicesNodePorts] = resource.MustParse("0") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - - By("Deleting a Service") - err = f.Client.Services(f.Namespace.Name).Delete(service.Name) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status released usage") - usedResources[api.ResourceServices] = resource.MustParse("0") - usedResources[api.ResourceServicesNodePorts] = resource.MustParse("0") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - }) - - It("should create a ResourceQuota and capture the life of a loadBalancer service.", func() { - By("Creating a ResourceQuota") - quotaName := "test-quota" - resourceQuota := newTestResourceQuota(quotaName) - resourceQuota, err := createResourceQuota(f.Client, f.Namespace.Name, resourceQuota) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status is calculated") - usedResources := api.ResourceList{} - usedResources[api.ResourceQuotas] = resource.MustParse("1") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - - By("Creating a loadBalancer type Service") - service := newTestServiceForQuota("test-service", api.ServiceTypeLoadBalancer) - service, err = f.Client.Services(f.Namespace.Name).Create(service) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status captures service creation") - usedResources = api.ResourceList{} - usedResources[api.ResourceQuotas] = resource.MustParse("1") - usedResources[api.ResourceServices] = resource.MustParse("1") - usedResources[api.ResourceServicesLoadBalancers] = resource.MustParse("1") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - - By("Deleting a Service") - err = f.Client.Services(f.Namespace.Name).Delete(service.Name) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status released usage") - usedResources[api.ResourceServices] = resource.MustParse("0") - usedResources[api.ResourceServicesLoadBalancers] = resource.MustParse("0") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - }) - - It("should create a ResourceQuota and capture the life of a nodePort service updated to loadBalancer.", func() { - By("Creating a ResourceQuota") - quotaName := "test-quota" - resourceQuota := newTestResourceQuota(quotaName) - resourceQuota, err := createResourceQuota(f.Client, f.Namespace.Name, resourceQuota) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status is calculated") - usedResources := api.ResourceList{} - usedResources[api.ResourceQuotas] = resource.MustParse("1") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - - By("Creating a nodePort type Service") - service := newTestServiceForQuota("test-service", api.ServiceTypeNodePort) - service, err = f.Client.Services(f.Namespace.Name).Create(service) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status captures service creation") - usedResources = api.ResourceList{} - usedResources[api.ResourceQuotas] = resource.MustParse("1") - usedResources[api.ResourceServices] = resource.MustParse("1") - usedResources[api.ResourceServicesLoadBalancers] = resource.MustParse("0") - usedResources[api.ResourceServicesNodePorts] = resource.MustParse("1") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - - By("Updating the service type to loadBalancer") - service.Spec.Type = api.ServiceTypeLoadBalancer - service.Spec.Ports[0].NodePort = 0 - _, err = f.Client.Services(f.Namespace.Name).Update(service) - Expect(err).NotTo(HaveOccurred()) - - By("Checking resource quota status capture service update") - usedResources = api.ResourceList{} - usedResources[api.ResourceQuotas] = resource.MustParse("1") - usedResources[api.ResourceServices] = resource.MustParse("1") - usedResources[api.ResourceServicesLoadBalancers] = resource.MustParse("1") - usedResources[api.ResourceServicesNodePorts] = resource.MustParse("0") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - - By("Deleting a Service") - err = f.Client.Services(f.Namespace.Name).Delete(service.Name) - Expect(err).NotTo(HaveOccurred()) - - By("Ensuring resource quota status released usage") - usedResources[api.ResourceServices] = resource.MustParse("0") - usedResources[api.ResourceServicesLoadBalancers] = resource.MustParse("0") - err = waitForResourceQuota(f.Client, f.Namespace.Name, quotaName, usedResources) - Expect(err).NotTo(HaveOccurred()) - }) - It("should create a ResourceQuota and capture the life of a pod.", func() { By("Creating a ResourceQuota") quotaName := "test-quota"