deps: convert zap to slog (#2825)

This commit is contained in:
miampf 2024-02-08 14:20:01 +00:00 committed by GitHub
parent 3765cb0762
commit 54cce77bab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
182 changed files with 1474 additions and 1509 deletions

View file

@ -12,7 +12,9 @@ import (
"encoding/json"
"flag"
"fmt"
"log/slog"
"net"
"os"
"strings"
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
@ -20,13 +22,10 @@ import (
"github.com/edgelesssys/constellation/v2/internal/grpc/dialer"
"github.com/edgelesssys/constellation/v2/internal/logger"
"github.com/edgelesssys/constellation/v2/joinservice/joinproto"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
log := logger.New(logger.JSONLog, zapcore.DebugLevel)
defer log.Sync()
log := logger.NewJSONLogger(slog.LevelDebug)
jsEndpoint := flag.String("js-endpoint", "", "Join service endpoint to use.")
csp := flag.String("csp", "", "Cloud service provider to use.")
@ -38,13 +37,13 @@ func main() {
)
flag.Parse()
log.With(
zap.String("js-endpoint", *jsEndpoint),
zap.String("csp", *csp),
zap.String("variant", *attVariant),
).Infof("Running tests with flags")
slog.String("js-endpoint", *jsEndpoint),
slog.String("csp", *csp),
slog.String("variant", *attVariant),
).Info("Running tests with flags")
testCases := map[string]struct {
fn func(attVariant, csp, jsEndpoint string, log *logger.Logger) error
fn func(attVariant, csp, jsEndpoint string, log *slog.Logger) error
wantErr bool
}{
"JoinFromUnattestedNode": {
@ -58,44 +57,45 @@ func main() {
TestCases: make(map[string]testCaseOutput),
}
for name, tc := range testCases {
log.With(zap.String("testcase", name)).Infof("Running testcase")
log.With(slog.String("testcase", name)).Info("Running testcase")
err := tc.fn(*attVariant, *csp, *jsEndpoint, log)
switch {
case err == nil && tc.wantErr:
log.With(zap.Error(err), zap.String("testcase", name)).Errorf("Test case failed: Expected error but got none")
log.With(slog.Any("error", err), slog.String("testcase", name)).Error("Test case failed: Expected error but got none")
testOutput.TestCases[name] = testCaseOutput{
Passed: false,
Message: "Expected error but got none",
}
allPassed = false
case !tc.wantErr && err != nil:
log.With(zap.Error(err), zap.String("testcase", name)).Errorf("Test case failed: Got unexpected error")
log.With(slog.Any("error", err), slog.String("testcase", name)).Error("Test case failed: Got unexpected error")
testOutput.TestCases[name] = testCaseOutput{
Passed: false,
Message: fmt.Sprintf("Got unexpected error: %s", err),
}
allPassed = false
case tc.wantErr && err != nil:
log.With(zap.String("testcase", name)).Infof("Test case succeeded")
log.With(slog.String("testcase", name)).Info("Test case succeeded")
testOutput.TestCases[name] = testCaseOutput{
Passed: true,
Message: fmt.Sprintf("Got expected error: %s", err),
}
case !tc.wantErr && err == nil:
log.With(zap.String("testcase", name)).Infof("Test case succeeded")
log.With(slog.String("testcase", name)).Info("Test case succeeded")
testOutput.TestCases[name] = testCaseOutput{
Passed: true,
Message: "No error, as expected",
}
default:
log.With(zap.String("testcase", name)).Fatalf("invalid result")
log.With(slog.String("testcase", name)).Error("invalid result")
os.Exit(1)
}
}
testOutput.AllPassed = allPassed
log.With(zap.Any("result", testOutput)).Infof("Test completed")
log.With(slog.Any("result", testOutput)).Info("Test completed")
}
type testOutput struct {
@ -110,7 +110,7 @@ type testCaseOutput struct {
// JoinFromUnattestedNode simulates a join request from a Node that uses a stub issuer
// and thus cannot be attested correctly.
func JoinFromUnattestedNode(attVariant, csp, jsEndpoint string, log *logger.Logger) error {
func JoinFromUnattestedNode(attVariant, csp, jsEndpoint string, log *slog.Logger) error {
joiner, err := newMaliciousJoiner(attVariant, csp, jsEndpoint, log)
if err != nil {
return fmt.Errorf("creating malicious joiner: %w", err)
@ -125,7 +125,7 @@ func JoinFromUnattestedNode(attVariant, csp, jsEndpoint string, log *logger.Logg
// newMaliciousJoiner creates a new malicious joiner, i.e. a simulated node that issues
// an invalid join request.
func newMaliciousJoiner(attVariant, csp, endpoint string, log *logger.Logger) (*maliciousJoiner, error) {
func newMaliciousJoiner(attVariant, csp, endpoint string, log *slog.Logger) (*maliciousJoiner, error) {
var attVariantOid variant.Variant
var err error
if strings.EqualFold(attVariant, "default") {
@ -149,30 +149,30 @@ func newMaliciousJoiner(attVariant, csp, endpoint string, log *logger.Logger) (*
// maliciousJoiner simulates a malicious node joining a cluster.
type maliciousJoiner struct {
endpoint string
logger *logger.Logger
logger *slog.Logger
dialer *dialer.Dialer
}
// join issues a join request to the join service endpoint.
func (j *maliciousJoiner) join(ctx context.Context) (*joinproto.IssueJoinTicketResponse, error) {
j.logger.Debugf("Dialing join service endpoint %s", j.endpoint)
j.logger.Debug(fmt.Sprintf("Dialing join service endpoint %s", j.endpoint))
conn, err := j.dialer.Dial(ctx, j.endpoint)
if err != nil {
return nil, fmt.Errorf("dialing join service endpoint: %w", err)
}
defer conn.Close()
j.logger.Debugf("Successfully dialed join service endpoint %s", j.endpoint)
j.logger.Debug(fmt.Sprintf("Successfully dialed join service endpoint %s", j.endpoint))
protoClient := joinproto.NewAPIClient(conn)
j.logger.Debugf("Issuing join ticket")
j.logger.Debug("Issuing join ticket")
req := &joinproto.IssueJoinTicketRequest{
DiskUuid: "",
CertificateRequest: []byte{},
IsControlPlane: false,
}
res, err := protoClient.IssueJoinTicket(ctx, req)
j.logger.Debugf("Got join ticket response: %+v", res)
j.logger.Debug(fmt.Sprintf("Got join ticket response: %+v", res))
if err != nil {
return nil, fmt.Errorf("issuing join ticket: %w", err)
}