github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/cfg/flag_test.go (about)

     1  package cfg
     2  
     3  import (
     4  	"flag"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  // TestDefaults checks that defaults are correctly obtained from a
    13  // flagext.Registerer
    14  func TestDefaults(t *testing.T) {
    15  	data := Data{}
    16  	fs := flag.NewFlagSet(t.Name(), flag.PanicOnError)
    17  
    18  	err := Unmarshal(&data,
    19  		Defaults(fs),
    20  	)
    21  
    22  	require.NoError(t, err)
    23  	assert.Equal(t, Data{
    24  		Verbose: false,
    25  		Server: Server{
    26  			Port:    80,
    27  			Timeout: 60 * time.Second,
    28  		},
    29  		TLS: TLS{
    30  			Cert: "DEFAULTCERT",
    31  			Key:  "DEFAULTKEY",
    32  		},
    33  	}, data)
    34  }
    35  
    36  // TestFlags checks that defaults and flag values (they can't be separated) are
    37  // correctly obtained from the command line
    38  func TestFlags(t *testing.T) {
    39  	data := Data{}
    40  	fs := flag.NewFlagSet(t.Name(), flag.PanicOnError)
    41  	err := Unmarshal(&data,
    42  		Defaults(fs),
    43  		dFlags(fs, []string{"-server.timeout=10h", "-verbose"}),
    44  	)
    45  	require.NoError(t, err)
    46  
    47  	assert.Equal(t, Data{
    48  		Verbose: true,
    49  		Server: Server{
    50  			Port:    80,
    51  			Timeout: 10 * time.Hour,
    52  		},
    53  		TLS: TLS{
    54  			Cert: "DEFAULTCERT",
    55  			Key:  "DEFAULTKEY",
    56  		},
    57  	}, data)
    58  }