1
0
mirror of https://github.com/rancher/os.git synced 2025-08-31 22:32:14 +00:00

move dependencies to vendor

This commit is contained in:
Ivan Mikushin
2015-11-26 17:37:01 +05:00
parent 63d7de67cd
commit 1d691cd8d6
2232 changed files with 154499 additions and 9037 deletions

154
vendor/github.com/docker/libcompose/utils/util.go generated vendored Normal file
View File

@@ -0,0 +1,154 @@
package utils
import (
"encoding/json"
"fmt"
"sync"
"github.com/Sirupsen/logrus"
yaml "github.com/cloudfoundry-incubator/candiedyaml"
)
// InParallel holds a pool and a waitgroup to execute tasks in parallel and to be able
// to wait for completion of all tasks.
type InParallel struct {
wg sync.WaitGroup
pool sync.Pool
}
// Add adds runs the specified task in parallel and add it to the waitGroup.
func (i *InParallel) Add(task func() error) {
i.wg.Add(1)
go func() {
defer i.wg.Done()
err := task()
if err != nil {
i.pool.Put(err)
}
}()
}
// Wait waits for all tasks to complete and returns the latests error encountered if any.
func (i *InParallel) Wait() error {
i.wg.Wait()
obj := i.pool.Get()
if err, ok := obj.(error); ok {
return err
}
return nil
}
// ConvertByJSON converts a struct (src) to another one (target) using json marshalling/unmarshalling.
// If the structure are not compatible, this will throw an error as the unmarshalling will fail.
func ConvertByJSON(src, target interface{}) error {
newBytes, err := json.Marshal(src)
if err != nil {
return err
}
err = json.Unmarshal(newBytes, target)
if err != nil {
logrus.Errorf("Failed to unmarshall: %v\n%s", err, string(newBytes))
}
return err
}
// Convert converts a struct (src) to another one (target) using yaml marshalling/unmarshalling.
// If the structure are not compatible, this will throw an error as the unmarshalling will fail.
func Convert(src, target interface{}) error {
newBytes, err := yaml.Marshal(src)
if err != nil {
return err
}
err = yaml.Unmarshal(newBytes, target)
if err != nil {
logrus.Errorf("Failed to unmarshall: %v\n%s", err, string(newBytes))
}
return err
}
// CopySlice creates an exact copy of the provided string slice
func CopySlice(s []string) []string {
if s == nil {
return nil
}
r := make([]string, len(s))
copy(r, s)
return r
}
// CopyMap creates an exact copy of the provided string-to-string map
func CopyMap(m map[string]string) map[string]string {
if m == nil {
return nil
}
r := map[string]string{}
for k, v := range m {
r[k] = v
}
return r
}
// FilterStringSet accepts a string set `s` (in the form of `map[string]bool`) and a filtering function `f`
// and returns a string set containing only the strings `x` for which `f(x) == true`
func FilterStringSet(s map[string]bool, f func(x string) bool) map[string]bool {
result := map[string]bool{}
for k := range s {
if f(k) {
result[k] = true
}
}
return result
}
// FilterString returns a json representation of the specified map
// that is used as filter for docker.
func FilterString(data map[string][]string) string {
// I can't imagine this would ever fail
bytes, _ := json.Marshal(data)
return string(bytes)
}
// LabelFilterString returns a label json string representation of the specifed couple (key,value)
// that is used as filter for docker.
func LabelFilterString(key, value string) string {
return FilterString(map[string][]string{
"label": {fmt.Sprintf("%s=%s", key, value)},
})
}
// LabelFilter returns a label map representation of the specifed couple (key,value)
// that is used as filter for docker.
func LabelFilter(key, value string) map[string][]string {
return map[string][]string{
"label": {fmt.Sprintf("%s=%s", key, value)},
}
}
// Contains checks if the specified string (key) is present in the specified collection.
func Contains(collection []string, key string) bool {
for _, value := range collection {
if value == key {
return true
}
}
return false
}
// Merge performs a union of two string slices: the result is an unordered slice
// that includes every item from either argument exactly once
func Merge(coll1, coll2 []string) []string {
m := map[string]struct{}{}
for _, v := range append(coll1, coll2...) {
m[v] = struct{}{}
}
r := make([]string, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}

287
vendor/github.com/docker/libcompose/utils/util_test.go generated vendored Normal file
View File

@@ -0,0 +1,287 @@
package utils
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type jsonfrom struct {
Element1 string `json:"element2"`
Element2 int `json:"element1"`
}
type jsonto struct {
Elt1 int `json:"element1"`
Elt2 string `json:"element2"`
Elt3 int
}
func TestInParallel(t *testing.T) {
size := 5
booleanMap := make(map[int]bool, size+1)
tasks := InParallel{}
for i := 0; i < size; i++ {
task := func(index int) func() error {
return func() error {
booleanMap[index] = true
return nil
}
}(i)
tasks.Add(task)
}
err := tasks.Wait()
if err != nil {
t.Fatal(err)
}
// Make sure every value is true
for _, value := range booleanMap {
if !value {
t.Fatalf("booleanMap expected to contain only true values, got at least one false")
}
}
}
func TestInParallelError(t *testing.T) {
size := 5
booleanMap := make(map[int]bool, size+1)
tasks := InParallel{}
for i := 0; i < size; i++ {
task := func(index int) func() error {
return func() error {
booleanMap[index] = true
if index%2 == 0 {
return fmt.Errorf("Error with %v", index)
}
return nil
}
}(i)
tasks.Add(task)
}
err := tasks.Wait()
if err == nil {
t.Fatalf("Expected an error on Wait, got nothing.")
}
for key, value := range booleanMap {
if key%2 != 0 && !value {
t.Fatalf("booleanMap expected to contain true values on odd number, got %v", booleanMap)
}
}
}
func TestConvertByJSON(t *testing.T) {
valids := []struct {
src jsonfrom
expected jsonto
}{
{
jsonfrom{Element2: 1},
jsonto{1, "", 0},
},
{
jsonfrom{},
jsonto{0, "", 0},
},
{
jsonfrom{"element1", 2},
jsonto{2, "element1", 0},
},
}
for _, valid := range valids {
var target jsonto
err := ConvertByJSON(valid.src, &target)
if err != nil || target.Elt1 != valid.expected.Elt1 || target.Elt2 != valid.expected.Elt2 || target.Elt3 != 0 {
t.Fatalf("Expected %v from %v got %v, %v", valid.expected, valid.src, target, err)
}
}
}
func TestConvertByJSONInvalid(t *testing.T) {
invalids := []interface{}{
// Incompatible struct
struct {
Element1 int `json:"element2"`
Element2 string `json:"element1"`
}{1, "element1"},
// Not marshable struct
struct {
Element1 func(int) int
}{
func(i int) int { return 0 },
},
}
for _, invalid := range invalids {
var target jsonto
if err := ConvertByJSON(invalid, &target); err == nil {
t.Fatalf("Expected an error converting %v to %v, got nothing", invalid, target)
}
}
}
type yamlfrom struct {
Element1 string `yaml:"element2"`
Element2 int `yaml:"element1"`
}
type yamlto struct {
Elt1 int `yaml:"element1"`
Elt2 string `yaml:"element2"`
Elt3 int
}
func TestConvert(t *testing.T) {
valids := []struct {
src yamlfrom
expected yamlto
}{
{
yamlfrom{Element2: 1},
yamlto{1, "", 0},
},
{
yamlfrom{},
yamlto{0, "", 0},
},
{
yamlfrom{"element1", 2},
yamlto{2, "element1", 0},
},
}
for _, valid := range valids {
var target yamlto
err := Convert(valid.src, &target)
if err != nil || target.Elt1 != valid.expected.Elt1 || target.Elt2 != valid.expected.Elt2 || target.Elt3 != 0 {
t.Fatalf("Expected %v from %v got %v, %v", valid.expected, valid.src, target, err)
}
}
}
func TestConvertInvalid(t *testing.T) {
invalids := []interface{}{
// Incompatible struct
struct {
Element1 int `yaml:"element2"`
Element2 string `yaml:"element1"`
}{1, "element1"},
// Not marshable struct
// This one panics :-|
// struct {
// Element1 func(int) int
// }{
// func(i int) int { return 0 },
// },
}
for _, invalid := range invalids {
var target yamlto
if err := Convert(invalid, &target); err == nil {
t.Fatalf("Expected an error converting %v to %v, got nothing", invalid, target)
}
}
}
func TestFilterStringSet(t *testing.T) {
s := map[string]bool{"abcd": true, "b": true, "cde": true, "d": true, "ef": true}
expected := map[string]bool{"abcd": true, "cde": true}
result := FilterStringSet(s, func(x string) bool { return len(x) > 2 })
assert.Equal(t, expected, result)
}
func TestFilterString(t *testing.T) {
datas := []struct {
value map[string][]string
expected string
}{
{
map[string][]string{},
"{}",
},
{
map[string][]string{
"key": {},
},
`{"key":[]}`,
},
{
map[string][]string{
"key": {"value1", "value2"},
},
`{"key":["value1","value2"]}`,
},
{
map[string][]string{
"key1": {"value1", "value2"},
"key2": {"value3", "value4"},
},
`{"key1":["value1","value2"],"key2":["value3","value4"]}`,
},
}
for _, data := range datas {
actual := FilterString(data.value)
if actual != data.expected {
t.Fatalf("Expected '%v' for %v, got '%v'", data.expected, data.value, actual)
}
}
}
func TestLabelFilter(t *testing.T) {
filters := []struct {
key string
value string
expected string
}{
{
"key", "value", `{"label":["key=value"]}`,
}, {
"key", "", `{"label":["key="]}`,
}, {
"", "", `{"label":["="]}`,
},
}
for _, filter := range filters {
actual := LabelFilterString(filter.key, filter.value)
if actual != filter.expected {
t.Fatalf("Expected '%s for key=%s and value=%s, got %s", filter.expected, filter.key, filter.value, actual)
}
}
}
func TestContains(t *testing.T) {
cases := []struct {
collection []string
key string
contains bool
}{
{
[]string{}, "", false,
},
{
[]string{""}, "", true,
},
{
[]string{"value1", "value2"}, "value3", false,
},
{
[]string{"value1", "value2"}, "value1", true,
},
{
[]string{"value1", "value2"}, "value2", true,
},
}
for _, element := range cases {
actual := Contains(element.collection, element.key)
if actual != element.contains {
t.Fatalf("Expected contains to be %v for %v in %v, but was %v", element.contains, element.key, element.collection, actual)
}
}
}
func TestMerge(t *testing.T) {
a := []string{"a", "b", "c"}
b := []string{"b", "c", "d", "e"}
expected := []string{"a", "b", "c", "d", "e"}
r := Merge(a, b)
assert.Equal(t, len(expected), len(r))
for _, v := range expected {
assert.True(t, Contains(r, v))
}
assert.Equal(t, "a:b", fmt.Sprint("a", ":", "b"))
}