diff --git a/macpine/README.md b/macpine/README.md new file mode 100644 index 0000000..1d1533b --- /dev/null +++ b/macpine/README.md @@ -0,0 +1,16 @@ +# Use `macpine` to spin up bravetools testing environment on Mac + +```bash +alpine launch --image alpine_3.16.0_lxd --name bravetools --mount $PWD +alpine exec bravetools "ash /root/mnt/macpine/bootstrap.sh" +``` + +## Unit Testing + +Run Unit tests directly on the VM + +```bash +alpine ssh bravetools +cd mnt +go test -v ./... +``` \ No newline at end of file diff --git a/macpine/bootstrap.sh b/macpine/bootstrap.sh new file mode 100644 index 0000000..7b8ca3f --- /dev/null +++ b/macpine/bootstrap.sh @@ -0,0 +1,21 @@ +set -e + +cat > /etc/apk/repositories << EOF; $(echo) + +https://dl-cdn.alpinelinux.org/alpine/v$(cat /etc/alpine-release | cut -d'.' -f1,2)/main/ +https://dl-cdn.alpinelinux.org/alpine/v$(cat /etc/alpine-release | cut -d'.' -f1,2)/community/ +https://dl-cdn.alpinelinux.org/alpine/edge/testing/ + +EOF +apk add --update --no-cache go make musl-dev curl linux-headers sudo + +export GOPATH=/root/go +export PATH=${GOPATH}/bin:/usr/local/go/bin:$PATH +export GOBIN=$GOROOT/bin +mkdir -p ${GOPATH}/src ${GOPATH}/bin +export GO111MODULE=on + +go version + +cd /mnt ; make linux; cd ../ +brave init diff --git a/platform/host_api_test.go b/platform/host_api_test.go new file mode 100644 index 0000000..41dd679 --- /dev/null +++ b/platform/host_api_test.go @@ -0,0 +1,164 @@ +package platform + +import ( + "testing" + + "github.com/bravetools/bravetools/shared" +) + +func Test_DeleteLocalImage(t *testing.T) { + host := *NewBraveHost() + + bravefile, err := shared.GetBravefileFromLXD("alpine/edge/amd64") + if err != nil { + t.Error("shared.GetBravefileFromLXD: ", err) + } + + err = host.BuildImage(bravefile) + if err != nil { + t.Error("host.BuildImage: ", err) + } + + err = host.DeleteLocalImage("brave-base-alpine-edge-1.0") + if err != nil { + t.Error("host.DeleteImageByName: ", err) + } + +} + +func Test_HostInfo(t *testing.T) { + host := *NewBraveHost() + backend := NewLxd(host.Settings) + + err := host.HostInfo(backend, false) + if err != nil { + t.Error("host.HostInfo: ", err) + } +} + +func Test_BuildImage(t *testing.T) { + host := *NewBraveHost() + + bravefile := *shared.NewBravefile() + bravefile.Base.Image = "alpine/edge/amd64" + bravefile.Base.Location = "public" + + bravefile.SystemPackages.Manager = "apk" + bravefile.SystemPackages.System = []string{"htop", "make"} + + runCommand := &shared.RunCommand{} + runCommand.Command = "echo" + runCommand.Args = []string{"Hello World"} + + bravefile.Run = []shared.RunCommand{*runCommand} + + bravefile.PlatformService.Name = "alpine-test" + bravefile.PlatformService.Version = "1.0" + + err := host.BuildImage(&bravefile) + if err != nil { + t.Error("host.BuildImage: ", err) + } + + err = host.DeleteLocalImage("alpine-test-1.0") + if err != nil { + t.Error("host.DeleteImageByName: ", err) + } +} + +func Test_InitUnit(t *testing.T) { + host := *NewBraveHost() + backend := NewLxd(host.Settings) + + bravefile := *shared.NewBravefile() + bravefile.Base.Image = "alpine/edge/amd64" + bravefile.Base.Location = "public" + + bravefile.SystemPackages.Manager = "apk" + bravefile.SystemPackages.System = []string{"htop", "make"} + + runCommand := &shared.RunCommand{} + runCommand.Command = "echo" + runCommand.Args = []string{"Hello World"} + + bravefile.Run = []shared.RunCommand{*runCommand} + + bravefile.PlatformService.Name = "alpine-test" + bravefile.PlatformService.Version = "1.0" + bravefile.PlatformService.Image = "alpine-test-1.0" + + bravefile.PlatformService.Resources.CPU = "1" + bravefile.PlatformService.Resources.RAM = "1GB" + + bravefile.PlatformService.Postdeploy.Run = []shared.RunCommand{ + { + Command: "echo", + Args: []string{"Hello World"}, + }, + } + + err := host.BuildImage(&bravefile) + if err != nil { + t.Error("host.BuildImage: ", err) + } + + err = host.InitUnit(backend, &bravefile) + if err != nil { + t.Error("host.InitUnit: ", err) + } + + err = host.Postdeploy(&bravefile) + if err != nil { + t.Error("host.Postdeploy: ", err) + } + + err = host.DeleteLocalImage("alpine-test-1.0") + if err != nil { + t.Error("host.DeleteImageByName: ", err) + } + + err = host.StopUnit("alpine-test", backend) + if err != nil { + t.Error("host.StopUnit: ", err) + } + + err = host.StartUnit("alpine-test", backend) + if err != nil { + t.Error("host.StartUnit: ", err) + } + + err = host.DeleteUnit("alpine-test") + if err != nil { + t.Error("host.DeleteUnit: ", err) + } +} + +func Test_ListLocalImages(t *testing.T) { + host := *NewBraveHost() + backend := NewLxd(host.Settings) + + err := host.HostInfo(backend, false) + if err != nil { + t.Error("host.HostInfo: ", err) + } + + err = host.ListLocalImages() + if err != nil { + t.Error("host.ListLocalImages: ", err) + } +} + +func Test_ListUnits(t *testing.T) { + host := *NewBraveHost() + backend := NewLxd(host.Settings) + + err := host.HostInfo(backend, false) + if err != nil { + t.Error("host.HostInfo: ", err) + } + + err = host.ListUnits(backend) + if err != nil { + t.Error("host.ListLocalImages: ", err) + } +} diff --git a/test/e2e.sh b/test/e2e.sh index f5887b8..2f47075 100644 --- a/test/e2e.sh +++ b/test/e2e.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -e echo ">> Installing Bravetools" cd ..