1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
// Copyright 2014 Google Inc. 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.
// Handler for Docker containers.
package docker
import (
"io/ioutil"
"os"
"path"
"strings"
"testing"
"github.com/docker/docker/api/types/container"
"github.com/stretchr/testify/assert"
)
func TestStorageDirDetectionWithOldVersions(t *testing.T) {
as := assert.New(t)
rwLayer, err := getRwLayerID("abcd", "/", aufsStorageDriver, []int{1, 9, 0})
as.Nil(err)
as.Equal(rwLayer, "abcd")
}
func TestStorageDirDetectionWithNewVersions(t *testing.T) {
as := assert.New(t)
testDir, err := ioutil.TempDir("", "")
as.Nil(err)
containerID := "abcd"
randomizedID := "xyz"
randomIDPath := path.Join(testDir, "image/aufs/layerdb/mounts/", containerID)
as.Nil(os.MkdirAll(randomIDPath, os.ModePerm))
as.Nil(ioutil.WriteFile(path.Join(randomIDPath, "mount-id"), []byte(randomizedID), os.ModePerm))
rwLayer, err := getRwLayerID(containerID, testDir, "aufs", []int{1, 10, 0})
as.Nil(err)
as.Equal(rwLayer, randomizedID)
rwLayer, err = getRwLayerID(containerID, testDir, "aufs", []int{1, 10, 0})
as.Nil(err)
as.Equal(rwLayer, randomizedID)
}
func rawMetadataEnvMatch(dockerEnvWhiteList string, cntConfig container.Config) map[string]string {
metadataEnvs := strings.Split(dockerEnvWhiteList, ",")
handlerEnvs := make(map[string]string)
// split env vars to get metadata map.
for _, exposedEnv := range metadataEnvs {
for _, envVar := range cntConfig.Env {
if envVar != "" {
splits := strings.SplitN(envVar, "=", 2)
if len(splits) == 2 && splits[0] == exposedEnv {
handlerEnvs[strings.ToLower(exposedEnv)] = splits[1]
}
}
}
}
return handlerEnvs
}
func newMetadataEnvMatch(dockerEnvWhiteList string, cntConfig container.Config) map[string]string {
metadataEnvs := strings.Split(dockerEnvWhiteList, ",")
handlerEnvs := make(map[string]string)
// split env vars to get metadata map.
for _, exposedEnv := range metadataEnvs {
if exposedEnv == "" {
// if no dockerEnvWhitelist provided, len(metadataEnvs) == 1, metadataEnvs[0] == ""
continue
}
for _, envVar := range cntConfig.Env {
if envVar != "" {
splits := strings.SplitN(envVar, "=", 2)
if len(splits) == 2 && strings.HasPrefix(splits[0], exposedEnv) {
handlerEnvs[strings.ToLower(splits[0])] = splits[1]
}
}
}
}
return handlerEnvs
}
func TestDockerEnvWhitelist(t *testing.T) {
as := assert.New(t)
envTotalMatch := "TEST_REGION,TEST_ZONE"
envMatchWithPrefix := "TEST_"
envMatchWithPrefixEmpty := ""
rawCntConfig := container.Config{Env: []string{"TEST_REGION=FRA", "TEST_ZONE=A", "HELLO=WORLD"}}
newCntConfig := container.Config{Env: []string{"TEST_REGION=FRA", "TEST_ZONE=A", "TEST_POOL=TOOLING", "HELLO=WORLD"}}
rawExpected := map[string]string{
"test_region": "FRA",
"test_zone": "A",
}
newExpected := map[string]string{
"test_region": "FRA",
"test_zone": "A",
"test_pool": "TOOLING",
}
emptyExpected := map[string]string{}
rawEnvsTotalMatch := rawMetadataEnvMatch(envTotalMatch, rawCntConfig)
newEnvsTotalMatch := newMetadataEnvMatch(envTotalMatch, rawCntConfig)
// make sure total match does not change
as.Equal(rawEnvsTotalMatch, newEnvsTotalMatch)
as.Equal(rawEnvsTotalMatch, rawExpected)
rawEnvsTotalMatch2 := rawMetadataEnvMatch(envTotalMatch, newCntConfig)
newEnvsTotalMatch2 := newMetadataEnvMatch(envTotalMatch, newCntConfig)
// make sure total match does not change with more envs exposed
as.Equal(rawEnvsTotalMatch2, newEnvsTotalMatch2)
as.Equal(rawEnvsTotalMatch2, rawExpected)
newEnvsMatchWithPrefix := newMetadataEnvMatch(envMatchWithPrefix, rawCntConfig)
newEnvsMatchWithPrefix2 := newMetadataEnvMatch(envMatchWithPrefix, newCntConfig)
// make sure new method can return envs with prefix specified
as.Equal(newEnvsMatchWithPrefix, rawExpected)
as.Equal(newEnvsMatchWithPrefix2, newExpected)
newEnvsMatchWithEmptyPrefix := newMetadataEnvMatch(envMatchWithPrefixEmpty, newCntConfig)
rawEnvsMatchWithEmptyWhitelist := rawMetadataEnvMatch(envMatchWithPrefixEmpty, newCntConfig)
// make sure empty whitelist returns nothing
as.Equal(newEnvsMatchWithEmptyPrefix, emptyExpected)
as.Equal(rawEnvsMatchWithEmptyWhitelist, emptyExpected)
}
|