github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/tools/test_monitor/level1/process_summary1_results_test.go (about) 1 package main 2 3 import ( 4 "log" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/onflow/flow-go/tools/test_monitor/common/testdata" 14 ) 15 16 func TestGenerateLevel1Summary_Struct(t *testing.T) { 17 const rawJsonFilePath = "../testdata/summary1/raw" 18 19 // data driven table test 20 testDataMap := map[string]testdata.Level1TestData{ 21 "1 count all pass": { 22 ExpectedLevel1Summary: testdata.GetTestData_Level1_1CountPass(), 23 RawJSONTestRunFile: "test-result-crypto-hash-1-count-pass.json", 24 }, 25 26 "1 count 1 fail the rest pass": { 27 ExpectedLevel1Summary: testdata.GetTestData_Level1_1Count1FailRestPass(), 28 RawJSONTestRunFile: "test-result-crypto-hash-1-count-fail.json", 29 }, 30 31 "1 count 2 skipped the rest pass": { 32 ExpectedLevel1Summary: testdata.GetTestData_Level1_1CountAllPass(), 33 RawJSONTestRunFile: "test-result-crypto-hash-1-count-skip-pass.json", 34 }, 35 36 // raw results generated with: go test -json -count 1 ./utils/unittest/... 37 "2 count all pass": { 38 ExpectedLevel1Summary: testdata.GetTestData_Level1_2CountPass(), 39 RawJSONTestRunFile: "test-result-crypto-hash-2-count-pass.json", 40 }, 41 42 // raw results generated with: go test -json -count 1 ./utils/unittest/... 43 "10 count all pass": { 44 ExpectedLevel1Summary: testdata.GetTestData_Level1_10CountPass(), 45 RawJSONTestRunFile: "test-result-crypto-hash-10-count-pass.json", 46 }, 47 48 // raw results generated with: go test -json -count 1 ./utils/unittest/... 49 "10 count some failures": { 50 ExpectedLevel1Summary: testdata.GetTestData_Level1_10CountSomeFailures(), 51 RawJSONTestRunFile: "test-result-crypto-hash-10-count-fail.json", 52 }, 53 54 // no result tests - tests below don't generate pass/fail result due to `go test` bug 55 // with using `fmt.printf("log message")` without newline `\n` 56 57 // raw results generated with: go test -v -count=1 -json ./model/encodable/. -test.run TestEncodableRandomBeaconPrivKeyMsgPack 58 // this is a single unit test that produces a no result 59 "1 count single no result test": { 60 ExpectedLevel1Summary: testdata.GetTestData_Level1_1CountSingleExceptionTest(), 61 RawJSONTestRunFile: "test-result-exception-single-1-count-pass.json", 62 }, 63 64 //raw results generated with: go test -v -count=5 -json ./model/encodable/. -test.run TestEncodableRandomBeaconPrivKeyMsgPack 65 //multiple no result tests in a row 66 "5 no result tests in a row": { 67 ExpectedLevel1Summary: testdata.GetTestData_Level1_5CountSingleExceptionTest(), 68 RawJSONTestRunFile: "test-result-exception-single-5-count-pass.json", 69 }, 70 71 //normal test at the end of a test run with multiple no result tests in front of it 72 "4 no result tests in a row, 1 normal test": { 73 ExpectedLevel1Summary: testdata.GetTestData_Level1_5CountMultipleExceptionTests(), 74 RawJSONTestRunFile: "test-result-exception-single-5-count-4-nil-1-normal-pass.json", 75 }, 76 77 // raw results generated with: go test -v -count=3 -json ./model/encodable/. 78 // group of unit tests with a single no result test 79 "3 count no result test with normal tests": { 80 ExpectedLevel1Summary: testdata.GetTestData_Leve1_3CountExceptionWithNormalTests(), 81 RawJSONTestRunFile: "test-result-exception-others-normal-3-count-pass.json", 82 }, 83 } 84 85 require.NoError(t, os.Setenv("COMMIT_DATE", testdata.COMMIT_DATE)) 86 require.NoError(t, os.Setenv("COMMIT_SHA", testdata.COMMIT_SHA)) 87 require.NoError(t, os.Setenv("JOB_STARTED", testdata.JOB_STARTED)) 88 require.NoError(t, os.Setenv("RUN_ID", testdata.RUN_ID)) 89 90 for k, testData := range testDataMap { 91 t.Run(k, func(t *testing.T) { 92 // simulate generating raw "go test -json" output by loading output from saved file 93 resultReader := FileResultReader{ 94 rawJsonFile: filepath.Join(rawJsonFilePath, testData.RawJSONTestRunFile), 95 } 96 // ***************************************************** 97 actualLevel1Summary, _ := generateLevel1Summary(&resultReader) 98 // ***************************************************** 99 100 require.ElementsMatch(t, testData.ExpectedLevel1Summary, actualLevel1Summary, "actual and expected level 1 summary do not match") 101 }) 102 } 103 } 104 105 // read raw results from local json file - for testing 106 type FileResultReader struct { 107 rawJsonFile string 108 file *os.File 109 } 110 111 // return reader for reading from local json file - for testing 112 func (fileResultReader *FileResultReader) getReader() *os.File { 113 f, err := os.Open(fileResultReader.rawJsonFile) 114 if err != nil { 115 log.Fatal("error opening file: " + err.Error()) 116 } 117 fileResultReader.file = f 118 return f 119 } 120 121 func (fileResultReader *FileResultReader) close() { 122 err := fileResultReader.file.Close() 123 if err != nil { 124 log.Fatal("error closing file: " + err.Error()) 125 } 126 } 127 128 // tests will create their own local result file based on time stamp vs production which uses a supplied result file name 129 func (fileResultReader FileResultReader) getResultsFileName() string { 130 t := time.Now() 131 return "test-run-" + strings.ReplaceAll(t.Format("2006-01-02-15-04-05.0000"), ".", "-") + ".json" 132 }