pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/knf/validators/system/validators_test.go (about)

     1  package system
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"io/ioutil"
    12  	"testing"
    13  
    14  	"pkg.re/essentialkaos/ek.v12/knf"
    15  
    16  	. "pkg.re/essentialkaos/check.v1"
    17  )
    18  
    19  // ////////////////////////////////////////////////////////////////////////////////// //
    20  
    21  const _CONFIG_DATA = `
    22  [user]
    23    test0:
    24    test1: root
    25    test2: somerandomuser
    26  
    27  [group]
    28    test0:
    29    test1: daemon
    30    test2: somerandomgroup
    31  
    32  [interface]
    33    test0:
    34    test1: lo
    35    test2: abc
    36  `
    37  
    38  // ////////////////////////////////////////////////////////////////////////////////// //
    39  
    40  type ValidatorSuite struct{}
    41  
    42  // ////////////////////////////////////////////////////////////////////////////////// //
    43  
    44  var _ = Suite(&ValidatorSuite{})
    45  
    46  // ////////////////////////////////////////////////////////////////////////////////// //
    47  
    48  func Test(t *testing.T) {
    49  	TestingT(t)
    50  }
    51  
    52  // ////////////////////////////////////////////////////////////////////////////////// //
    53  
    54  func (s *ValidatorSuite) TestUserValidator(c *C) {
    55  	configFile := createConfig(c, _CONFIG_DATA)
    56  
    57  	err := knf.Global(configFile)
    58  	c.Assert(err, IsNil)
    59  
    60  	errs := knf.Validate([]*knf.Validator{
    61  		{"user:test0", User, nil},
    62  		{"user:test1", User, nil},
    63  	})
    64  
    65  	c.Assert(errs, HasLen, 0)
    66  
    67  	errs = knf.Validate([]*knf.Validator{
    68  		{"user:test2", User, nil},
    69  	})
    70  
    71  	c.Assert(errs, HasLen, 1)
    72  }
    73  
    74  func (s *ValidatorSuite) TestGroupValidator(c *C) {
    75  	configFile := createConfig(c, _CONFIG_DATA)
    76  
    77  	err := knf.Global(configFile)
    78  	c.Assert(err, IsNil)
    79  
    80  	errs := knf.Validate([]*knf.Validator{
    81  		{"group:test0", Group, nil},
    82  		{"group:test1", Group, nil},
    83  	})
    84  
    85  	c.Assert(errs, HasLen, 0)
    86  
    87  	errs = knf.Validate([]*knf.Validator{
    88  		{"group:test2", Group, nil},
    89  	})
    90  
    91  	c.Assert(errs, HasLen, 1)
    92  }
    93  
    94  // ////////////////////////////////////////////////////////////////////////////////// //
    95  
    96  func createConfig(c *C, data string) string {
    97  	configPath := c.MkDir() + "/config.knf"
    98  
    99  	err := ioutil.WriteFile(configPath, []byte(data), 0644)
   100  
   101  	if err != nil {
   102  		c.Fatal(err.Error())
   103  	}
   104  
   105  	return configPath
   106  }