2014-07-15 14:52:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. 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 config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-10-08 19:56:02 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
|
2014-07-15 14:52:39 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
|
2014-10-08 19:56:02 +00:00
|
|
|
|
2014-07-15 14:52:39 +00:00
|
|
|
"gopkg.in/v1/yaml"
|
|
|
|
)
|
|
|
|
|
2014-10-08 19:56:02 +00:00
|
|
|
func ExampleManifestAndPod(id string) (api.ContainerManifest, api.BoundPod) {
|
|
|
|
manifest := api.ContainerManifest{
|
|
|
|
ID: id,
|
|
|
|
UUID: "uid",
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "c" + id,
|
|
|
|
Image: "foo",
|
2014-11-05 19:08:26 +00:00
|
|
|
TerminationMessagePath: "/somepath",
|
2014-10-08 19:56:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Volumes: []api.Volume{
|
|
|
|
{
|
|
|
|
Name: "host-dir",
|
|
|
|
Source: &api.VolumeSource{
|
|
|
|
HostDir: &api.HostDir{"/dir/path"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expectedPod := api.BoundPod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2014-10-22 17:02:02 +00:00
|
|
|
Name: id,
|
2014-10-08 19:56:02 +00:00
|
|
|
UID: "uid",
|
|
|
|
Namespace: "default",
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "c" + id,
|
|
|
|
Image: "foo",
|
2014-11-05 19:08:26 +00:00
|
|
|
TerminationMessagePath: "/somepath",
|
2014-10-08 19:56:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Volumes: []api.Volume{
|
|
|
|
{
|
|
|
|
Name: "host-dir",
|
|
|
|
Source: &api.VolumeSource{
|
|
|
|
HostDir: &api.HostDir{"/dir/path"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return manifest, expectedPod
|
|
|
|
}
|
|
|
|
|
2014-07-15 14:52:39 +00:00
|
|
|
func TestExtractFromNonExistentFile(t *testing.T) {
|
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
c := SourceFile{"/some/fake/file", ch}
|
|
|
|
err := c.extractFromPath()
|
2014-07-27 13:30:32 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error")
|
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateOnNonExistentFile(t *testing.T) {
|
|
|
|
ch := make(chan interface{})
|
|
|
|
NewSourceFile("random_non_existent_path", time.Millisecond, ch)
|
|
|
|
select {
|
|
|
|
case got := <-ch:
|
|
|
|
t.Errorf("Expected no update, Got %#v", got)
|
|
|
|
case <-time.After(2 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeTestFile(t *testing.T, dir, name string, contents string) *os.File {
|
|
|
|
file, err := ioutil.TempFile(os.TempDir(), "test_pod_config")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to create test file %#v", err)
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
if err := ioutil.WriteFile(file.Name(), []byte(contents), 0555); err != nil {
|
|
|
|
t.Fatalf("Unable to write test file %#v", err)
|
|
|
|
}
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadFromFile(t *testing.T) {
|
|
|
|
file := writeTestFile(t, os.TempDir(), "test_pod_config", "version: v1beta1\nid: test\ncontainers:\n- image: test/image")
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
ch := make(chan interface{})
|
|
|
|
NewSourceFile(file.Name(), time.Millisecond, ch)
|
|
|
|
select {
|
|
|
|
case got := <-ch:
|
|
|
|
update := got.(kubelet.PodUpdate)
|
2014-10-08 19:56:02 +00:00
|
|
|
expected := CreatePodUpdate(kubelet.SET, api.BoundPod{
|
2014-10-23 20:51:34 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2014-10-22 17:02:02 +00:00
|
|
|
Name: simpleSubdomainSafeHash(file.Name()),
|
2014-10-08 19:56:02 +00:00
|
|
|
UID: simpleSubdomainSafeHash(file.Name()),
|
|
|
|
Namespace: "default",
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
2014-11-05 19:08:26 +00:00
|
|
|
Containers: []api.Container{{Image: "test/image", TerminationMessagePath: "/dev/termination-log"}},
|
2014-07-15 14:52:39 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if !reflect.DeepEqual(expected, update) {
|
2014-10-08 19:56:02 +00:00
|
|
|
t.Fatalf("Expected %#v, Got %#v", expected, update)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-time.After(2 * time.Millisecond):
|
|
|
|
t.Errorf("Expected update, timeout instead")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractFromBadDataFile(t *testing.T) {
|
|
|
|
file := writeTestFile(t, os.TempDir(), "test_pod_config", string([]byte{1, 2, 3}))
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
c := SourceFile{file.Name(), ch}
|
|
|
|
err := c.extractFromPath()
|
2014-07-27 13:30:32 +00:00
|
|
|
if err == nil {
|
2014-10-08 19:56:02 +00:00
|
|
|
t.Fatalf("Expected error")
|
2014-07-27 13:30:32 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
expectEmptyChannel(t, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractFromValidDataFile(t *testing.T) {
|
2014-10-08 19:56:02 +00:00
|
|
|
manifest, expectedPod := ExampleManifestAndPod("id")
|
2014-07-15 14:52:39 +00:00
|
|
|
|
|
|
|
text, err := json.Marshal(manifest)
|
2014-07-27 13:30:32 +00:00
|
|
|
if err != nil {
|
2014-10-08 19:56:02 +00:00
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
2014-07-27 13:30:32 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
file := writeTestFile(t, os.TempDir(), "test_pod_config", string(text))
|
|
|
|
defer os.Remove(file.Name())
|
|
|
|
|
2014-10-22 17:02:02 +00:00
|
|
|
expectedPod.Name = simpleSubdomainSafeHash(file.Name())
|
2014-10-08 19:56:02 +00:00
|
|
|
|
2014-07-15 14:52:39 +00:00
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
c := SourceFile{file.Name(), ch}
|
|
|
|
err = c.extractFromPath()
|
2014-07-27 13:30:32 +00:00
|
|
|
if err != nil {
|
2014-10-08 19:56:02 +00:00
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
2014-07-27 13:30:32 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
update := (<-ch).(kubelet.PodUpdate)
|
2014-10-08 19:56:02 +00:00
|
|
|
expected := CreatePodUpdate(kubelet.SET, expectedPod)
|
2014-07-15 14:52:39 +00:00
|
|
|
if !reflect.DeepEqual(expected, update) {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", expected, update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractFromEmptyDir(t *testing.T) {
|
|
|
|
dirName, err := ioutil.TempDir("", "foo")
|
2014-07-27 13:30:32 +00:00
|
|
|
if err != nil {
|
2014-10-08 19:56:02 +00:00
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
2014-07-27 13:30:32 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
defer os.RemoveAll(dirName)
|
|
|
|
|
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
c := SourceFile{dirName, ch}
|
|
|
|
err = c.extractFromPath()
|
2014-07-27 13:30:32 +00:00
|
|
|
if err != nil {
|
2014-10-08 19:56:02 +00:00
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
2014-07-27 13:30:32 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
|
|
|
|
update := (<-ch).(kubelet.PodUpdate)
|
|
|
|
expected := CreatePodUpdate(kubelet.SET)
|
|
|
|
if !reflect.DeepEqual(expected, update) {
|
|
|
|
t.Errorf("Expected %#v, Got %#v", expected, update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractFromDir(t *testing.T) {
|
2014-10-08 19:56:02 +00:00
|
|
|
manifest, expectedPod := ExampleManifestAndPod("1")
|
|
|
|
manifest2, expectedPod2 := ExampleManifestAndPod("2")
|
|
|
|
|
|
|
|
manifests := []api.ContainerManifest{manifest, manifest2}
|
|
|
|
pods := []api.BoundPod{expectedPod, expectedPod2}
|
2014-07-15 14:52:39 +00:00
|
|
|
files := make([]*os.File, len(manifests))
|
|
|
|
|
|
|
|
dirName, err := ioutil.TempDir("", "foo")
|
2014-07-27 13:30:32 +00:00
|
|
|
if err != nil {
|
2014-10-08 19:56:02 +00:00
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
2014-07-27 13:30:32 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
|
|
|
|
for i, manifest := range manifests {
|
|
|
|
data, err := json.Marshal(manifest)
|
2014-07-27 13:30:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
2014-10-08 19:56:02 +00:00
|
|
|
continue
|
2014-07-27 13:30:32 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
file, err := ioutil.TempFile(dirName, manifest.ID)
|
2014-07-27 13:30:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
2014-10-08 19:56:02 +00:00
|
|
|
continue
|
2014-07-27 13:30:32 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
name := file.Name()
|
2014-07-27 13:30:32 +00:00
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
2014-10-08 19:56:02 +00:00
|
|
|
continue
|
2014-07-27 13:30:32 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
ioutil.WriteFile(name, data, 0755)
|
|
|
|
files[i] = file
|
2014-10-22 17:02:02 +00:00
|
|
|
pods[i].Name = simpleSubdomainSafeHash(name)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan interface{}, 1)
|
|
|
|
c := SourceFile{dirName, ch}
|
|
|
|
err = c.extractFromPath()
|
2014-07-27 13:30:32 +00:00
|
|
|
if err != nil {
|
2014-10-08 19:56:02 +00:00
|
|
|
t.Fatalf("Unexpected error: %v", err)
|
2014-07-27 13:30:32 +00:00
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
|
|
|
|
update := (<-ch).(kubelet.PodUpdate)
|
2014-10-08 19:56:02 +00:00
|
|
|
expected := CreatePodUpdate(kubelet.SET, pods...)
|
2014-07-15 14:52:39 +00:00
|
|
|
sort.Sort(sortedPods(update.Pods))
|
|
|
|
sort.Sort(sortedPods(expected.Pods))
|
|
|
|
if !reflect.DeepEqual(expected, update) {
|
2014-10-08 19:56:02 +00:00
|
|
|
t.Fatalf("Expected %#v, Got %#v", expected, update)
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
2014-08-03 20:55:34 +00:00
|
|
|
for i := range update.Pods {
|
2014-10-08 19:56:02 +00:00
|
|
|
if errs := validation.ValidateBoundPod(&update.Pods[i]); len(errs) != 0 {
|
2014-08-03 20:55:34 +00:00
|
|
|
t.Errorf("Expected no validation errors on %#v, Got %#v", update.Pods[i], errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubdomainSafeName(t *testing.T) {
|
|
|
|
type Case struct {
|
|
|
|
Input string
|
|
|
|
Expected string
|
|
|
|
}
|
|
|
|
testCases := []Case{
|
|
|
|
{"/some/path/invalidUPPERCASE", "invaliduppercasa6hlenc0vpqbbdtt26ghneqsq3pvud"},
|
|
|
|
{"/some/path/_-!%$#&@^&*(){}", "nvhc03p016m60huaiv3avts372rl2p"},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
value := simpleSubdomainSafeHash(testCase.Input)
|
|
|
|
if value != testCase.Expected {
|
|
|
|
t.Errorf("Expected %s, Got %s", testCase.Expected, value)
|
|
|
|
}
|
|
|
|
value2 := simpleSubdomainSafeHash(testCase.Input)
|
|
|
|
if value != value2 {
|
|
|
|
t.Errorf("Value for %s was not stable across runs: %s %s", testCase.Input, value, value2)
|
|
|
|
}
|
|
|
|
}
|
2014-07-15 14:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// These are used for testing extract json (below)
|
|
|
|
type TestData struct {
|
|
|
|
Value string
|
|
|
|
Number int
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestObject struct {
|
|
|
|
Name string
|
|
|
|
Data TestData
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyStringEquals(t *testing.T, actual, expected string) {
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("Verification failed. Expected: %s, Found %s", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyIntEquals(t *testing.T, actual, expected int) {
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("Verification failed. Expected: %d, Found %d", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtractJSON(t *testing.T) {
|
|
|
|
obj := TestObject{}
|
|
|
|
data := `{ "name": "foo", "data": { "value": "bar", "number": 10 } }`
|
|
|
|
|
|
|
|
if err := yaml.Unmarshal([]byte(data), &obj); err != nil {
|
|
|
|
t.Fatalf("Could not unmarshal JSON: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyStringEquals(t, obj.Name, "foo")
|
|
|
|
verifyStringEquals(t, obj.Data.Value, "bar")
|
|
|
|
verifyIntEquals(t, obj.Data.Number, 10)
|
|
|
|
}
|