Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor tests enhancements #232

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions cmd/keymaster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -475,7 +476,7 @@
}
if *roundRobinDialer {
if rrDialer, err := rrdialer.New(rawDialer, "", logger); err != nil {
logger.Fatalln(err)
return nil, err

Check warning on line 479 in cmd/keymaster/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/keymaster/main.go#L479

Added line #L479 was not covered by tests
} else {
defer rrDialer.WaitForBackgroundResults(time.Second)
dialer = rrDialer
Expand All @@ -493,33 +494,31 @@
flag.PrintDefaults()
}

func main() {
flag.Usage = Usage
flag.Parse()
logger := cmdlogger.New()
// We assume here flags are parsed
func mainWithError(stdout io.Writer, logger log.DebugLogger) error {
if *printVersion {
fmt.Println(Version)
return
fmt.Fprintln(stdout, Version)
return nil
}
rootCAs, err := maybeGetRootCas(*rootCAFilename, logger)
if err != nil {
logger.Fatal(err)
return err

Check warning on line 505 in cmd/keymaster/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/keymaster/main.go#L505

Added line #L505 was not covered by tests
}
client, err := getHttpClient(rootCAs, logger)
if err != nil {
logger.Fatal(err)
return err

Check warning on line 509 in cmd/keymaster/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/keymaster/main.go#L509

Added line #L509 was not covered by tests
}
if *checkDevices {
err = u2f.CheckU2FDevices(logger)
if err != nil {
logger.Fatal(err)
return err
}
return
return nil

Check warning on line 516 in cmd/keymaster/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/keymaster/main.go#L516

Added line #L516 was not covered by tests
}
computeUserAgent()
userName, homeDir, err := util.GetUserNameAndHomeDir()
if err != nil {
logger.Fatal(err)
return err

Check warning on line 521 in cmd/keymaster/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/keymaster/main.go#L521

Added line #L521 was not covered by tests
}
config := loadConfigFile(client, logger)
logger.Debugf(3, "loaded Config=%+v", config)
Expand All @@ -543,7 +542,19 @@
err = setupCerts(userName, homeDir, config, client, logger)
}
if err != nil {
logger.Fatal(err)
return err

Check warning on line 545 in cmd/keymaster/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/keymaster/main.go#L545

Added line #L545 was not covered by tests
}
logger.Printf("Success")
return nil

Check warning on line 548 in cmd/keymaster/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/keymaster/main.go#L548

Added line #L548 was not covered by tests
}

func main() {
flag.Usage = Usage
flag.Parse()
logger := cmdlogger.New()
err := mainWithError(os.Stdout, logger)
if err != nil {
logger.Fatal(err)

Check warning on line 557 in cmd/keymaster/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/keymaster/main.go#L551-L557

Added lines #L551 - L557 were not covered by tests
}

}
32 changes: 32 additions & 0 deletions cmd/keymaster/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"crypto/ed25519"
"crypto/rand"
"crypto/tls"
Expand All @@ -20,6 +21,7 @@ import (

"github.com/Cloud-Foundations/golib/pkg/log/testlogger"
"github.com/Cloud-Foundations/keymaster/lib/client/config"
"github.com/Cloud-Foundations/keymaster/lib/client/twofa/u2f"
"github.com/Cloud-Foundations/keymaster/lib/client/util"
"github.com/Cloud-Foundations/keymaster/lib/webapi/v0/proto"
)
Expand Down Expand Up @@ -275,3 +277,33 @@ func TestInsertSSHCertIntoAgentORWriteToFilesystem(t *testing.T) {
// TODO: on linux/macos create agent + unix socket and pass that

}

func TestMainSimple(t *testing.T) {
logger := testlogger.New(t)
var b bytes.Buffer

// version
*printVersion = true
err := mainWithError(&b, logger)
if err != nil {
t.Fatal(err)
}
t.Logf("versionout='%s'", b.String())
// TODO: compara out to version string
*printVersion = false
b.Reset()

// checkDevices
*checkDevices = true
// As of May 2024, no devices returns an error on checkForDevices
// Because this will run inside or outside testing infra, we can
// only check if the error is consistent if any
checkDevRvalue := u2f.CheckU2FDevices(logger)
err = mainWithError(&b, logger)
if err != nil && (err.Error() != checkDevRvalue.Error()) {
t.Fatalf("manual an executed error mismatch mainerr=%s; chdevDerr=%s", err, checkDevRvalue)
}
*checkDevices = false
b.Reset()

}
Loading