2024-02-27 22:12:36 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2024-04-25 17:33:07 +02:00
|
|
|
"github.com/docker/docker/api/types/system"
|
2024-02-27 22:12:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockInfoClient struct {
|
2024-04-25 17:33:07 +02:00
|
|
|
result system.Info
|
2024-02-27 22:12:36 +01:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2024-04-25 17:33:07 +02:00
|
|
|
func (m *mockInfoClient) Info(context.Context) (system.Info, error) {
|
2024-02-27 22:12:36 +01:00
|
|
|
return m.result, m.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsSwarm(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
client *mockInfoClient
|
|
|
|
expected bool
|
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"swarm",
|
|
|
|
&mockInfoClient{
|
2024-04-25 17:33:07 +02:00
|
|
|
result: system.Info{
|
2024-02-27 22:12:36 +01:00
|
|
|
Swarm: swarm.Info{
|
|
|
|
LocalNodeState: swarm.LocalNodeStateActive,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"compose",
|
|
|
|
&mockInfoClient{
|
2024-04-25 17:33:07 +02:00
|
|
|
result: system.Info{
|
2024-02-27 22:12:36 +01:00
|
|
|
Swarm: swarm.Info{
|
|
|
|
LocalNodeState: swarm.LocalNodeStateInactive,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"balena",
|
|
|
|
&mockInfoClient{
|
2024-04-25 17:33:07 +02:00
|
|
|
result: system.Info{
|
2024-02-27 22:12:36 +01:00
|
|
|
Swarm: swarm.Info{
|
|
|
|
LocalNodeState: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"error",
|
|
|
|
&mockInfoClient{
|
|
|
|
err: errors.New("the dinosaurs escaped"),
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
result, err := isSwarm(test.client)
|
|
|
|
if (err != nil) != test.expectError {
|
|
|
|
t.Errorf("Unexpected error value %v", err)
|
|
|
|
}
|
|
|
|
if test.expected != result {
|
|
|
|
t.Errorf("Expected %v, got %v", test.expected, result)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|