2016-08-27 02:04:09 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes Authors.
|
|
|
|
|
|
|
|
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 dockershim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2017-06-07 08:54:28 +00:00
|
|
|
runtimeapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
2016-08-27 02:04:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSandboxNameRoundTrip(t *testing.T) {
|
|
|
|
config := makeSandboxConfig("foo", "bar", "iamuid", 3)
|
|
|
|
actualName := makeSandboxName(config)
|
|
|
|
assert.Equal(t, "k8s_POD_foo_bar_iamuid_3", actualName)
|
|
|
|
|
|
|
|
actualMetadata, err := parseSandboxName(actualName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, config.Metadata, actualMetadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNonParsableSandboxNames(t *testing.T) {
|
|
|
|
// All names must start with the kubernetes prefix "k8s".
|
|
|
|
_, err := parseSandboxName("owner_POD_foo_bar_iamuid_4")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// All names must contain exactly 6 parts.
|
|
|
|
_, err = parseSandboxName("k8s_POD_dummy_foo_bar_iamuid_4")
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = parseSandboxName("k8s_foo_bar_iamuid_4")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// Should be able to parse attempt number.
|
|
|
|
_, err = parseSandboxName("k8s_POD_foo_bar_iamuid_notanumber")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerNameRoundTrip(t *testing.T) {
|
|
|
|
sConfig := makeSandboxConfig("foo", "bar", "iamuid", 3)
|
|
|
|
name, attempt := "pause", uint32(5)
|
2016-11-30 07:27:27 +00:00
|
|
|
config := &runtimeapi.ContainerConfig{
|
|
|
|
Metadata: &runtimeapi.ContainerMetadata{
|
2017-01-20 01:55:37 +00:00
|
|
|
Name: name,
|
|
|
|
Attempt: attempt,
|
2016-08-27 02:04:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
actualName := makeContainerName(sConfig, config)
|
|
|
|
assert.Equal(t, "k8s_pause_foo_bar_iamuid_5", actualName)
|
|
|
|
|
|
|
|
actualMetadata, err := parseContainerName(actualName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, config.Metadata, actualMetadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNonParsableContainerNames(t *testing.T) {
|
|
|
|
// All names must start with the kubernetes prefix "k8s".
|
|
|
|
_, err := parseContainerName("owner_frontend_foo_bar_iamuid_4")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// All names must contain exactly 6 parts.
|
|
|
|
_, err = parseContainerName("k8s_frontend_dummy_foo_bar_iamuid_4")
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = parseContainerName("k8s_foo_bar_iamuid_4")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// Should be able to parse attempt number.
|
|
|
|
_, err = parseContainerName("k8s_frontend_foo_bar_iamuid_notanumber")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2017-01-25 19:47:01 +00:00
|
|
|
|
|
|
|
func TestParseRandomizedNames(t *testing.T) {
|
|
|
|
// Test randomized sandbox name.
|
|
|
|
sConfig := makeSandboxConfig("foo", "bar", "iamuid", 3)
|
|
|
|
sActualName := randomizeName(makeSandboxName(sConfig))
|
|
|
|
sActualMetadata, err := parseSandboxName(sActualName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, sConfig.Metadata, sActualMetadata)
|
|
|
|
|
|
|
|
// Test randomized container name.
|
|
|
|
name, attempt := "pause", uint32(5)
|
|
|
|
config := &runtimeapi.ContainerConfig{
|
|
|
|
Metadata: &runtimeapi.ContainerMetadata{
|
|
|
|
Name: name,
|
|
|
|
Attempt: attempt,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actualName := randomizeName(makeContainerName(sConfig, config))
|
|
|
|
actualMetadata, err := parseContainerName(actualName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, config.Metadata, actualMetadata)
|
|
|
|
}
|