Skip to content

Commit 302ec4a

Browse files
authored
Add some test coverage (#4)
1 parent e74550d commit 302ec4a

29 files changed

+1284
-179
lines changed

.github/workflows/codeql.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [ "main" ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ "main" ]
20+
schedule:
21+
- cron: '18 2 * * 2'
22+
23+
jobs:
24+
analyze:
25+
name: Analyze
26+
runs-on: ubuntu-latest
27+
permissions:
28+
actions: read
29+
contents: read
30+
security-events: write
31+
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
language: [ 'go' ]
36+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
37+
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
38+
39+
steps:
40+
- name: Checkout repository
41+
uses: actions/checkout@v3
42+
43+
# Initializes the CodeQL tools for scanning.
44+
- name: Initialize CodeQL
45+
uses: github/codeql-action/init@v2
46+
with:
47+
languages: ${{ matrix.language }}
48+
# If you wish to specify custom queries, you can do so here or in a config file.
49+
# By default, queries listed here will override any specified in a config file.
50+
# Prefix the list here with "+" to use these queries and those in the config file.
51+
52+
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
53+
# queries: security-extended,security-and-quality
54+
55+
56+
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).
57+
# If this step fails, then you should remove it and run the build manually (see below)
58+
- name: Autobuild
59+
uses: github/codeql-action/autobuild@v2
60+
61+
# ℹ️ Command-line programs to run using the OS shell.
62+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
63+
64+
# If the Autobuild fails above, remove it and uncomment the following three lines.
65+
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
66+
67+
# - run: |
68+
# echo "Run, Build Application using script"
69+
# ./location_of_script_within_repo/buildscript.sh
70+
71+
- name: Perform CodeQL Analysis
72+
uses: github/codeql-action/analyze@v2
73+
with:
74+
category: "/language:${{matrix.language}}"

.github/workflows/unit-tests.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Unit tests
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- main
7+
concurrency:
8+
group: tests-${{ github.head_ref || github.ref }}-${{ github.repository }}
9+
cancel-in-progress: true
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Install Go
16+
uses: actions/setup-go@v3
17+
with:
18+
go-version-file: go.mod
19+
- name: Run tests
20+
run: make test
21+
- name: Codecov
22+
uses: codecov/codecov-action@v3
23+
with:
24+
file: ./coverage.out

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.20 as builder
2+
FROM golang:1.21 as builder
33
ARG TARGETOS
44
ARG TARGETARCH
55

Dockerfile.agent

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.20 as builder
2+
FROM golang:1.21 as builder
33
ARG TARGETOS
44
ARG TARGETARCH
55

@@ -35,7 +35,6 @@ RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build \
3535
FROM opensuse/tumbleweed:latest
3636

3737
RUN zypper install -y systemd
38-
#RUN zypper install -y fuse-overlayfs
3938

4039
COPY --from=builder /workspace/agent /usr/local/sbin/elemental-agent
4140
COPY test/test-agent-config.yaml /etc/elemental/agent/config.yaml

Makefile

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
GIT_COMMIT?=$(shell git rev-parse HEAD)
22
GIT_COMMIT_SHORT?=$(shell git rev-parse --short HEAD)
33
GIT_TAG?=$(shell git describe --abbrev=0 --tags 2>/dev/null || echo "v0.0.0" )
4+
GIT_COMMIT_DATE?=$(shell git show -s --format='%cI' HEAD)
45
# Image URL to use all building/pushing image targets
56
IMG_NAME ?= ghcr.io/rancher-sandbox/cluster-api-provider-elemental
67
IMG_TAG ?= latest
@@ -29,7 +30,17 @@ SHELL = /usr/bin/env bash -o pipefail
2930
LDFLAGS := -w -s
3031
LDFLAGS += -X "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/version.Version=${GIT_TAG}"
3132
LDFLAGS += -X "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/version.Commit=${GIT_COMMIT}"
32-
LDFLAGS += -X "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/version.CommitDate=${COMMITDATE}"
33+
LDFLAGS += -X "github.com/rancher-sandbox/cluster-api-provider-elemental/internal/version.CommitDate=${GIT_COMMIT_DATE}"
34+
35+
ABS_TOOLS_DIR := $(abspath bin/)
36+
GO_INSTALL := ./test/scripts/go_install.sh
37+
38+
GINKGO_VER := v2.13.0
39+
GINKGO := $(ABS_TOOLS_DIR)/ginkgo-$(GINKGO_VER)
40+
GINKGO_PKG := github.com/onsi/ginkgo/v2/ginkgo
41+
42+
$(GINKGO):
43+
GOBIN=$(ABS_TOOLS_DIR) $(GO_INSTALL) $(GINKGO_PKG) ginkgo $(GINKGO_VER)
3344

3445
.PHONY: all
3546
all: build-provider
@@ -60,6 +71,7 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust
6071
.PHONY: generate
6172
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
6273
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
74+
./test/scripts/generate_mocks.sh
6375

6476
.PHONY: openapi
6577
openapi: ## Generate Elemental OpenAPI specs
@@ -74,8 +86,8 @@ vet: ## Run go vet against code.
7486
go vet ./...
7587

7688
.PHONY: test
77-
test: manifests generate fmt vet envtest ## Run tests.
78-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
89+
test: manifests generate fmt vet envtest $(GINKGO) ## Run tests.
90+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" $(GINKGO) -v -r --trace --race --covermode=atomic --coverprofile=coverage.out --coverpkg=github.com/rancher-sandbox/cluster-api-provider-elemental/... ./internal/... ./cmd/...
7991

8092
##@ Build
8193

@@ -104,15 +116,15 @@ docker-build: test ## Build docker image with the manager.
104116
$(CONTAINER_TOOL) build \
105117
--build-arg "TAG=${GIT_TAG}" \
106118
--build-arg "COMMIT=${GIT_COMMIT}" \
107-
--build-arg "COMMITDATE=${COMMITDATE}" \
119+
--build-arg "COMMITDATE=${GIT_COMMIT_DATE}" \
108120
-t ${IMG} .
109121

110122
.PHONY: docker-build-agent
111123
docker-build-agent: test ## Build docker image with the manager.
112124
$(CONTAINER_TOOL) build \
113125
--build-arg "TAG=${GIT_TAG}" \
114126
--build-arg "COMMIT=${GIT_COMMIT}" \
115-
--build-arg "COMMITDATE=${COMMITDATE}" \
127+
--build-arg "COMMITDATE=${GIT_COMMIT_DATE}" \
116128
-t agent:latest -f Dockerfile.agent .
117129

118130
.PHONY: docker-push

api/v1beta1/elementalregistration_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type ElementalRegistrationSpec struct {
3333
HostAnnotations map[string]string `json:"hostAnnotations,omitempty"`
3434
// Config points to Elemental machine configuration.
3535
// +optional
36-
Config *Config `json:"config,omitempty"`
36+
Config Config `json:"config,omitempty"`
3737
}
3838

3939
// ElementalRegistrationStatus defines the observed state of ElementalRegistration.

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/agent/main.go

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
const (
2525
configPathDefault = "/etc/elemental/agent/config.yaml"
2626
bootstrapSentinelFile = "/run/cluster-api/bootstrap-success.complete"
27-
installerUnmanaged = "unmanaged"
28-
installerElemental = "elemental"
2927
)
3028

3129
// Flags.
@@ -41,31 +39,37 @@ var (
4139
configPath string
4240
)
4341

44-
// Errors.
4542
var (
46-
ErrUnknownInstaller = errors.New("unknown installer")
43+
ErrIncorrectArguments = errors.New("incorrect arguments, run 'elemental-agent --help' for usage")
4744
)
4845

4946
func main() {
5047
fs := vfs.OSFS
51-
cmd := newCommand(fs)
48+
installerSelector := host.NewInstallerSelector()
49+
hostnameManager := hostname.NewManager()
50+
client := client.NewClient()
51+
cmd := newCommand(fs, installerSelector, hostnameManager, client)
5252
if err := cmd.Execute(); err != nil {
5353
log.Error(err, "running elemental-agent")
5454
os.Exit(1)
5555
}
5656
}
5757

58-
func newCommand(fs vfs.FS) *cobra.Command {
58+
func newCommand(fs vfs.FS, installerSelector host.InstallerSelector, hostnameManager hostname.Manager, client client.Client) *cobra.Command {
5959
cmd := &cobra.Command{
6060
Use: "elemental-agent",
6161
Short: "Elemental Agent command",
6262
Long: "elemental-agent registers a node with the elemental-operator via a config file",
6363
RunE: func(_ *cobra.Command, args []string) error {
6464
// Display version
6565
if versionFlag {
66-
log.Infof("Register version %s, commit %s, commit date %s", version.Version, version.Commit, version.CommitDate)
66+
log.Infof("Agent version %s, commit %s, commit date %s", version.Version, version.Commit, version.CommitDate)
6767
return nil
6868
}
69+
// Sanity checks
70+
if installFlag && resetFlag {
71+
return fmt.Errorf("--install and --reset are mutually exclusive: %w", ErrIncorrectArguments)
72+
}
6973
// Parse config file
7074
conf, err := getConfig(fs)
7175
if err != nil {
@@ -76,43 +80,31 @@ func newCommand(fs vfs.FS) *cobra.Command {
7680
log.EnableDebug()
7781
log.Debug("Debug logging enabled")
7882
}
79-
// Sanity checks
80-
if installFlag && resetFlag {
81-
log.Info("--install and --reset are mutually exclusive")
82-
return nil
83-
}
83+
8484
// Initialize WorkDir
8585
if err := utils.CreateDirectory(fs, conf.Agent.WorkDir); err != nil {
8686
return fmt.Errorf("creating work directory '%s': %w", conf.Agent.WorkDir, err)
8787
}
8888
// Initialize Elemental API Client
89-
client, err := client.NewClient(fs, conf)
90-
if err != nil {
89+
if err := client.Init(fs, conf); err != nil {
9190
return fmt.Errorf("initializing Elemental API client: %w", err)
9291
}
9392
// Get current hostname
94-
currentHostname, err := hostname.GetCurrentHostname()
93+
currentHostname, err := hostnameManager.GetCurrentHostname()
9594
if err != nil {
9695
return fmt.Errorf("getting current hostname: %w", err)
9796
}
9897
// Initialize installer
9998
log.Info("Initializing Installer")
100-
var installer host.Installer
101-
switch conf.Agent.Installer {
102-
case installerUnmanaged:
103-
log.Info("Using Unmanaged OS Installer")
104-
installer = host.NewUnmanagedInstaller(fs, configPath, conf.Agent.WorkDir)
105-
case installerElemental:
106-
log.Info("Using Elemental Installer")
107-
installer = host.NewElementalInstaller(fs)
108-
default:
109-
return fmt.Errorf("parsing installer '%s': %w", conf.Agent.Installer, ErrUnknownInstaller)
99+
installer, err := installerSelector.GetInstaller(fs, configPath, conf)
100+
if err != nil {
101+
return fmt.Errorf("initializing installer: %w", err)
110102
}
111103

112104
// Install
113105
if installFlag {
114106
log.Info("Installing Elemental")
115-
handleInstall(client, installer, conf.Agent.Reconciliation)
107+
handleInstall(client, hostnameManager, installer, conf.Agent.Reconciliation)
116108
log.Info("Installation successful")
117109
return nil
118110
}
@@ -194,7 +186,7 @@ func getConfig(fs vfs.FS) (config.Config, error) {
194186
// This could introduce a new --register flag, leaving the --install as optional (for unmanaged OS for example).
195187
// However, consider that setting the hostname must be part of the registration workflow,
196188
// so maybe decoupling would not be possible without a state/cache file where to store the hostname-to-be-set.
197-
func handleInstall(client client.Client, installer host.Installer, installationRecoveryPeriod time.Duration) {
189+
func handleInstall(client client.Client, hostnameManager hostname.Manager, installer host.Installer, installationRecoveryPeriod time.Duration) {
198190
alreadyRegistered := false
199191
installationError := false
200192
var newHostname string
@@ -214,7 +206,7 @@ func handleInstall(client client.Client, installer host.Installer, installationR
214206
}
215207
// Pick the new hostname if not done yet
216208
if len(newHostname) == 0 {
217-
newHostname, err = hostname.PickHostname(registration.Config.Elemental.Agent.Hostname)
209+
newHostname, err = hostnameManager.PickHostname(registration.Config.Elemental.Agent.Hostname)
218210
log.Debugf("Selected hostname: %s", newHostname)
219211
if err != nil {
220212
log.Error(err, "picking new hostname")

0 commit comments

Comments
 (0)