github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/syz-cluster/pkg/report/email_test.go (about)

     1  // Copyright 2025 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package report
     5  
     6  import (
     7  	"encoding/json"
     8  	"flag"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/google/syzkaller/syz-cluster/pkg/api"
    15  	"github.com/google/syzkaller/syz-cluster/pkg/app"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  var flagWrite = flag.Bool("write", false, "overwrite out.txt files")
    20  
    21  func TestRender(t *testing.T) {
    22  	config := &app.EmailConfig{
    23  		Name:         "syzbot",
    24  		DocsLink:     "http://docs/link",
    25  		CreditEmail:  "credit@email.com",
    26  		SupportEmail: "support@email.com",
    27  	}
    28  	flag.Parse()
    29  	basePath := "testdata"
    30  	files, err := os.ReadDir(basePath)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	for _, file := range files {
    35  		if filepath.Ext(file.Name()) != ".json" {
    36  			continue
    37  		}
    38  		fullName := file.Name()
    39  		name := strings.TrimSuffix(fullName, ".in.json")
    40  		t.Run(name, func(t *testing.T) {
    41  			t.Parallel()
    42  			inPath := filepath.Join(basePath, fullName)
    43  			inputData, err := os.ReadFile(inPath)
    44  			assert.NoError(t, err)
    45  
    46  			var report api.SessionReport
    47  			err = json.Unmarshal(inputData, &report)
    48  			assert.NoError(t, err)
    49  
    50  			for _, value := range []bool{false, true} {
    51  				report.Moderation = value
    52  				suffix := "upstream"
    53  				if value {
    54  					suffix = "moderation"
    55  				}
    56  				t.Run(suffix, func(t *testing.T) {
    57  					output, err := Render(&report, config)
    58  					assert.NoError(t, err)
    59  
    60  					outPath := filepath.Join(basePath, name+"."+suffix+".txt")
    61  					if *flagWrite {
    62  						err := os.WriteFile(outPath, output, 0644)
    63  						assert.NoError(t, err)
    64  					} else {
    65  						expected, err := os.ReadFile(outPath)
    66  						assert.NoError(t, err)
    67  						assert.Equal(t, string(expected), string(output))
    68  					}
    69  				})
    70  			}
    71  		})
    72  	}
    73  }