-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
971284a
commit bdea7e5
Showing
3 changed files
with
201 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package config | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func NewLogger(logLevel string, logFormat string) *slog.Logger { | ||
var l slog.Level | ||
switch strings.ToUpper(logLevel) { | ||
case "DEBUG": | ||
l = slog.LevelDebug | ||
case "INFO": | ||
l = slog.LevelInfo | ||
case "WARN": | ||
l = slog.LevelWarn | ||
case "ERROR": | ||
l = slog.LevelError | ||
default: | ||
l = slog.LevelDebug | ||
} | ||
|
||
var h slog.Handler | ||
switch strings.ToUpper(logFormat) { | ||
case "TEXT": | ||
h = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: l, AddSource: false}) | ||
case "JSON": | ||
h = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: l, AddSource: false}) | ||
default: | ||
h = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: l, AddSource: false}) | ||
} | ||
|
||
return slog.New(h) | ||
} | ||
|
||
func RequireConfig() ([]byte, error) { | ||
|
||
configPath := flag.String("config", "", "path to the configuration file (required)") | ||
|
||
flag.Parse() | ||
|
||
if *configPath == "" { | ||
fmt.Fprintf(os.Stderr, "Usage of network-discovery:\n") | ||
flag.PrintDefaults() | ||
return nil, fmt.Errorf("") | ||
|
||
} | ||
if _, err := os.Stat(*configPath); os.IsNotExist(err) { | ||
return nil, fmt.Errorf("configuration file '%s' does not exist\n", *configPath) | ||
} | ||
|
||
fileData, err := os.ReadFile(*configPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading configuration file: %v\n", err) | ||
} | ||
|
||
return fileData, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package config_test | ||
|
||
import ( | ||
"flag" | ||
"log/slog" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/netboxlabs/orb-discovery/network-discovery/config" | ||
) | ||
|
||
func TestNewLogger(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
loggingLevel string | ||
loggingFormat string | ||
}{ | ||
{ | ||
desc: "with debug level and json format", | ||
loggingLevel: "debug", | ||
loggingFormat: "json", | ||
}, | ||
{ | ||
desc: "with debug level and text format", | ||
loggingLevel: "debug", | ||
loggingFormat: "text", | ||
}, | ||
{ | ||
desc: "with info level and json format", | ||
loggingLevel: "info", | ||
loggingFormat: "json", | ||
}, | ||
{ | ||
desc: "with info level and text format", | ||
loggingLevel: "warn", | ||
loggingFormat: "json", | ||
}, | ||
{ | ||
desc: "with error level and text format", | ||
loggingLevel: "error", | ||
loggingFormat: "text", | ||
}, | ||
{ | ||
desc: "with error level and empty format", | ||
loggingLevel: "error", | ||
loggingFormat: "", | ||
}, | ||
{ | ||
desc: "with empty level and text format", | ||
loggingLevel: "", | ||
loggingFormat: "text", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
log := config.NewLogger(tt.loggingLevel, tt.loggingFormat) | ||
require.NotNil(t, log) | ||
|
||
handlerOK := false | ||
if tt.loggingFormat == "text" { | ||
_, handlerOK = log.Handler().(*slog.TextHandler) | ||
} else { | ||
_, handlerOK = log.Handler().(*slog.JSONHandler) | ||
} | ||
assert.True(t, handlerOK) | ||
}) | ||
} | ||
} | ||
|
||
func TestRequireConfig(t *testing.T) { | ||
t.Run("No Config Path Provided", func(t *testing.T) { | ||
// Simulate no flags passed | ||
os.Args = []string{"network-discovery"} | ||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) // Reset flags | ||
|
||
data, err := config.RequireConfig() | ||
assert.Nil(t, data) | ||
assert.EqualError(t, err, "") | ||
}) | ||
|
||
t.Run("Config File Does Not Exist", func(t *testing.T) { | ||
// Simulate a non-existent file | ||
os.Args = []string{"network-discovery", "-config", "/non/existent/path"} | ||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) // Reset flags | ||
|
||
data, err := config.RequireConfig() | ||
assert.Nil(t, data) | ||
assert.EqualError(t, err, "configuration file '/non/existent/path' does not exist\n") | ||
}) | ||
|
||
t.Run("Error Reading Config File", func(t *testing.T) { | ||
// Create a file and simulate an error by removing it before reading | ||
tmpFile, err := os.CreateTemp("", "test-config") | ||
assert.NoError(t, err) | ||
tmpFilePath := tmpFile.Name() | ||
tmpFile.Close() | ||
|
||
// Remove the file to simulate read error | ||
os.Remove(tmpFilePath) | ||
|
||
os.Args = []string{"network-discovery", "-config", tmpFilePath} | ||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) // Reset flags | ||
|
||
data, err := config.RequireConfig() | ||
assert.Nil(t, data) | ||
assert.Contains(t, err.Error(), "does not exist") | ||
}) | ||
|
||
t.Run("Valid Config File", func(t *testing.T) { | ||
// Create a temporary file with valid content | ||
tmpFile, err := os.CreateTemp("", "test-config") | ||
assert.NoError(t, err) | ||
defer os.Remove(tmpFile.Name()) | ||
|
||
// Write YAML content to the file | ||
content := "network:\n policies:\n discovery_1:\n config:\n schedule: '* * * * *'" | ||
_, err = tmpFile.WriteString(content) | ||
assert.NoError(t, err) | ||
tmpFile.Close() | ||
|
||
// Pass the file path as a flag | ||
os.Args = []string{"network-discovery", "-config", tmpFile.Name()} | ||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) // Reset flags | ||
|
||
data, err := config.RequireConfig() | ||
assert.NoError(t, err) | ||
assert.Equal(t, content, string(data)) | ||
}) | ||
} |