Skip to content

Commit

Permalink
Merge pull request #3 from fabi200123/unit-tests
Browse files Browse the repository at this point in the history
Adding unit tests
  • Loading branch information
gabriel-samfira authored Mar 14, 2024
2 parents 7ecef80 + 6a571a1 commit cc4f117
Show file tree
Hide file tree
Showing 61 changed files with 18,846 additions and 8 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
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
307 changes: 307 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2024 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 config

import (
"fmt"
"os"
"testing"

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

func TestNewConfig(t *testing.T) {
// Create a temporary file
tempFile, err := os.CreateTemp("", "test.toml")
require.NoError(t, err, "Failed to create temp file")
defer os.Remove(tempFile.Name())

// Write some dummy TOML data to the temp file
dummyTOML := `
availability_domain = "mQqX:US-ASHBURN-AD-2"
compartment_id = "ocid1.compartment.oc1...fsbq"
subnet_id = "ocid1.subnet.oc1.iad....feoplaka"
network_security_group_id = "ocid1.networksecuritygroup....pfzya"
tenancy_id = "ocid1.tenancy.oc1..aaaaaaaajds7tbqbvrcaiavm2uk34t7wke7jg75aemsacljymbjxcio227oq"
user_id = "ocid1.user.oc1...ug6l37u6a"
region = "us-ashburn-1"
fingerprint = "38...6f:bb"
private_key_path = "/home/ubuntu/.oci/private_key.pem"
private_key_password = ""
`

_, err = tempFile.Write([]byte(dummyTOML))
require.NoError(t, err, "Failed to write to temp file")

err = tempFile.Close()
require.NoError(t, err, "Failed to close temp file")

// Test case for successful read
t.Run("success", func(t *testing.T) {
got, err := NewConfig(tempFile.Name())
require.NoError(t, err, "NewConfig() should not have returned an error")
require.Equal(t, &Config{
AvailabilityDomain: "mQqX:US-ASHBURN-AD-2",
CompartmentId: "ocid1.compartment.oc1...fsbq",
SubnetID: "ocid1.subnet.oc1.iad....feoplaka",
NsgID: "ocid1.networksecuritygroup....pfzya",
TenancyID: "ocid1.tenancy.oc1..aaaaaaaajds7tbqbvrcaiavm2uk34t7wke7jg75aemsacljymbjxcio227oq",
UserID: "ocid1.user.oc1...ug6l37u6a",
Region: "us-ashburn-1",
Fingerprint: "38...6f:bb",
PrivateKeyPath: "/home/ubuntu/.oci/private_key.pem",
PrivateKeyPassword: "",
}, got, "NewConfig() returned unexpected content")
})

// Test case for failed read (file does not exist)
t.Run("fail", func(t *testing.T) {
_, err := NewConfig("nonexistent.toml")
require.Error(t, err, "NewConfig() expected an error, got none")
})

// Test case for failed read (invalid TOML)
t.Run("fail", func(t *testing.T) {
// Create a temporary file
tempFile, err := os.CreateTemp("", "test.toml")
require.NoError(t, err, "Failed to create temp file")
defer os.Remove(tempFile.Name())

// Write some invalid TOML data to the temp file
invalidTOML := "invalid TOML"
_, err = tempFile.Write([]byte(invalidTOML))
require.NoError(t, err, "Failed to write to temp file")

err = tempFile.Close()
require.NoError(t, err, "Failed to close temp file")

_, err = NewConfig(tempFile.Name())
require.Error(t, err, "NewConfig() expected an error, got none")
})
}

func TestValidate(t *testing.T) {
tests := []struct {
name string
config *Config
errString error
}{
{
name: "valid config",
config: &Config{
AvailabilityDomain: "ad",
CompartmentId: "compartment",
SubnetID: "subnet",
NsgID: "nsg",
TenancyID: "tenancy",
UserID: "user",
Region: "region",
Fingerprint: "fingerprint",
PrivateKeyPath: "path",
PrivateKeyPassword: "password",
},
errString: nil,
},
{
name: "missing availability domain",
config: &Config{
CompartmentId: "compartment",
SubnetID: "subnet",
NsgID: "nsg",
TenancyID: "tenancy",
UserID: "user",
Region: "region",
Fingerprint: "fingerprint",
PrivateKeyPath: "path",
PrivateKeyPassword: "password",
},
errString: fmt.Errorf("availability_domain is required"),
},
{
name: "missing compartment id",
config: &Config{
AvailabilityDomain: "ad",
SubnetID: "subnet",
NsgID: "nsg",
TenancyID: "tenancy",
UserID: "user",
Region: "region",
Fingerprint: "fingerprint",
PrivateKeyPath: "path",
PrivateKeyPassword: "password",
},
errString: fmt.Errorf("compartment_id is required"),
},
{
name: "missing subnet id",
config: &Config{
AvailabilityDomain: "ad",
CompartmentId: "compartment",
NsgID: "nsg",
TenancyID: "tenancy",
UserID: "user",
Region: "region",
Fingerprint: "fingerprint",
PrivateKeyPath: "path",
PrivateKeyPassword: "password",
},
errString: fmt.Errorf("subnet_id is required"),
},
{
name: "missing nsg id",
config: &Config{
AvailabilityDomain: "ad",
CompartmentId: "compartment",
SubnetID: "subnet",
TenancyID: "tenancy",
UserID: "user",
Region: "region",
Fingerprint: "fingerprint",
PrivateKeyPath: "path",
PrivateKeyPassword: "password",
},
errString: fmt.Errorf("ngs_id is required"),
},
{
name: "missing tenancy id",
config: &Config{
AvailabilityDomain: "ad",
CompartmentId: "compartment",
SubnetID: "subnet",
NsgID: "nsg",
UserID: "user",
Region: "region",
Fingerprint: "fingerprint",
PrivateKeyPath: "path",
PrivateKeyPassword: "password",
},
errString: fmt.Errorf("tenancy_id is required"),
},
{
name: "missing user id",
config: &Config{
AvailabilityDomain: "ad",
CompartmentId: "compartment",
SubnetID: "subnet",
NsgID: "nsg",
TenancyID: "tenancy",
Region: "region",
Fingerprint: "fingerprint",
PrivateKeyPath: "path",
PrivateKeyPassword: "password",
},
errString: fmt.Errorf("user_id is required"),
},
{
name: "missing region",
config: &Config{
AvailabilityDomain: "ad",
CompartmentId: "compartment",
SubnetID: "subnet",
NsgID: "nsg",
TenancyID: "tenancy",
UserID: "user",
Fingerprint: "fingerprint",
PrivateKeyPath: "path",
PrivateKeyPassword: "password",
},
errString: fmt.Errorf("region is required"),
},
{
name: "missing fingerprint",
config: &Config{
AvailabilityDomain: "ad",
CompartmentId: "compartment",
SubnetID: "subnet",
NsgID: "nsg",
TenancyID: "tenancy",
UserID: "user",
Region: "region",
PrivateKeyPath: "path",
PrivateKeyPassword: "password",
},
errString: fmt.Errorf("fingerprint is required"),
},
{
name: "missing private key path",
config: &Config{
AvailabilityDomain: "ad",
CompartmentId: "compartment",
SubnetID: "subnet",
NsgID: "nsg",
TenancyID: "tenancy",
UserID: "user",
Region: "region",
Fingerprint: "fingerprint",
PrivateKeyPassword: "password",
},
errString: fmt.Errorf("private_key_path is required"),
},
{
name: "valid config with empty private key password",
config: &Config{
AvailabilityDomain: "ad",
CompartmentId: "compartment",
SubnetID: "subnet",
NsgID: "nsg",
TenancyID: "tenancy",
UserID: "user",
Region: "region",
Fingerprint: "fingerprint",
PrivateKeyPath: "path",
},
errString: nil,
},
}

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

}

func TestGetPrivateKey(t *testing.T) {
// Create a temporary file
tempFile, err := os.CreateTemp("", "test.pem")
require.NoError(t, err, "Failed to create temp file")
defer os.Remove(tempFile.Name())

// Write some dummy PEM data to the temp file
dummyPEM := "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqh...\n-----END PRIVATE KEY-----"
_, err = tempFile.Write([]byte(dummyPEM))
require.NoError(t, err, "Failed to write to temp file")

err = tempFile.Close()
require.NoError(t, err, "Failed to close temp file")

// Test case for successful read
t.Run("success", func(t *testing.T) {
c := Config{PrivateKeyPath: tempFile.Name()}
got, err := c.GetPrivateKey()
require.NoError(t, err, "GetPrivateKey() should not have returned an error")
require.Equal(t, dummyPEM, got, "GetPrivateKey() returned unexpected content")
})

// Test case for failed read (file does not exist)
t.Run("fail", func(t *testing.T) {
c := Config{PrivateKeyPath: "nonexistent.pem"}
_, err := c.GetPrivateKey()
require.Error(t, err, "GetPrivateKey() expected an error, got none")
})
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ require (
github.com/BurntSushi/toml v1.3.2
github.com/cloudbase/garm-provider-common v0.1.2-0.20240216125425-bbe4930a1ebf
github.com/oracle/oci-go-sdk/v49 v49.2.0
github.com/stretchr/testify v1.9.0
github.com/xeipuuv/gojsonschema v1.2.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
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/sony/gobreaker v0.4.2-0.20210216022020-dd874f9dd33b // indirect
github.com/stretchr/objx v0.5.2 // 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
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sony/gobreaker v0.4.2-0.20210216022020-dd874f9dd33b h1:br+bPNZsJWKicw/5rALEo67QHs5weyD5tf8WST+4sJ0=
github.com/sony/gobreaker v0.4.2-0.20210216022020-dd874f9dd33b/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
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 cc4f117

Please sign in to comment.