github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/sanity/check_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package sanity_test
    21  
    22  import (
    23  	"go/ast"
    24  	"go/parser"
    25  	"go/token"
    26  	"path/filepath"
    27  	"reflect"
    28  	"runtime"
    29  	"sort"
    30  	"strings"
    31  	"testing"
    32  
    33  	. "gopkg.in/check.v1"
    34  
    35  	"github.com/snapcore/snapd/osutil"
    36  	"github.com/snapcore/snapd/sanity"
    37  	"github.com/snapcore/snapd/testutil"
    38  )
    39  
    40  // Hook up check.v1 into the "go test" runner
    41  func Test(t *testing.T) { TestingT(t) }
    42  
    43  type sanitySuite struct {
    44  	testutil.BaseTest
    45  }
    46  
    47  func (s *sanitySuite) SetUpTest(c *C) {
    48  	restore := osutil.MockMountInfo("")
    49  	s.AddCleanup(restore)
    50  }
    51  
    52  var _ = Suite(&sanitySuite{})
    53  
    54  func (s *sanitySuite) TestRunHappy(c *C) {
    55  	var happyChecks []func() error
    56  	var happyCheckRan int
    57  
    58  	happyChecks = append(happyChecks, func() error {
    59  		happyCheckRan += 1
    60  		return nil
    61  	})
    62  
    63  	restore := sanity.MockChecks(happyChecks)
    64  	defer restore()
    65  
    66  	err := sanity.Check()
    67  	c.Check(err, IsNil)
    68  	c.Check(happyCheckRan, Equals, 1)
    69  }
    70  
    71  func (s *sanitySuite) TestRunNotHappy(c *C) {
    72  	var unhappyChecks []func() error
    73  	var unhappyCheckRan int
    74  
    75  	unhappyChecks = append(unhappyChecks, func() error {
    76  		unhappyCheckRan += 1
    77  		return nil
    78  	})
    79  
    80  	restore := sanity.MockChecks(unhappyChecks)
    81  	defer restore()
    82  
    83  	err := sanity.Check()
    84  	c.Check(err, IsNil)
    85  	c.Check(unhappyCheckRan, Equals, 1)
    86  }
    87  
    88  func (s *sanitySuite) TestUnexportedChecks(c *C) {
    89  	// collect what funcs we run in sanity.Check
    90  	var runCheckers []string
    91  	v := reflect.ValueOf(sanity.Checks())
    92  	for i := 0; i < v.Len(); i++ {
    93  		v := v.Index(i)
    94  		fname := runtime.FuncForPC(v.Pointer()).Name()
    95  		pos := strings.LastIndexByte(fname, '.')
    96  		checker := fname[pos+1:]
    97  		if !strings.HasPrefix(checker, "check") {
    98  			c.Fatalf(`%q in sanity.Checks does not have "check" prefix`, checker)
    99  		}
   100  		runCheckers = append(runCheckers, checker)
   101  	}
   102  
   103  	// collect all "check*" functions
   104  	goFiles, err := filepath.Glob("*.go")
   105  	c.Assert(err, IsNil)
   106  	fset := token.NewFileSet()
   107  
   108  	var checkers []string
   109  	for _, fn := range goFiles {
   110  		f, err := parser.ParseFile(fset, fn, nil, 0)
   111  		c.Assert(err, IsNil)
   112  		ast.Inspect(f, func(n ast.Node) bool {
   113  			switch x := n.(type) {
   114  			case *ast.File:
   115  				return true
   116  			case *ast.FuncDecl:
   117  				name := x.Name.Name
   118  				if strings.HasPrefix(name, "check") {
   119  					checkers = append(checkers, name)
   120  				}
   121  				return false
   122  			default:
   123  				return false
   124  			}
   125  		})
   126  	}
   127  
   128  	sort.Strings(checkers)
   129  	sort.Strings(runCheckers)
   130  
   131  	c.Check(checkers, DeepEquals, runCheckers)
   132  }