Skip to content

Commit

Permalink
test: fix the integrtion tests for apply-config
Browse files Browse the repository at this point in the history
They got broken after refactoring.

Also use this PR to test things before the release.

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
smira committed Jul 8, 2024
1 parent 076f3c4 commit 2512ef4
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 54 deletions.
24 changes: 12 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,9 @@ FROM scratch AS talosctl
ARG TARGETARCH
COPY --from=talosctl-all /talosctl-linux-${TARGETARCH} /talosctl
ARG TAG
ENV VERSION ${TAG}
ENV VERSION=${TAG}
LABEL "alpha.talos.dev/version"="${VERSION}"
LABEL org.opencontainers.image.source https://github.com/siderolabs/talos
LABEL org.opencontainers.image.source=https://github.com/siderolabs/talos
ENTRYPOINT ["/talosctl"]

# The kernel target is the linux kernel.
Expand Down Expand Up @@ -755,7 +755,7 @@ COPY --from=initramfs-archive /initramfs.xz /initramfs-${TARGETARCH}.xz

FROM scratch AS talos
COPY --from=rootfs / /
LABEL org.opencontainers.image.source https://github.com/siderolabs/talos
LABEL org.opencontainers.image.source=https://github.com/siderolabs/talos
ENTRYPOINT ["/sbin/init"]

# The installer target generates an image that can be used to install Talos to
Expand Down Expand Up @@ -793,7 +793,7 @@ FROM install-artifacts-${INSTALLER_ARCH} AS install-artifacts

FROM alpine:3.18.4 AS installer-image
ARG SOURCE_DATE_EPOCH
ENV SOURCE_DATE_EPOCH ${SOURCE_DATE_EPOCH}
ENV SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH}
RUN apk add --no-cache --update --no-scripts \
bash \
binutils-aarch64 \
Expand All @@ -813,7 +813,7 @@ RUN apk add --no-cache --update --no-scripts \
xz \
zstd
ARG TARGETARCH
ENV TARGETARCH ${TARGETARCH}
ENV TARGETARCH=${TARGETARCH}
COPY --from=installer-build /installer /bin/installer
COPY --chmod=0644 hack/extra-modules.conf /etc/modules.d/10-extra-modules.conf
COPY --from=pkg-grub / /
Expand All @@ -827,9 +827,9 @@ RUN find /bin /etc /lib /usr /sbin | grep -Ev '/etc/hosts|/etc/resolv.conf' \
FROM scratch AS installer-image-squashed
COPY --from=installer-image / /
ARG TAG
ENV VERSION ${TAG}
ENV VERSION=${TAG}
LABEL "alpha.talos.dev/version"="${VERSION}"
LABEL org.opencontainers.image.source https://github.com/siderolabs/talos
LABEL org.opencontainers.image.source=https://github.com/siderolabs/talos
ENTRYPOINT ["/bin/installer"]

FROM installer-image-squashed AS installer
Expand All @@ -841,15 +841,15 @@ ENTRYPOINT ["/bin/imager"]

FROM imager AS iso-amd64-build
ARG SOURCE_DATE_EPOCH
ENV SOURCE_DATE_EPOCH ${SOURCE_DATE_EPOCH}
ENV SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH}
RUN /bin/installer \
iso \
--arch amd64 \
--output /out

FROM imager AS iso-arm64-build
ARG SOURCE_DATE_EPOCH
ENV SOURCE_DATE_EPOCH ${SOURCE_DATE_EPOCH}
ENV SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH}
RUN /bin/installer \
iso \
--arch arm64 \
Expand All @@ -868,7 +868,7 @@ FROM base AS unit-tests-runner
RUN unlink /etc/ssl
COPY --from=rootfs / /
ARG TESTPKGS
ENV PLATFORM container
ENV PLATFORM=container
ARG GO_LDFLAGS
RUN --security=insecure --mount=type=cache,id=testspace,target=/tmp --mount=type=cache,target=/.cache go test -failfast -v \
-ldflags "${GO_LDFLAGS}" \
Expand All @@ -882,8 +882,8 @@ FROM base AS unit-tests-race
RUN unlink /etc/ssl
COPY --from=rootfs / /
ARG TESTPKGS
ENV PLATFORM container
ENV CGO_ENABLED 1
ENV PLATFORM=container
ENV CGO_ENABLED=1
ARG GO_LDFLAGS
RUN --security=insecure --mount=type=cache,id=testspace,target=/tmp --mount=type=cache,target=/.cache go test -v \
-ldflags "${GO_LDFLAGS}" \
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ require (
github.com/gizak/termui/v3 v3.1.0
github.com/godbus/dbus/v5 v5.1.0
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.6.0
github.com/google/go-containerregistry v0.19.2
github.com/google/go-tpm v0.9.1
github.com/google/nftables v0.2.0
Expand Down Expand Up @@ -249,6 +248,7 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
Expand Down
36 changes: 10 additions & 26 deletions internal/app/machined/internal/server/v1alpha1/v1alpha1_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"archive/tar"
"bufio"
"bytes"
stdcmp "cmp"
"compress/gzip"
"context"
"encoding/json"
Expand All @@ -28,7 +27,6 @@ import (
"github.com/cosi-project/runtime/pkg/safe"
"github.com/cosi-project/runtime/pkg/state"
"github.com/cosi-project/runtime/pkg/state/protobuf/server"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/gopacket/gopacket/afpacket"
multierror "github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -78,11 +76,10 @@ import (
timeapi "github.com/siderolabs/talos/pkg/machinery/api/time"
clientconfig "github.com/siderolabs/talos/pkg/machinery/client/config"
"github.com/siderolabs/talos/pkg/machinery/config"
docscfg "github.com/siderolabs/talos/pkg/machinery/config/config"
"github.com/siderolabs/talos/pkg/machinery/config/configdiff"
"github.com/siderolabs/talos/pkg/machinery/config/configloader"
"github.com/siderolabs/talos/pkg/machinery/config/generate/secrets"
machinetype "github.com/siderolabs/talos/pkg/machinery/config/machine"
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/nethelpers"
etcdresource "github.com/siderolabs/talos/pkg/machinery/resources/etcd"
Expand Down Expand Up @@ -221,7 +218,10 @@ func (s *Server) ApplyConfiguration(ctx context.Context, in *machine.ApplyConfig
}

if in.DryRun {
details := generateDiff(s.Controller.Runtime(), cfgProvider)
details, err := generateDiff(s.Controller.Runtime(), cfgProvider)
if err != nil {
return nil, fmt.Errorf("failed to generate diff: %w", err)
}

return &machine.ApplyConfigurationResponse{
Messages: []*machine.ApplyConfiguration{
Expand Down Expand Up @@ -295,33 +295,17 @@ func (s *Server) ApplyConfiguration(ctx context.Context, in *machine.ApplyConfig
}, nil
}

func generateDiff(r runtime.Runtime, provider config.Provider) string {
var cfg *v1alpha1.Config

if r.Config() != nil {
cfg = r.ConfigContainer().RawV1Alpha1()
}

v1alpha1Diff := cmp.Diff(cfg, provider.RawV1Alpha1(), cmp.AllowUnexported(v1alpha1.InstallDiskSizeMatcher{}))
if v1alpha1Diff == "" {
v1alpha1Diff = "No changes."
func generateDiff(r runtime.Runtime, provider config.Provider) (string, error) {
documentsDiff, err := configdiff.DiffToString(r.ConfigContainer(), provider)
if err != nil {
return "", err
}

origDocs := slices.DeleteFunc(r.ConfigContainer().Documents(), func(doc docscfg.Document) bool { return doc.Kind() == v1alpha1.Version })
newDocs := slices.DeleteFunc(provider.Documents(), func(doc docscfg.Document) bool { return doc.Kind() == v1alpha1.Version })

slices.SortStableFunc(origDocs, func(a, b docscfg.Document) int { return stdcmp.Compare(a.Kind(), b.Kind()) })
slices.SortStableFunc(newDocs, func(a, b docscfg.Document) int { return stdcmp.Compare(a.Kind(), b.Kind()) })

documentsDiff := cmp.Diff(origDocs, newDocs)
if documentsDiff == "" {
documentsDiff = "No changes."
}

return fmt.Sprintf(`Config diff:
%s
Documents diff:
%s`, v1alpha1Diff, documentsDiff)
return "Config diff:\n\n" + documentsDiff, nil
}

// GenerateConfiguration implements the machine.MachineServer interface.
Expand Down
11 changes: 7 additions & 4 deletions internal/app/machined/pkg/runtime/v1alpha1/v1alpha1_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (

"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/google/go-cmp/cmp"

"github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
"github.com/siderolabs/talos/internal/app/machined/pkg/system"
"github.com/siderolabs/talos/internal/app/machined/pkg/system/services"
"github.com/siderolabs/talos/pkg/machinery/config"
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
"github.com/siderolabs/talos/pkg/machinery/config/configdiff"
"github.com/siderolabs/talos/pkg/machinery/config/container"
"github.com/siderolabs/talos/pkg/machinery/resources/hardware"
"github.com/siderolabs/talos/pkg/machinery/resources/k8s"
)
Expand Down Expand Up @@ -165,9 +165,12 @@ func (r *Runtime) CanApplyImmediate(cfg config.Provider) error {
}

if !reflect.DeepEqual(currentConfig, newConfig) {
diff := cmp.Diff(currentConfig, newConfig, cmp.AllowUnexported(v1alpha1.InstallDiskSizeMatcher{}))
diff, err := configdiff.DiffToString(container.NewV1Alpha1(currentConfig), container.NewV1Alpha1(newConfig))
if err != nil {
return fmt.Errorf("error calculating diff: %w", err)
}

return fmt.Errorf("this config change can't be applied in immediate mode\ndiff: %s", diff)
return fmt.Errorf("this config change can't be applied in immediate mode\ndiff:\n%s", diff)
}

return nil
Expand Down
22 changes: 11 additions & 11 deletions internal/integration/api/apply-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (suite *ApplyConfigSuite) TestApply() {
nodeCtx := client.WithNode(suite.ctx, node)

provider, err := suite.ReadConfigFromNode(nodeCtx)
suite.Assert().NoErrorf(err, "failed to read existing config from node %q: %w", node, err)
suite.Assert().NoErrorf(err, "failed to read existing config from node %q", node)

cfgDataOut := suite.PatchV1Alpha1Config(provider, func(cfg *v1alpha1.Config) {
if cfg.MachineConfig.MachineSysctls == nil {
Expand All @@ -118,7 +118,7 @@ func (suite *ApplyConfigSuite) TestApply() {
)
if err != nil {
// It is expected that the connection will EOF here, so just log the error
suite.Assert().NoErrorf(err, "failed to apply configuration (node %q): %w", node, err)
suite.Assert().NoErrorf(err, "failed to apply configuration (node %q)", node)
}

return nil
Expand All @@ -138,7 +138,7 @@ func (suite *ApplyConfigSuite) TestApply() {

return nil
},
), "failed to read updated configuration from node %q: %w", node, err,
), "failed to read updated configuration from node %q", node,
)

suite.Assert().Equal(
Expand Down Expand Up @@ -177,14 +177,14 @@ func (suite *ApplyConfigSuite) TestApplyWithoutReboot() {
Mode: mode,
},
)
suite.Require().NoError(err, "failed to apply deferred configuration (node %q): %w", node)
suite.Require().NoError(err, "failed to apply deferred configuration (node %q)", node)

// Verify configuration change
var newProvider config.Provider

newProvider, err = suite.ReadConfigFromNode(nodeCtx)

suite.Require().NoError(err, "failed to read updated configuration from node %q: %w", node)
suite.Require().NoError(err, "failed to read updated configuration from node %q", node)

if mode == machineapi.ApplyConfigurationRequest_AUTO {
suite.Assert().Equal(
Expand All @@ -206,7 +206,7 @@ func (suite *ApplyConfigSuite) TestApplyWithoutReboot() {
Mode: mode,
},
)
suite.Require().NoError(err, "failed to apply deferred configuration (node %q): %w", node)
suite.Require().NoError(err, "failed to apply deferred configuration (node %q)", node)
}
}

Expand Down Expand Up @@ -303,7 +303,7 @@ func (suite *ApplyConfigSuite) TestApplyConfigRotateEncryptionSecrets() {
)
if err != nil {
// It is expected that the connection will EOF here, so just log the error
suite.Assert().Errorf(err, "failed to apply configuration (node %q): %w", node, err)
suite.T().Logf("failed to apply configuration (node %q): %s", node, err)
}

return nil
Expand All @@ -315,7 +315,7 @@ func (suite *ApplyConfigSuite) TestApplyConfigRotateEncryptionSecrets() {
// Verify configuration change
var newProvider config.Provider

suite.Require().Errorf(
suite.Require().NoError(
retry.Constant(time.Minute, retry.WithUnits(time.Second)).Retry(
func() error {
newProvider, err = suite.ReadConfigFromNode(nodeCtx)
Expand All @@ -325,7 +325,7 @@ func (suite *ApplyConfigSuite) TestApplyConfigRotateEncryptionSecrets() {

return nil
},
), "failed to read updated configuration from node %q: %w", node, err,
), "failed to read updated configuration from node %q", node,
)

e := newProvider.Machine().SystemDiskEncryption().Get(constants.EphemeralPartitionLabel)
Expand Down Expand Up @@ -504,10 +504,10 @@ func (suite *ApplyConfigSuite) TestApplyTry() {
TryModeTimeout: durationpb.New(time.Second * 1),
},
)
suite.Assert().NoErrorf(err, "failed to apply configuration (node %q): %s", node, err)
suite.Assert().NoErrorf(err, "failed to apply configuration (node %q)", node)

provider, err = getMachineConfig(nodeCtx)
suite.Require().NoErrorf(err, "failed to read existing config from node %q: %w", node, err)
suite.Require().NoErrorf(err, "failed to read existing config from node %q", node)

suite.Assert().NotNil(provider.Config().Machine().Network())
suite.Assert().NotNil(provider.Config().Machine().Network().Devices())
Expand Down
Loading

0 comments on commit 2512ef4

Please sign in to comment.