|
| 1 | +package manager |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/docker/infrakit/pkg/spi/group" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestErrorOnCallsToNilPlugin(t *testing.T) { |
| 12 | + |
| 13 | + errMessage := "no-plugin" |
| 14 | + proxy := NewProxy(func() (group.Plugin, error) { |
| 15 | + return nil, errors.New(errMessage) |
| 16 | + }) |
| 17 | + |
| 18 | + err := proxy.FreeGroup(group.ID("test")) |
| 19 | + require.Error(t, err) |
| 20 | + require.Equal(t, errMessage, err.Error()) |
| 21 | +} |
| 22 | + |
| 23 | +type fakeGroupPlugin struct { |
| 24 | + group.Plugin |
| 25 | + commitGroup func(grp group.Spec, pretend bool) (string, error) |
| 26 | + freeGroup func(id group.ID) error |
| 27 | +} |
| 28 | + |
| 29 | +func (f *fakeGroupPlugin) CommitGroup(grp group.Spec, pretend bool) (string, error) { |
| 30 | + return f.commitGroup(grp, pretend) |
| 31 | +} |
| 32 | +func (f *fakeGroupPlugin) FreeGroup(id group.ID) error { |
| 33 | + return f.freeGroup(id) |
| 34 | +} |
| 35 | + |
| 36 | +func TestDelayPluginLookupCallingMethod(t *testing.T) { |
| 37 | + |
| 38 | + called := false |
| 39 | + fake := &fakeGroupPlugin{ |
| 40 | + commitGroup: func(grp group.Spec, pretend bool) (string, error) { |
| 41 | + called = true |
| 42 | + require.Equal(t, group.Spec{ID: "foo"}, grp) |
| 43 | + require.Equal(t, true, pretend) |
| 44 | + return "some-response", nil |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + proxy := NewProxy(func() (group.Plugin, error) { return fake, nil }) |
| 49 | + |
| 50 | + require.False(t, called) |
| 51 | + |
| 52 | + actualStr, actualErr := proxy.CommitGroup(group.Spec{ID: "foo"}, true) |
| 53 | + require.True(t, called) |
| 54 | + require.NoError(t, actualErr) |
| 55 | + require.Equal(t, "some-response", actualStr) |
| 56 | +} |
| 57 | + |
| 58 | +func TestDelayPluginLookupCallingMethodReturnsError(t *testing.T) { |
| 59 | + |
| 60 | + called := false |
| 61 | + fake := &fakeGroupPlugin{ |
| 62 | + freeGroup: func(id group.ID) error { |
| 63 | + called = true |
| 64 | + require.Equal(t, group.ID("foo"), id) |
| 65 | + return errors.New("can't-free") |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + proxy := NewProxy(func() (group.Plugin, error) { return fake, nil }) |
| 70 | + |
| 71 | + require.False(t, called) |
| 72 | + |
| 73 | + actualErr := proxy.FreeGroup(group.ID("foo")) |
| 74 | + require.True(t, called) |
| 75 | + require.Error(t, actualErr) |
| 76 | + require.Equal(t, "can't-free", actualErr.Error()) |
| 77 | +} |
| 78 | + |
| 79 | +func TestDelayPluginLookupCallingMultipleMethods(t *testing.T) { |
| 80 | + |
| 81 | + called := false |
| 82 | + fake := &fakeGroupPlugin{ |
| 83 | + commitGroup: func(grp group.Spec, pretend bool) (string, error) { |
| 84 | + called = true |
| 85 | + require.Equal(t, group.Spec{ID: "foo"}, grp) |
| 86 | + require.Equal(t, true, pretend) |
| 87 | + return "some-response", nil |
| 88 | + }, |
| 89 | + freeGroup: func(id group.ID) error { |
| 90 | + called = true |
| 91 | + require.Equal(t, group.ID("foo"), id) |
| 92 | + return errors.New("can't-free") |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + proxy := NewProxy(func() (group.Plugin, error) { return fake, nil }) |
| 97 | + |
| 98 | + require.False(t, called) |
| 99 | + |
| 100 | + actualStr, actualErr := proxy.CommitGroup(group.Spec{ID: "foo"}, true) |
| 101 | + require.True(t, called) |
| 102 | + require.NoError(t, actualErr) |
| 103 | + require.Equal(t, "some-response", actualStr) |
| 104 | + |
| 105 | + called = false |
| 106 | + actualErr = proxy.FreeGroup(group.ID("foo")) |
| 107 | + require.True(t, called) |
| 108 | + require.Error(t, actualErr) |
| 109 | + require.Equal(t, "can't-free", actualErr.Error()) |
| 110 | +} |
0 commit comments