github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/section_config/section_config_z_unit_test.go (about)

     1  package section_config_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"syscall"
     7  	"testing"
     8  
     9  	. "github.com/onsi/gomega"
    10  
    11  	"github.com/machinefi/w3bstream/pkg/depends/base/cmd"
    12  	"github.com/machinefi/w3bstream/pkg/depends/base/types"
    13  	. "github.com/machinefi/w3bstream/pkg/depends/conf/section_config"
    14  )
    15  
    16  type Config struct {
    17  	Section
    18  	Command      cmd.Command             `name:"command"`
    19  	AutoStart    bool                    `name:"autostart"`
    20  	AutoRestart  bool                    `name:"autorestart"`
    21  	StartSecs    types.Second            `name:"startsecs"`
    22  	StartRetries int                     `name:"startretries"`
    23  	User         string                  `name:"user"`
    24  	Priority     int                     `name:"priority"`
    25  	ExitCodes    types.CommaSplitInts    `name:"exitcodes"`
    26  	StopSignal   types.Signal            `name:"stopsignal"`
    27  	Envs         types.CommaSplitStrings `name:"environment"`
    28  	*LogConfig
    29  }
    30  
    31  func (c *Config) GetSection() *Section { return &c.Section }
    32  
    33  func (c *Config) SetSection(k, v string) { c.Name, c.Value = k, v }
    34  
    35  type LogConfig struct {
    36  	StdoutLogFile         string   `name:"stdout_logfile"`
    37  	StdoutLogFileMaxBytes types.MB `name:"stdout_logfile_maxbytes"`
    38  	StdoutLogFileBackups  int      `name:"stdout_logfile_backups"`
    39  }
    40  
    41  func TestLoadFile(t *testing.T) {
    42  	var (
    43  		cwd, _ = os.Getwd()
    44  		path   = filepath.Join(cwd, "__example__", "demo.conf")
    45  		cfg    = &Config{
    46  			Section:      Section{Name: "program", Value: "crond"},
    47  			Command:      cmd.Command{Name: "/opt/iTR/core/sbin/crond", Args: []string{"-n"}},
    48  			AutoStart:    true,
    49  			AutoRestart:  true,
    50  			StartSecs:    types.Second(3),
    51  			StartRetries: 10,
    52  			User:         "root",
    53  			Priority:     200,
    54  			ExitCodes:    types.CommaSplitInts{0},
    55  			StopSignal:   types.Signal(syscall.SIGTERM),
    56  			Envs:         types.CommaSplitStrings{`PATH="/opt/iTR/core/bin:/opt/iTR/core/sbin"`},
    57  			LogConfig: &LogConfig{
    58  				StdoutLogFile:         "/opt/iTR/core/var/log/supervisor/crond.log",
    59  				StdoutLogFileMaxBytes: types.MB(1),
    60  				StdoutLogFileBackups:  3,
    61  			},
    62  		}
    63  		cpy = &Config{}
    64  	)
    65  
    66  	if err := NewEncoder('=').MarshalToFile(cfg, path); err != nil {
    67  		t.Log(err)
    68  		return
    69  	}
    70  
    71  	if err := NewDecoder('=').UnmarshalFromFile(cpy, path); err != nil {
    72  		t.Log(err)
    73  		return
    74  	}
    75  
    76  	NewWithT(t).Expect(cpy).To(Equal(cfg))
    77  
    78  }