2018-06-20 08:48:50 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-02-25 08:35:28 -05:00
|
|
|
"io"
|
2018-06-20 08:48:50 -04:00
|
|
|
"testing"
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/env"
|
2018-06-20 08:48:50 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOrchestratorSwitch(t *testing.T) {
|
|
|
|
var testcases = []struct {
|
|
|
|
doc string
|
2018-12-17 05:27:07 -05:00
|
|
|
globalOrchestrator string
|
2018-06-20 08:48:50 -04:00
|
|
|
envOrchestrator string
|
|
|
|
flagOrchestrator string
|
2018-12-17 05:27:07 -05:00
|
|
|
contextOrchestrator string
|
2018-06-20 08:48:50 -04:00
|
|
|
expectedOrchestrator string
|
|
|
|
expectedKubernetes bool
|
|
|
|
expectedSwarm bool
|
|
|
|
}{
|
|
|
|
{
|
2018-12-17 05:27:07 -05:00
|
|
|
doc: "default",
|
2018-06-20 08:48:50 -04:00
|
|
|
expectedOrchestrator: "swarm",
|
|
|
|
expectedKubernetes: false,
|
|
|
|
expectedSwarm: true,
|
|
|
|
},
|
|
|
|
{
|
2018-12-17 05:27:07 -05:00
|
|
|
doc: "kubernetesConfigFile",
|
|
|
|
globalOrchestrator: "kubernetes",
|
2018-06-20 08:48:50 -04:00
|
|
|
expectedOrchestrator: "kubernetes",
|
|
|
|
expectedKubernetes: true,
|
|
|
|
expectedSwarm: false,
|
|
|
|
},
|
|
|
|
{
|
2018-12-17 05:27:07 -05:00
|
|
|
doc: "kubernetesEnv",
|
2018-06-20 08:48:50 -04:00
|
|
|
envOrchestrator: "kubernetes",
|
|
|
|
expectedOrchestrator: "kubernetes",
|
|
|
|
expectedKubernetes: true,
|
|
|
|
expectedSwarm: false,
|
|
|
|
},
|
|
|
|
{
|
2018-12-17 05:27:07 -05:00
|
|
|
doc: "kubernetesFlag",
|
2018-06-20 08:48:50 -04:00
|
|
|
flagOrchestrator: "kubernetes",
|
|
|
|
expectedOrchestrator: "kubernetes",
|
|
|
|
expectedKubernetes: true,
|
|
|
|
expectedSwarm: false,
|
|
|
|
},
|
|
|
|
{
|
2018-12-17 05:27:07 -05:00
|
|
|
doc: "allOrchestratorFlag",
|
2018-06-20 08:48:50 -04:00
|
|
|
flagOrchestrator: "all",
|
|
|
|
expectedOrchestrator: "all",
|
|
|
|
expectedKubernetes: true,
|
|
|
|
expectedSwarm: true,
|
|
|
|
},
|
|
|
|
{
|
2018-12-17 05:27:07 -05:00
|
|
|
doc: "kubernetesContext",
|
|
|
|
contextOrchestrator: "kubernetes",
|
|
|
|
expectedOrchestrator: "kubernetes",
|
|
|
|
expectedKubernetes: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "contextOverridesConfigFile",
|
|
|
|
globalOrchestrator: "kubernetes",
|
|
|
|
contextOrchestrator: "swarm",
|
|
|
|
expectedOrchestrator: "swarm",
|
|
|
|
expectedKubernetes: false,
|
|
|
|
expectedSwarm: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "envOverridesConfigFile",
|
|
|
|
globalOrchestrator: "kubernetes",
|
2018-06-20 08:48:50 -04:00
|
|
|
envOrchestrator: "swarm",
|
|
|
|
expectedOrchestrator: "swarm",
|
|
|
|
expectedKubernetes: false,
|
|
|
|
expectedSwarm: true,
|
|
|
|
},
|
|
|
|
{
|
2018-12-17 05:27:07 -05:00
|
|
|
doc: "flagOverridesEnv",
|
2018-06-20 08:48:50 -04:00
|
|
|
envOrchestrator: "kubernetes",
|
|
|
|
flagOrchestrator: "swarm",
|
|
|
|
expectedOrchestrator: "swarm",
|
|
|
|
expectedKubernetes: false,
|
|
|
|
expectedSwarm: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
t.Run(testcase.doc, func(t *testing.T) {
|
|
|
|
if testcase.envOrchestrator != "" {
|
|
|
|
defer env.Patch(t, "DOCKER_STACK_ORCHESTRATOR", testcase.envOrchestrator)()
|
|
|
|
}
|
2022-02-25 08:35:28 -05:00
|
|
|
orchestrator, err := GetStackOrchestrator(testcase.flagOrchestrator, testcase.contextOrchestrator, testcase.globalOrchestrator, io.Discard)
|
2018-06-20 08:48:50 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(testcase.expectedKubernetes, orchestrator.HasKubernetes()))
|
|
|
|
assert.Check(t, is.Equal(testcase.expectedSwarm, orchestrator.HasSwarm()))
|
|
|
|
assert.Check(t, is.Equal(testcase.expectedOrchestrator, string(orchestrator)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|