50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package protocol
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestServiceKey(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
svc Service
|
|
want string
|
|
}{
|
|
{"compose", Service{Kind: KindCompose, ComposeProject: "slusk", ComposeService: "web"}, "compose|slusk|web"},
|
|
{"swarm", Service{Kind: KindSwarm, SwarmService: "gitea_gitea"}, "swarm|gitea_gitea"},
|
|
{"container", Service{Kind: KindContainer, ContainerName: "adhoc"}, "container|adhoc"},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
assert.Equal(t, c.want, c.svc.Key())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEncodeDecodeRoundTrip(t *testing.T) {
|
|
in := Snapshot{Services: []Service{
|
|
{Kind: KindCompose, ComposeProject: "slusk", ComposeService: "web", Image: "slusk:1.4.2", Status: "running"},
|
|
}}
|
|
env, err := Encode(TypeSnapshot, in)
|
|
require.NoError(t, err)
|
|
|
|
raw, err := json.Marshal(env)
|
|
require.NoError(t, err)
|
|
var back Envelope
|
|
require.NoError(t, json.Unmarshal(raw, &back))
|
|
|
|
assert.Equal(t, TypeSnapshot, back.Type)
|
|
out, err := Decode[Snapshot](back)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, in, out)
|
|
}
|
|
|
|
func TestDecodeRejectsMalformedPayload(t *testing.T) {
|
|
_, err := Decode[Hello](Envelope{Type: TypeHello, Payload: json.RawMessage(`{"hostname": 5}`)})
|
|
assert.ErrorContains(t, err, "decode hello")
|
|
}
|