2015-11-19 02:35:31 +00:00
|
|
|
/*
|
|
|
|
Copyright 2015 The Kubernetes Authors 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 rkt
|
|
|
|
|
|
|
|
import (
|
2015-11-21 00:56:35 +00:00
|
|
|
"encoding/json"
|
2015-11-19 02:35:31 +00:00
|
|
|
"fmt"
|
2016-01-25 21:57:42 +00:00
|
|
|
"os"
|
2016-01-08 21:21:17 +00:00
|
|
|
"sort"
|
2015-11-19 02:35:31 +00:00
|
|
|
"testing"
|
2015-12-12 01:09:21 +00:00
|
|
|
"time"
|
2015-11-19 02:35:31 +00:00
|
|
|
|
2015-11-21 00:56:35 +00:00
|
|
|
appcschema "github.com/appc/spec/schema"
|
|
|
|
appctypes "github.com/appc/spec/schema/types"
|
2015-11-19 02:35:31 +00:00
|
|
|
rktapi "github.com/coreos/rkt/api/v1alpha"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-01-08 21:21:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/resource"
|
2015-12-12 01:09:21 +00:00
|
|
|
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
|
2016-01-25 21:57:42 +00:00
|
|
|
utiltesting "k8s.io/kubernetes/pkg/util/testing"
|
2015-11-19 02:35:31 +00:00
|
|
|
)
|
|
|
|
|
2015-12-15 01:26:43 +00:00
|
|
|
func mustMarshalPodManifest(man *appcschema.PodManifest) []byte {
|
|
|
|
manblob, err := json.Marshal(man)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return manblob
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustMarshalImageManifest(man *appcschema.ImageManifest) []byte {
|
|
|
|
manblob, err := json.Marshal(man)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return manblob
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustRktHash(hash string) *appctypes.Hash {
|
|
|
|
h, err := appctypes.NewHash(hash)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeRktPod(rktPodState rktapi.PodState,
|
|
|
|
rktPodID, podUID, podName, podNamespace,
|
|
|
|
podIP, podCreationTs, podRestartCount string,
|
|
|
|
appNames, imgIDs, imgNames, containerHashes []string,
|
2016-01-12 02:30:29 +00:00
|
|
|
appStates []rktapi.AppState, exitcodes []int32) *rktapi.Pod {
|
2015-12-15 01:26:43 +00:00
|
|
|
|
|
|
|
podManifest := &appcschema.PodManifest{
|
|
|
|
ACKind: appcschema.PodManifestKind,
|
|
|
|
ACVersion: appcschema.AppContainerVersion,
|
|
|
|
Annotations: appctypes.Annotations{
|
|
|
|
appctypes.Annotation{
|
|
|
|
Name: *appctypes.MustACIdentifier(k8sRktKubeletAnno),
|
|
|
|
Value: k8sRktKubeletAnnoValue,
|
|
|
|
},
|
|
|
|
appctypes.Annotation{
|
|
|
|
Name: *appctypes.MustACIdentifier(k8sRktUIDAnno),
|
|
|
|
Value: podUID,
|
|
|
|
},
|
|
|
|
appctypes.Annotation{
|
|
|
|
Name: *appctypes.MustACIdentifier(k8sRktNameAnno),
|
|
|
|
Value: podName,
|
|
|
|
},
|
|
|
|
appctypes.Annotation{
|
|
|
|
Name: *appctypes.MustACIdentifier(k8sRktNamespaceAnno),
|
|
|
|
Value: podNamespace,
|
|
|
|
},
|
|
|
|
appctypes.Annotation{
|
|
|
|
Name: *appctypes.MustACIdentifier(k8sRktCreationTimeAnno),
|
|
|
|
Value: podCreationTs,
|
|
|
|
},
|
|
|
|
appctypes.Annotation{
|
|
|
|
Name: *appctypes.MustACIdentifier(k8sRktRestartCountAnno),
|
|
|
|
Value: podRestartCount,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
appNum := len(appNames)
|
|
|
|
if appNum != len(imgNames) ||
|
|
|
|
appNum != len(imgIDs) ||
|
|
|
|
appNum != len(containerHashes) ||
|
|
|
|
appNum != len(appStates) {
|
|
|
|
panic("inconsistent app number")
|
|
|
|
}
|
|
|
|
|
|
|
|
apps := make([]*rktapi.App, appNum)
|
|
|
|
for i := range appNames {
|
|
|
|
apps[i] = &rktapi.App{
|
|
|
|
Name: appNames[i],
|
|
|
|
State: appStates[i],
|
|
|
|
Image: &rktapi.Image{
|
|
|
|
Id: imgIDs[i],
|
|
|
|
Name: imgNames[i],
|
|
|
|
Manifest: mustMarshalImageManifest(
|
|
|
|
&appcschema.ImageManifest{
|
|
|
|
ACKind: appcschema.ImageManifestKind,
|
|
|
|
ACVersion: appcschema.AppContainerVersion,
|
|
|
|
Name: *appctypes.MustACIdentifier(imgNames[i]),
|
|
|
|
Annotations: appctypes.Annotations{
|
|
|
|
appctypes.Annotation{
|
|
|
|
Name: *appctypes.MustACIdentifier(k8sRktContainerHashAnno),
|
|
|
|
Value: containerHashes[i],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
2016-01-12 02:30:29 +00:00
|
|
|
ExitCode: exitcodes[i],
|
2015-12-15 01:26:43 +00:00
|
|
|
}
|
|
|
|
podManifest.Apps = append(podManifest.Apps, appcschema.RuntimeApp{
|
|
|
|
Name: *appctypes.MustACName(appNames[i]),
|
|
|
|
Image: appcschema.RuntimeImage{ID: *mustRktHash("sha512-foo")},
|
|
|
|
Annotations: appctypes.Annotations{
|
|
|
|
appctypes.Annotation{
|
|
|
|
Name: *appctypes.MustACIdentifier(k8sRktContainerHashAnno),
|
|
|
|
Value: containerHashes[i],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rktapi.Pod{
|
|
|
|
Id: rktPodID,
|
|
|
|
State: rktPodState,
|
|
|
|
Networks: []*rktapi.Network{{Name: defaultNetworkName, Ipv4: podIP}},
|
|
|
|
Apps: apps,
|
|
|
|
Manifest: mustMarshalPodManifest(podManifest),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-19 02:35:31 +00:00
|
|
|
func TestCheckVersion(t *testing.T) {
|
|
|
|
fr := newFakeRktInterface()
|
|
|
|
fs := newFakeSystemd()
|
|
|
|
r := &Runtime{apisvc: fr, systemd: fs}
|
|
|
|
|
|
|
|
fr.info = rktapi.Info{
|
|
|
|
RktVersion: "1.2.3+git",
|
|
|
|
AppcVersion: "1.2.4+git",
|
|
|
|
ApiVersion: "1.2.6-alpha",
|
|
|
|
}
|
|
|
|
fs.version = "100"
|
|
|
|
tests := []struct {
|
|
|
|
minimumRktBinVersion string
|
|
|
|
recommendedRktBinVersion string
|
|
|
|
minimumAppcVersion string
|
|
|
|
minimumRktApiVersion string
|
|
|
|
minimumSystemdVersion string
|
|
|
|
err error
|
|
|
|
calledGetInfo bool
|
|
|
|
calledSystemVersion bool
|
|
|
|
}{
|
|
|
|
// Good versions.
|
|
|
|
{
|
|
|
|
"1.2.3",
|
|
|
|
"1.2.3",
|
|
|
|
"1.2.4",
|
|
|
|
"1.2.5",
|
|
|
|
"99",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
// Good versions.
|
|
|
|
{
|
|
|
|
"1.2.3+git",
|
|
|
|
"1.2.3+git",
|
|
|
|
"1.2.4+git",
|
|
|
|
"1.2.6-alpha",
|
|
|
|
"100",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
// Requires greater binary version.
|
|
|
|
{
|
|
|
|
"1.2.4",
|
|
|
|
"1.2.4",
|
|
|
|
"1.2.4",
|
|
|
|
"1.2.6-alpha",
|
|
|
|
"100",
|
|
|
|
fmt.Errorf("rkt: binary version is too old(%v), requires at least %v", fr.info.RktVersion, "1.2.4"),
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
// Requires greater Appc version.
|
|
|
|
{
|
|
|
|
"1.2.3",
|
|
|
|
"1.2.3",
|
|
|
|
"1.2.5",
|
|
|
|
"1.2.6-alpha",
|
|
|
|
"100",
|
|
|
|
fmt.Errorf("rkt: appc version is too old(%v), requires at least %v", fr.info.AppcVersion, "1.2.5"),
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
// Requires greater API version.
|
|
|
|
{
|
|
|
|
"1.2.3",
|
|
|
|
"1.2.3",
|
|
|
|
"1.2.4",
|
|
|
|
"1.2.6",
|
|
|
|
"100",
|
|
|
|
fmt.Errorf("rkt: API version is too old(%v), requires at least %v", fr.info.ApiVersion, "1.2.6"),
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
// Requires greater API version.
|
|
|
|
{
|
|
|
|
"1.2.3",
|
|
|
|
"1.2.3",
|
|
|
|
"1.2.4",
|
|
|
|
"1.2.7",
|
|
|
|
"100",
|
|
|
|
fmt.Errorf("rkt: API version is too old(%v), requires at least %v", fr.info.ApiVersion, "1.2.7"),
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
// Requires greater systemd version.
|
|
|
|
{
|
|
|
|
"1.2.3",
|
|
|
|
"1.2.3",
|
|
|
|
"1.2.4",
|
|
|
|
"1.2.7",
|
|
|
|
"101",
|
|
|
|
fmt.Errorf("rkt: systemd version(%v) is too old, requires at least %v", fs.version, "101"),
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
testCaseHint := fmt.Sprintf("test case #%d", i)
|
|
|
|
err := r.checkVersion(tt.minimumRktBinVersion, tt.recommendedRktBinVersion, tt.minimumAppcVersion, tt.minimumRktApiVersion, tt.minimumSystemdVersion)
|
2016-01-08 21:21:17 +00:00
|
|
|
assert.Equal(t, tt.err, err, testCaseHint)
|
2015-11-19 02:35:31 +00:00
|
|
|
|
|
|
|
if tt.calledGetInfo {
|
|
|
|
assert.Equal(t, fr.called, []string{"GetInfo"}, testCaseHint)
|
|
|
|
}
|
|
|
|
if tt.calledSystemVersion {
|
|
|
|
assert.Equal(t, fs.called, []string{"Version"}, testCaseHint)
|
|
|
|
}
|
|
|
|
if err == nil {
|
2016-01-08 21:21:17 +00:00
|
|
|
assert.Equal(t, fr.info.RktVersion, r.binVersion.String(), testCaseHint)
|
|
|
|
assert.Equal(t, fr.info.AppcVersion, r.appcVersion.String(), testCaseHint)
|
|
|
|
assert.Equal(t, fr.info.ApiVersion, r.apiVersion.String(), testCaseHint)
|
2015-11-19 02:35:31 +00:00
|
|
|
}
|
|
|
|
fr.CleanCalls()
|
|
|
|
fs.CleanCalls()
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 23:57:56 +00:00
|
|
|
|
|
|
|
func TestListImages(t *testing.T) {
|
|
|
|
fr := newFakeRktInterface()
|
|
|
|
fs := newFakeSystemd()
|
|
|
|
r := &Runtime{apisvc: fr, systemd: fs}
|
|
|
|
|
|
|
|
tests := []struct {
|
2015-12-17 00:52:39 +00:00
|
|
|
images []*rktapi.Image
|
|
|
|
expected []kubecontainer.Image
|
2015-11-20 23:57:56 +00:00
|
|
|
}{
|
2015-12-17 00:52:39 +00:00
|
|
|
{nil, []kubecontainer.Image{}},
|
2015-11-20 23:57:56 +00:00
|
|
|
{
|
|
|
|
[]*rktapi.Image{
|
|
|
|
{
|
|
|
|
Id: "sha512-a2fb8f390702",
|
|
|
|
Name: "quay.io/coreos/alpine-sh",
|
|
|
|
Version: "latest",
|
|
|
|
},
|
|
|
|
},
|
2015-12-17 00:52:39 +00:00
|
|
|
[]kubecontainer.Image{
|
|
|
|
{
|
2015-12-02 08:53:56 +00:00
|
|
|
ID: "sha512-a2fb8f390702",
|
|
|
|
RepoTags: []string{"quay.io/coreos/alpine-sh:latest"},
|
2015-12-17 00:52:39 +00:00
|
|
|
},
|
|
|
|
},
|
2015-11-20 23:57:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
[]*rktapi.Image{
|
|
|
|
{
|
|
|
|
Id: "sha512-a2fb8f390702",
|
|
|
|
Name: "quay.io/coreos/alpine-sh",
|
|
|
|
Version: "latest",
|
2016-01-12 01:54:42 +00:00
|
|
|
Size: 400,
|
2015-11-20 23:57:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "sha512-c6b597f42816",
|
|
|
|
Name: "coreos.com/rkt/stage1-coreos",
|
|
|
|
Version: "0.10.0",
|
2016-01-12 01:54:42 +00:00
|
|
|
Size: 400,
|
2015-11-20 23:57:56 +00:00
|
|
|
},
|
|
|
|
},
|
2015-12-17 00:52:39 +00:00
|
|
|
[]kubecontainer.Image{
|
|
|
|
{
|
2015-12-02 08:53:56 +00:00
|
|
|
ID: "sha512-a2fb8f390702",
|
|
|
|
RepoTags: []string{"quay.io/coreos/alpine-sh:latest"},
|
2016-01-12 01:54:42 +00:00
|
|
|
Size: 400,
|
2015-12-17 00:52:39 +00:00
|
|
|
},
|
|
|
|
{
|
2015-12-02 08:53:56 +00:00
|
|
|
ID: "sha512-c6b597f42816",
|
|
|
|
RepoTags: []string{"coreos.com/rkt/stage1-coreos:0.10.0"},
|
2016-01-12 01:54:42 +00:00
|
|
|
Size: 400,
|
2015-12-17 00:52:39 +00:00
|
|
|
},
|
|
|
|
},
|
2015-11-20 23:57:56 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
fr.images = tt.images
|
|
|
|
|
|
|
|
images, err := r.ListImages()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-12-17 00:52:39 +00:00
|
|
|
assert.Equal(t, tt.expected, images)
|
2015-11-20 23:57:56 +00:00
|
|
|
assert.Equal(t, fr.called, []string{"ListImages"}, fmt.Sprintf("test case %d: unexpected called list", i))
|
|
|
|
|
|
|
|
fr.CleanCalls()
|
|
|
|
}
|
|
|
|
}
|
2015-11-21 00:56:35 +00:00
|
|
|
|
|
|
|
func TestGetPods(t *testing.T) {
|
|
|
|
fr := newFakeRktInterface()
|
|
|
|
fs := newFakeSystemd()
|
|
|
|
r := &Runtime{apisvc: fr, systemd: fs}
|
|
|
|
|
|
|
|
tests := []struct {
|
2015-12-15 01:26:43 +00:00
|
|
|
pods []*rktapi.Pod
|
|
|
|
result []*kubecontainer.Pod
|
2015-11-21 00:56:35 +00:00
|
|
|
}{
|
2015-12-15 01:26:43 +00:00
|
|
|
// No pods.
|
2015-11-21 00:56:35 +00:00
|
|
|
{},
|
2015-12-15 01:26:43 +00:00
|
|
|
// One pod.
|
2015-11-21 00:56:35 +00:00
|
|
|
{
|
2015-12-15 01:26:43 +00:00
|
|
|
[]*rktapi.Pod{
|
|
|
|
makeRktPod(rktapi.PodState_POD_STATE_RUNNING,
|
|
|
|
"uuid-4002", "42", "guestbook", "default",
|
|
|
|
"10.10.10.42", "100000", "7",
|
|
|
|
[]string{"app-1", "app-2"},
|
|
|
|
[]string{"img-id-1", "img-id-2"},
|
|
|
|
[]string{"img-name-1", "img-name-2"},
|
|
|
|
[]string{"1001", "1002"},
|
|
|
|
[]rktapi.AppState{rktapi.AppState_APP_STATE_RUNNING, rktapi.AppState_APP_STATE_EXITED},
|
2016-01-12 02:30:29 +00:00
|
|
|
[]int32{0, 0},
|
2015-12-15 01:26:43 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
[]*kubecontainer.Pod{
|
2015-11-21 00:56:35 +00:00
|
|
|
{
|
2015-12-15 01:26:43 +00:00
|
|
|
ID: "42",
|
|
|
|
Name: "guestbook",
|
|
|
|
Namespace: "default",
|
|
|
|
Containers: []*kubecontainer.Container{
|
2015-11-21 00:56:35 +00:00
|
|
|
{
|
2015-12-15 01:26:43 +00:00
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4002:app-1"),
|
|
|
|
Name: "app-1",
|
|
|
|
Image: "img-name-1",
|
|
|
|
Hash: 1001,
|
|
|
|
Created: 100000,
|
|
|
|
State: "running",
|
2015-11-21 00:56:35 +00:00
|
|
|
},
|
2015-12-15 01:26:43 +00:00
|
|
|
{
|
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4002:app-2"),
|
|
|
|
Name: "app-2",
|
|
|
|
Image: "img-name-2",
|
|
|
|
Hash: 1002,
|
|
|
|
Created: 100000,
|
|
|
|
State: "exited",
|
2015-11-21 00:56:35 +00:00
|
|
|
},
|
2015-12-15 01:26:43 +00:00
|
|
|
},
|
2015-11-21 00:56:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-12-15 01:26:43 +00:00
|
|
|
// Multiple pods.
|
2015-11-21 00:56:35 +00:00
|
|
|
{
|
2015-12-15 01:26:43 +00:00
|
|
|
[]*rktapi.Pod{
|
|
|
|
makeRktPod(rktapi.PodState_POD_STATE_RUNNING,
|
|
|
|
"uuid-4002", "42", "guestbook", "default",
|
|
|
|
"10.10.10.42", "100000", "7",
|
|
|
|
[]string{"app-1", "app-2"},
|
|
|
|
[]string{"img-id-1", "img-id-2"},
|
|
|
|
[]string{"img-name-1", "img-name-2"},
|
|
|
|
[]string{"1001", "1002"},
|
|
|
|
[]rktapi.AppState{rktapi.AppState_APP_STATE_RUNNING, rktapi.AppState_APP_STATE_EXITED},
|
2016-01-12 02:30:29 +00:00
|
|
|
[]int32{0, 0},
|
2015-12-15 01:26:43 +00:00
|
|
|
),
|
|
|
|
makeRktPod(rktapi.PodState_POD_STATE_EXITED,
|
|
|
|
"uuid-4003", "43", "guestbook", "default",
|
|
|
|
"10.10.10.43", "90000", "7",
|
|
|
|
[]string{"app-11", "app-22"},
|
|
|
|
[]string{"img-id-11", "img-id-22"},
|
|
|
|
[]string{"img-name-11", "img-name-22"},
|
|
|
|
[]string{"10011", "10022"},
|
|
|
|
[]rktapi.AppState{rktapi.AppState_APP_STATE_RUNNING, rktapi.AppState_APP_STATE_EXITED},
|
2016-01-12 02:30:29 +00:00
|
|
|
[]int32{0, 0},
|
2015-12-15 01:26:43 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
[]*kubecontainer.Pod{
|
2015-11-21 00:56:35 +00:00
|
|
|
{
|
2015-12-15 01:26:43 +00:00
|
|
|
ID: "42",
|
|
|
|
Name: "guestbook",
|
|
|
|
Namespace: "default",
|
|
|
|
Containers: []*kubecontainer.Container{
|
2015-11-21 00:56:35 +00:00
|
|
|
{
|
2015-12-15 01:26:43 +00:00
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4002:app-1"),
|
|
|
|
Name: "app-1",
|
|
|
|
Image: "img-name-1",
|
|
|
|
Hash: 1001,
|
|
|
|
Created: 100000,
|
|
|
|
State: "running",
|
2015-11-21 00:56:35 +00:00
|
|
|
},
|
|
|
|
{
|
2015-12-15 01:26:43 +00:00
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4002:app-2"),
|
|
|
|
Name: "app-2",
|
|
|
|
Image: "img-name-2",
|
|
|
|
Hash: 1002,
|
|
|
|
Created: 100000,
|
|
|
|
State: "exited",
|
2015-11-21 00:56:35 +00:00
|
|
|
},
|
|
|
|
},
|
2015-12-15 01:26:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "43",
|
|
|
|
Name: "guestbook",
|
|
|
|
Namespace: "default",
|
|
|
|
Containers: []*kubecontainer.Container{
|
|
|
|
{
|
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4003:app-11"),
|
|
|
|
Name: "app-11",
|
|
|
|
Image: "img-name-11",
|
|
|
|
Hash: 10011,
|
|
|
|
Created: 90000,
|
|
|
|
State: "running",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4003:app-22"),
|
|
|
|
Name: "app-22",
|
|
|
|
Image: "img-name-22",
|
|
|
|
Hash: 10022,
|
|
|
|
Created: 90000,
|
|
|
|
State: "exited",
|
2015-11-21 00:56:35 +00:00
|
|
|
},
|
2015-12-15 01:26:43 +00:00
|
|
|
},
|
2015-11-21 00:56:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2015-12-15 01:26:43 +00:00
|
|
|
testCaseHint := fmt.Sprintf("test case #%d", i)
|
2015-11-21 00:56:35 +00:00
|
|
|
fr.pods = tt.pods
|
|
|
|
|
|
|
|
pods, err := r.GetPods(true)
|
|
|
|
if err != nil {
|
2015-12-15 01:26:43 +00:00
|
|
|
t.Errorf("test case #%d: unexpected error: %v", i, err)
|
2015-11-21 00:56:35 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 01:26:43 +00:00
|
|
|
assert.Equal(t, tt.result, pods, testCaseHint)
|
2015-12-17 00:52:39 +00:00
|
|
|
assert.Equal(t, []string{"ListPods"}, fr.called, fmt.Sprintf("test case %d: unexpected called list", i))
|
2015-11-21 00:56:35 +00:00
|
|
|
|
|
|
|
fr.CleanCalls()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 00:52:39 +00:00
|
|
|
func TestGetPodsFilters(t *testing.T) {
|
2015-11-21 00:56:35 +00:00
|
|
|
fr := newFakeRktInterface()
|
|
|
|
fs := newFakeSystemd()
|
|
|
|
r := &Runtime{apisvc: fr, systemd: fs}
|
|
|
|
|
|
|
|
for _, test := range []struct {
|
2015-12-17 00:52:39 +00:00
|
|
|
All bool
|
|
|
|
ExpectedFilters []*rktapi.PodFilter
|
2015-11-21 00:56:35 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
true,
|
2015-12-17 00:52:39 +00:00
|
|
|
[]*rktapi.PodFilter{
|
|
|
|
{
|
|
|
|
Annotations: []*rktapi.KeyValue{
|
|
|
|
{
|
|
|
|
Key: k8sRktKubeletAnno,
|
|
|
|
Value: k8sRktKubeletAnnoValue,
|
|
|
|
},
|
2015-11-21 00:56:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
false,
|
2015-12-17 00:52:39 +00:00
|
|
|
[]*rktapi.PodFilter{
|
|
|
|
{
|
|
|
|
States: []rktapi.PodState{rktapi.PodState_POD_STATE_RUNNING},
|
|
|
|
Annotations: []*rktapi.KeyValue{
|
|
|
|
{
|
|
|
|
Key: k8sRktKubeletAnno,
|
|
|
|
Value: k8sRktKubeletAnnoValue,
|
|
|
|
},
|
2015-11-21 00:56:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
_, err := r.GetPods(test.All)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-12-17 00:52:39 +00:00
|
|
|
assert.Equal(t, test.ExpectedFilters, fr.podFilters, "filters didn't match when all=%b", test.All)
|
2015-11-21 00:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-12 01:09:21 +00:00
|
|
|
func TestGetPodStatus(t *testing.T) {
|
|
|
|
fr := newFakeRktInterface()
|
|
|
|
fs := newFakeSystemd()
|
|
|
|
r := &Runtime{apisvc: fr, systemd: fs}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
pods []*rktapi.Pod
|
|
|
|
result *kubecontainer.PodStatus
|
|
|
|
}{
|
|
|
|
// No pods.
|
|
|
|
{
|
|
|
|
nil,
|
|
|
|
&kubecontainer.PodStatus{ID: "42", Name: "guestbook", Namespace: "default"},
|
|
|
|
},
|
|
|
|
// One pod.
|
|
|
|
{
|
|
|
|
[]*rktapi.Pod{
|
|
|
|
makeRktPod(rktapi.PodState_POD_STATE_RUNNING,
|
|
|
|
"uuid-4002", "42", "guestbook", "default",
|
|
|
|
"10.10.10.42", "100000", "7",
|
|
|
|
[]string{"app-1", "app-2"},
|
|
|
|
[]string{"img-id-1", "img-id-2"},
|
|
|
|
[]string{"img-name-1", "img-name-2"},
|
|
|
|
[]string{"1001", "1002"},
|
|
|
|
[]rktapi.AppState{rktapi.AppState_APP_STATE_RUNNING, rktapi.AppState_APP_STATE_EXITED},
|
2016-01-12 02:30:29 +00:00
|
|
|
[]int32{0, 0},
|
2015-12-12 01:09:21 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
&kubecontainer.PodStatus{
|
|
|
|
ID: "42",
|
|
|
|
Name: "guestbook",
|
|
|
|
Namespace: "default",
|
|
|
|
IP: "10.10.10.42",
|
|
|
|
ContainerStatuses: []*kubecontainer.ContainerStatus{
|
|
|
|
{
|
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4002:app-1"),
|
|
|
|
Name: "app-1",
|
|
|
|
State: kubecontainer.ContainerStateRunning,
|
|
|
|
CreatedAt: time.Unix(100000, 0),
|
|
|
|
StartedAt: time.Unix(100000, 0),
|
|
|
|
Image: "img-name-1",
|
|
|
|
ImageID: "rkt://img-id-1",
|
|
|
|
Hash: 1001,
|
|
|
|
RestartCount: 7,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4002:app-2"),
|
|
|
|
Name: "app-2",
|
|
|
|
State: kubecontainer.ContainerStateExited,
|
|
|
|
CreatedAt: time.Unix(100000, 0),
|
|
|
|
StartedAt: time.Unix(100000, 0),
|
|
|
|
Image: "img-name-2",
|
|
|
|
ImageID: "rkt://img-id-2",
|
|
|
|
Hash: 1002,
|
|
|
|
RestartCount: 7,
|
2016-01-12 02:30:29 +00:00
|
|
|
Reason: "Completed",
|
2015-12-12 01:09:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Multiple pods.
|
|
|
|
{
|
|
|
|
[]*rktapi.Pod{
|
|
|
|
makeRktPod(rktapi.PodState_POD_STATE_EXITED,
|
|
|
|
"uuid-4002", "42", "guestbook", "default",
|
|
|
|
"10.10.10.42", "90000", "7",
|
|
|
|
[]string{"app-1", "app-2"},
|
|
|
|
[]string{"img-id-1", "img-id-2"},
|
|
|
|
[]string{"img-name-1", "img-name-2"},
|
|
|
|
[]string{"1001", "1002"},
|
|
|
|
[]rktapi.AppState{rktapi.AppState_APP_STATE_RUNNING, rktapi.AppState_APP_STATE_EXITED},
|
2016-01-12 02:30:29 +00:00
|
|
|
[]int32{0, 0},
|
2015-12-12 01:09:21 +00:00
|
|
|
),
|
|
|
|
makeRktPod(rktapi.PodState_POD_STATE_RUNNING, // The latest pod is running.
|
|
|
|
"uuid-4003", "42", "guestbook", "default",
|
|
|
|
"10.10.10.42", "100000", "10",
|
|
|
|
[]string{"app-1", "app-2"},
|
|
|
|
[]string{"img-id-1", "img-id-2"},
|
|
|
|
[]string{"img-name-1", "img-name-2"},
|
|
|
|
[]string{"1001", "1002"},
|
|
|
|
[]rktapi.AppState{rktapi.AppState_APP_STATE_RUNNING, rktapi.AppState_APP_STATE_EXITED},
|
2016-01-12 02:30:29 +00:00
|
|
|
[]int32{0, 1},
|
2015-12-12 01:09:21 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
&kubecontainer.PodStatus{
|
|
|
|
ID: "42",
|
|
|
|
Name: "guestbook",
|
|
|
|
Namespace: "default",
|
|
|
|
IP: "10.10.10.42",
|
|
|
|
// Result should contain all contianers.
|
|
|
|
ContainerStatuses: []*kubecontainer.ContainerStatus{
|
|
|
|
{
|
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4002:app-1"),
|
|
|
|
Name: "app-1",
|
|
|
|
State: kubecontainer.ContainerStateRunning,
|
|
|
|
CreatedAt: time.Unix(90000, 0),
|
|
|
|
StartedAt: time.Unix(90000, 0),
|
|
|
|
Image: "img-name-1",
|
|
|
|
ImageID: "rkt://img-id-1",
|
|
|
|
Hash: 1001,
|
|
|
|
RestartCount: 7,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4002:app-2"),
|
|
|
|
Name: "app-2",
|
|
|
|
State: kubecontainer.ContainerStateExited,
|
|
|
|
CreatedAt: time.Unix(90000, 0),
|
|
|
|
StartedAt: time.Unix(90000, 0),
|
|
|
|
Image: "img-name-2",
|
|
|
|
ImageID: "rkt://img-id-2",
|
|
|
|
Hash: 1002,
|
|
|
|
RestartCount: 7,
|
2016-01-12 02:30:29 +00:00
|
|
|
Reason: "Completed",
|
2015-12-12 01:09:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4003:app-1"),
|
|
|
|
Name: "app-1",
|
|
|
|
State: kubecontainer.ContainerStateRunning,
|
|
|
|
CreatedAt: time.Unix(100000, 0),
|
|
|
|
StartedAt: time.Unix(100000, 0),
|
|
|
|
Image: "img-name-1",
|
|
|
|
ImageID: "rkt://img-id-1",
|
|
|
|
Hash: 1001,
|
|
|
|
RestartCount: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: kubecontainer.BuildContainerID("rkt", "uuid-4003:app-2"),
|
|
|
|
Name: "app-2",
|
|
|
|
State: kubecontainer.ContainerStateExited,
|
|
|
|
CreatedAt: time.Unix(100000, 0),
|
|
|
|
StartedAt: time.Unix(100000, 0),
|
|
|
|
Image: "img-name-2",
|
|
|
|
ImageID: "rkt://img-id-2",
|
|
|
|
Hash: 1002,
|
|
|
|
RestartCount: 10,
|
2016-01-12 02:30:29 +00:00
|
|
|
ExitCode: 1,
|
|
|
|
Reason: "Error",
|
2015-12-12 01:09:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
testCaseHint := fmt.Sprintf("test case #%d", i)
|
|
|
|
fr.pods = tt.pods
|
|
|
|
|
|
|
|
status, err := r.GetPodStatus("42", "guestbook", "default")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("test case #%d: unexpected error: %v", i, err)
|
|
|
|
}
|
2015-12-15 01:26:43 +00:00
|
|
|
|
2015-12-12 01:09:21 +00:00
|
|
|
assert.Equal(t, tt.result, status, testCaseHint)
|
2015-12-17 00:52:39 +00:00
|
|
|
assert.Equal(t, []string{"ListPods"}, fr.called, testCaseHint)
|
2015-12-12 01:09:21 +00:00
|
|
|
fr.CleanCalls()
|
|
|
|
}
|
|
|
|
}
|
2016-01-08 21:21:17 +00:00
|
|
|
|
|
|
|
func generateCapRetainIsolator(t *testing.T, caps ...string) appctypes.Isolator {
|
|
|
|
retain, err := appctypes.NewLinuxCapabilitiesRetainSet(caps...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error generating cap retain isolator", err)
|
|
|
|
}
|
|
|
|
return retain.AsIsolator()
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateCapRevokeIsolator(t *testing.T, caps ...string) appctypes.Isolator {
|
|
|
|
revoke, err := appctypes.NewLinuxCapabilitiesRevokeSet(caps...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error generating cap revoke isolator", err)
|
|
|
|
}
|
|
|
|
return revoke.AsIsolator()
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateCPUIsolator(t *testing.T, request, limit string) appctypes.Isolator {
|
|
|
|
cpu, err := appctypes.NewResourceCPUIsolator(request, limit)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error generating cpu resource isolator", err)
|
|
|
|
}
|
|
|
|
return cpu.AsIsolator()
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateMemoryIsolator(t *testing.T, request, limit string) appctypes.Isolator {
|
|
|
|
memory, err := appctypes.NewResourceMemoryIsolator(request, limit)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error generating memory resource isolator", err)
|
|
|
|
}
|
|
|
|
return memory.AsIsolator()
|
|
|
|
}
|
|
|
|
|
|
|
|
func baseApp(t *testing.T) *appctypes.App {
|
|
|
|
return &appctypes.App{
|
|
|
|
Exec: appctypes.Exec{"/bin/foo"},
|
|
|
|
SupplementaryGIDs: []int{4, 5, 6},
|
|
|
|
WorkingDirectory: "/foo",
|
|
|
|
Environment: []appctypes.EnvironmentVariable{
|
|
|
|
{"env-foo", "bar"},
|
|
|
|
},
|
|
|
|
MountPoints: []appctypes.MountPoint{
|
|
|
|
{Name: *appctypes.MustACName("mnt-foo"), Path: "/mnt-foo", ReadOnly: false},
|
|
|
|
},
|
|
|
|
Ports: []appctypes.Port{
|
|
|
|
{Name: *appctypes.MustACName("port-foo"), Protocol: "TCP", Port: 4242},
|
|
|
|
},
|
|
|
|
Isolators: []appctypes.Isolator{
|
|
|
|
generateCapRetainIsolator(t, "CAP_SYS_ADMIN"),
|
|
|
|
generateCapRevokeIsolator(t, "CAP_NET_ADMIN"),
|
|
|
|
generateCPUIsolator(t, "100m", "200m"),
|
|
|
|
generateMemoryIsolator(t, "10M", "20M"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 19:55:56 +00:00
|
|
|
func baseAppWithRootUserGroup(t *testing.T) *appctypes.App {
|
|
|
|
app := baseApp(t)
|
|
|
|
app.User, app.Group = "0", "0"
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2016-01-08 21:21:17 +00:00
|
|
|
type envByName []appctypes.EnvironmentVariable
|
|
|
|
|
|
|
|
func (s envByName) Len() int { return len(s) }
|
|
|
|
func (s envByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
|
|
|
|
func (s envByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
|
|
|
type mountsByName []appctypes.MountPoint
|
|
|
|
|
|
|
|
func (s mountsByName) Len() int { return len(s) }
|
|
|
|
func (s mountsByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
|
|
|
|
func (s mountsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
|
|
|
type portsByName []appctypes.Port
|
|
|
|
|
|
|
|
func (s portsByName) Len() int { return len(s) }
|
|
|
|
func (s portsByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
|
|
|
|
func (s portsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
|
|
|
type isolatorsByName []appctypes.Isolator
|
|
|
|
|
|
|
|
func (s isolatorsByName) Len() int { return len(s) }
|
|
|
|
func (s isolatorsByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
|
|
|
|
func (s isolatorsByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
|
|
|
func sortAppFields(app *appctypes.App) {
|
|
|
|
sort.Sort(envByName(app.Environment))
|
|
|
|
sort.Sort(mountsByName(app.MountPoints))
|
|
|
|
sort.Sort(portsByName(app.Ports))
|
|
|
|
sort.Sort(isolatorsByName(app.Isolators))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetApp(t *testing.T) {
|
2016-01-25 21:57:42 +00:00
|
|
|
tmpDir, err := utiltesting.MkTmpdir("rkt_test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2016-01-08 21:21:17 +00:00
|
|
|
rootUser := int64(0)
|
|
|
|
nonRootUser := int64(42)
|
|
|
|
runAsNonRootTrue := true
|
|
|
|
fsgid := int64(3)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
container *api.Container
|
|
|
|
opts *kubecontainer.RunContainerOptions
|
|
|
|
ctx *api.SecurityContext
|
|
|
|
podCtx *api.PodSecurityContext
|
|
|
|
expect *appctypes.App
|
|
|
|
err error
|
|
|
|
}{
|
2016-01-27 19:55:56 +00:00
|
|
|
// Nothing should change, but the "User" and "Group" should be filled.
|
2016-01-08 21:21:17 +00:00
|
|
|
{
|
|
|
|
container: &api.Container{},
|
|
|
|
opts: &kubecontainer.RunContainerOptions{},
|
|
|
|
ctx: nil,
|
|
|
|
podCtx: nil,
|
2016-01-27 19:55:56 +00:00
|
|
|
expect: baseAppWithRootUserGroup(t),
|
2016-01-08 21:21:17 +00:00
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
// error verifying non-root.
|
|
|
|
{
|
|
|
|
container: &api.Container{},
|
|
|
|
opts: &kubecontainer.RunContainerOptions{},
|
|
|
|
ctx: &api.SecurityContext{
|
|
|
|
RunAsNonRoot: &runAsNonRootTrue,
|
|
|
|
RunAsUser: &rootUser,
|
|
|
|
},
|
|
|
|
podCtx: nil,
|
|
|
|
expect: nil,
|
|
|
|
err: fmt.Errorf("container has no runAsUser and image will run as root"),
|
|
|
|
},
|
|
|
|
|
|
|
|
// app should be changed.
|
|
|
|
{
|
|
|
|
container: &api.Container{
|
|
|
|
Command: []string{"/bin/bar"},
|
|
|
|
Args: []string{"hello", "world"},
|
2016-01-25 21:57:42 +00:00
|
|
|
WorkingDir: tmpDir,
|
2016-01-08 21:21:17 +00:00
|
|
|
Resources: api.ResourceRequirements{
|
|
|
|
Limits: api.ResourceList{"cpu": resource.MustParse("50m"), "memory": resource.MustParse("50M")},
|
|
|
|
Requests: api.ResourceList{"cpu": resource.MustParse("5m"), "memory": resource.MustParse("5M")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
opts: &kubecontainer.RunContainerOptions{
|
|
|
|
Envs: []kubecontainer.EnvVar{
|
|
|
|
{Name: "env-bar", Value: "foo"},
|
|
|
|
},
|
|
|
|
Mounts: []kubecontainer.Mount{
|
|
|
|
{Name: "mnt-bar", ContainerPath: "/mnt-bar", ReadOnly: true},
|
|
|
|
},
|
|
|
|
PortMappings: []kubecontainer.PortMapping{
|
|
|
|
{Name: "port-bar", Protocol: api.ProtocolTCP, ContainerPort: 1234},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ctx: &api.SecurityContext{
|
|
|
|
Capabilities: &api.Capabilities{
|
|
|
|
Add: []api.Capability{"CAP_SYS_CHROOT", "CAP_SYS_BOOT"},
|
|
|
|
Drop: []api.Capability{"CAP_SETUID", "CAP_SETGID"},
|
|
|
|
},
|
|
|
|
RunAsUser: &nonRootUser,
|
|
|
|
RunAsNonRoot: &runAsNonRootTrue,
|
|
|
|
},
|
|
|
|
podCtx: &api.PodSecurityContext{
|
|
|
|
SupplementalGroups: []int64{1, 2},
|
|
|
|
FSGroup: &fsgid,
|
|
|
|
},
|
|
|
|
expect: &appctypes.App{
|
|
|
|
Exec: appctypes.Exec{"/bin/bar", "hello", "world"},
|
|
|
|
User: "42",
|
2016-01-27 19:55:56 +00:00
|
|
|
Group: "0",
|
2016-01-08 21:21:17 +00:00
|
|
|
SupplementaryGIDs: []int{1, 2, 3},
|
2016-01-25 21:57:42 +00:00
|
|
|
WorkingDirectory: tmpDir,
|
2016-01-08 21:21:17 +00:00
|
|
|
Environment: []appctypes.EnvironmentVariable{
|
|
|
|
{"env-foo", "bar"},
|
|
|
|
{"env-bar", "foo"},
|
|
|
|
},
|
|
|
|
MountPoints: []appctypes.MountPoint{
|
|
|
|
{Name: *appctypes.MustACName("mnt-foo"), Path: "/mnt-foo", ReadOnly: false},
|
|
|
|
{Name: *appctypes.MustACName("mnt-bar"), Path: "/mnt-bar", ReadOnly: true},
|
|
|
|
},
|
|
|
|
Ports: []appctypes.Port{
|
|
|
|
{Name: *appctypes.MustACName("port-foo"), Protocol: "TCP", Port: 4242},
|
|
|
|
{Name: *appctypes.MustACName("port-bar"), Protocol: "TCP", Port: 1234},
|
|
|
|
},
|
|
|
|
Isolators: []appctypes.Isolator{
|
|
|
|
generateCapRetainIsolator(t, "CAP_SYS_CHROOT", "CAP_SYS_BOOT"),
|
|
|
|
generateCapRevokeIsolator(t, "CAP_SETUID", "CAP_SETGID"),
|
|
|
|
generateCPUIsolator(t, "5m", "50m"),
|
|
|
|
generateMemoryIsolator(t, "5M", "50M"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// app should be changed. (env, mounts, ports, are overrided).
|
|
|
|
{
|
|
|
|
container: &api.Container{
|
2016-01-12 02:30:29 +00:00
|
|
|
Name: "hello-world",
|
2016-01-28 07:14:50 +00:00
|
|
|
Command: []string{"/bin/bar", "$(env-foo)"},
|
|
|
|
Args: []string{"hello", "world", "$(env-bar)"},
|
2016-01-25 21:57:42 +00:00
|
|
|
WorkingDir: tmpDir,
|
2016-01-08 21:21:17 +00:00
|
|
|
Resources: api.ResourceRequirements{
|
2016-01-29 18:43:00 +00:00
|
|
|
Limits: api.ResourceList{"cpu": resource.MustParse("50m")},
|
|
|
|
Requests: api.ResourceList{"memory": resource.MustParse("5M")},
|
2016-01-08 21:21:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
opts: &kubecontainer.RunContainerOptions{
|
|
|
|
Envs: []kubecontainer.EnvVar{
|
|
|
|
{Name: "env-foo", Value: "foo"},
|
2016-01-28 07:14:50 +00:00
|
|
|
{Name: "env-bar", Value: "bar"},
|
2016-01-08 21:21:17 +00:00
|
|
|
},
|
|
|
|
Mounts: []kubecontainer.Mount{
|
|
|
|
{Name: "mnt-foo", ContainerPath: "/mnt-bar", ReadOnly: true},
|
|
|
|
},
|
|
|
|
PortMappings: []kubecontainer.PortMapping{
|
|
|
|
{Name: "port-foo", Protocol: api.ProtocolTCP, ContainerPort: 1234},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ctx: &api.SecurityContext{
|
|
|
|
Capabilities: &api.Capabilities{
|
|
|
|
Add: []api.Capability{"CAP_SYS_CHROOT", "CAP_SYS_BOOT"},
|
|
|
|
Drop: []api.Capability{"CAP_SETUID", "CAP_SETGID"},
|
|
|
|
},
|
|
|
|
RunAsUser: &nonRootUser,
|
|
|
|
RunAsNonRoot: &runAsNonRootTrue,
|
|
|
|
},
|
|
|
|
podCtx: &api.PodSecurityContext{
|
|
|
|
SupplementalGroups: []int64{1, 2},
|
|
|
|
FSGroup: &fsgid,
|
|
|
|
},
|
|
|
|
expect: &appctypes.App{
|
2016-01-28 07:14:50 +00:00
|
|
|
Exec: appctypes.Exec{"/bin/bar", "foo", "hello", "world", "bar"},
|
2016-01-08 21:21:17 +00:00
|
|
|
User: "42",
|
2016-01-27 19:55:56 +00:00
|
|
|
Group: "0",
|
2016-01-08 21:21:17 +00:00
|
|
|
SupplementaryGIDs: []int{1, 2, 3},
|
2016-01-25 21:57:42 +00:00
|
|
|
WorkingDirectory: tmpDir,
|
2016-01-08 21:21:17 +00:00
|
|
|
Environment: []appctypes.EnvironmentVariable{
|
|
|
|
{"env-foo", "foo"},
|
2016-01-28 07:14:50 +00:00
|
|
|
{"env-bar", "bar"},
|
2016-01-08 21:21:17 +00:00
|
|
|
},
|
|
|
|
MountPoints: []appctypes.MountPoint{
|
|
|
|
{Name: *appctypes.MustACName("mnt-foo"), Path: "/mnt-bar", ReadOnly: true},
|
|
|
|
},
|
|
|
|
Ports: []appctypes.Port{
|
|
|
|
{Name: *appctypes.MustACName("port-foo"), Protocol: "TCP", Port: 1234},
|
|
|
|
},
|
|
|
|
Isolators: []appctypes.Isolator{
|
|
|
|
generateCapRetainIsolator(t, "CAP_SYS_CHROOT", "CAP_SYS_BOOT"),
|
|
|
|
generateCapRevokeIsolator(t, "CAP_SETUID", "CAP_SETGID"),
|
2016-01-29 18:43:00 +00:00
|
|
|
generateCPUIsolator(t, "50m", "50m"),
|
|
|
|
generateMemoryIsolator(t, "5M", "5M"),
|
2016-01-08 21:21:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
testCaseHint := fmt.Sprintf("test case #%d", i)
|
|
|
|
app := baseApp(t)
|
|
|
|
err := setApp(app, tt.container, tt.opts, tt.ctx, tt.podCtx)
|
|
|
|
if err == nil && tt.err != nil || err != nil && tt.err == nil {
|
|
|
|
t.Errorf("%s: expect %v, saw %v", testCaseHint, tt.err, err)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
sortAppFields(tt.expect)
|
|
|
|
sortAppFields(app)
|
|
|
|
assert.Equal(t, tt.expect, app, testCaseHint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 00:01:01 +00:00
|
|
|
|
|
|
|
func TestGenerateRunCommand(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
pod *api.Pod
|
|
|
|
uuid string
|
|
|
|
|
|
|
|
dnsServers []string
|
|
|
|
dnsSearches []string
|
|
|
|
err error
|
|
|
|
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
// Case #0, returns error.
|
|
|
|
{
|
|
|
|
&api.Pod{
|
|
|
|
Spec: api.PodSpec{},
|
|
|
|
},
|
|
|
|
"rkt-uuid-foo",
|
|
|
|
[]string{},
|
|
|
|
[]string{},
|
|
|
|
fmt.Errorf("failed to get cluster dns"),
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
// Case #1, returns no dns, with private-net.
|
|
|
|
{
|
|
|
|
&api.Pod{},
|
|
|
|
"rkt-uuid-foo",
|
|
|
|
[]string{},
|
|
|
|
[]string{},
|
|
|
|
nil,
|
|
|
|
"/bin/rkt/rkt --debug=false --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=rkt.kubernetes.io rkt-uuid-foo",
|
|
|
|
},
|
|
|
|
// Case #2, returns no dns, with host-net.
|
|
|
|
{
|
|
|
|
&api.Pod{
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
SecurityContext: &api.PodSecurityContext{
|
|
|
|
HostNetwork: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"rkt-uuid-foo",
|
|
|
|
[]string{},
|
|
|
|
[]string{},
|
|
|
|
nil,
|
|
|
|
"/bin/rkt/rkt --debug=false --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=host rkt-uuid-foo",
|
|
|
|
},
|
|
|
|
// Case #3, returns dns, dns searches, with private-net.
|
|
|
|
{
|
|
|
|
&api.Pod{
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
SecurityContext: &api.PodSecurityContext{
|
|
|
|
HostNetwork: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"rkt-uuid-foo",
|
|
|
|
[]string{"127.0.0.1"},
|
|
|
|
[]string{"."},
|
|
|
|
nil,
|
|
|
|
"/bin/rkt/rkt --debug=false --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=rkt.kubernetes.io --dns=127.0.0.1 --dns-search=. --dns-opt=ndots:5 rkt-uuid-foo",
|
|
|
|
},
|
|
|
|
// Case #4, returns dns, dns searches, with host-network.
|
|
|
|
{
|
|
|
|
&api.Pod{
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
SecurityContext: &api.PodSecurityContext{
|
|
|
|
HostNetwork: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"rkt-uuid-foo",
|
|
|
|
[]string{"127.0.0.1"},
|
|
|
|
[]string{"."},
|
|
|
|
nil,
|
|
|
|
"/bin/rkt/rkt --debug=false --insecure-options=image,ondisk --local-config=/var/rkt/local/data --dir=/var/data run-prepared --net=host --dns=127.0.0.1 --dns-search=. --dns-opt=ndots:5 rkt-uuid-foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
rkt := &Runtime{
|
|
|
|
rktBinAbsPath: "/bin/rkt/rkt",
|
|
|
|
config: &Config{
|
|
|
|
Path: "/bin/rkt/rkt",
|
|
|
|
Stage1Image: "/bin/rkt/stage1-coreos.aci",
|
|
|
|
Dir: "/var/data",
|
|
|
|
InsecureOptions: "image,ondisk",
|
|
|
|
LocalConfigDir: "/var/rkt/local/data",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
testCaseHint := fmt.Sprintf("test case #%d", i)
|
|
|
|
rkt.runtimeHelper = &fakeRuntimeHelper{tt.dnsServers, tt.dnsSearches, tt.err}
|
|
|
|
|
|
|
|
result, err := rkt.generateRunCommand(tt.pod, tt.uuid)
|
|
|
|
assert.Equal(t, tt.err, err, testCaseHint)
|
|
|
|
assert.Equal(t, tt.expect, result, testCaseHint)
|
|
|
|
}
|
|
|
|
}
|