func TestEnvironmentResolve(t *testing.T) { factory := &TestServiceFactory{ Counts: map[string]int{}, } p := NewProject(nil, &Context{ ServiceFactory: factory, EnvironmentLookup: &TestEnvironmentLookup{}, }, nil) p.ServiceConfigs = config.NewServiceConfigs() p.ServiceConfigs.Add("foo", &config.ServiceConfig{ Environment: yaml.MaporEqualSlice([]string{ "A", "A=", "A=B", }), }) service, err := p.CreateService("foo") if err != nil { t.Fatal(err) } if !reflect.DeepEqual(service.Config().Environment, yaml.MaporEqualSlice{"A=X", "A=X", "A=B"}) { t.Fatal("Invalid environment", service.Config().Environment) } }
func TestGetServiceConfig(t *testing.T) { p := NewProject(&Context{}, nil, nil) p.ServiceConfigs = config.NewServiceConfigs() fooService := &config.ServiceConfig{} p.ServiceConfigs.Add("foo", fooService) config, ok := p.GetServiceConfig("foo") if !ok { t.Fatal("Foo service not found") } if config != fooService { t.Fatal("Incorrect Service Config returned") } config, ok = p.GetServiceConfig("unknown") if ok { t.Fatal("Found service incorrectly") } if config != nil { t.Fatal("Incorrect Service Config returned") } }
func TestVolumesFromServices(t *testing.T) { cases := []struct { volumeConfigs map[string]*config.VolumeConfig services map[string]*config.ServiceConfig volumeEnabled bool expectedVolumes []*Volume expectedError bool }{ {}, { volumeConfigs: map[string]*config.VolumeConfig{ "vol1": {}, }, services: map[string]*config.ServiceConfig{}, expectedVolumes: []*Volume{ { name: "vol1", projectName: "prj", }, }, expectedError: false, }, } for index, c := range cases { services := config.NewServiceConfigs() for name, service := range c.services { services.Add(name, service) } volumes, err := VolumesFromServices(&volumeClient{}, "prj", c.volumeConfigs, services, c.volumeEnabled) if c.expectedError { if err == nil { t.Fatalf("%d: expected an error, got nothing", index) } } else { if err != nil { t.Fatalf("%d: didn't expect an error, got one %s", index, err.Error()) } } if volumes.volumeEnabled != c.volumeEnabled { t.Fatalf("%d: expected volume enabled %v, got %v", index, c.volumeEnabled, volumes.volumeEnabled) } if len(volumes.volumes) != len(c.expectedVolumes) { t.Fatalf("%d: expected %v, got %v", index, c.expectedVolumes, volumes.volumes) } for _, volume := range volumes.volumes { testExpectedContainsVolume(t, index, c.expectedVolumes, volume) } } }
// NewProject creates a new project with the specified context. func NewProject(context *Context, runtime RuntimeProject, parseOptions *config.ParseOptions) *Project { p := &Project{ context: context, runtime: runtime, ParseOptions: parseOptions, ServiceConfigs: config.NewServiceConfigs(), VolumeConfigs: make(map[string]*config.VolumeConfig), NetworkConfigs: make(map[string]*config.NetworkConfig), } if context.LoggerFactory == nil { context.LoggerFactory = &logger.NullLogger{} } context.Project = p p.listeners = []chan<- events.Event{NewDefaultListener(p)} return p }
// NewProject creates a new project with the specified context. func NewProject(context *Context, runtime RuntimeProject, parseOptions *config.ParseOptions) *Project { p := &Project{ context: context, runtime: runtime, ParseOptions: parseOptions, ServiceConfigs: config.NewServiceConfigs(), VolumeConfigs: make(map[string]*config.VolumeConfig), NetworkConfigs: make(map[string]*config.NetworkConfig), } if context.LoggerFactory == nil { context.LoggerFactory = &logger.NullLogger{} } if context.ResourceLookup == nil { context.ResourceLookup = &lookup.FileResourceLookup{} } if context.EnvironmentLookup == nil { cwd, err := os.Getwd() if err != nil { log.Errorf("Could not get the rooted path name to the current directory: %v", err) return nil } context.EnvironmentLookup = &lookup.ComposableEnvLookup{ Lookups: []config.EnvironmentLookup{ &lookup.EnvfileLookup{ Path: filepath.Join(cwd, ".env"), }, &lookup.OsEnvLookup{}, }, } } context.Project = p p.listeners = []chan<- events.Event{NewDefaultListener(p)} return p }
func TestTwoCall(t *testing.T) { factory := &TestServiceFactory{ Counts: map[string]int{}, } p := NewProject(nil, &Context{ ServiceFactory: factory, }, nil) p.ServiceConfigs = config.NewServiceConfigs() p.ServiceConfigs.Add("foo", &config.ServiceConfig{}) if err := p.Create(context.Background(), options.Create{}, "foo"); err != nil { t.Fatal(err) } if err := p.Create(context.Background(), options.Create{}, "foo"); err != nil { t.Fatal(err) } if factory.Counts["foo.create"] != 2 { t.Fatal("Failed to create twice") } }
func convertToTaskDefinitionInTest(t *testing.T, name string, serviceConfig *config.ServiceConfig) *ecs.TaskDefinition { serviceConfigs := config.NewServiceConfigs() serviceConfigs.Add(name, serviceConfig) taskDefName := "ProjectName" envLookup, err := GetDefaultEnvironmentLookup() if err != nil { t.Fatal("Unexpected error setting up environment lookup") } resourceLookup, err := GetDefaultResourceLookup() if err != nil { t.Fatal("Unexpected error setting up resource lookup") } context := &project.Context{ Project: &project.Project{}, EnvironmentLookup: envLookup, ResourceLookup: resourceLookup, } taskDefinition, err := ConvertToTaskDefinition(taskDefName, context, serviceConfigs) if err != nil { t.Errorf("Expected to convert [%v] serviceConfigs without errors. But got [%v]", serviceConfig, err) } return taskDefinition }
func TestNetworksFromServices(t *testing.T) { cases := []struct { networkConfigs map[string]*config.NetworkConfig services map[string]*config.ServiceConfig networkEnabled bool expectedNetworks []*Network expectedError bool }{ { expectedNetworks: []*Network{}, }, { networkConfigs: map[string]*config.NetworkConfig{ "net1": {}, }, services: map[string]*config.ServiceConfig{}, expectedNetworks: []*Network{ { name: "net1", projectName: "prj", }, }, expectedError: true, }, { networkConfigs: map[string]*config.NetworkConfig{ "net1": {}, "net2": {}, }, services: map[string]*config.ServiceConfig{ "svc1": { Networks: &yaml.Networks{ Networks: []*yaml.Network{ { Name: "net1", }, }, }, }, }, expectedNetworks: []*Network{ { name: "default", projectName: "prj", }, { name: "net1", projectName: "prj", }, { name: "net2", projectName: "prj", }, }, expectedError: true, }, { networkConfigs: map[string]*config.NetworkConfig{ "net1": {}, "net2": {}, }, services: map[string]*config.ServiceConfig{ "svc1": { Networks: &yaml.Networks{ Networks: []*yaml.Network{ { Name: "net1", }, }, }, }, "svc2": { Networks: &yaml.Networks{ Networks: []*yaml.Network{ { Name: "net1", }, { Name: "net2", }, }, }, }, }, expectedNetworks: []*Network{ { name: "net1", projectName: "prj", }, { name: "net2", projectName: "prj", }, }, expectedError: false, }, { networkConfigs: map[string]*config.NetworkConfig{ "net1": {}, "net2": {}, }, services: map[string]*config.ServiceConfig{ "svc1": { Networks: &yaml.Networks{ Networks: []*yaml.Network{ { Name: "net1", }, }, }, }, "svc2": { Networks: &yaml.Networks{ Networks: []*yaml.Network{ { Name: "net1", }, { Name: "net2", }, }, }, }, "svc3": { NetworkMode: "host", Networks: &yaml.Networks{ Networks: []*yaml.Network{ { Name: "net3", }, }, }, }, }, expectedNetworks: []*Network{ { name: "net1", projectName: "prj", }, { name: "net2", projectName: "prj", }, }, expectedError: false, }, } for index, c := range cases { services := config.NewServiceConfigs() for name, service := range c.services { services.Add(name, service) } networks, err := NetworksFromServices(&networkClient{}, "prj", c.networkConfigs, services, c.networkEnabled) if c.expectedError { if err == nil { t.Fatalf("%d: expected an error, got nothing", index) } } else { if err != nil { t.Fatalf("%d: didn't expect an error, got one %s", index, err.Error()) } if networks.networkEnabled != c.networkEnabled { t.Fatalf("%d: expected network enabled %v, got %v", index, c.networkEnabled, networks.networkEnabled) } if len(networks.networks) != len(c.expectedNetworks) { t.Fatalf("%d: expected %v, got %v", index, c.expectedNetworks, networks.networks) } for _, network := range networks.networks { testExpectedContainsNetwork(t, index, c.expectedNetworks, network) } } } }