From e0ef253f7df10811af2871da98d5c306b7cb16b1 Mon Sep 17 00:00:00 2001 From: Paul Morie Date: Mon, 18 Jan 2016 09:35:29 -0500 Subject: [PATCH] Add integration test for ConfigMap --- test/integration/configmap_test.go | 134 +++++++++++++++++++++++++++++ test/integration/secret_test.go | 5 -- test/integration/utils.go | 8 ++ 3 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 test/integration/configmap_test.go diff --git a/test/integration/configmap_test.go b/test/integration/configmap_test.go new file mode 100644 index 00000000000..6a904d8a502 --- /dev/null +++ b/test/integration/configmap_test.go @@ -0,0 +1,134 @@ +// +build integration,!no-etcd + +/* +Copyright 2015 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 integration + +// This file tests use of the configMap API resource. + +import ( + "net/http" + "net/http/httptest" + "testing" + + "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/api/testapi" + client "k8s.io/kubernetes/pkg/client/unversioned" + "k8s.io/kubernetes/pkg/master" + "k8s.io/kubernetes/test/integration/framework" +) + +// TestConfigMap tests apiserver-side behavior of creation of ConfigMaps and pods that consume them. +func TestConfigMap(t *testing.T) { + var m *master.Master + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + m.Handler.ServeHTTP(w, req) + })) + // TODO: Uncomment when fix #19254 + // defer s.Close() + + masterConfig := framework.NewIntegrationTestMasterConfig() + m, err := master.New(masterConfig) + if err != nil { + t.Fatalf("Error in bringing up the master: %v", err) + } + + framework.DeleteAllEtcdKeys() + client := client.NewOrDie(&client.Config{Host: s.URL, ContentConfig: client.ContentConfig{GroupVersion: testapi.Default.GroupVersion()}}) + + DoTestConfigMap(t, client) +} + +func DoTestConfigMap(t *testing.T, client *client.Client) { + ns := "ns" + cfg := api.ConfigMap{ + ObjectMeta: api.ObjectMeta{ + Name: "configmap", + Namespace: ns, + }, + Data: map[string]string{ + "data-1": "value-1", + "data-2": "value-2", + "data-3": "value-3", + }, + } + + if _, err := client.ConfigMaps(cfg.Namespace).Create(&cfg); err != nil { + t.Errorf("unable to create test configMap: %v", err) + } + defer deleteConfigMapOrErrorf(t, client, cfg.Namespace, cfg.Name) + + pod := &api.Pod{ + ObjectMeta: api.ObjectMeta{ + Name: "XXX", + }, + Spec: api.PodSpec{ + Containers: []api.Container{ + { + Name: "fake-name", + Image: "fakeimage", + Env: []api.EnvVar{ + { + Name: "CONFIG_DATA_1", + ValueFrom: &api.EnvVarSource{ + ConfigMapKeyRef: &api.ConfigMapKeySelector{ + LocalObjectReference: api.LocalObjectReference{ + Name: "configmap", + }, + Key: "data-1", + }, + }, + }, + { + Name: "CONFIG_DATA_2", + ValueFrom: &api.EnvVarSource{ + ConfigMapKeyRef: &api.ConfigMapKeySelector{ + LocalObjectReference: api.LocalObjectReference{ + Name: "configmap", + }, + Key: "data-2", + }, + }, + }, { + Name: "CONFIG_DATA_3", + ValueFrom: &api.EnvVarSource{ + ConfigMapKeyRef: &api.ConfigMapKeySelector{ + LocalObjectReference: api.LocalObjectReference{ + Name: "configmap", + }, + Key: "data-3", + }, + }, + }, + }, + }, + }, + }, + } + + pod.ObjectMeta.Name = "uses-configmap" + if _, err := client.Pods(ns).Create(pod); err != nil { + t.Errorf("Failed to create pod: %v", err) + } + defer deletePodOrErrorf(t, client, ns, pod.Name) +} + +func deleteConfigMapOrErrorf(t *testing.T, c *client.Client, ns, name string) { + if err := c.ConfigMaps(ns).Delete(name); err != nil { + t.Errorf("unable to delete ConfigMap %v: %v", name, err) + } +} diff --git a/test/integration/secret_test.go b/test/integration/secret_test.go index 8c89d868465..6ae16b274b4 100644 --- a/test/integration/secret_test.go +++ b/test/integration/secret_test.go @@ -32,11 +32,6 @@ import ( "k8s.io/kubernetes/test/integration/framework" ) -func deletePodOrErrorf(t *testing.T, c *client.Client, ns, name string) { - if err := c.Pods(ns).Delete(name, nil); err != nil { - t.Errorf("unable to delete pod %v: %v", name, err) - } -} func deleteSecretOrErrorf(t *testing.T, c *client.Client, ns, name string) { if err := c.Secrets(ns).Delete(name); err != nil { t.Errorf("unable to delete secret %v: %v", name, err) diff --git a/test/integration/utils.go b/test/integration/utils.go index 107bb5dd774..62d617d2002 100644 --- a/test/integration/utils.go +++ b/test/integration/utils.go @@ -21,10 +21,12 @@ import ( "io/ioutil" "math/rand" "os" + "testing" etcd "github.com/coreos/etcd/client" "github.com/golang/glog" "golang.org/x/net/context" + client "k8s.io/kubernetes/pkg/client/unversioned" ) func newEtcdClient() etcd.Client { @@ -77,3 +79,9 @@ func MakeTempDirOrDie(prefix string, baseDir string) string { } return tempDir } + +func deletePodOrErrorf(t *testing.T, c *client.Client, ns, name string) { + if err := c.Pods(ns).Delete(name, nil); err != nil { + t.Errorf("unable to delete pod %v: %v", name, err) + } +}