feat: initialize tests
ci/woodpecker/pr/test Pipeline failed Details
ci/woodpecker/pr/build Pipeline failed Details
ci/woodpecker/pr/lint Pipeline failed Details

This commit is contained in:
Aire-One 2024-09-23 23:46:12 +02:00
parent 17160e38cf
commit f085f66d66
4 changed files with 101 additions and 0 deletions

9
.woodpecker/test.yaml Normal file
View File

@ -0,0 +1,9 @@
---
when:
event: pull_request
steps:
golang:
image: golang:1.23.1
commands:
- go test -v ./...

View File

@ -0,0 +1,31 @@
package labtime
import (
"log"
"os"
"testing"
)
func TestNewApp(t *testing.T) {
flag := &FlagConfig{
configFile: "../../../configs/example-config.yaml", // we shouldn't rely on the actual file
}
logger := log.New(os.Stdout, "", 0) // silent?
app, err := NewApp(flag, logger)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if app.logger != logger {
t.Errorf("expected logger to be set")
}
if app.scheduler == nil {
t.Errorf("expected scheduler to be initialized")
}
if app.prometheusHTTPServer == nil {
t.Errorf("expected prometheusHTTPServer to be initialized")
}
}

View File

@ -0,0 +1,45 @@
package labtime
import (
"flag"
"os"
"testing"
)
func TestLoadFlag(t *testing.T) {
tests := []struct {
name string
args []string
expectedFile string
}{
{
name: "No flag provided",
args: []string{},
expectedFile: defaultConfigFile,
},
{
name: "Flag provided",
args: []string{"-config", "custom.yaml"},
expectedFile: "custom.yaml",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Save and restore the original os.Args
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
// Make sure we are using a clean flag set
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
os.Args = append([]string{"cmd"}, tt.args...)
cfg := LoadFlag()
if cfg.configFile != tt.expectedFile {
t.Errorf("got %s, want %s", cfg.configFile, tt.expectedFile)
}
})
}
}

View File

@ -0,0 +1,16 @@
package monitors
import "testing"
func TestHTTPMonitor_ID(t *testing.T) {
monitor := &HTTPMonitor{
Label: "example",
}
expectedID := "example"
actualID := monitor.ID()
if actualID != expectedID {
t.Errorf("expected ID to be %s, but got %s", expectedID, actualID)
}
}