github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/cmd/testsuite/html.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"html/template"
     6  	"io"
     7  	"sort"
     8  
     9  	"github.com/dylandreimerink/gobpfld/kernelsupport"
    10  )
    11  
    12  func renderHTMLReport(testResults map[string]map[string]testResult, out io.Writer) error {
    13  	tpl := template.New("report")
    14  
    15  	var err error
    16  	tpl, err = tpl.Parse(htmlTpl)
    17  	if err != nil {
    18  		return fmt.Errorf("parse tpl: %w", err)
    19  	}
    20  
    21  	envs := make([]string, 0, len(testResults))
    22  	for k := range testResults {
    23  		envs = append(envs, k)
    24  	}
    25  
    26  	// Sort environments from oldest to newest
    27  	sort.Slice(envs, func(i, j int) bool {
    28  		iV, err := kernelsupport.ParseKernelVersion(availableEnvs[envs[i]].kernel, true)
    29  		if err != nil {
    30  			panic(err)
    31  		}
    32  
    33  		jV, err := kernelsupport.ParseKernelVersion(availableEnvs[envs[j]].kernel, true)
    34  		if err != nil {
    35  			panic(err)
    36  		}
    37  
    38  		return jV.Higher(iV)
    39  	})
    40  
    41  	testsMap := make(map[string]bool)
    42  	for _, t := range testResults {
    43  		for testName := range t {
    44  			testsMap[testName] = true
    45  		}
    46  	}
    47  
    48  	tests := make([]string, len(testsMap))
    49  	i := 0
    50  	for testsMap := range testsMap {
    51  		tests[i] = testsMap
    52  		i++
    53  	}
    54  	sort.Strings(tests)
    55  
    56  	err = tpl.Execute(out, htmlData{
    57  		Envs:         envs,
    58  		Tests:        tests,
    59  		TestResults:  testResults,
    60  		FlagCoverage: flagCover,
    61  	})
    62  	if err != nil {
    63  		return fmt.Errorf("execute tpl: %w", err)
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  type htmlData struct {
    70  	Envs         []string
    71  	Tests        []string
    72  	TestResults  map[string]map[string]testResult
    73  	FlagCoverage bool
    74  }
    75  
    76  var htmlTpl = `<html>
    77  	<head>
    78  		<title>GoBPFLD test report</title>
    79  		<style>
    80  			.test-matrix {
    81  				border-spacing: 0px;
    82  			}
    83  
    84  			.test-matrix td {
    85  				padding: 4px;
    86  				border-width: 1px 0px 0px 0px;
    87  				border-style: solid;
    88  			}
    89  
    90  			.test-matrix td.result {
    91  				border-width: 1px 0px 0px 1px;
    92  			}
    93  
    94  			td.PASS {
    95  				background-color: #50CC50;
    96  			}
    97  
    98  			td.FAIL {
    99  				background-color: #FF3333;
   100  			}
   101  
   102  			td.SKIP {
   103  				background-color: #FFC107;
   104  			}
   105  
   106  			td.UNTESTED {
   107  				background-color: #BDBDBD;
   108  			}
   109  		</style>
   110  	</head>
   111  	<body>
   112  		<h1>GoBPFLD test report</h1>
   113  		<div>
   114  			<h2>Test matrix</h2>
   115  			<table class="test-matrix">
   116  				<thead>
   117  					<tr>
   118  						<th>Test name</th>
   119  					{{range $i, $env := .Envs}}
   120  						<th>{{$env}}</th>
   121  					{{end}}
   122  					</tr>
   123  				<thead>
   124  				<tbody>
   125  				{{range $i, $test := .Tests}}
   126  					<tr>
   127  						<td>{{$test}}</td>
   128  					{{range $ii, $env := $.Envs}}
   129  					{{with $res := index (index $.TestResults $env) $test}}
   130  						<td class="{{$res.Status}} result">{{$res.Status}}</td>
   131  					{{end}}
   132  					{{end}}
   133  					</tr>
   134  				{{end}}
   135  				</tbody>
   136  			</table>
   137  		</div>
   138  		<div>
   139  			<h2>Code coverage per env</h2>
   140  			<ul>
   141  			{{range $i, $env := .Envs}}
   142  				<li><a href="./{{$env}}/gobpfld.cover.html">{{$env}}</a></li>
   143  			{{end}}
   144  			</ul>
   145  		</div>
   146  	</body>
   147  </html>`