115 lines
3.8 KiB
Go
115 lines
3.8 KiB
Go
/*
|
|
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.
|
|
*/
|
|
|
|
// 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"
|
|
"log"
|
|
"net/http/httptest"
|
|
"time"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/controller"
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet"
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/master"
|
|
"github.com/coreos/go-etcd/etcd"
|
|
)
|
|
|
|
func main() {
|
|
// Setup
|
|
servers := []string{"http://localhost:4001"}
|
|
log.Printf("Creating etcd client pointing to %v", servers)
|
|
machineList := []string{"localhost", "machine"}
|
|
|
|
// Master
|
|
m := master.New(servers, machineList, nil)
|
|
apiserver := httptest.NewServer(m.ConstructHandler("/api/v1beta1"))
|
|
|
|
controllerManager := controller.MakeReplicationManager(etcd.NewClient(servers), client.New(apiserver.URL, nil))
|
|
|
|
controllerManager.Run(10 * time.Second)
|
|
|
|
// Kublet
|
|
fakeDocker := &kubelet.FakeDockerClient{}
|
|
myKubelet := kubelet.Kubelet{
|
|
Hostname: machineList[0],
|
|
DockerClient: fakeDocker,
|
|
FileCheckFrequency: 5 * time.Second,
|
|
SyncFrequency: 5 * time.Second,
|
|
HTTPCheckFrequency: 5 * time.Second,
|
|
}
|
|
go myKubelet.RunKubelet("", "https://raw.githubusercontent.com/GoogleCloudPlatform/container-vm-guestbook-redis-python/master/manifest.yaml", servers[0], "localhost", 0)
|
|
|
|
// Create a second kublet so that the guestbook example's two redis slaves both
|
|
// have a place they can schedule.
|
|
otherKubelet := kubelet.Kubelet{
|
|
Hostname: machineList[1],
|
|
DockerClient: &kubelet.FakeDockerClient{},
|
|
FileCheckFrequency: 5 * time.Second,
|
|
SyncFrequency: 5 * time.Second,
|
|
HTTPCheckFrequency: 5 * time.Second,
|
|
}
|
|
go otherKubelet.RunKubelet("", "", servers[0], "localhost", 0)
|
|
|
|
// Ok. we're good to go.
|
|
log.Printf("API Server started on %s", apiserver.URL)
|
|
// Wait for the synchronization threads to come up.
|
|
time.Sleep(time.Second * 10)
|
|
|
|
kubeClient := client.New(apiserver.URL, nil)
|
|
data, err := ioutil.ReadFile("api/examples/controller.json")
|
|
if err != nil {
|
|
log.Fatalf("Unexpected error: %#v", err)
|
|
}
|
|
var controllerRequest api.ReplicationController
|
|
if err = json.Unmarshal(data, &controllerRequest); err != nil {
|
|
log.Fatalf("Unexpected error: %#v", err)
|
|
}
|
|
|
|
if _, err = kubeClient.CreateReplicationController(controllerRequest); err != nil {
|
|
log.Fatalf("Unexpected error: %#v", err)
|
|
}
|
|
// Give the controllers some time to actually create the pods
|
|
time.Sleep(time.Second * 10)
|
|
|
|
// Validate that they're truly up.
|
|
pods, err := kubeClient.ListPods(nil)
|
|
if err != nil || len(pods.Items) != 2 {
|
|
log.Fatal("FAILED")
|
|
}
|
|
|
|
// 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{}{}
|
|
for _, p := range fakeDocker.Created {
|
|
// The last 8 characters are random, so slice them off.
|
|
if n := len(p); n > 8 {
|
|
createdPods[p[:n-8]] = struct{}{}
|
|
}
|
|
}
|
|
// We expect 3: 1 net container + 1 pod from the replication controller + 1 pod from the URL.
|
|
if len(createdPods) != 3 {
|
|
log.Fatalf("Unexpected list of created pods: %#v\n", createdPods)
|
|
}
|
|
log.Printf("OK")
|
|
}
|