Skip to content

Commit

Permalink
Merge pull request #4 from fabi200123/unit-testing
Browse files Browse the repository at this point in the history
Adding unit-tests
  • Loading branch information
gabriel-samfira authored Apr 25, 2024
2 parents 9ac72e1 + d2de74d commit e2245c7
Show file tree
Hide file tree
Showing 33 changed files with 6,542 additions and 16 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/go-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Go Tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
go-tests-linux:
runs-on: ubuntu-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: 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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SHELL := bash

.PHONY: go-test

go-test:
go test -v ./... $(TEST_ARGS) -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 @@ -132,7 +132,7 @@ func (l *Incus) GetInstanceType() IncusImageType {
func (l *Incus) 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 @@ -156,16 +156,16 @@ func (l *Incus) 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/incus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package config

import (
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -93,7 +94,7 @@ func TestIncusWithInvalidUnixSocket(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: \"stat bogus unix socket: no such file or directory\"")
require.ErrorIs(t, err, os.ErrNotExist)
}

func TestMissingUnixSocketAndMissingURL(t *testing.T) {
Expand Down Expand Up @@ -144,14 +145,14 @@ func TestIncusIvalidCertOrKeyPaths(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: \"stat /i/am/not/here: no such file or directory\"")
require.ErrorIs(t, err, os.ErrNotExist)

cfg.ClientCertificate = "../testdata/incus/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: \"stat /me/neither: no such file or directory\"")
require.ErrorIs(t, err, os.ErrNotExist)
}

func TestIncusInvalidServerCertPath(t *testing.T) {
Expand All @@ -160,7 +161,7 @@ func TestIncusInvalidServerCertPath(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: \"stat /not/a/valid/server/cert/path: no such file or directory\"")
require.ErrorIs(t, err, os.ErrNotExist)
}

func TestInvalidIncusImageRemotes(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21.5
require (
github.com/BurntSushi/toml v1.2.1
github.com/cloudbase/garm-provider-common v0.1.2-0.20240111235646-a9efac12b060
github.com/gorilla/websocket v1.5.1
github.com/juju/clock v1.0.3
github.com/juju/retry v1.0.0
github.com/lxc/incus v0.3.0
Expand All @@ -21,7 +22,6 @@ require (
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/schema v1.2.1 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/juju/errors v1.0.0 // indirect
github.com/juju/loggo v1.0.0 // indirect
github.com/juju/testing v1.0.2 // indirect
Expand All @@ -34,6 +34,7 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand Down
5 changes: 2 additions & 3 deletions provider/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
"github.com/cloudbase/garm-provider-incus/config"

incus "github.com/lxc/incus/client"
"github.com/lxc/incus/shared/api"
"github.com/pkg/errors"
)
Expand All @@ -47,7 +46,7 @@ func (i *image) parseImageName(imageName string) (config.IncusImageRemote, strin
return config.IncusImageRemote{}, "", fmt.Errorf("could not find %s in %v: %w", imageName, i.remotes, runnerErrors.ErrNotFound)
}

func (i *image) getLocalImageByAlias(imageName string, imageType config.IncusImageType, arch string, cli incus.InstanceServer) (*api.Image, error) {
func (i *image) getLocalImageByAlias(imageName string, imageType config.IncusImageType, arch string, cli InstanceServerInterface) (*api.Image, error) {
aliases, err := cli.GetImageAliasArchitectures(imageType.String(), imageName)
if err != nil {
return nil, errors.Wrapf(err, "resolving alias: %s", imageName)
Expand All @@ -65,7 +64,7 @@ func (i *image) getLocalImageByAlias(imageName string, imageType config.IncusIma
return image, nil
}

func (i *image) getInstanceSource(imageName string, imageType config.IncusImageType, arch string, cli incus.InstanceServer) (api.InstanceSource, error) {
func (i *image) getInstanceSource(imageName string, imageType config.IncusImageType, arch string, cli InstanceServerInterface) (api.InstanceSource, error) {
instanceSource := api.InstanceSource{
Type: "image",
}
Expand Down
186 changes: 186 additions & 0 deletions provider/images_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023 Cloudbase Solutions SRL
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package provider

import (
"fmt"
"testing"

"github.com/cloudbase/garm-provider-incus/config"
"github.com/lxc/incus/shared/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParseImageNa(t *testing.T) {
tests := []struct {
name string
image *image
imageName string
expectedRemote config.IncusImageRemote
expectedImageName string
errString string
}{
{
name: "image with remote",
image: &image{
remotes: map[string]config.IncusImageRemote{
"remote1": {},
},
},
imageName: "remote1:image1",
expectedRemote: config.IncusImageRemote{},
expectedImageName: "image1",
errString: "",
},
{
name: "image without remote",
image: &image{
remotes: map[string]config.IncusImageRemote{
"remote1": {},
},
},
imageName: "image1",
expectedRemote: config.IncusImageRemote{},
expectedImageName: "",
errString: "image does not include a remote",
},
{
name: "image with invalid remote",
image: &image{
remotes: map[string]config.IncusImageRemote{
"remote1": {},
},
},
imageName: "invalid:image1",
expectedRemote: config.IncusImageRemote{},
expectedImageName: "",
errString: "could not find invalid:image1 in map",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
remote, imageName, err := tt.image.parseImageName(tt.imageName)
if tt.errString == "" {
require.NoError(t, err)
} else {
require.Error(t, err)
require.ErrorContains(t, err, tt.errString)
}
assert.Equal(t, tt.expectedRemote, remote)
assert.Equal(t, tt.expectedImageName, imageName)
})
}
}

func TestGetLocalImageByAlias_Success(t *testing.T) {
cli := new(MockIncusServer)
i := &image{
remotes: map[string]config.IncusImageRemote{
"remote1": {},
},
}
imageName := "image1"
imageType := config.IncusImageType("container")
arch := "amd64"
expectedImage := &api.Image{
Fingerprint: "fingerprint",
}
aliases := map[string]*api.ImageAliasesEntry{
"amd64": {},
}

cli.On("GetImageAliasArchitectures", "container", imageName).Return(aliases, nil)
cli.On("GetImage", aliases[arch].Target).Return(expectedImage, "", nil)

image, err := i.getLocalImageByAlias(imageName, imageType, arch, cli)
require.NoError(t, err)
assert.Equal(t, expectedImage, image)
}

func TestGetLocalImageByAlias_Error(t *testing.T) {
cli := new(MockIncusServer)
i := &image{
remotes: map[string]config.IncusImageRemote{
"remote1": {},
},
}
imageName := "image1"
imageType := config.IncusImageType("container")
arch := "amd64"
aliases := map[string]*api.ImageAliasesEntry{
"amd64": {},
}

cli.On("GetImageAliasArchitectures", "container", imageName).Return(aliases, fmt.Errorf("error"))

image, err := i.getLocalImageByAlias(imageName, imageType, arch, cli)
require.Error(t, err)
assert.Nil(t, image)
cli.AssertExpectations(t)
}

func TestGetInstanceSource_Success(t *testing.T) {
cli := new(MockIncusServer)
i := &image{
remotes: map[string]config.IncusImageRemote{
"remote1": {},
},
}
imageName := "image1"
imageType := config.IncusImageType("container")
arch := "amd64"
aliases := map[string]*api.ImageAliasesEntry{
"amd64": {},
}
expectedImage := &api.Image{
Fingerprint: "fingerprint",
}

cli.On("GetImageAliasArchitectures", "container", imageName).Return(aliases, nil)
cli.On("GetImage", aliases[arch].Target).Return(expectedImage, "", nil)

instanceSource, err := i.getInstanceSource(imageName, imageType, arch, cli)
require.NoError(t, err)
assert.Equal(t, api.InstanceSource{
Type: "image",
Fingerprint: "fingerprint",
}, instanceSource)
cli.AssertExpectations(t)
}

func TestGetInstanceSource_Error(t *testing.T) {
cli := new(MockIncusServer)
i := &image{
remotes: map[string]config.IncusImageRemote{
"remote1": {},
},
}
imageName := "image1"
imageType := config.IncusImageType("container")
arch := "amd64"
aliases := map[string]*api.ImageAliasesEntry{
"amd64": {},
}

cli.On("GetImageAliasArchitectures", "container", imageName).Return(aliases, fmt.Errorf("error"))

instanceSource, err := i.getInstanceSource(imageName, imageType, arch, cli)
require.Error(t, err)
assert.Equal(t, api.InstanceSource{}, instanceSource)
cli.AssertExpectations(t)
}
Loading

0 comments on commit e2245c7

Please sign in to comment.