Skip to content

Commit

Permalink
Run tests in parallel and output github formats on workflow (#543)
Browse files Browse the repository at this point in the history
* Run tests in parallel and output github formats on workflow

Signed-off-by: Itxaka <[email protected]>

* Fix broken parallel tests

We were using a fixed file for the tests which several tests could be
accessing at the same time.

This fixes it by setting a temp random file at the test start

Signed-off-by: Itxaka <[email protected]>

* Fix the tests logging to stdout

Signed-off-by: Itxaka <[email protected]>

* Drop the verbose

Signed-off-by: Itxaka <[email protected]>

* Fix agent test

Signed-off-by: Itxaka <[email protected]>

* Let the event consumer create the logfile or whatever

Signed-off-by: Itxaka <[email protected]>

* Drop Focus

Signed-off-by: Itxaka <[email protected]>

---------

Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka authored Sep 17, 2024
1 parent 3e99d75 commit fbb64f2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 45 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ jobs:
unit-tests:
strategy:
matrix:
go-version: [ "1.23-bookworm" ]
go-version: [ "1.23" ]
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install earthly
uses: Luet-lab/luet-install-action@v1
- name: Setup Go environment
uses: actions/[email protected]
with:
repository: quay.io/kairos/packages
packages: utils/earthly
go-version: '${{ matrix.go-version }}'
- name: Run tests
run: |
earthly -P +test --GO_VERSION=${{ matrix.go-version }}
go run github.com/onsi/ginkgo/v2/ginkgo run -p --github-output --covermode=atomic --coverprofile=coverage.out --race -r ./...
- name: Codecov
uses: codecov/codecov-action@v4
env:
Expand Down
2 changes: 1 addition & 1 deletion Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test:
ARG TEST_PATHS=./...
ARG LABEL_FILTER=
ENV CGO_ENABLED=1
RUN go run github.com/onsi/ginkgo/v2/ginkgo run --label-filter "$LABEL_FILTER" --covermode=atomic --coverprofile=coverage.out -v --race -r $TEST_PATHS
RUN go run github.com/onsi/ginkgo/v2/ginkgo run -p --label-filter "$LABEL_FILTER" --covermode=atomic --coverprofile=coverage.out --race -r $TEST_PATHS
SAVE ARTIFACT coverage.out AS LOCAL coverage.out

version:
Expand Down
20 changes: 4 additions & 16 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package agent

import (
"fmt"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
"os"
"path/filepath"

hook "github.com/kairos-io/kairos-agent/v2/internal/agent/hooks"
"github.com/kairos-io/kairos-agent/v2/internal/bus"
config "github.com/kairos-io/kairos-agent/v2/pkg/config"
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1"
events "github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/collector"
"github.com/kairos-io/kairos-sdk/machine"
"github.com/kairos-io/kairos-sdk/utils"
"os"
)

// Run starts the agent provider emitting the bootstrap event.
Expand All @@ -38,18 +36,8 @@ func Run(opts ...Option) error {
return nil
}

os.MkdirAll("/var/log/kairos", 0600) //nolint:errcheck

fileName := filepath.Join("/var/log/kairos", "agent-provider.log")

// Create if not exist
if _, err := os.Stat(fileName); err != nil {
err = os.WriteFile(fileName, []byte{}, os.ModePerm)
if err != nil {
return err
}
}

fileName := "/var/log/kairos/agent-provider.log"

if !machine.SentinelExist("firstboot") {
spec := v1.EmptySpec{}
if err := hook.Run(*c, &spec, hook.FirstBoot...); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var _ = Describe("Bootstrap provider", func() {
defer os.RemoveAll(f)

wd, _ := os.Getwd()
os.WriteFile(filepath.Join(wd, "agent-provider-test"), []byte(testProvider), 0655)
os.WriteFile(filepath.Join(wd, "agent-provider-test"), []byte(testProvider), 0777)

defer os.RemoveAll(filepath.Join(wd, "agent-provider-test"))

Expand Down
7 changes: 5 additions & 2 deletions pkg/action/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@ var _ = Describe("Install action tests", func() {
})

Describe("Install Action", Label("install"), func() {
var device, cmdFail string
var device, cmdFail, tmpdir string
var err error
var spec *v1.InstallSpec
var installer *action.InstallAction

BeforeEach(func() {
device = "/tmp/test.img"
tmpdir, err = os.MkdirTemp("", "install-*")
Expect(err).Should(BeNil())
device = filepath.Join(tmpdir, "test.img")
Expect(os.RemoveAll(device)).Should(Succeed())
// at least 2Gb in size as state is set to 1G
_, err = diskfs.Create(device, 2*1024*1024*1024, diskfs.Raw, 512)
Expand Down Expand Up @@ -201,6 +203,7 @@ var _ = Describe("Install action tests", func() {
}
Expect(os.RemoveAll(device)).ToNot(HaveOccurred())
ghwTest.Clean()
Expect(os.RemoveAll(tmpdir)).ToNot(HaveOccurred())
})

It("Successfully installs", func() {
Expand Down
27 changes: 15 additions & 12 deletions pkg/config/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,19 @@ var _ = Describe("Types", Label("types", "config"), func() {
var logger sdkTypes.KairosLogger
var ci *v1mock.FakeCloudInitRunner
var c *config.Config
var memLog bytes.Buffer

BeforeEach(func() {
memLog = bytes.Buffer{}
logger = sdkTypes.NewBufferLogger(&memLog)
logger.SetLevel("debug")

fs, cleanup, err = vfst.NewTestFS(nil)
Expect(err).ToNot(HaveOccurred())
mounter = v1mock.NewErrorMounter()
runner = v1mock.NewFakeRunner()
client = &v1mock.FakeHTTPClient{}
sysc = &v1mock.FakeSyscall{}
logger = sdkTypes.NewNullLogger()
ci = &v1mock.FakeCloudInitRunner{}
c = config.NewConfig(
config.WithFs(fs),
Expand Down Expand Up @@ -535,10 +540,11 @@ cloud-init-paths:
ghwTest.Clean()
})
It("Reads properly the cloud config for install", func() {
cfg, err := config.Scan(collector.Directories([]string{dir}...),
cfg, err := config.ScanNoLogs(collector.Directories([]string{dir}...),
collector.NoLogs,
)
cfg.Fs = fs
cfg.Logger = logger

Expect(err).ToNot(HaveOccurred())
// Once we got the cfg override the fs to our test fs
Expand All @@ -559,13 +565,14 @@ cloud-init-paths:
})
It("Reads properly the cloud config for reset", func() {
bootedFrom = constants.SystemLabel
cfg, err := config.Scan(collector.Directories([]string{dir}...), collector.NoLogs)
cfg, err := config.ScanNoLogs(collector.Directories([]string{dir}...), collector.NoLogs)
Expect(err).ToNot(HaveOccurred())
// Override the config with our test params
cfg.Runner = runner
cfg.Fs = fs
cfg.Mounter = mounter
cfg.CloudInitRunner = ci
cfg.Logger = logger
spec, err := config.ReadSpecFromCloudConfig(cfg, "reset")
Expect(err).ToNot(HaveOccurred())
resetSpec := spec.(*v1.ResetSpec)
Expand All @@ -574,37 +581,33 @@ cloud-init-paths:
Expect(resetSpec.Passive.Label).To(Equal("MY_LABEL"))
})
It("Reads properly the cloud config for upgrade", func() {
cfg, err := config.Scan(collector.Directories([]string{dir}...), collector.NoLogs)
cfg, err := config.ScanNoLogs(collector.Directories([]string{dir}...), collector.NoLogs)
Expect(err).ToNot(HaveOccurred())
// Override the config with our test params
cfg.Runner = runner
cfg.Fs = fs
cfg.Mounter = mounter
cfg.CloudInitRunner = ci
cfg.Logger = logger
spec, err := config.ReadSpecFromCloudConfig(cfg, "upgrade")
Expect(err).ToNot(HaveOccurred())
upgradeSpec := spec.(*v1.UpgradeSpec)
Expect(upgradeSpec.RecoveryUpgrade()).To(BeTrue())
})
It("Fails when a wrong action is read", func() {
cfg, err := config.Scan(collector.Directories([]string{dir}...), collector.NoLogs)
cfg, err := config.ScanNoLogs(collector.Directories([]string{dir}...), collector.NoLogs)
cfg.Logger = logger
Expect(err).ToNot(HaveOccurred())
_, err = config.ReadSpecFromCloudConfig(cfg, "nope")
Expect(err).To(HaveOccurred())
})
It("Sets info level if its not on the cloud-config", func() {
// Now again but with no config
cfg, err := config.Scan(collector.Directories([]string{""}...), collector.NoLogs)
Expect(err).ToNot(HaveOccurred())
Expect(cfg.Logger.GetLevel()).To(Equal(zerolog.InfoLevel))
})
It("Sets debug level if its on the cloud-config", func() {
ccdata := []byte(`#cloud-config
debug: true
`)
err = os.WriteFile(filepath.Join(dir, "cc.yaml"), ccdata, os.ModePerm)
Expect(err).ToNot(HaveOccurred())
cfg, err := config.Scan(collector.Directories([]string{dir}...), collector.NoLogs)
cfg, err := config.ScanNoLogs(collector.Directories([]string{dir}...), collector.NoLogs)
Expect(err).ToNot(HaveOccurred())
Expect(cfg.Logger.GetLevel()).To(Equal(zerolog.DebugLevel))

Expand Down
17 changes: 10 additions & 7 deletions pkg/elemental/elemental_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,31 +355,34 @@ var _ = Describe("Elemental", Label("elemental"), func() {
var install *v1.InstallSpec
var err error
var el *elemental.Elemental
var tmpDir string

BeforeEach(func() {
cInit = &v1mock.FakeCloudInitRunner{ExecStages: []string{}, Error: false}
config.CloudInitRunner = cInit
Expect(os.RemoveAll("/tmp/test.img")).ToNot(HaveOccurred())
tmpDir, err = os.MkdirTemp("", "elements-*")
Expect(err).To(BeNil())
Expect(os.RemoveAll(filepath.Join(tmpDir, "/test.img"))).ToNot(HaveOccurred())
// at least 2Gb in size as state is set to 1G
_, err = diskfs.Create("/tmp/test.img", 2*1024*1024*1024, diskfs.Raw, 512)
_, err = diskfs.Create(filepath.Join(tmpDir, "/test.img"), 2*1024*1024*1024, diskfs.Raw, 512)
Expect(err).ToNot(HaveOccurred())
config.Install.Device = "/tmp/test.img"
config.Install.Device = filepath.Join(tmpDir, "/test.img")
install, err = agentConfig.NewInstallSpec(config)
Expect(err).ToNot(HaveOccurred())
install.Target = "/tmp/test.img"
install.Target = filepath.Join(tmpDir, "/test.img")
el = elemental.NewElemental(config)
})

AfterEach(func() {
Expect(os.RemoveAll("/tmp/test.img")).ToNot(HaveOccurred())
Expect(os.RemoveAll(tmpDir)).ToNot(HaveOccurred())
})

It("Successfully creates partitions and formats them, EFI boot", func() {
install.PartTable = v1.GPT
install.Firmware = v1.EFI
Expect(install.Partitions.SetFirmwarePartitions(v1.EFI, v1.GPT)).To(BeNil())
Expect(el.PartitionAndFormatDevice(install)).To(BeNil())
disk, err := diskfs.Open("/tmp/test.img", diskfs.WithOpenMode(diskfs.ReadOnly))
disk, err := diskfs.Open(filepath.Join(tmpDir, "/test.img"), diskfs.WithOpenMode(diskfs.ReadOnly))
defer disk.Close()
Expect(err).ToNot(HaveOccurred())
// check that its type GPT
Expand Down Expand Up @@ -412,7 +415,7 @@ var _ = Describe("Elemental", Label("elemental"), func() {
install.Firmware = v1.BIOS
Expect(install.Partitions.SetFirmwarePartitions(v1.BIOS, v1.GPT)).To(BeNil())
Expect(el.PartitionAndFormatDevice(install)).To(BeNil())
disk, err := diskfs.Open("/tmp/test.img", diskfs.WithOpenMode(diskfs.ReadOnly))
disk, err := diskfs.Open(filepath.Join(tmpDir, "/test.img"), diskfs.WithOpenMode(diskfs.ReadOnly))
defer disk.Close()
Expect(err).ToNot(HaveOccurred())
// check that its type GPT
Expand Down

0 comments on commit fbb64f2

Please sign in to comment.