Skip to content

Commit aca9e93

Browse files
committed
Add support for multiple service deployers
1 parent 4fd1b58 commit aca9e93

File tree

2 files changed

+70
-44
lines changed

2 files changed

+70
-44
lines changed

internal/testrunner/runners/system/runner.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -446,26 +446,34 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
446446

447447
// Setup service.
448448
logger.Debug("setting up service...")
449-
serviceDeployer, err := servicedeployer.Factory(serviceOptions)
449+
serviceDeployers, err := servicedeployer.Factory(serviceOptions)
450450
if err != nil {
451451
return result.WithError(fmt.Errorf("could not create service runner: %w", err))
452452
}
453453

454454
if config.Service != "" {
455455
ctxt.Name = config.Service
456456
}
457-
service, err := serviceDeployer.SetUp(ctxt)
458-
if err != nil {
459-
return result.WithError(fmt.Errorf("could not setup service: %w", err))
460-
}
461-
ctxt = service.Context()
462-
r.shutdownServiceHandler = func() error {
463-
logger.Debug("tearing down service...")
464-
if err := service.TearDown(); err != nil {
465-
return fmt.Errorf("error tearing down service: %w", err)
457+
458+
var services []servicedeployer.DeployedService
459+
460+
for _, serviceDeployer := range serviceDeployers {
461+
462+
service, err := serviceDeployer.SetUp(ctxt)
463+
if err != nil {
464+
return result.WithError(fmt.Errorf("could not setup service: %w", err))
466465
}
466+
ctxt = service.Context()
467+
r.shutdownServiceHandler = func() error {
468+
logger.Debug("tearing down service...")
469+
if err := service.TearDown(); err != nil {
470+
return fmt.Errorf("error tearing down service: %w", err)
471+
}
467472

468-
return nil
473+
return nil
474+
}
475+
476+
services = append(services, service)
469477
}
470478

471479
// Reload test config with ctx variable substitution.
@@ -601,8 +609,12 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
601609

602610
// Signal to the service that the agent is ready (policy is assigned).
603611
if config.ServiceNotifySignal != "" {
604-
if err = service.Signal(config.ServiceNotifySignal); err != nil {
605-
return result.WithError(fmt.Errorf("failed to notify test service: %w", err))
612+
for _, service := range services {
613+
if service.Context().Name == "docker" {
614+
if err = service.Signal(config.ServiceNotifySignal); err != nil {
615+
return result.WithError(fmt.Errorf("failed to notify test service: %w", err))
616+
}
617+
}
606618
}
607619
}
608620

internal/testrunner/runners/system/servicedeployer/factory.go

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,59 @@ type FactoryOptions struct {
2424

2525
// Factory chooses the appropriate service runner for the given data stream, depending
2626
// on service configuration files defined in the package or data stream.
27-
func Factory(options FactoryOptions) (ServiceDeployer, error) {
27+
func Factory(options FactoryOptions) (map[string]ServiceDeployer, error) {
2828
devDeployPath, err := FindDevDeployPath(options)
2929
if err != nil {
3030
return nil, errors.Wrapf(err, "can't find \"%s\" directory", devDeployDir)
3131
}
3232

33-
serviceDeployerName, err := findServiceDeployer(devDeployPath)
33+
serviceDeployers, err := findServiceDeployer(devDeployPath)
3434
if err != nil {
3535
return nil, errors.Wrap(err, "can't find any valid service deployer")
3636
}
3737

38-
serviceDeployerPath := filepath.Join(devDeployPath, serviceDeployerName)
38+
serviceDeployerInstances := make(map[string]ServiceDeployer)
3939

40-
switch serviceDeployerName {
41-
case "k8s":
42-
if _, err := os.Stat(serviceDeployerPath); err == nil {
43-
return NewKubernetesServiceDeployer(serviceDeployerPath)
44-
}
45-
case "docker":
46-
dockerComposeYMLPath := filepath.Join(serviceDeployerPath, "docker-compose.yml")
47-
if _, err := os.Stat(dockerComposeYMLPath); err == nil {
48-
sv, err := useServiceVariant(devDeployPath, options.Variant)
49-
if err != nil {
50-
return nil, errors.Wrap(err, "can't use service variant")
40+
for _, serviceDeployerName := range serviceDeployers {
41+
serviceDeployerPath := filepath.Join(devDeployPath, serviceDeployerName)
42+
43+
switch serviceDeployerName {
44+
case "k8s":
45+
if _, err := os.Stat(serviceDeployerPath); err == nil {
46+
k8sDeployer, _ := NewKubernetesServiceDeployer(serviceDeployerPath)
47+
serviceDeployerInstances["k8s"] = k8sDeployer
48+
}
49+
50+
case "docker":
51+
dockerComposeYMLPath := filepath.Join(serviceDeployerPath, "docker-compose.yml")
52+
if _, err := os.Stat(dockerComposeYMLPath); err == nil {
53+
sv, err := useServiceVariant(devDeployPath, options.Variant)
54+
if err != nil {
55+
return nil, fmt.Errorf("can't use service variant: %w", err)
56+
}
57+
dcDeployer, _ := NewDockerComposeServiceDeployer([]string{dockerComposeYMLPath}, sv)
58+
serviceDeployerInstances["docker"] = dcDeployer
59+
}
60+
61+
case "agent":
62+
customAgentCfgYMLPath := filepath.Join(serviceDeployerPath, "custom-agent.yml")
63+
if _, err := os.Stat(customAgentCfgYMLPath); err != nil {
64+
return nil, fmt.Errorf("can't find expected file custom-agent.yml: %w", err)
65+
}
66+
agentDeployer, _ := NewCustomAgentDeployer(customAgentCfgYMLPath)
67+
serviceDeployerInstances["agent"] = agentDeployer
68+
69+
case "tf":
70+
if _, err := os.Stat(serviceDeployerPath); err == nil {
71+
tfDeployer, _ := NewTerraformServiceDeployer(serviceDeployerPath)
72+
serviceDeployerInstances["tf"] = tfDeployer
5173
}
52-
return NewDockerComposeServiceDeployer([]string{dockerComposeYMLPath}, sv)
53-
}
54-
case "agent":
55-
customAgentCfgYMLPath := filepath.Join(serviceDeployerPath, "custom-agent.yml")
56-
if _, err := os.Stat(customAgentCfgYMLPath); err != nil {
57-
return nil, errors.Wrap(err, "can't find expected file custom-agent.yml")
58-
}
59-
return NewCustomAgentDeployer(customAgentCfgYMLPath)
6074

61-
case "tf":
62-
if _, err := os.Stat(serviceDeployerPath); err == nil {
63-
return NewTerraformServiceDeployer(serviceDeployerPath)
75+
default:
76+
return nil, fmt.Errorf("unsupported service deployer (name: %s)", serviceDeployerName)
6477
}
6578
}
66-
return nil, fmt.Errorf("unsupported service deployer (name: %s)", serviceDeployerName)
79+
return serviceDeployerInstances, nil
6780
}
6881

6982
// FindDevDeployPath function returns a path reference to the "_dev/deploy" directory.
@@ -86,10 +99,10 @@ func FindDevDeployPath(options FactoryOptions) (string, error) {
8699
return "", fmt.Errorf("\"%s\" directory doesn't exist", devDeployDir)
87100
}
88101

89-
func findServiceDeployer(devDeployPath string) (string, error) {
102+
func findServiceDeployer(devDeployPath string) ([]string, error) {
90103
fis, err := os.ReadDir(devDeployPath)
91104
if err != nil {
92-
return "", errors.Wrapf(err, "can't read directory (path: %s)", devDeployDir)
105+
return nil, errors.Wrapf(err, "can't read directory (path: %s)", devDeployDir)
93106
}
94107

95108
var folders []os.DirEntry
@@ -99,8 +112,9 @@ func findServiceDeployer(devDeployPath string) (string, error) {
99112
}
100113
}
101114

102-
if len(folders) != 1 {
103-
return "", fmt.Errorf("expected to find only one service deployer in \"%s\"", devDeployPath)
115+
var folderNames []string
116+
for _, fname := range folders {
117+
folderNames = append(folderNames, fname.Name())
104118
}
105-
return folders[0].Name(), nil
119+
return folderNames, nil
106120
}

0 commit comments

Comments
 (0)