feat: initialize tests
This commit is contained in:
parent
17160e38cf
commit
f085f66d66
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
when:
|
||||
event: pull_request
|
||||
|
||||
steps:
|
||||
golang:
|
||||
image: golang:1.23.1
|
||||
commands:
|
||||
- go test -v ./...
|
|
@ -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")
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue