Skip to content

Commit

Permalink
Fixups to upgrade process (#115)
Browse files Browse the repository at this point in the history
* Attach error logs when syncing data fails

Signed-off-by: Ettore Di Giacinto <edigiacinto@suse.com>

* Add 'host' to excludes

Signed-off-by: Ettore Di Giacinto <edigiacinto@suse.com>

* Add 'run' to excludes

Signed-off-by: Ettore Di Giacinto <edigiacinto@suse.com>

* Use Errorf

Signed-off-by: Ettore Di Giacinto <edigiacinto@suse.com>

* tests: go get ginkgo as well

Signed-off-by: Ettore Di Giacinto <edigiacinto@suse.com>

* tests: Add unit test for excludes

Signed-off-by: Ettore Di Giacinto <edigiacinto@suse.com>
  • Loading branch information
mudler committed Feb 21, 2022
1 parent 9f1859e commit 5e109c1
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 24 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ else
endif

test_deps:
go get github.com/onsi/ginkgo/v2
go get github.com/onsi/gomega/...
go get github.com/onsi/ginkgo/v2/ginkgo/internal@v2.1.3
go get github.com/onsi/ginkgo/v2/ginkgo/generators@v2.1.3
go get github.com/onsi/ginkgo/v2/ginkgo/labels@v2.1.3
go install github.com/onsi/ginkgo/v2/ginkgo

test: $(GINKGO)
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ require (
github.com/docker/docker v20.10.12+incompatible
github.com/docker/go-units v0.4.0
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-getter v1.5.11 // indirect
github.com/hashicorp/go-getter v1.5.11
github.com/hashicorp/go-multierror v1.1.1
github.com/ishidawataru/sctp v0.0.0-20210707070123-9a39160e9062 // indirect
github.com/itchyny/gojq v0.12.6 // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/joho/godotenv v1.4.0
github.com/kevinburke/ssh_config v1.1.0 // indirect
github.com/mudler/go-pluggable v0.0.0-20211206135551-9263b05c562e // indirect
github.com/mudler/go-pluggable v0.0.0-20211206135551-9263b05c562e
github.com/mudler/luet v0.0.0-20220127164253-6f77fa2b3a72
github.com/mudler/yip v0.0.0-20220127115532-dd6192ae26bf
github.com/onsi/ginkgo/v2 v2.1.3
Expand All @@ -34,7 +34,7 @@ require (
golang.org/x/crypto v0.0.0-20220126234351-aa10faf2a1f8 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
gopkg.in/ini.v1 v1.66.3 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
k8s.io/mount-utils v0.23.0
pault.ag/go/topsort v0.1.1 // indirect
)
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBp
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ=
github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/MarvinJWendt/testza v0.1.0/go.mod h1:7AxNvlfeHP7Z/hDQ5JtE3OKYT3XFUeLCDE2DQninSqs=
github.com/MarvinJWendt/testza v0.2.1/go.mod h1:God7bhG8n6uQxwdScay+gjm9/LnO4D3kkcZX4hv9Rp8=
Expand Down Expand Up @@ -113,6 +114,7 @@ github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/Sabayon/pkgs-checker v0.8.4 h1:63wYz7NqsEVWnq2tkSFKcI4MTd+1jog2blRDl878qiw=
github.com/Sabayon/pkgs-checker v0.8.4/go.mod h1:g0SbGeqXvwSdDMbERRYn6dnmyUG9+KlLMmaAyioAzvc=
github.com/Sereal/Sereal v0.0.0-20181211220259-509a78ddbda3 h1:Xu7z47ZiE/J+sKXHZMGxEor/oY2q6dq51fkO0JqdSwY=
github.com/Sereal/Sereal v0.0.0-20181211220259-509a78ddbda3/go.mod h1:D0JMgToj/WdxCgd30Kc1UcA9E+WdZoJqeVOuYW7iTBM=
github.com/Shopify/logrus-bugsnag v0.0.0-20170309145241-6dbc35f2c30d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d h1:UrqY+r/OJnIp5u0s1SbQ8dVfLCZJsnvazdBP5hS4iRs=
Expand Down Expand Up @@ -458,7 +460,6 @@ github.com/go-sql-driver/mysql v1.3.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/godbus/dbus v0.0.0-20151105175453-c7fdd8b5cd55/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
github.com/godbus/dbus v0.0.0-20180201030542-885f9cc04c9c/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
Expand Down Expand Up @@ -512,6 +513,7 @@ github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=
Expand All @@ -535,9 +537,11 @@ github.com/google/go-containerregistry v0.7.0 h1:u0onUUOcyoCDHEiJoyR1R1gx5er1+r0
github.com/google/go-containerregistry v0.7.0/go.mod h1:2zaoelrL0d08gGbpdP3LqyUuBmhWbpD6IOe2s9nLS2k=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.2.1 h1:d8MncMlErDFTwQGBK1xhv026j9kqhvw1Qv9IbWT1VLQ=
github.com/google/martian/v3 v3.2.1/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand All @@ -553,7 +557,6 @@ github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/renameio v1.0.0 h1:xhp2CnJmgQmpJU4RY8chagahUq5mbPPAbiSQstKpVMA=
Expand Down Expand Up @@ -868,8 +871,6 @@ github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo/v2 v2.0.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.0/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.1 h1:LCnPB85AvFNr91s0B2aDzEiiIg6MUwLYbryC1NSlWi8=
github.com/onsi/ginkgo/v2 v2.1.1/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.3 h1:e/3Cwtogj0HA+25nMP1jCMDIf8RtRYbGwGGuBIFztkc=
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
Expand Down Expand Up @@ -1138,6 +1139,7 @@ github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg=
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
github.com/vmihailenco/msgpack v4.0.1+incompatible h1:RMF1enSPeKTlXrXdOcqjFUElywVZjjC6pqse21bKbEU=
github.com/vmihailenco/msgpack v4.0.1+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/willdonnelly/passwd v0.0.0-20141013001024-7935dab3074c h1:4+NVyrLUuEmvE3r3Xst7gPuKhAP6X04ACpMmPvtK0M0=
github.com/willdonnelly/passwd v0.0.0-20141013001024-7935dab3074c/go.mod h1:xcvfY9pOw6s4wyrhilFSbMthL6KzgrfCIETHHUOQ/fQ=
Expand Down Expand Up @@ -1559,7 +1561,6 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
9 changes: 5 additions & 4 deletions pkg/elemental/elemental.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package elemental
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

cnst "github.com/rancher-sandbox/elemental/pkg/constants"
"github.com/rancher-sandbox/elemental/pkg/partitioner"
v1 "github.com/rancher-sandbox/elemental/pkg/types/v1"
"github.com/rancher-sandbox/elemental/pkg/utils"
"github.com/spf13/afero"
"os"
"path/filepath"
"strings"
)

// Elemental is the struct meant to self-contain most utils and actions related to Elemental, like installing or applying selinux
Expand Down Expand Up @@ -332,7 +333,7 @@ func (c *Elemental) CopyImage(img *v1.Image) error {
return err
}
} else if img.Source.IsDir {
excludes := []string{"mnt", "proc", "sys", "dev", "tmp"}
excludes := []string{"mnt", "proc", "sys", "dev", "tmp", "host", "run"}
err = utils.SyncData(img.Source.Source, img.MountPoint, excludes...)
if err != nil {
return err
Expand Down
18 changes: 12 additions & 6 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/joho/godotenv"
"github.com/rancher-sandbox/elemental/pkg/types/v1"
"github.com/spf13/afero"
"github.com/zloylos/grsync"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/joho/godotenv"
v1 "github.com/rancher-sandbox/elemental/pkg/types/v1"
"github.com/spf13/afero"
"github.com/zloylos/grsync"
)

// tmpBlockdevices is a temporal struct to extract the output of lsblk json
Expand Down Expand Up @@ -116,7 +117,7 @@ func CopyFile(fs afero.Fs, source string, target string) (err error) {

// Copies source file to target file using afero.Fs interface
func CreateDirStructure(fs afero.Fs, target string) error {
for _, dir := range []string{"sys", "proc", "dev", "tmp", "boot", "usr/local", "oem"} {
for _, dir := range []string{"run", "sys", "proc", "dev", "tmp", "boot", "usr/local", "oem"} {
err := fs.MkdirAll(fmt.Sprintf("%s/%s", target, dir), 0755)
if err != nil {
return err
Expand Down Expand Up @@ -148,7 +149,12 @@ func SyncData(source string, target string, excludes ...string) error {
},
)

return task.Run()
err := task.Run()
if err != nil {
return fmt.Errorf("%w: %s", err, strings.Join([]string{task.Log().Stderr, task.Log().Stdout}, "\n"))
}

return nil
}

// Reboot reboots the system afater the given delay (in seconds) time passed.
Expand Down
55 changes: 50 additions & 5 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rancher-sandbox/elemental/pkg/action"
conf "github.com/rancher-sandbox/elemental/pkg/config"
"github.com/rancher-sandbox/elemental/pkg/constants"
"github.com/rancher-sandbox/elemental/pkg/types/v1"
v1 "github.com/rancher-sandbox/elemental/pkg/types/v1"
"github.com/rancher-sandbox/elemental/pkg/utils"
v1mock "github.com/rancher-sandbox/elemental/tests/mocks"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"io/ioutil"
"os"
"testing"
"time"
)

func getNamesFromListFiles(list []os.FileInfo) []string {
Expand Down Expand Up @@ -366,13 +368,56 @@ var _ = Describe("Utils", Label("utils"), func() {
Expect(utils.SyncData(sourceDir, destDir)).To(BeNil())

filesDest, err := ioutil.ReadDir(destDir)
Expect(err).To(BeNil())

destNames := getNamesFromListFiles(filesDest)
filesSource, err := ioutil.ReadDir(sourceDir)
Expect(err).To(BeNil())

SourceNames := getNamesFromListFiles(filesSource)

// Should be the same files in both dirs now
Expect(destNames).To(Equal(SourceNames))
})

It("Copies all files from source to target respecting excludes", func() {
sourceDir, err := os.MkdirTemp("", "elemental")
Expect(err).To(BeNil())
defer os.RemoveAll(sourceDir)
destDir, err := os.MkdirTemp("", "elemental")
Expect(err).To(BeNil())
defer os.RemoveAll(destDir)

os.MkdirAll(filepath.Join(sourceDir, "host"), os.ModePerm)
os.MkdirAll(filepath.Join(sourceDir, "run"), os.ModePerm)
for i := 0; i < 5; i++ {
_, _ = os.CreateTemp(sourceDir, "file*")
}

Expect(utils.SyncData(sourceDir, destDir, "host", "run")).To(BeNil())

filesDest, err := ioutil.ReadDir(destDir)
Expect(err).To(BeNil())

destNames := getNamesFromListFiles(filesDest)

filesSource, err := ioutil.ReadDir(sourceDir)
Expect(err).To(BeNil())

SourceNames := getNamesFromListFiles(filesSource)

// Shouldn't be the same
Expect(destNames).ToNot(Equal(SourceNames))
expected := []string{}

for _, s := range SourceNames {
if s != "host" && s != "run" {
expected = append(expected, s)
}
}
Expect(destNames).To(Equal(expected))
})

It("should not fail if dirs are empty", func() {
sourceDir, err := os.MkdirTemp("", "elemental")
Expect(err).To(BeNil())
Expand Down

0 comments on commit 5e109c1

Please sign in to comment.