Skip to content

Commit

Permalink
Use structured logging for all parts of the test
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Weiße <[email protected]>
  • Loading branch information
daniel-weisse committed Nov 6, 2023
1 parent ac4ac6a commit e2b6562
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/actions/e2e_malicious_join/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ runs:
kubectl apply -n malicious-join -f job.yaml
kubectl wait -n malicious-join --for=condition=complete --timeout=10m job/malicious-join
kubectl logs -n malicious-join job/malicious-join | tail -n 1 | jq '.'
ALL_TESTS_PASSED=$(kubectl logs -n malicious-join job/malicious-join | tail -n 1 | jq -r '.allPassed')
ALL_TESTS_PASSED=$(kubectl logs -n malicious-join job/malicious-join | tail -n 1 | jq -r '.result.allPassed')
if [[ "$ALL_TESTS_PASSED" != "true" ]]; then
kubectl logs -n malicious-join job/malicious-join
kubectl logs -n kube-system svc/join-service
Expand Down
1 change: 1 addition & 0 deletions e2e/malicious-join/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
"//internal/grpc/dialer",
"//internal/logger",
"//joinservice/joinproto",
"@org_uber_go_zap//:zap",
"@org_uber_go_zap//zapcore",
],
)
Expand Down
44 changes: 19 additions & 25 deletions e2e/malicious-join/malicious-join.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ 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()

jsEndpoint := flag.String("js-endpoint", "", "Join service endpoint to use.")
csp := flag.String("csp", "", "Cloud service provider to use.")
attVariant := flag.String(
Expand All @@ -33,10 +37,14 @@ func main() {
"or one of: %s", variant.GetAvailableAttestationVariants()),
)
flag.Parse()
fmt.Println(formatFlags(*attVariant, *csp, *jsEndpoint))
log.With(
zap.String("js-endpoint", *jsEndpoint),
zap.String("csp", *csp),
zap.String("variant", *attVariant),
).Infof("Running tests with flags")

testCases := map[string]struct {
fn func(attVariant, csp, jsEndpoint string) error
fn func(attVariant, csp, jsEndpoint string, log *logger.Logger) error
wantErr bool
}{
"JoinFromUnattestedNode": {
Expand All @@ -50,48 +58,44 @@ func main() {
TestCases: make(map[string]testCaseOutput),
}
for name, tc := range testCases {
fmt.Printf("Running testcase %s\n", name)
log.With(zap.String("testcase", name)).Infof("Running testcase")

err := tc.fn(*attVariant, *csp, *jsEndpoint)
err := tc.fn(*attVariant, *csp, *jsEndpoint, log)

switch {
case err == nil && tc.wantErr:
fmt.Printf("Test case %s failed: Expected error but got none\n", name)
log.With(zap.Error(err), zap.String("testcase", name)).Errorf("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:
fmt.Printf("Test case %s failed: Got unexpected error: %s\n", name, err)
log.With(zap.Error(err), zap.String("testcase", name)).Errorf("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:
fmt.Printf("Test case %s succeeded\n", name)
log.With(zap.String("testcase", name)).Infof("Test case succeeded")
testOutput.TestCases[name] = testCaseOutput{
Passed: true,
Message: fmt.Sprintf("Got expected error: %s", err),
}
case !tc.wantErr && err == nil:
fmt.Printf("Test case %s succeeded\n", name)
log.With(zap.String("testcase", name)).Infof("Test case succeeded")
testOutput.TestCases[name] = testCaseOutput{
Passed: true,
Message: "No error, as expected",
}
default:
panic("invalid result")
log.With(zap.String("testcase", name)).Fatalf("invalid result")
}
}

testOutput.AllPassed = allPassed
out, err := json.Marshal(testOutput)
if err != nil {
panic(fmt.Sprintf("marshalling test output: %s", err))
}
fmt.Println(string(out))
log.With(zap.Any("result", testOutput)).Infof("Test completed")
}

type testOutput struct {
Expand All @@ -104,19 +108,9 @@ type testCaseOutput struct {
Message string `json:"message"`
}

func formatFlags(attVariant, csp, jsEndpoint string) string {
var sb strings.Builder
sb.WriteString("Using Flags:\n")
sb.WriteString(fmt.Sprintf("\tjs-endpoint: %s\n", jsEndpoint))
sb.WriteString(fmt.Sprintf("\tcsp: %s\n", csp))
sb.WriteString(fmt.Sprintf("\tvariant: %s\n", attVariant))
return sb.String()
}

// 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) error {
log := logger.New(logger.JSONLog, zapcore.DebugLevel)
func JoinFromUnattestedNode(attVariant, csp, jsEndpoint string, log *logger.Logger) error {
joiner, err := newMaliciousJoiner(attVariant, csp, jsEndpoint, log)
if err != nil {
return fmt.Errorf("creating malicious joiner: %w", err)
Expand Down

0 comments on commit e2b6562

Please sign in to comment.