2014-06-06 23:40:48 +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.
|
|
|
|
*/
|
2014-06-11 20:03:46 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
// A basic integration test for the service.
|
|
|
|
// Assumes that there is a pre-existing etcd server running on localhost.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2014-06-24 23:31:33 +00:00
|
|
|
"net/http"
|
2014-06-06 23:40:48 +00:00
|
|
|
"net/http/httptest"
|
2014-06-27 03:24:10 +00:00
|
|
|
"runtime"
|
2014-06-06 23:40:48 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-06-11 20:03:46 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
2014-06-18 00:57:06 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/controller"
|
2014-06-24 01:28:06 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/master"
|
2014-06-25 03:51:57 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-06-06 23:40:48 +00:00
|
|
|
"github.com/coreos/go-etcd/etcd"
|
2014-06-25 03:51:57 +00:00
|
|
|
"github.com/golang/glog"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2014-06-27 03:24:10 +00:00
|
|
|
runtime.GOMAXPROCS(4)
|
2014-06-25 03:51:57 +00:00
|
|
|
util.InitLogs()
|
|
|
|
defer util.FlushLogs()
|
|
|
|
|
2014-06-24 23:31:33 +00:00
|
|
|
manifestUrl := ServeCachedManifestFile()
|
2014-06-06 23:40:48 +00:00
|
|
|
// Setup
|
|
|
|
servers := []string{"http://localhost:4001"}
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Infof("Creating etcd client pointing to %v", servers)
|
2014-06-24 01:28:06 +00:00
|
|
|
machineList := []string{"localhost", "machine"}
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2014-06-24 01:28:06 +00:00
|
|
|
// Master
|
|
|
|
m := master.New(servers, machineList, nil)
|
|
|
|
apiserver := httptest.NewServer(m.ConstructHandler("/api/v1beta1"))
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2014-06-24 01:28:06 +00:00
|
|
|
controllerManager := controller.MakeReplicationManager(etcd.NewClient(servers), client.New(apiserver.URL, nil))
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2014-06-27 03:24:10 +00:00
|
|
|
controllerManager.Run(1 * time.Second)
|
2014-06-06 23:40:48 +00:00
|
|
|
|
2014-06-24 01:28:06 +00:00
|
|
|
// Kublet
|
2014-06-24 23:31:33 +00:00
|
|
|
fakeDocker1 := &kubelet.FakeDockerClient{}
|
2014-06-24 17:19:47 +00:00
|
|
|
myKubelet := kubelet.Kubelet{
|
2014-06-24 01:28:06 +00:00
|
|
|
Hostname: machineList[0],
|
2014-06-24 23:31:33 +00:00
|
|
|
DockerClient: fakeDocker1,
|
|
|
|
DockerPuller: &kubelet.FakeDockerPuller{},
|
2014-06-24 01:28:06 +00:00
|
|
|
FileCheckFrequency: 5 * time.Second,
|
|
|
|
SyncFrequency: 5 * time.Second,
|
|
|
|
HTTPCheckFrequency: 5 * time.Second,
|
|
|
|
}
|
2014-06-24 23:31:33 +00:00
|
|
|
go myKubelet.RunKubelet("", manifestUrl, servers[0], "localhost", 0)
|
2014-06-24 01:28:06 +00:00
|
|
|
|
|
|
|
// Create a second kublet so that the guestbook example's two redis slaves both
|
|
|
|
// have a place they can schedule.
|
2014-06-24 23:31:33 +00:00
|
|
|
fakeDocker2 := &kubelet.FakeDockerClient{}
|
2014-06-24 17:19:47 +00:00
|
|
|
otherKubelet := kubelet.Kubelet{
|
2014-06-24 01:28:06 +00:00
|
|
|
Hostname: machineList[1],
|
2014-06-24 23:31:33 +00:00
|
|
|
DockerClient: fakeDocker2,
|
|
|
|
DockerPuller: &kubelet.FakeDockerPuller{},
|
2014-06-24 01:28:06 +00:00
|
|
|
FileCheckFrequency: 5 * time.Second,
|
|
|
|
SyncFrequency: 5 * time.Second,
|
|
|
|
HTTPCheckFrequency: 5 * time.Second,
|
|
|
|
}
|
2014-06-24 17:19:47 +00:00
|
|
|
go otherKubelet.RunKubelet("", "", servers[0], "localhost", 0)
|
2014-06-24 01:28:06 +00:00
|
|
|
|
2014-06-06 23:40:48 +00:00
|
|
|
// Ok. we're good to go.
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Infof("API Server started on %s", apiserver.URL)
|
2014-06-06 23:40:48 +00:00
|
|
|
// Wait for the synchronization threads to come up.
|
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
|
2014-06-24 01:28:06 +00:00
|
|
|
kubeClient := client.New(apiserver.URL, nil)
|
2014-06-06 23:40:48 +00:00
|
|
|
data, err := ioutil.ReadFile("api/examples/controller.json")
|
|
|
|
if err != nil {
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Fatalf("Unexpected error: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
var controllerRequest api.ReplicationController
|
|
|
|
if err = json.Unmarshal(data, &controllerRequest); err != nil {
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Fatalf("Unexpected error: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = kubeClient.CreateReplicationController(controllerRequest); err != nil {
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Fatalf("Unexpected error: %#v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-09 05:38:45 +00:00
|
|
|
// Give the controllers some time to actually create the pods
|
2014-06-06 23:40:48 +00:00
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
|
|
|
|
// Validate that they're truly up.
|
2014-06-09 05:38:45 +00:00
|
|
|
pods, err := kubeClient.ListPods(nil)
|
|
|
|
if err != nil || len(pods.Items) != 2 {
|
2014-06-27 03:24:10 +00:00
|
|
|
glog.Fatal("FAILED: %#v", pods.Items)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-24 01:28:06 +00:00
|
|
|
|
|
|
|
// Check that kubelet tried to make the pods.
|
|
|
|
// Using a set to list unique creation attempts. Our fake is
|
|
|
|
// really stupid, so kubelet tries to create these multiple times.
|
|
|
|
createdPods := map[string]struct{}{}
|
2014-06-24 23:31:33 +00:00
|
|
|
for _, p := range fakeDocker1.Created {
|
2014-06-24 01:28:06 +00:00
|
|
|
// The last 8 characters are random, so slice them off.
|
2014-06-24 17:19:47 +00:00
|
|
|
if n := len(p); n > 8 {
|
2014-06-24 01:28:06 +00:00
|
|
|
createdPods[p[:n-8]] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2014-06-24 23:31:33 +00:00
|
|
|
for _, p := range fakeDocker2.Created {
|
|
|
|
// The last 8 characters are random, so slice them off.
|
|
|
|
if n := len(p); n > 8 {
|
|
|
|
createdPods[p[:n-8]] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We expect 5: 2 net containers + 2 pods from the replication controller +
|
|
|
|
// 1 net container + 2 pods from the URL.
|
|
|
|
if len(createdPods) != 7 {
|
2014-06-27 03:24:10 +00:00
|
|
|
glog.Fatalf("Unexpected list of created pods: %#v %#v %#v\n", createdPods, fakeDocker1.Created, fakeDocker2.Created)
|
2014-06-24 01:28:06 +00:00
|
|
|
}
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Infof("OK")
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-06-24 23:31:33 +00:00
|
|
|
|
|
|
|
// Serve a file for kubelet to read.
|
|
|
|
func ServeCachedManifestFile() (servingAddress string) {
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/manifest" {
|
|
|
|
w.Write([]byte(testManifestFile))
|
|
|
|
return
|
|
|
|
}
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Fatalf("Got request: %#v\n", r)
|
2014-06-24 23:31:33 +00:00
|
|
|
http.NotFound(w, r)
|
|
|
|
}))
|
|
|
|
return server.URL + "/manifest"
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// This is copied from, and should be kept in sync with:
|
|
|
|
// https://raw.githubusercontent.com/GoogleCloudPlatform/container-vm-guestbook-redis-python/master/manifest.yaml
|
|
|
|
testManifestFile = `version: v1beta1
|
2014-06-27 03:24:10 +00:00
|
|
|
id: web-test
|
2014-06-24 23:31:33 +00:00
|
|
|
containers:
|
|
|
|
- name: redis
|
|
|
|
image: dockerfile/redis
|
|
|
|
volumeMounts:
|
|
|
|
- name: redis-data
|
|
|
|
path: /data
|
|
|
|
|
|
|
|
- name: guestbook
|
|
|
|
image: google/guestbook-python-redis
|
|
|
|
ports:
|
|
|
|
- name: www
|
|
|
|
hostPort: 80
|
|
|
|
containerPort: 80
|
|
|
|
|
|
|
|
volumes:
|
|
|
|
- name: redis-data`
|
|
|
|
)
|