Compare commits

...

16 commits

Author SHA1 Message Date
grumbulon 3d1f63144c yea 2022-06-30 08:43:06 -04:00
grumbulon 3467eabfe7 More tests 2022-06-29 18:21:43 -04:00
grumbulon c2eab0212a Test coverage 2022-06-29 14:28:00 -04:00
grumbulon 4353aa9559 Accidentally left something I was testing in..oops 2022-06-29 09:25:06 -04:00
grumbulon f7e5a7adee Added fatal logging, made doc for package better, and some minor changes 2022-06-29 09:22:28 -04:00
grumbulon be3b81029e Merge branch 'master' into master 2022-06-29 13:20:45 +00:00
grumbulon dfea8060dd Merge branch 'master' into master 2022-06-28 18:20:24 +00:00
grumbulon 6d30a5ad9b Logging works bretty good now 2022-06-28 14:11:49 -04:00
grumbulon a36f1b8685 Added debuge flag, and added init for logawl 2022-06-28 12:12:52 -04:00
Renovate Bot d4b8d1469f Update module github.com/urfave/cli/v2 to v2.10.3 2022-06-28 11:48:23 -04:00
Renovate Bot f50b793931 Update module github.com/stretchr/testify to v1.7.5 2022-06-28 11:48:23 -04:00
grumbulon 68e6e54741 Accidentally reverted to old commit with dns.go 2022-06-26 14:16:38 -04:00
grumbulon 8b1a5f9147 Merge remote-tracking branch 'upstream/master'
Merging upstream into fork
2022-06-26 14:10:35 -04:00
grumbulon 6da6a55f6e Logging refinements 2022-06-26 14:01:13 -04:00
grumbulon 8c61f9f409 logging (#1)
Promoting branch to master

Co-authored-by: grumbulon <grumbulon@dismail.de>
Reviewed-on: #1
2022-06-26 04:07:58 +00:00
grumbulon 182ec20469 Revert "hehe off-by-one"
This reverts commit db78235ecd.

Reverting to old commit because I fucked up
2022-06-25 13:46:38 -04:00
7 changed files with 193 additions and 3 deletions

5
cli.go
View file

@ -134,6 +134,11 @@ func prepareCLI() *cli.App {
Aliases: []string{"x"},
Usage: "do a reverse lookup",
},
&cli.BoolFlag{
Name: "debug",
Usage: "enable debug logging",
Value: false,
},
},
Action: doQuery,
}

View file

@ -2,6 +2,7 @@
package main
import (
"os"
"testing"
"git.froth.zone/sam/awl/util"
@ -40,3 +41,21 @@ func TestArgParse(t *testing.T) {
assert.Equal(t, test.want, act)
}
}
func TestQuery(t *testing.T) {
app := prepareCLI()
args := os.Args[0:1]
args = append(args, "--debug")
err := app.Run(args)
assert.Nil(t, err)
}
func TestHTTPS(t *testing.T) {
app := prepareCLI()
args := os.Args[0:1]
args = append(args, "-H")
args = append(args, "@https://cloudflare-dns.com/dns-query")
args = append(args, "git.froth.zone")
err := app.Run(args)
assert.Nil(t, err)
}

30
logawl/doc.go Normal file
View file

@ -0,0 +1,30 @@
/*
LogAwl is a package for custom logging needs
LogAwl extends the standard log library with support for log levels
This is _different_ from the syslog package in the standard library because you do not define a file
because awl is a cli utility it writes directly to std err.
*/
// Use the New() function to init logawl
//
// logger := logawl.New()
//
// You can call specific logging levels from your new logger using
//
// logger.Debug("Message to log")
// logger.Fatal("Message to log")
// logger.Info("Message to log")
// logger.Error("Message to log")
//
// You may also set the log level on the fly with
//
// Logger.SetLevel(3)
// This allows you to change the default level (Info) and prevent log messages from being posted at higher verbosity levels
//for example if
// Logger.SetLevel(3)
// is not called and you call
// Logger.Debug()
// this runs through
// IsLevel(level)
// to verify if the debug log should be sent to std.Err or not based on the current expected log level
package logawl

View file

@ -46,7 +46,7 @@ func (l *Logger) UnMarshalLevel(lv Level) (string, error) {
case 3:
return "DEBUG ", nil
}
return "", fmt.Errorf("Invalid log level choice")
return "", fmt.Errorf("invalid log level choice")
}
func (l *Logger) IsLevel(level Level) bool {

View file

@ -37,8 +37,6 @@ func (l *Logger) Println(level Level, v ...any) {
l.Printer(2, fmt.Sprintln(v...)) //Info level
case 3:
l.Printer(3, fmt.Sprintln(v...)) //Debug level
default:
break
}
}
}

82
logawl/logging_test.go Normal file
View file

@ -0,0 +1,82 @@
package logawl
import (
"bytes"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var logger = New()
func TestLogawl(t *testing.T) {
assert.Equal(t, Level(2), logger.Level) //cast 2 (int) to 2 (level)
//Validate setting and getting levels from memory works
for i := range AllLevels {
logger.SetLevel(Level(i))
assert.Equal(t, Level(i), logger.GetLevel())
}
}
func TestUnmarshalLevels(t *testing.T) {
m := make(map[int]string)
var err error
//Fill map with unmarshalled level info
for i := range AllLevels {
m[i], err = logger.UnMarshalLevel(Level(i))
assert.Nil(t, err)
}
//iterate over map and assert equal
for i := range AllLevels {
lv, err := logger.UnMarshalLevel(Level(i))
assert.Nil(t, err)
assert.Equal(t, m[i], lv)
}
lv, err := logger.UnMarshalLevel(Level(9001))
assert.NotNil(t, err)
assert.Equal(t, "", lv)
assert.ErrorContains(t, err, "invalid log level choice")
}
func TestLogger(t *testing.T) {
for i := range AllLevels {
// only test non-exiting log levels
switch i {
case 1:
fn := func() {
logger.Info("")
}
var buffer bytes.Buffer
logger.Out = &buffer
fn()
case 2:
fn := func() {
logger.Info("Test")
}
var buffer bytes.Buffer
logger.Out = &buffer
fn()
case 3:
fn := func() {
logger.Debug("Test")
}
var buffer bytes.Buffer
logger.Out = &buffer
fn()
}
}
}
func TestFmt(t *testing.T) {
ti := time.Now()
test := []byte("test")
logger.formatHeader(&test, ti, 0, Level(9001))
}

56
query/query_test.go Normal file
View file

@ -0,0 +1,56 @@
package query
import (
"testing"
"time"
"git.froth.zone/sam/awl/util"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
func TestHTTPS(t *testing.T) {
for i := range queries {
switch i {
case 1:
msg := new(dns.Msg)
testCase := util.Answers{Server: UnmarshallHTTPS(i), Request: dns.TypeAAAA, Name: "localhost"}
msg = msg.SetQuestion(testCase.Name, testCase.Request)
m, rtt, err := ResolveHTTPS(msg, testCase.Name)
assert.Error(t, err)
assert.Nil(t, m)
assert.Equal(t, rtt, time.Duration(0))
case 2:
msg := new(dns.Msg)
testCase := util.Answers{Server: UnmarshallHTTPS(i), Request: dns.TypeAAAA, Name: "localhost"}
msg = msg.SetQuestion(testCase.Name, testCase.Request)
m, rtt, err := ResolveHTTPS(msg, testCase.Name)
assert.Nil(t, err)
assert.Nil(t, m)
assert.Equal(t, rtt, time.Duration(0))
}
}
}
const (
one int = iota
cloudflare
)
var queries = []int{
one,
cloudflare,
}
func UnmarshallHTTPS(i int) string {
switch i {
case 0:
return "::1"
case 1:
return "https://cloudflare-dns.com/dns-query"
}
return ""
}