Very preliminary CI/CD pipeline to see if I need to manually pull the code.

This commit is contained in:
James Wells 2024-09-06 06:59:19 -07:00
parent 823a16a97b
commit 9727822b8a
Signed by: jwells
GPG key ID: 73196D10B8E65666
5 changed files with 149 additions and 81 deletions

View file

@ -1,70 +1,120 @@
package main
import (
os "os"
testing "testing"
"bytes"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
// main "github.com/dragonheim/gagent/cmd/gagent"
env "github.com/caarlos0/env/v6"
gstructs "github.com/dragonheim/gagent/internal/gstructs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// This function will create a temporary config file for testing purposes
func createTestConfigFile() (string, error) {
tmpfile, err := os.CreateTemp("", "test_config_*.hcl")
if err != nil {
return "", err
}
content := []byte(`mode = "setup"
listen_addr = "0.0.0.0"
monitor_port = 8888
client_port = 35572
router_port = 35570
worker_port = 35571
`)
if _, err := tmpfile.Write(content); err != nil {
return "", err
}
if err := tmpfile.Close(); err != nil {
return "", err
}
return tmpfile.Name(), nil
// Mocking the WaitGroup to avoid actually waiting in tests
type MockWaitGroup struct {
mock.Mock
}
func TestMain(t *testing.T) {
t.Run("Test setup mode with temp config file", func(t *testing.T) {
tmpConfig, err := createTestConfigFile()
if err != nil {
t.Fatalf("Failed to create temp config file: %v", err)
}
defer os.Remove(tmpConfig)
func (m *MockWaitGroup) Add(delta int) {
m.Called(delta)
}
config := gstructs.GagentConfig{
File: tmpConfig,
Mode: "setup",
}
func (m *MockWaitGroup) Done() {
m.Called()
}
// Run the main function with the temporary config
t.Run(config)
func (m *MockWaitGroup) Wait() {
m.Called()
}
// Check if the config has been set up correctly
expectedConfig := gstructs.GagentConfig{
Mode: "setup",
ListenAddr: "0.0.0.0",
MonitorPort: 8888,
ClientPort: 35572,
RouterPort: 35570,
WorkerPort: 35571,
}
// Mocking the config loader function to inject test configurations
func mockInitConfig() {
config = gstructs.GagentConfig{
Mode: "client",
MonitorPort: 8080,
// Populate other required fields as needed
}
}
if config.Mode != expectedConfig.Mode ||
config.ListenAddr != expectedConfig.ListenAddr ||
config.MonitorPort != expectedConfig.MonitorPort ||
config.ClientPort != expectedConfig.ClientPort ||
config.RouterPort != expectedConfig.RouterPort ||
config.WorkerPort != expectedConfig.WorkerPort {
t.Fatalf("Expected config %+v, got %+v", expectedConfig, config)
}
func TestMainFunction(t *testing.T) {
var wg MockWaitGroup
wg.On("Add", 1).Return()
wg.On("Wait").Return()
wg.On("Done").Return()
mockInitConfig()
// Test the client mode
config.Mode = "client"
main()
wg.AssertCalled(t, "Add", 1)
// Test the router mode
config.Mode = "router"
main()
wg.AssertCalled(t, "Add", 1)
// Test the worker mode
config.Mode = "worker"
main()
wg.AssertCalled(t, "Add", 1)
// Test the setup mode
config.Mode = "setup"
main()
wg.AssertCalled(t, "Add", 1)
// Test an invalid mode
config.Mode = "invalid"
assert.Panics(t, func() { main() }, "Expected main() to panic with invalid mode")
}
func TestInitFunction(t *testing.T) {
// Backup original stdout and defer restoration
origStdout := os.Stdout
defer func() { os.Stdout = origStdout }()
// Capture stdout output to test log output
var logOutput bytes.Buffer
log.SetOutput(&logOutput)
// Test init
init()
// Assertions
assert.Contains(t, logOutput.String(), "[DEBUG] Arguments are")
assert.NotEmpty(t, config.Version, "Config version should not be empty")
assert.NotEmpty(t, config.UUID, "Config UUID should not be empty")
}
func TestPrometheusMetricsExporter(t *testing.T) {
mockInitConfig()
config.MonitorPort = 8080
req, err := http.NewRequest("GET", "/metrics", nil)
assert.NoError(t, err)
rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.DefaultServeMux.ServeHTTP(w, r)
})
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code, "Handler returned wrong status code")
assert.Contains(t, rr.Body.String(), "go_gc_duration_seconds", "Expected metrics output")
}
func TestEnvironmentParsing(t *testing.T) {
cfg := environment
err := env.Parse(&cfg)
assert.NoError(t, err)
assert.Equal(t, "/etc/gagent/gagent.hcl", cfg.ConfigFile, "Expected default config file path")
assert.Equal(t, "WARN", cfg.LogLevel, "Expected default log level")
assert.Equal(t, "setup", cfg.Mode, "Expected default mode")
assert.Equal(t, 0, cfg.MonitorPort, "Expected default monitor port")
}