Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabi200123 committed Mar 6, 2024
1 parent 9733b4a commit e076f98
Show file tree
Hide file tree
Showing 37 changed files with 12,604 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/go-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Go Tests

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

jobs:
go-tests:
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
129 changes: 129 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package config

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestConfig_Validate(t *testing.T) {
tests := []struct {
name string
config *Config
errString error
}{
{
name: "ValidConfig",
config: &Config{
Zone: "europe-west1-d",
ProjectId: "my-project",
NetworkID: "my-network",
SubnetworkID: "my-subnetwork",
CredentialsFile: "path/to/credentials.json",
ExternalIPAccess: true,
},
errString: nil,
},
{
name: "MissingRegion",
config: &Config{
ProjectId: "my-project",
NetworkID: "my-network",
SubnetworkID: "my-subnetwork",
CredentialsFile: "path/to/credentials.json",
ExternalIPAccess: true,
},
errString: fmt.Errorf("missing region"),
},
{
name: "MissingProjectID",
config: &Config{
Zone: "europe-west1-d",
NetworkID: "my-network",
SubnetworkID: "my-subnetwork",
CredentialsFile: "path/to/credentials.json",
},
errString: fmt.Errorf("missing project_id"),
},
{
name: "MissingNetworkID",
config: &Config{
Zone: "europe-west1-d",
ProjectId: "my-project",
SubnetworkID: "my-subnetwork",
CredentialsFile: "path/to/credentials.json",
ExternalIPAccess: true,
},
errString: fmt.Errorf("missing network_id"),
},
{
name: "MissingSubnetworkID",
config: &Config{
Zone: "europe-west1-d",
ProjectId: "my-project",
NetworkID: "my-network",
CredentialsFile: "path/to/credentials.json",
ExternalIPAccess: true,
},
errString: fmt.Errorf("missing subnetwork_id"),
},
{
name: "MissingCredentialsFile",
config: &Config{
Zone: "europe-west1-d",
ProjectId: "my-project",
NetworkID: "my-network",
SubnetworkID: "my-subnetwork",
ExternalIPAccess: true,
},
errString: fmt.Errorf("missing credentials_file"),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := tc.config.Validate()
if tc.errString == nil {
require.Nil(t, err)
} else {
require.Equal(t, tc.errString, err)
}

})
}
}

func TestNewConfig(t *testing.T) {
mockData := `
project_id = "garm-testing"
zone = "europe-west1-d"
network_id = "projects/garm-testing/global/networks/garm"
subnetwork_id = "projects/garm-testing/regions/europe-west1/subnetworks/garm"
credentials_file = "/home/ubuntu/service-account-key.json"
external_ip_access = true
`
// Create a temporary file
tmpFile, err := os.CreateTemp("", "config-*.toml")
require.NoError(t, err, "Failed to create temporary file")
defer os.Remove(tmpFile.Name()) // Clean up after the test

// Write the mock TOML data to the temporary file
_, err = tmpFile.WriteString(mockData)
require.NoError(t, err, "Failed to write to temporary file")
err = tmpFile.Close()
require.NoError(t, err, "Failed to close temporary file")

// Use the temporary file path as the argument to NewConfig
cfg, err := NewConfig(tmpFile.Name())
require.NoError(t, err, "NewConfig returned an error")

// Validate the content of the Config object
require.Equal(t, "garm-testing", cfg.ProjectId, "ProjectId value did not match expected")
require.Equal(t, "europe-west1-d", cfg.Zone, "Zone value did not match expected")
require.Equal(t, "projects/garm-testing/global/networks/garm", cfg.NetworkID, "NetworkId value did not match expected")
require.Equal(t, "projects/garm-testing/regions/europe-west1/subnetworks/garm", cfg.SubnetworkID, "SubnetworkId value did not match expected")
require.Equal(t, "/home/ubuntu/service-account-key.json", cfg.CredentialsFile, "CredentialsFile value did not match expected")
require.Equal(t, true, cfg.ExternalIPAccess, "ExternalIpAccess value did not match expected")
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ require (
github.com/cloudbase/garm-provider-common v0.1.2-0.20240216125425-bbe4930a1ebf
github.com/googleapis/gax-go/v2 v2.12.0
github.com/xeipuuv/gojsonschema v1.2.0
github.com/stretchr/testify v1.9.0
golang.org/x/oauth2 v0.16.0
google.golang.org/api v0.156.0
google.golang.org/protobuf v1.32.0
)

require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -27,6 +29,7 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/minio/sio v0.3.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
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=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 h1:xzABM9let0HLLqFypcxvLmlvEciCHL7+Lv+4vwZqecI=
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569/go.mod h1:2Ly+NIftZN4de9zRmENdYbvPQeaVIYKWpLFStLFEBgI=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
Expand Down
Loading

0 comments on commit e076f98

Please sign in to comment.