bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/csconfig/common_test.go (about)

     1  package csconfig
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestLoadCommon(t *testing.T) {
    13  	PidDirFullPath, err := filepath.Abs("./tests/")
    14  	if err != nil {
    15  		t.Fatalf(err.Error())
    16  	}
    17  
    18  	LogDirFullPath, err := filepath.Abs("./tests/log/")
    19  	if err != nil {
    20  		t.Fatalf(err.Error())
    21  	}
    22  
    23  	WorkingDirFullPath, err := filepath.Abs("./tests")
    24  	if err != nil {
    25  		t.Fatalf(err.Error())
    26  	}
    27  
    28  	tests := []struct {
    29  		name           string
    30  		Input          *Config
    31  		expectedResult *CommonCfg
    32  		err            string
    33  	}{
    34  		{
    35  			name: "basic valid configuration",
    36  			Input: &Config{
    37  				Common: &CommonCfg{
    38  					Daemonize:  true,
    39  					PidDir:     "./tests",
    40  					LogMedia:   "file",
    41  					LogDir:     "./tests/log/",
    42  					WorkingDir: "./tests/",
    43  				},
    44  			},
    45  			expectedResult: &CommonCfg{
    46  				Daemonize:  true,
    47  				PidDir:     PidDirFullPath,
    48  				LogMedia:   "file",
    49  				LogDir:     LogDirFullPath,
    50  				WorkingDir: WorkingDirFullPath,
    51  			},
    52  		},
    53  		{
    54  			name: "empty working dir",
    55  			Input: &Config{
    56  				Common: &CommonCfg{
    57  					Daemonize: true,
    58  					PidDir:    "./tests",
    59  					LogMedia:  "file",
    60  					LogDir:    "./tests/log/",
    61  				},
    62  			},
    63  			expectedResult: &CommonCfg{
    64  				Daemonize: true,
    65  				PidDir:    PidDirFullPath,
    66  				LogMedia:  "file",
    67  				LogDir:    LogDirFullPath,
    68  			},
    69  		},
    70  		{
    71  			name:           "no common",
    72  			Input:          &Config{},
    73  			expectedResult: nil,
    74  		},
    75  	}
    76  
    77  	for idx, test := range tests {
    78  		err := test.Input.LoadCommon()
    79  		if err == nil && test.err != "" {
    80  			fmt.Printf("TEST '%s': NOK\n", test.name)
    81  			t.Fatalf("%d/%d expected error, didn't get it", idx, len(tests))
    82  		} else if test.err != "" {
    83  			if !strings.HasPrefix(fmt.Sprintf("%s", err), test.err) {
    84  				fmt.Printf("TEST '%s': NOK\n", test.name)
    85  				t.Fatalf("%d/%d expected '%s' got '%s'", idx, len(tests),
    86  					test.err,
    87  					fmt.Sprintf("%s", err))
    88  			}
    89  		}
    90  
    91  		isOk := assert.Equal(t, test.expectedResult, test.Input.Common)
    92  		if !isOk {
    93  			t.Fatalf("TEST '%s': NOK", test.name)
    94  		} else {
    95  			fmt.Printf("TEST '%s': OK\n", test.name)
    96  		}
    97  	}
    98  }