mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
chore: replace immutable hash with container spec comparison
This commit is contained in:
+51
-3
@@ -1,13 +1,16 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
mapset "github.com/deckarep/golang-set/v2"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/go-connections/nat"
|
||||
machinedocker "github.com/psviderski/uncloud/internal/machine/docker"
|
||||
"github.com/psviderski/uncloud/pkg/api"
|
||||
"github.com/psviderski/uncloud/pkg/client/deploy"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func assertServiceMatchesSpec(t *testing.T, svc api.Service, spec api.ServiceSpec) {
|
||||
@@ -26,9 +29,54 @@ func assertServiceMatchesSpec(t *testing.T, svc api.Service, spec api.ServiceSpe
|
||||
}
|
||||
|
||||
func assertContainerMatchesSpec(t *testing.T, ctr api.ServiceContainer, spec api.ServiceSpec) {
|
||||
status, err := deploy.CompareContainerToSpec(ctr, spec)
|
||||
require.NoError(t, err)
|
||||
status := deploy.EvalContainerSpecChange(ctr.ServiceSpec, spec)
|
||||
assert.Equal(t, deploy.ContainerUpToDate, status)
|
||||
|
||||
spec = spec.SetDefaults()
|
||||
// Verify labels.
|
||||
assert.Equal(t, spec.Name, ctr.Config.Labels[api.LabelServiceName])
|
||||
assert.Equal(t, spec.Mode, ctr.Config.Labels[api.LabelServiceMode])
|
||||
assert.Contains(t, ctr.Config.Labels, api.LabelManaged)
|
||||
|
||||
// Command and Entrypoint can only be compared if they are set in the spec.
|
||||
// Otherwise, the container takes them from the image.
|
||||
if spec.Container.Command != nil {
|
||||
assert.EqualValues(t, spec.Container.Command, ctr.Config.Cmd)
|
||||
}
|
||||
if spec.Container.Entrypoint != nil {
|
||||
assert.EqualValues(t, spec.Container.Entrypoint, ctr.Config.Entrypoint)
|
||||
}
|
||||
|
||||
assert.Equal(t, spec.Container.Image, ctr.Config.Image)
|
||||
assert.Equal(t, spec.Container.Init, ctr.HostConfig.Init)
|
||||
assert.ElementsMatch(t, spec.Container.Volumes, ctr.HostConfig.Binds)
|
||||
|
||||
// Compare host ports.
|
||||
portBindings := make(nat.PortMap)
|
||||
for _, p := range spec.Ports {
|
||||
if p.Mode != api.PortModeHost {
|
||||
continue
|
||||
}
|
||||
|
||||
port, err := nat.NewPort(p.Protocol, strconv.Itoa(int(p.ContainerPort)))
|
||||
assert.NoError(t, err)
|
||||
|
||||
binding := nat.PortBinding{HostPort: strconv.Itoa(int(p.PublishedPort))}
|
||||
if p.HostIP.IsValid() {
|
||||
binding.HostIP = p.HostIP.String()
|
||||
}
|
||||
portBindings[port] = append(portBindings[port], binding)
|
||||
}
|
||||
assert.Equal(t, portBindings, ctr.HostConfig.PortBindings)
|
||||
|
||||
assert.Equal(t, container.RestartPolicy{
|
||||
Name: container.RestartPolicyAlways,
|
||||
MaximumRetryCount: 0,
|
||||
}, ctr.HostConfig.RestartPolicy)
|
||||
|
||||
// Verify network settings.
|
||||
assert.Len(t, ctr.NetworkSettings.Networks, 1)
|
||||
assert.Contains(t, ctr.NetworkSettings.Networks, machinedocker.NetworkName)
|
||||
}
|
||||
|
||||
// serviceContainersByMachine returns a map of machine ID to service containers on that machine.
|
||||
|
||||
Reference in New Issue
Block a user