Skip to content

Commit

Permalink
minor tests enhancements (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
cviecco authored May 27, 2024
1 parent f35df8b commit 413cb76
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
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 @@ import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -475,7 +476,7 @@ func getHttpClient(rootCAs *x509.CertPool, logger log.DebugLogger) (*http.Client
}
if *roundRobinDialer {
if rrDialer, err := rrdialer.New(rawDialer, "", logger); err != nil {
logger.Fatalln(err)
return nil, err
} else {
defer rrDialer.WaitForBackgroundResults(time.Second)
dialer = rrDialer
Expand All @@ -493,33 +494,31 @@ func Usage() {
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
}
client, err := getHttpClient(rootCAs, logger)
if err != nil {
logger.Fatal(err)
return err
}
if *checkDevices {
err = u2f.CheckU2FDevices(logger)
if err != nil {
logger.Fatal(err)
return err
}
return
return nil
}
computeUserAgent()
userName, homeDir, err := util.GetUserNameAndHomeDir()
if err != nil {
logger.Fatal(err)
return err
}
config := loadConfigFile(client, logger)
logger.Debugf(3, "loaded Config=%+v", config)
Expand All @@ -543,7 +542,19 @@ func main() {
err = setupCerts(userName, homeDir, config, client, logger)
}
if err != nil {
logger.Fatal(err)
return err
}
logger.Printf("Success")
return nil
}

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

}
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()

}

0 comments on commit 413cb76

Please sign in to comment.