1
0
mirror of https://github.com/rancher/os.git synced 2025-07-12 14:18:01 +00:00

test getHash() consistency

This commit is contained in:
Ivan Mikushin 2015-05-17 17:49:04 +05:00
parent c058cb1a39
commit 2547db84e5
2 changed files with 34 additions and 27 deletions

View File

@ -41,12 +41,11 @@ func (c ByCreated) Len() int { return len(c) }
func (c ByCreated) Swap(i, j int) { c[i], c[j] = c[j], c[i] } func (c ByCreated) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c ByCreated) Less(i, j int) bool { return c[j].Created < c[i].Created } func (c ByCreated) Less(i, j int) bool { return c[j].Created < c[i].Created }
func getHash(containerCfg *config.ContainerConfig) (string, error) { func getHash(containerCfg *config.ContainerConfig) string {
hash := sha1.New() hash := sha1.New()
w := util.NewErrorWriter(hash)
w.Write([]byte(containerCfg.Id)) hash.Write([]byte(containerCfg.Id))
w.Write([]byte(containerCfg.Cmd)) hash.Write([]byte(containerCfg.Cmd))
if containerCfg.Service != nil { if containerCfg.Service != nil {
//Get values of Service through reflection //Get values of Service through reflection
val := reflect.ValueOf(containerCfg.Service).Elem() val := reflect.ValueOf(containerCfg.Service).Elem()
@ -76,7 +75,7 @@ func getHash(containerCfg *config.ContainerConfig) (string, error) {
switch s := serviceValue.(type) { switch s := serviceValue.(type) {
default: default:
w.Write([]byte(fmt.Sprintf("%v", serviceValue))) hash.Write([]byte(fmt.Sprintf("%v", serviceValue)))
case *project.SliceorMap: case *project.SliceorMap:
for lkey := range s.MapParts() { for lkey := range s.MapParts() {
if lkey != "io.rancher.os.hash" { if lkey != "io.rancher.os.hash" {
@ -86,31 +85,27 @@ func getHash(containerCfg *config.ContainerConfig) (string, error) {
sort.Strings(sliceKeys) sort.Strings(sliceKeys)
for j := 0; j < len(sliceKeys); j++ { for j := 0; j < len(sliceKeys); j++ {
w.Write([]byte(fmt.Sprintf("%s=%v", sliceKeys[j], s.MapParts()[sliceKeys[j]]))) hash.Write([]byte(fmt.Sprintf("%s=%v", sliceKeys[j], s.MapParts()[sliceKeys[j]])))
} }
case *project.Stringorslice: case *project.Stringorslice:
sliceKeys = s.Slice() sliceKeys = s.Slice()
sort.Strings(sliceKeys) sort.Strings(sliceKeys)
for j := 0; j < len(sliceKeys); j++ { for j := 0; j < len(sliceKeys); j++ {
w.Write([]byte(fmt.Sprintf("%s", sliceKeys[j]))) hash.Write([]byte(fmt.Sprintf("%s", sliceKeys[j])))
} }
case []string: case []string:
sliceKeys = s sliceKeys = s
sort.Strings(sliceKeys) sort.Strings(sliceKeys)
for j := 0; j < len(sliceKeys); j++ { for j := 0; j < len(sliceKeys); j++ {
w.Write([]byte(fmt.Sprintf("%s", sliceKeys[j]))) hash.Write([]byte(fmt.Sprintf("%s", sliceKeys[j])))
} }
} }
} }
} }
if w.Err != nil { return hex.EncodeToString(hash.Sum([]byte{}))
return "", w.Err
}
return hex.EncodeToString(hash.Sum([]byte{})), nil
} }
func StartAndWait(dockerHost string, containerCfg *config.ContainerConfig) error { func StartAndWait(dockerHost string, containerCfg *config.ContainerConfig) error {
@ -171,10 +166,7 @@ func (c *Container) Lookup() *Container {
return c return c
} }
hash, err := getHash(c.ContainerCfg) hash := getHash(c.ContainerCfg)
if err != nil {
return c.returnErr(err)
}
client, err := NewClient(c.dockerHost) client, err := NewClient(c.dockerHost)
if err != nil { if err != nil {
@ -486,10 +478,7 @@ func (c *Container) getCreateOpts(client *dockerClient.Client) (*dockerClient.Cr
opts.Config.Labels = make(map[string]string) opts.Config.Labels = make(map[string]string)
} }
hash, err := getHash(c.ContainerCfg) hash := getHash(c.ContainerCfg)
if err != nil {
return nil, err
}
opts.Config.Labels[config.HASH] = hash opts.Config.Labels[config.HASH] = hash
opts.Config.Labels[config.ID] = c.ContainerCfg.Id opts.Config.Labels[config.ID] = c.ContainerCfg.Id

View File

@ -5,31 +5,30 @@ import (
"testing" "testing"
"github.com/rancherio/os/config" "github.com/rancherio/os/config"
"github.com/rancherio/rancher-compose/librcompose/project"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
dockerClient "github.com/fsouza/go-dockerclient" dockerClient "github.com/fsouza/go-dockerclient"
"github.com/Sirupsen/logrus"
) )
func TestHash(t *testing.T) { func TestHash(t *testing.T) {
assert := require.New(t) assert := require.New(t)
hash, err := getHash(&config.ContainerConfig{ hash := getHash(&config.ContainerConfig{
Id: "id", Id: "id",
Cmd: "1 2 3", Cmd: "1 2 3",
}) })
assert.NoError(err, "")
hash2, err := getHash(&config.ContainerConfig{ hash2 := getHash(&config.ContainerConfig{
Id: "id2", Id: "id2",
Cmd: "1 2 3", Cmd: "1 2 3",
}) })
assert.NoError(err, "")
hash3, err := getHash(&config.ContainerConfig{ hash3 := getHash(&config.ContainerConfig{
Id: "id3", Id: "id3",
Cmd: "1 2 3 4", Cmd: "1 2 3 4",
}) })
assert.NoError(err, "")
assert.Equal("510b68938cba936876588b0143093a5850d4a142", hash, "") assert.Equal("510b68938cba936876588b0143093a5850d4a142", hash, "")
assert.NotEqual(hash, hash2, "") assert.NotEqual(hash, hash2, "")
@ -37,6 +36,25 @@ func TestHash(t *testing.T) {
assert.NotEqual(hash, hash3, "") assert.NotEqual(hash, hash3, "")
} }
func TestHash2(t *testing.T) {
assert := require.New(t)
cfg := &config.ContainerConfig{
Id: "docker-volumes",
Cmd: "",
MigrateVolumes: false,
ReloadConfig: false,
CreateOnly: true,
Service: &project.ServiceConfig{CapAdd:[]string(nil), CapDrop:[]string(nil), CpuShares:0, Command:"", Detach:"", Dns:project.NewStringorslice(), DnsSearch:project.NewStringorslice(), DomainName:"", Entrypoint:"", EnvFile:"", Environment:project.NewMaporslice([]string{}), Hostname:"", Image:"state", Labels:project.NewSliceorMap(map[string]string{"io.rancher.os.createonly":"true", "io.rancher.os.scope":"system"}), Links:[]string(nil), LogDriver:"json-file", MemLimit:0, Name:"", Net:"none", Pid:"", Ipc:"", Ports:[]string(nil), Privileged:true, Restart:"", ReadOnly:true, StdinOpen:false, Tty:false, User:"", Volumes:[]string{"/var/lib/docker:/var/lib/docker", "/var/lib/rancher/conf:/var/lib/rancher/conf", "/var/lib/system-docker:/var/lib/system-docker"}, VolumesFrom:[]string(nil), WorkingDir:"", Expose:[]string(nil), ExternalLinks:[]string(nil)},
}
for i := 0; i < 10000; i++ {
logrus.Infoln(i)
assert.Equal(getHash(cfg), getHash(cfg), "")
}
}
func TestParse(t *testing.T) { func TestParse(t *testing.T) {
assert := require.New(t) assert := require.New(t)