-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swarm mode check fails on non-standard Info responses (#376)
* Swarm mode check fails on non-standard Info responses * Add unit test * Remove balena tests, add note to docs
- Loading branch information
Showing
3 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/api/types/swarm" | ||
) | ||
|
||
type mockInfoClient struct { | ||
result types.Info | ||
err error | ||
} | ||
|
||
func (m *mockInfoClient) Info(context.Context) (types.Info, error) { | ||
return m.result, m.err | ||
} | ||
|
||
func TestIsSwarm(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
client *mockInfoClient | ||
expected bool | ||
expectError bool | ||
}{ | ||
{ | ||
"swarm", | ||
&mockInfoClient{ | ||
result: types.Info{ | ||
Swarm: swarm.Info{ | ||
LocalNodeState: swarm.LocalNodeStateActive, | ||
}, | ||
}, | ||
}, | ||
true, | ||
false, | ||
}, | ||
{ | ||
"compose", | ||
&mockInfoClient{ | ||
result: types.Info{ | ||
Swarm: swarm.Info{ | ||
LocalNodeState: swarm.LocalNodeStateInactive, | ||
}, | ||
}, | ||
}, | ||
false, | ||
false, | ||
}, | ||
{ | ||
"balena", | ||
&mockInfoClient{ | ||
result: types.Info{ | ||
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters