mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Add lifecycle tests from kubelet_test to lifecycle.
This commit is contained in:
parent
7a737892f5
commit
bb8ee5b0db
@ -17,9 +17,13 @@ limitations under the License.
|
||||
package lifecycle
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
kubecontainer "github.com/GoogleCloudPlatform/kubernetes/pkg/kubelet/container"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
@ -67,3 +71,124 @@ func TestResolvePortStringUnknown(t *testing.T) {
|
||||
t.Error("unexpected non-error")
|
||||
}
|
||||
}
|
||||
|
||||
type fakeContainerCommandRunner struct {
|
||||
Cmd []string
|
||||
ID string
|
||||
}
|
||||
|
||||
func (f *fakeContainerCommandRunner) RunInContainer(id string, cmd []string) ([]byte, error) {
|
||||
f.Cmd = cmd
|
||||
f.ID = id
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeContainerCommandRunner) ExecInContainer(id string, cmd []string, in io.Reader, out, err io.WriteCloser, tty bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeContainerCommandRunner) PortForward(pod *kubecontainer.Pod, port uint16, stream io.ReadWriteCloser) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestRunHandlerExec(t *testing.T) {
|
||||
fakeCommandRunner := fakeContainerCommandRunner{}
|
||||
handlerRunner := NewHandlerRunner(&fakeHTTP{}, &fakeCommandRunner, nil)
|
||||
|
||||
containerID := "abc1234"
|
||||
podName := "podFoo"
|
||||
podNamespace := "nsFoo"
|
||||
containerName := "containerFoo"
|
||||
|
||||
container := api.Container{
|
||||
Name: containerName,
|
||||
Lifecycle: &api.Lifecycle{
|
||||
PostStart: &api.Handler{
|
||||
Exec: &api.ExecAction{
|
||||
Command: []string{"ls", "-a"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
pod := api.Pod{}
|
||||
pod.ObjectMeta.Name = podName
|
||||
pod.ObjectMeta.Namespace = podNamespace
|
||||
pod.Spec.Containers = []api.Container{container}
|
||||
err := handlerRunner.Run(containerID, &pod, &container, container.Lifecycle.PostStart)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if fakeCommandRunner.ID != containerID ||
|
||||
!reflect.DeepEqual(container.Lifecycle.PostStart.Exec.Command, fakeCommandRunner.Cmd) {
|
||||
t.Errorf("unexpected commands: %v", fakeCommandRunner)
|
||||
}
|
||||
}
|
||||
|
||||
type fakeHTTP struct {
|
||||
url string
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeHTTP) Get(url string) (*http.Response, error) {
|
||||
f.url = url
|
||||
return nil, f.err
|
||||
}
|
||||
|
||||
func TestRunHandlerHttp(t *testing.T) {
|
||||
fakeHttp := fakeHTTP{}
|
||||
handlerRunner := NewHandlerRunner(&fakeHttp, &fakeContainerCommandRunner{}, nil)
|
||||
|
||||
containerID := "abc1234"
|
||||
podName := "podFoo"
|
||||
podNamespace := "nsFoo"
|
||||
containerName := "containerFoo"
|
||||
|
||||
container := api.Container{
|
||||
Name: containerName,
|
||||
Lifecycle: &api.Lifecycle{
|
||||
PostStart: &api.Handler{
|
||||
HTTPGet: &api.HTTPGetAction{
|
||||
Host: "foo",
|
||||
Port: util.IntOrString{IntVal: 8080, Kind: util.IntstrInt},
|
||||
Path: "bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
pod := api.Pod{}
|
||||
pod.ObjectMeta.Name = podName
|
||||
pod.ObjectMeta.Namespace = podNamespace
|
||||
pod.Spec.Containers = []api.Container{container}
|
||||
err := handlerRunner.Run(containerID, &pod, &container, container.Lifecycle.PostStart)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if fakeHttp.url != "http://foo:8080/bar" {
|
||||
t.Errorf("unexpected url: %s", fakeHttp.url)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunHandlerNil(t *testing.T) {
|
||||
handlerRunner := NewHandlerRunner(&fakeHTTP{}, &fakeContainerCommandRunner{}, nil)
|
||||
containerID := "abc1234"
|
||||
podName := "podFoo"
|
||||
podNamespace := "nsFoo"
|
||||
containerName := "containerFoo"
|
||||
|
||||
container := api.Container{
|
||||
Name: containerName,
|
||||
Lifecycle: &api.Lifecycle{
|
||||
PostStart: &api.Handler{},
|
||||
},
|
||||
}
|
||||
pod := api.Pod{}
|
||||
pod.ObjectMeta.Name = podName
|
||||
pod.ObjectMeta.Namespace = podNamespace
|
||||
pod.Spec.Containers = []api.Container{container}
|
||||
err := handlerRunner.Run(containerID, &pod, &container, container.Lifecycle.PostStart)
|
||||
if err == nil {
|
||||
t.Errorf("expect error, but got nil")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user