Skip to content

Commit

Permalink
Adding abstract errors for config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
fabi200123 authored and gabriel-samfira committed Apr 19, 2024
1 parent c18a62d commit a0f67ce
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
21 changes: 19 additions & 2 deletions .github/workflows/go-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- main

jobs:
go-tests:
go-tests-linux:
runs-on: ubuntu-latest

steps:
Expand All @@ -24,4 +24,21 @@ jobs:
- run: go version

- name: Run GARM Go Tests
run: make go-test
run: make go-test

go-tests-windows:
runs-on: windows-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Golang
uses: actions/setup-go@v3
with:
go-version-file: go.mod

- run: go version

- name: Run GARM Go Tests
run: go test -v ./... -timeout=15m -parallel=4
8 changes: 4 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (l *LXD) GetInstanceType() LXDImageType {
func (l *LXD) Validate() error {
if l.UnixSocket != "" {
if _, err := os.Stat(l.UnixSocket); err != nil {
return fmt.Errorf("could not access unix socket %s: %q", l.UnixSocket, err)
return fmt.Errorf("could not access unix socket %s: %w", l.UnixSocket, err)
}

return nil
Expand All @@ -146,16 +146,16 @@ func (l *LXD) Validate() error {
}

if _, err := os.Stat(l.ClientCertificate); err != nil {
return fmt.Errorf("failed to access client certificate %s: %q", l.ClientCertificate, err)
return fmt.Errorf("failed to access client certificate %s: %w", l.ClientCertificate, err)
}

if _, err := os.Stat(l.ClientKey); err != nil {
return fmt.Errorf("failed to access client key %s: %q", l.ClientKey, err)
return fmt.Errorf("failed to access client key %s: %w", l.ClientKey, err)
}

if l.TLSServerCert != "" {
if _, err := os.Stat(l.TLSServerCert); err != nil {
return fmt.Errorf("failed to access tls_server_certificate %s: %q", l.TLSServerCert, err)
return fmt.Errorf("failed to access tls_server_certificate %s: %w", l.TLSServerCert, err)
}
}

Expand Down
9 changes: 5 additions & 4 deletions config/lxd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package config

import (
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -83,7 +84,7 @@ func TestLXDWithInvalidUnixSocket(t *testing.T) {
cfg.UnixSocket = "bogus unix socket"
err := cfg.Validate()
require.NotNil(t, err)
require.EqualError(t, err, "could not access unix socket bogus unix socket: \"CreateFile bogus unix socket: The system cannot find the file specified.\"")
require.ErrorIs(t, err, os.ErrNotExist)
}

func TestMissingUnixSocketAndMissingURL(t *testing.T) {
Expand Down Expand Up @@ -134,14 +135,14 @@ func TestLXDIvalidCertOrKeyPaths(t *testing.T) {
cfg.ClientCertificate = "/i/am/not/here"
err := cfg.Validate()
require.NotNil(t, err)
require.EqualError(t, err, "failed to access client certificate /i/am/not/here: \"CreateFile /i/am/not/here: The system cannot find the path specified.\"")
require.ErrorIs(t, err, os.ErrNotExist)

cfg.ClientCertificate = "../testdata/lxd/certs/client.crt"
cfg.ClientKey = "/me/neither"

err = cfg.Validate()
require.NotNil(t, err)
require.EqualError(t, err, "failed to access client key /me/neither: \"CreateFile /me/neither: The system cannot find the path specified.\"")
require.ErrorIs(t, err, os.ErrNotExist)
}

func TestLXDInvalidServerCertPath(t *testing.T) {
Expand All @@ -150,7 +151,7 @@ func TestLXDInvalidServerCertPath(t *testing.T) {

err := cfg.Validate()
require.NotNil(t, err)
require.EqualError(t, err, "failed to access tls_server_certificate /not/a/valid/server/cert/path: \"CreateFile /not/a/valid/server/cert/path: The system cannot find the path specified.\"")
require.ErrorIs(t, err, os.ErrNotExist)
}

func TestInvalidLXDImageRemotes(t *testing.T) {
Expand Down

0 comments on commit a0f67ce

Please sign in to comment.