github.com/xmidt-org/webpa-common@v1.11.9/server/viper_test.go (about)

     1  package server
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/spf13/pflag"
    10  	"github.com/spf13/viper"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func ExampleInitialize() {
    16  	_, _, webPA, err := Initialize("example", nil, nil, viper.New())
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  
    21  	fmt.Println(webPA.Primary.Name)
    22  	fmt.Println(webPA.Primary.Address)
    23  	fmt.Println(webPA.Primary.LogConnectionState)
    24  
    25  	fmt.Println(webPA.Alternate.Name)
    26  	fmt.Println(webPA.Alternate.Address)
    27  	fmt.Println(webPA.Alternate.LogConnectionState)
    28  
    29  	fmt.Println(webPA.Health.Name)
    30  	fmt.Println(webPA.Health.Address)
    31  	fmt.Println(webPA.Health.LogInterval)
    32  	fmt.Println(webPA.Health.Options)
    33  
    34  	// Output:
    35  	// example
    36  	// localhost:10010
    37  	// true
    38  	// example.alternate
    39  	// :10011
    40  	// false
    41  	// example.health
    42  	// :9001
    43  	// 45s
    44  	// [TotalRequests TotalResponses SomeOtherStat]
    45  }
    46  
    47  func ExampleInitializeWithFlags() {
    48  	var (
    49  		f = pflag.NewFlagSet("applicationName", pflag.ContinueOnError)
    50  		v = viper.New()
    51  
    52  		// simulates passing `-f example` on the command line
    53  		_, _, webPA, err = Initialize("applicationName", []string{"-f", "example"}, f, v)
    54  	)
    55  
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  
    60  	fmt.Println(webPA.Primary.Name)
    61  	fmt.Println(webPA.Primary.Address)
    62  	fmt.Println(webPA.Primary.LogConnectionState)
    63  
    64  	fmt.Println(webPA.Alternate.Name)
    65  	fmt.Println(webPA.Alternate.Address)
    66  	fmt.Println(webPA.Alternate.LogConnectionState)
    67  
    68  	fmt.Println(webPA.Health.Name)
    69  	fmt.Println(webPA.Health.Address)
    70  	fmt.Println(webPA.Health.LogInterval)
    71  	fmt.Println(webPA.Health.Options)
    72  
    73  	// Output:
    74  	// applicationName
    75  	// localhost:10010
    76  	// true
    77  	// applicationName.alternate
    78  	// :10011
    79  	// false
    80  	// applicationName.health
    81  	// :9001
    82  	// 45s
    83  	// [TotalRequests TotalResponses SomeOtherStat]
    84  }
    85  
    86  func TestConfigureWhenParseError(t *testing.T) {
    87  	var (
    88  		assert = assert.New(t)
    89  
    90  		f   = pflag.NewFlagSet("applicationName", pflag.ContinueOnError)
    91  		v   = viper.New()
    92  		err = Configure("applicationName", []string{"-unknown"}, f, v)
    93  	)
    94  
    95  	assert.NotNil(err)
    96  }
    97  
    98  func TestInitializeWhenConfigureError(t *testing.T) {
    99  	var (
   100  		assert = assert.New(t)
   101  
   102  		f = pflag.NewFlagSet("applicationName", pflag.ContinueOnError)
   103  		v = viper.New()
   104  
   105  		logger, registry, webPA, err = Initialize("applicationName", []string{"-unknown"}, f, v)
   106  	)
   107  
   108  	assert.NotNil(logger)
   109  	assert.Nil(registry)
   110  	assert.Nil(webPA)
   111  	assert.NotNil(err)
   112  }
   113  
   114  func TestInitializeWhenReadInConfigError(t *testing.T) {
   115  	var (
   116  		assert = assert.New(t)
   117  
   118  		f = pflag.NewFlagSet("applicationName", pflag.ContinueOnError)
   119  		v = viper.New()
   120  
   121  		logger, registry, webPA, err = Initialize("applicationName", []string{"-f", "thisfiledoesnotexist"}, f, v)
   122  	)
   123  
   124  	assert.NotNil(logger)
   125  	assert.Nil(registry)
   126  	assert.Nil(webPA)
   127  	assert.NotNil(err)
   128  }
   129  
   130  func TestInitializeWhenWebPAUnmarshalError(t *testing.T) {
   131  	var (
   132  		assert = assert.New(t)
   133  
   134  		f = pflag.NewFlagSet("invalidWebPA", pflag.ContinueOnError)
   135  		v = viper.New()
   136  
   137  		logger, registry, webPA, err = Initialize("invalidWebPA", nil, f, v)
   138  	)
   139  
   140  	assert.NotNil(logger)
   141  	assert.Nil(registry)
   142  	assert.Nil(webPA)
   143  	assert.NotNil(err)
   144  }
   145  
   146  func TestInitializeWhenWebPANewLoggerError(t *testing.T) {
   147  	var (
   148  		assert = assert.New(t)
   149  
   150  		f = pflag.NewFlagSet("invalidLog", pflag.ContinueOnError)
   151  		v = viper.New()
   152  
   153  		logger, registry, webPA, err = Initialize("invalidLog", nil, f, v)
   154  	)
   155  
   156  	assert.NotNil(logger)
   157  	assert.NotNil(registry)
   158  	assert.NotNil(webPA)
   159  	assert.Nil(err)
   160  }
   161  
   162  func TestInitializeMetrics(t *testing.T) {
   163  	var (
   164  		assert  = assert.New(t)
   165  		require = require.New(t)
   166  		v       = viper.New()
   167  		w       = new(WebPA)
   168  	)
   169  
   170  	v.SetConfigType("json")
   171  	require.NoError(v.ReadConfig(strings.NewReader(`
   172  		{
   173  			"metric": {
   174  				"address": ":8080",
   175  				"metricsOptions": {
   176  					"namespace": "foo",
   177  					"subsystem": "bar"
   178  				}
   179  			}
   180  		}
   181  	`)))
   182  
   183  	require.NoError(v.Unmarshal(w))
   184  
   185  	assert.Equal("foo", w.Metric.MetricsOptions.Namespace)
   186  	assert.Equal("bar", w.Metric.MetricsOptions.Subsystem)
   187  }
   188  
   189  func TestCreateCPUProfiles(t *testing.T) {
   190  	t.Run("test case with flag", testCreateCPUProfileFile)
   191  	t.Run("test case with no flag", testCreateMemProfileFileNoFlag)
   192  }
   193  
   194  // ./app --cpuprofile=filename
   195  func testCreateCPUProfileFile(t *testing.T) {
   196  	var (
   197  		v         = viper.New()
   198  		f         = pflag.NewFlagSet("test", pflag.ContinueOnError)
   199  		app       = ""
   200  		inputFlag = "--cpuprofile=file"
   201  		_         = f.StringP(CPUProfileFlagName, CPUProfileShorthand, "cpuprofile", "base name of the cpuprofile file")
   202  		input     = []string{app, inputFlag}
   203  	)
   204  
   205  	f.Parse(input)
   206  	// ./themis --cpuprofile=filename
   207  
   208  	CreateCPUProfileFile(v, f, nil)
   209  
   210  	if _, err := os.Stat("file"); os.IsNotExist(err) {
   211  		t.Fatalf("Expecting file to exist")
   212  	}
   213  
   214  	if _, err := os.Stat("file"); !os.IsNotExist(err) {
   215  		os.Remove("cpuprofile")
   216  	}
   217  }
   218  
   219  // testCreateCPUProfileFileNoFlag tests if function completes fine without the desired flag
   220  // --cpupropfile=""
   221  func testCreateCPUProfileFileNoFlag(t *testing.T) {
   222  	var (
   223  		v         = viper.New()
   224  		f         = pflag.NewFlagSet("test", pflag.ContinueOnError)
   225  		app       = "testApp"
   226  		inputFlag = ""
   227  		_         = f.StringP(CPUProfileFlagName, CPUProfileShorthand, "cpuprofile", "base name of the cpuprofile file")
   228  		input     = []string{app, inputFlag}
   229  	)
   230  
   231  	f.Parse(input)
   232  
   233  	CreateCPUProfileFile(v, f, nil)
   234  }
   235  
   236  func TestCreateMemProfiles(t *testing.T) {
   237  	t.Run("test case with flag", testCreateMemProfileFile)
   238  	t.Run("test case with no flag", testCreateMemProfileFileNoFlag)
   239  }
   240  
   241  func testCreateMemProfileFile(t *testing.T) {
   242  	var (
   243  		v         = viper.New()
   244  		f         = pflag.NewFlagSet("test", pflag.ContinueOnError)
   245  		app       = "testApp"
   246  		inputFlag = "--memprofile=file"
   247  
   248  		_     = f.StringP(MemProfileFlagName, MemProfileShorthand, "memprofile", "base name of the memprofile file")
   249  		input = []string{app, inputFlag}
   250  	)
   251  
   252  	f.Parse(input)
   253  
   254  	CreateMemoryProfileFile(v, f, nil)
   255  
   256  	if _, err := os.Stat("file"); os.IsNotExist(err) {
   257  		t.Fatalf("Expecting file to exist")
   258  	}
   259  
   260  	if _, err := os.Stat("file"); !os.IsNotExist(err) {
   261  		os.Remove("file")
   262  	}
   263  }
   264  
   265  // testCreateCPUProfileFileNoFlag tests if function completes fine without the desired flag
   266  // --memprofile=""
   267  func testCreateMemProfileFileNoFlag(t *testing.T) {
   268  	var (
   269  		v         = viper.New()
   270  		f         = pflag.NewFlagSet("test", pflag.ContinueOnError)
   271  		app       = "testApp"
   272  		inputFlag = ""
   273  		_         = f.StringP(MemProfileFlagName, MemProfileShorthand, "memprofile", "base name of the memprofile file")
   274  		input     = []string{app, inputFlag}
   275  	)
   276  
   277  	f.Parse(input)
   278  
   279  	CreateMemoryProfileFile(v, f, nil)
   280  }