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 Apr 2, 2024
2 parents d93db1d + ff87607 commit 9bf7d89
Show file tree
Hide file tree
Showing 63 changed files with 18,822 additions and 16 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
207 changes: 207 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// 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 (
"os"
"testing"

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

func TestConfigValidate(t *testing.T) {
tests := []struct {
name string
c *Config
errString string
}{
{
name: "valid config",
c: &Config{
Credentials: Credentials{
AccessKeyID: "access_key_id",
SecretAccessKey: "secret_access_key",
SessionToken: "session_token",
},
SubnetID: "subnet_id",
Region: "region",
},
errString: "",
},
{
name: "missing subnet_id",
c: &Config{
Credentials: Credentials{
AccessKeyID: "access_key_id",
SecretAccessKey: "secret_access_key",
SessionToken: "session_token",
},
Region: "region",
},
errString: "missing subnet_id",
},
{
name: "missing region",
c: &Config{
Credentials: Credentials{
AccessKeyID: "access_key_id",
SecretAccessKey: "secret_access_key",
SessionToken: "session_token",
},
SubnetID: "subnet_id",
},
errString: "missing region",
},
{
name: "missing credentials",
c: &Config{
SubnetID: "subnet_id",
Region: "region",
},
errString: "failed to validate credentials: missing access_key_id",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.c.Validate()
if tt.errString == "" {
require.Nil(t, err)
} else {
require.EqualError(t, err, tt.errString)
}
})
}
}

func TestCredentialsValidate(t *testing.T) {
tests := []struct {
name string
c Credentials
errString string
}{
{
name: "valid credentials",
c: Credentials{
AccessKeyID: "access_key_id",
SecretAccessKey: "secret_access_key",
SessionToken: "session_token",
},
errString: "",
},
{
name: "missing access_key_id",
c: Credentials{
AccessKeyID: "",
SecretAccessKey: "secret_access_key",
SessionToken: "session_token",
},
errString: "missing access_key_id",
},
{
name: "missing secret_access_key",
c: Credentials{
AccessKeyID: "access_key_id",
SecretAccessKey: "",
SessionToken: "session_token",
},
errString: "missing secret_access_key",
},
{
name: "missing session_token",
c: Credentials{
AccessKeyID: "access_key_id",
SecretAccessKey: "secret_access_key",
SessionToken: "",
},
errString: "missing session_token",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.c.Validate()
if tt.errString == "" {
require.Nil(t, err)
} else {
require.EqualError(t, err, tt.errString)
}
})
}
}

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 := `
region = "region"
subnet_id = "subnet_id"
[credentials]
access_key_id = "access_key_id"
secret_access_key = "secret"
session_token = "token"
`

_, 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{
Credentials: Credentials{
AccessKeyID: "access_key_id",
SecretAccessKey: "secret",
SessionToken: "token",
},
SubnetID: "subnet_id",
Region: "region",
}, 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")
})
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.16.14
github.com/aws/aws-sdk-go-v2/service/ec2 v1.144.0
github.com/aws/smithy-go v1.19.0
github.com/cloudbase/garm-provider-common v0.1.2-0.20240111235646-a9efac12b060
github.com/cloudbase/garm-provider-common v0.1.2-0.20240216125425-bbe4930a1ebf
github.com/stretchr/testify v1.9.0
github.com/xeipuuv/gojsonschema v1.2.0
)

Expand All @@ -23,13 +24,16 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.18.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.26.7 // indirect
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/jmespath/go-jmespath v0.4.0 // 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/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
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.26.7 h1:NzO4Vrau795RkUdSHKEwiR01FaGz
github.com/aws/aws-sdk-go-v2/service/sts v1.26.7/go.mod h1:6h2YuIoxaMSCFf5fi1EgZAwdfkGMgDY+DVfa61uLe4U=
github.com/aws/smithy-go v1.19.0 h1:KWFKQV80DpP3vJrrA9sVAHQ5gc2z8i4EzrLhLlWXcBM=
github.com/aws/smithy-go v1.19.0/go.mod h1:NukqUGpCZIILqqiV0NIjeFh24kd/FAa4beRb6nbIUPE=
github.com/cloudbase/garm-provider-common v0.1.2-0.20240111235646-a9efac12b060 h1:R1x91MisDJq61uioMULPycnQo6P4HWFyR3Pa9ePz8c4=
github.com/cloudbase/garm-provider-common v0.1.2-0.20240111235646-a9efac12b060/go.mod h1:igxJRT3OlykERYc6ssdRQXcb+BCaeSfnucg6I0OSoDc=
github.com/cloudbase/garm-provider-common v0.1.2-0.20240216125425-bbe4930a1ebf h1:E5TVBkaxlX6hoBo5RVEWumjkjT9QM7Y421gGUVJg7so=
github.com/cloudbase/garm-provider-common v0.1.2-0.20240216125425-bbe4930a1ebf/go.mod h1:igxJRT3OlykERYc6ssdRQXcb+BCaeSfnucg6I0OSoDc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -54,9 +54,11 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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.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
30 changes: 26 additions & 4 deletions internal/client/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,40 @@ func NewAwsCli(ctx context.Context, cfg *config.Config) (*AwsCli, error) {
client := ec2.NewFromConfig(cliCfg)
awsCli := &AwsCli{
cfg: cfg,
client: *client,
region: cfg.Region,
client: client,
}

return awsCli, nil
}

type ClientInterface interface {
StartInstances(ctx context.Context, params *ec2.StartInstancesInput, optFns ...func(*ec2.Options)) (*ec2.StartInstancesOutput, error)
StopInstances(ctx context.Context, params *ec2.StopInstancesInput, optFns ...func(*ec2.Options)) (*ec2.StopInstancesOutput, error)
DescribeInstances(ctx context.Context, params *ec2.DescribeInstancesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeInstancesOutput, error)
TerminateInstances(ctx context.Context, params *ec2.TerminateInstancesInput, optFns ...func(*ec2.Options)) (*ec2.TerminateInstancesOutput, error)
RunInstances(ctx context.Context, params *ec2.RunInstancesInput, optFns ...func(*ec2.Options)) (*ec2.RunInstancesOutput, error)
}

type AwsCli struct {
cfg *config.Config

client ec2.Client
region string
client ClientInterface
}

func (a *AwsCli) Config() *config.Config {
return a.cfg
}

func (a *AwsCli) Client() ClientInterface {
return a.client
}

func (a *AwsCli) SetConfig(cfg *config.Config) {
a.cfg = cfg
}

func (a *AwsCli) SetClient(client ClientInterface) {
a.client = client
}

func (a *AwsCli) StartInstance(ctx context.Context, vmName string) error {
Expand Down
Loading

0 comments on commit 9bf7d89

Please sign in to comment.