Add unit test for the work around.

This commit is contained in:
Random-Liu 2017-01-25 11:47:01 -08:00
parent 1b1b444c46
commit 90e91a8989
3 changed files with 98 additions and 0 deletions

View File

@ -19,6 +19,7 @@ package dockershim
import ( import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"strings"
"testing" "testing"
"time" "time"
@ -215,3 +216,69 @@ func TestContainerLogPath(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, fakeOS.Removes, []string{kubeletContainerLogPath}) assert.Equal(t, fakeOS.Removes, []string{kubeletContainerLogPath})
} }
// TestContainerCreationConflict tests the logic to work around docker container
// creation naming conflict bug.
func TestContainerCreationConflict(t *testing.T) {
sConfig := makeSandboxConfig("foo", "bar", "1", 0)
config := makeContainerConfig(sConfig, "pause", "iamimage", 0, map[string]string{}, map[string]string{})
containerName := makeContainerName(sConfig, config)
const sandboxId = "sandboxid"
const containerId = "containerid"
conflictError := fmt.Errorf("Error response from daemon: Conflict. The name \"/%s\" is already in use by container %s. You have to remove (or rename) that container to be able to reuse that name.",
containerName, containerId)
noContainerError := fmt.Errorf("Error response from daemon: No such container: %s", containerId)
randomError := fmt.Errorf("random error")
for desc, test := range map[string]struct {
createError error
removeError error
expectError error
expectCalls []string
expectFields int
}{
"no create error": {
expectCalls: []string{"create"},
expectFields: 6,
},
"random create error": {
createError: randomError,
expectError: randomError,
expectCalls: []string{"create"},
expectFields: 1,
},
"conflict create error with successful remove": {
createError: conflictError,
expectError: conflictError,
expectCalls: []string{"create", "remove"},
expectFields: 1,
},
"conflict create error with random remove error": {
createError: conflictError,
removeError: randomError,
expectError: conflictError,
expectCalls: []string{"create", "remove"},
expectFields: 1,
},
"conflict create error with no such container remove error": {
createError: conflictError,
removeError: noContainerError,
expectCalls: []string{"create", "remove", "create"},
expectFields: 7,
},
} {
t.Logf("TestCase: %s", desc)
ds, fDocker, _ := newTestDockerService()
if test.createError != nil {
fDocker.InjectError("create", test.createError)
}
if test.removeError != nil {
fDocker.InjectError("remove", test.removeError)
}
name, err := ds.CreateContainer(sandboxId, config, sConfig)
assert.Equal(t, test.expectError, err)
assert.NoError(t, fDocker.AssertCalls(test.expectCalls))
assert.Len(t, strings.Split(name, nameDelimiter), test.expectFields)
}
}

View File

@ -236,3 +236,12 @@ func TestParsingCreationConflictError(t *testing.T) {
require.Len(t, matches, 2) require.Len(t, matches, 2)
require.Equal(t, matches[1], "24666ab8c814d16f986449e504ea0159468ddf8da01897144a770f66dce0e14e") require.Equal(t, matches[1], "24666ab8c814d16f986449e504ea0159468ddf8da01897144a770f66dce0e14e")
} }
func TestParsingRemovalNoContainerError(t *testing.T) {
// Expected error message from docker.
match := "Error response from daemon: No such container: 96e914f31579e44fe49b239266385330a9b2125abeb9254badd9fca74580c95a"
notMatch := "Error response from daemon: Other errors"
assert.True(t, noContainerRE.MatchString(match))
assert.False(t, noContainerRE.MatchString(notMatch))
}

View File

@ -82,3 +82,25 @@ func TestNonParsableContainerNames(t *testing.T) {
_, err = parseContainerName("k8s_frontend_foo_bar_iamuid_notanumber") _, err = parseContainerName("k8s_frontend_foo_bar_iamuid_notanumber")
assert.Error(t, err) assert.Error(t, err)
} }
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)
}