38 lines
1.3 KiB
Go
38 lines
1.3 KiB
Go
package inventory
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/shcizo/package-updater/internal/protocol"
|
|
)
|
|
|
|
func TestDiff(t *testing.T) {
|
|
a := protocol.Service{Kind: protocol.KindContainer, ContainerName: "a", Image: "a:1", Status: "running"}
|
|
b := protocol.Service{Kind: protocol.KindContainer, ContainerName: "b", Image: "b:1", Status: "running"}
|
|
bStopped := b
|
|
bStopped.Status = "stopped"
|
|
c := protocol.Service{Kind: protocol.KindContainer, ContainerName: "c", Image: "c:1", Status: "running"}
|
|
|
|
cases := []struct {
|
|
name string
|
|
prev, next []protocol.Service
|
|
wantUpserts []protocol.Service
|
|
wantRemoves []string
|
|
}{
|
|
{"unchanged", []protocol.Service{a, b}, []protocol.Service{a, b}, nil, nil},
|
|
{"added", []protocol.Service{a}, []protocol.Service{a, c}, []protocol.Service{c}, nil},
|
|
{"changed", []protocol.Service{a, b}, []protocol.Service{a, bStopped}, []protocol.Service{bStopped}, nil},
|
|
{"removed", []protocol.Service{a, b}, []protocol.Service{a}, nil, []string{"container|b"}},
|
|
{"from empty", nil, []protocol.Service{a}, []protocol.Service{a}, nil},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ups, rms := Diff(tc.prev, tc.next)
|
|
assert.Equal(t, tc.wantUpserts, ups)
|
|
assert.Equal(t, tc.wantRemoves, rms)
|
|
})
|
|
}
|
|
}
|