github.com/skippbox/kompose-origin@v0.0.0-20160524133224-16a9dca7bac2/project/project_test.go (about) 1 package project 2 3 import ( 4 "fmt" 5 "reflect" 6 "strings" 7 "testing" 8 9 "golang.org/x/net/context" 10 11 "github.com/docker/libcompose/config" 12 "github.com/docker/libcompose/project/options" 13 "github.com/docker/libcompose/yaml" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 type TestServiceFactory struct { 18 Counts map[string]int 19 } 20 21 type TestService struct { 22 factory *TestServiceFactory 23 name string 24 config *config.ServiceConfig 25 EmptyService 26 Count int 27 } 28 29 func (t *TestService) Config() *config.ServiceConfig { 30 return t.config 31 } 32 33 func (t *TestService) Name() string { 34 return t.name 35 } 36 37 func (t *TestService) Run(ctx context.Context, commandParts []string) (int, error) { 38 return 0, nil 39 } 40 41 func (t *TestService) Create(ctx context.Context, options options.Create) error { 42 key := t.name + ".create" 43 t.factory.Counts[key] = t.factory.Counts[key] + 1 44 return nil 45 } 46 47 func (t *TestService) DependentServices() []ServiceRelationship { 48 return nil 49 } 50 51 func (t *TestServiceFactory) Create(project *Project, name string, serviceConfig *config.ServiceConfig) (Service, error) { 52 return &TestService{ 53 factory: t, 54 config: serviceConfig, 55 name: name, 56 }, nil 57 } 58 59 func TestTwoCall(t *testing.T) { 60 factory := &TestServiceFactory{ 61 Counts: map[string]int{}, 62 } 63 64 p := NewProject(nil, &Context{ 65 ServiceFactory: factory, 66 }, nil) 67 p.ServiceConfigs = config.NewServiceConfigs() 68 p.ServiceConfigs.Add("foo", &config.ServiceConfig{}) 69 70 if err := p.Create(context.Background(), options.Create{}, "foo"); err != nil { 71 t.Fatal(err) 72 } 73 74 if err := p.Create(context.Background(), options.Create{}, "foo"); err != nil { 75 t.Fatal(err) 76 } 77 78 if factory.Counts["foo.create"] != 2 { 79 t.Fatal("Failed to create twice") 80 } 81 } 82 83 func TestParseWithBadContent(t *testing.T) { 84 p := NewProject(nil, &Context{ 85 ComposeBytes: [][]byte{ 86 []byte("garbage"), 87 }, 88 }, nil) 89 90 err := p.Parse() 91 if err == nil { 92 t.Fatal("Should have failed parse") 93 } 94 95 if !strings.HasPrefix(err.Error(), "Invalid timestamp: 'garbage'") { 96 t.Fatalf("Should have failed parse: %#v", err) 97 } 98 } 99 100 func TestParseWithGoodContent(t *testing.T) { 101 p := NewProject(nil, &Context{ 102 ComposeBytes: [][]byte{ 103 []byte("not-garbage:\n image: foo"), 104 }, 105 }, nil) 106 107 err := p.Parse() 108 if err != nil { 109 t.Fatal(err) 110 } 111 } 112 113 type TestEnvironmentLookup struct { 114 } 115 116 func (t *TestEnvironmentLookup) Lookup(key, serviceName string, config *config.ServiceConfig) []string { 117 return []string{fmt.Sprintf("%s=X", key)} 118 } 119 120 func TestEnvironmentResolve(t *testing.T) { 121 factory := &TestServiceFactory{ 122 Counts: map[string]int{}, 123 } 124 125 p := NewProject(nil, &Context{ 126 ServiceFactory: factory, 127 EnvironmentLookup: &TestEnvironmentLookup{}, 128 }, nil) 129 p.ServiceConfigs = config.NewServiceConfigs() 130 p.ServiceConfigs.Add("foo", &config.ServiceConfig{ 131 Environment: yaml.MaporEqualSlice([]string{ 132 "A", 133 "A=", 134 "A=B", 135 }), 136 }) 137 138 service, err := p.CreateService("foo") 139 if err != nil { 140 t.Fatal(err) 141 } 142 143 if !reflect.DeepEqual(service.Config().Environment, yaml.MaporEqualSlice{"A=X", "A=X", "A=B"}) { 144 t.Fatal("Invalid environment", service.Config().Environment) 145 } 146 } 147 148 func TestParseWithMultipleComposeFiles(t *testing.T) { 149 configOne := []byte(` 150 multiple: 151 image: tianon/true 152 ports: 153 - 8000`) 154 155 configTwo := []byte(` 156 multiple: 157 image: busybox 158 container_name: multi 159 ports: 160 - 9000`) 161 162 configThree := []byte(` 163 multiple: 164 image: busybox 165 mem_limit: 40000000 166 ports: 167 - 10000`) 168 169 p := NewProject(nil, &Context{ 170 ComposeBytes: [][]byte{configOne, configTwo}, 171 }, nil) 172 173 err := p.Parse() 174 175 assert.Nil(t, err) 176 177 multipleConfig, _ := p.ServiceConfigs.Get("multiple") 178 assert.Equal(t, "busybox", multipleConfig.Image) 179 assert.Equal(t, "multi", multipleConfig.ContainerName) 180 assert.Equal(t, []string{"8000", "9000"}, multipleConfig.Ports) 181 182 p = NewProject(nil, &Context{ 183 ComposeBytes: [][]byte{configTwo, configOne}, 184 }, nil) 185 186 err = p.Parse() 187 188 assert.Nil(t, err) 189 190 multipleConfig, _ = p.ServiceConfigs.Get("multiple") 191 assert.Equal(t, "tianon/true", multipleConfig.Image) 192 assert.Equal(t, "multi", multipleConfig.ContainerName) 193 assert.Equal(t, []string{"9000", "8000"}, multipleConfig.Ports) 194 195 p = NewProject(nil, &Context{ 196 ComposeBytes: [][]byte{configOne, configTwo, configThree}, 197 }, nil) 198 199 err = p.Parse() 200 201 assert.Nil(t, err) 202 203 multipleConfig, _ = p.ServiceConfigs.Get("multiple") 204 assert.Equal(t, "busybox", multipleConfig.Image) 205 assert.Equal(t, "multi", multipleConfig.ContainerName) 206 assert.Equal(t, []string{"8000", "9000", "10000"}, multipleConfig.Ports) 207 assert.Equal(t, int64(40000000), multipleConfig.MemLimit) 208 }