mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-08 03:33:56 +00:00
added tests
This commit is contained in:
parent
3d0cd81feb
commit
c72c2a0d1e
@ -255,31 +255,7 @@ func TestKubeletDirsCompat(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestKillContainerWithError(t *testing.T) {
|
func TestKillContainerWithError(t *testing.T) {
|
||||||
fakeDocker := &dockertools.FakeDockerClient{
|
containers := []docker.APIContainers{
|
||||||
Err: fmt.Errorf("sample error"),
|
|
||||||
ContainerList: []docker.APIContainers{
|
|
||||||
{
|
|
||||||
ID: "1234",
|
|
||||||
Names: []string{"/k8s_foo_qux_1234_42"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ID: "5678",
|
|
||||||
Names: []string{"/k8s_bar_qux_5678_42"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
kubelet, _ := newTestKubelet(t)
|
|
||||||
kubelet.dockerClient = fakeDocker
|
|
||||||
err := kubelet.killContainer(&fakeDocker.ContainerList[0])
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("expected error, found nil")
|
|
||||||
}
|
|
||||||
verifyCalls(t, fakeDocker, []string{"stop"})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestKillContainer(t *testing.T) {
|
|
||||||
kubelet, fakeDocker := newTestKubelet(t)
|
|
||||||
fakeDocker.ContainerList = []docker.APIContainers{
|
|
||||||
{
|
{
|
||||||
ID: "1234",
|
ID: "1234",
|
||||||
Names: []string{"/k8s_foo_qux_1234_42"},
|
Names: []string{"/k8s_foo_qux_1234_42"},
|
||||||
@ -289,15 +265,63 @@ func TestKillContainer(t *testing.T) {
|
|||||||
Names: []string{"/k8s_bar_qux_5678_42"},
|
Names: []string{"/k8s_bar_qux_5678_42"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
fakeDocker := &dockertools.FakeDockerClient{
|
||||||
|
Err: fmt.Errorf("sample error"),
|
||||||
|
ContainerList: append([]docker.APIContainers{}, containers...),
|
||||||
|
}
|
||||||
|
kubelet, _ := newTestKubelet(t)
|
||||||
|
for _, c := range fakeDocker.ContainerList {
|
||||||
|
kubelet.readiness.set(c.ID, true)
|
||||||
|
}
|
||||||
|
kubelet.dockerClient = fakeDocker
|
||||||
|
err := kubelet.killContainer(&fakeDocker.ContainerList[0])
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected error, found nil")
|
||||||
|
}
|
||||||
|
verifyCalls(t, fakeDocker, []string{"stop"})
|
||||||
|
killedContainer := containers[0]
|
||||||
|
liveContainer := containers[1]
|
||||||
|
if _, found := kubelet.readiness.states[killedContainer.ID]; found {
|
||||||
|
t.Errorf("exepcted container entry ID '%v' to not be found. states: %+v", killedContainer.ID, kubelet.readiness.states)
|
||||||
|
}
|
||||||
|
if _, found := kubelet.readiness.states[liveContainer.ID]; !found {
|
||||||
|
t.Errorf("exepcted container entry ID '%v' to be found. states: %+v", liveContainer.ID, kubelet.readiness.states)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKillContainer(t *testing.T) {
|
||||||
|
containers := []docker.APIContainers{
|
||||||
|
{
|
||||||
|
ID: "1234",
|
||||||
|
Names: []string{"/k8s_foo_qux_1234_42"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "5678",
|
||||||
|
Names: []string{"/k8s_bar_qux_5678_42"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
kubelet, fakeDocker := newTestKubelet(t)
|
||||||
|
fakeDocker.ContainerList = append([]docker.APIContainers{}, containers...)
|
||||||
fakeDocker.Container = &docker.Container{
|
fakeDocker.Container = &docker.Container{
|
||||||
Name: "foobar",
|
Name: "foobar",
|
||||||
}
|
}
|
||||||
|
for _, c := range fakeDocker.ContainerList {
|
||||||
|
kubelet.readiness.set(c.ID, true)
|
||||||
|
}
|
||||||
|
|
||||||
err := kubelet.killContainer(&fakeDocker.ContainerList[0])
|
err := kubelet.killContainer(&fakeDocker.ContainerList[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
verifyCalls(t, fakeDocker, []string{"stop"})
|
verifyCalls(t, fakeDocker, []string{"stop"})
|
||||||
|
killedContainer := containers[0]
|
||||||
|
liveContainer := containers[1]
|
||||||
|
if _, found := kubelet.readiness.states[killedContainer.ID]; found {
|
||||||
|
t.Errorf("exepcted container entry ID '%v' to not be found. states: %+v", killedContainer.ID, kubelet.readiness.states)
|
||||||
|
}
|
||||||
|
if _, found := kubelet.readiness.states[liveContainer.ID]; !found {
|
||||||
|
t.Errorf("exepcted container entry ID '%v' to be found. states: %+v", liveContainer.ID, kubelet.readiness.states)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type channelReader struct {
|
type channelReader struct {
|
||||||
@ -2560,3 +2584,96 @@ func TestPodPhaseWithRestartOnFailure(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetPodReadyCondition(t *testing.T) {
|
||||||
|
ready := []api.PodCondition{{
|
||||||
|
Kind: api.PodReady,
|
||||||
|
Status: api.ConditionFull,
|
||||||
|
}}
|
||||||
|
unready := []api.PodCondition{{
|
||||||
|
Kind: api.PodReady,
|
||||||
|
Status: api.ConditionNone,
|
||||||
|
}}
|
||||||
|
tests := []struct {
|
||||||
|
spec *api.PodSpec
|
||||||
|
info api.PodInfo
|
||||||
|
expected []api.PodCondition
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
spec: nil,
|
||||||
|
info: nil,
|
||||||
|
expected: unready,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
spec: &api.PodSpec{},
|
||||||
|
info: api.PodInfo{},
|
||||||
|
expected: ready,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
spec: &api.PodSpec{
|
||||||
|
Containers: []api.Container{
|
||||||
|
{Name: "1234"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
info: api.PodInfo{},
|
||||||
|
expected: unready,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
spec: &api.PodSpec{
|
||||||
|
Containers: []api.Container{
|
||||||
|
{Name: "1234"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
info: api.PodInfo{
|
||||||
|
"1234": api.ContainerStatus{Ready: true},
|
||||||
|
},
|
||||||
|
expected: ready,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
spec: &api.PodSpec{
|
||||||
|
Containers: []api.Container{
|
||||||
|
{Name: "1234"},
|
||||||
|
{Name: "5678"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
info: api.PodInfo{
|
||||||
|
"1234": api.ContainerStatus{Ready: true},
|
||||||
|
"5678": api.ContainerStatus{Ready: true},
|
||||||
|
},
|
||||||
|
expected: ready,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
spec: &api.PodSpec{
|
||||||
|
Containers: []api.Container{
|
||||||
|
{Name: "1234"},
|
||||||
|
{Name: "5678"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
info: api.PodInfo{
|
||||||
|
"1234": api.ContainerStatus{Ready: true},
|
||||||
|
},
|
||||||
|
expected: unready,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
spec: &api.PodSpec{
|
||||||
|
Containers: []api.Container{
|
||||||
|
{Name: "1234"},
|
||||||
|
{Name: "5678"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
info: api.PodInfo{
|
||||||
|
"1234": api.ContainerStatus{Ready: true},
|
||||||
|
"5678": api.ContainerStatus{Ready: false},
|
||||||
|
},
|
||||||
|
expected: unready,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
condition := getPodReadyCondition(test.spec, test.info)
|
||||||
|
if !reflect.DeepEqual(condition, test.expected) {
|
||||||
|
t.Errorf("On test case %v, expected:\n%+v\ngot\n%+v\n", i, test.expected, condition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -17,10 +17,17 @@ limitations under the License.
|
|||||||
package kubelet
|
package kubelet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/exec"
|
||||||
|
|
||||||
|
"github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFindPortByName(t *testing.T) {
|
func TestFindPortByName(t *testing.T) {
|
||||||
@ -128,3 +135,110 @@ func TestGetTCPAddrParts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type fakeExecProber struct {
|
||||||
|
result probe.Result
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p fakeExecProber) Probe(_ exec.Cmd) (probe.Result, error) {
|
||||||
|
return p.result, p.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeTestKubelet(result probe.Result, err error) *Kubelet {
|
||||||
|
return &Kubelet{
|
||||||
|
prober: probeHolder{
|
||||||
|
exec: fakeExecProber{
|
||||||
|
result: result,
|
||||||
|
err: err,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProbeContainer(t *testing.T) {
|
||||||
|
dc := &docker.APIContainers{Created: time.Now().Unix()}
|
||||||
|
tests := []struct {
|
||||||
|
p *api.Probe
|
||||||
|
defaultResult probe.Result
|
||||||
|
expectError bool
|
||||||
|
expectedResult probe.Result
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
defaultResult: probe.Success,
|
||||||
|
expectedResult: probe.Success,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
defaultResult: probe.Failure,
|
||||||
|
expectedResult: probe.Success,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
p: &api.Probe{InitialDelaySeconds: 100},
|
||||||
|
defaultResult: probe.Failure,
|
||||||
|
expectError: false,
|
||||||
|
expectedResult: probe.Failure,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
p: &api.Probe{
|
||||||
|
InitialDelaySeconds: -100,
|
||||||
|
},
|
||||||
|
defaultResult: probe.Failure,
|
||||||
|
expectError: false,
|
||||||
|
expectedResult: probe.Unknown,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
p: &api.Probe{
|
||||||
|
InitialDelaySeconds: -100,
|
||||||
|
Handler: api.Handler{
|
||||||
|
Exec: &api.ExecAction{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultResult: probe.Failure,
|
||||||
|
expectError: false,
|
||||||
|
expectedResult: probe.Success,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
p: &api.Probe{
|
||||||
|
InitialDelaySeconds: -100,
|
||||||
|
Handler: api.Handler{
|
||||||
|
Exec: &api.ExecAction{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultResult: probe.Failure,
|
||||||
|
expectError: true,
|
||||||
|
expectedResult: probe.Unknown,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
p: &api.Probe{
|
||||||
|
InitialDelaySeconds: -100,
|
||||||
|
Handler: api.Handler{
|
||||||
|
Exec: &api.ExecAction{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultResult: probe.Success,
|
||||||
|
expectError: false,
|
||||||
|
expectedResult: probe.Failure,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
var kl *Kubelet
|
||||||
|
|
||||||
|
if test.expectError {
|
||||||
|
kl = makeTestKubelet(test.expectedResult, errors.New("error"))
|
||||||
|
} else {
|
||||||
|
kl = makeTestKubelet(test.expectedResult, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := kl.probeContainer(test.p, "", types.UID(""), api.PodStatus{}, api.Container{}, dc, test.defaultResult)
|
||||||
|
if test.expectError && err == nil {
|
||||||
|
t.Error("Expected error but did no error was returned.")
|
||||||
|
}
|
||||||
|
if !test.expectError && err != nil {
|
||||||
|
t.Errorf("Expected error but got: %v", err)
|
||||||
|
}
|
||||||
|
if test.expectedResult != result {
|
||||||
|
t.Errorf("Expected result was %v but probeContainer() returned %v", test.expectedResult, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user