github.com/influxdata/influxdb/v2@v2.7.6/tsdb/config_test.go (about)

     1  package tsdb_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/BurntSushi/toml"
     8  	"github.com/influxdata/influxdb/v2/tsdb"
     9  )
    10  
    11  func TestConfig_Parse(t *testing.T) {
    12  	// Parse configuration.
    13  	c := tsdb.NewConfig()
    14  	if _, err := toml.Decode(`
    15  dir = "/var/lib/influxdb/data"
    16  wal-dir = "/var/lib/influxdb/wal"
    17  wal-fsync-delay = "10s"
    18  tsm-use-madv-willneed = true
    19  `, &c); err != nil {
    20  		t.Fatal(err)
    21  	}
    22  
    23  	if err := c.Validate(); err != nil {
    24  		t.Errorf("unexpected validate error: %s", err)
    25  	}
    26  
    27  	if got, exp := c.Dir, "/var/lib/influxdb/data"; got != exp {
    28  		t.Errorf("unexpected dir:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
    29  	}
    30  	if got, exp := c.WALDir, "/var/lib/influxdb/wal"; got != exp {
    31  		t.Errorf("unexpected wal-dir:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
    32  	}
    33  	if got, exp := c.WALFsyncDelay, time.Duration(10*time.Second); time.Duration(got).Nanoseconds() != exp.Nanoseconds() {
    34  		t.Errorf("unexpected wal-fsync-delay:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
    35  	}
    36  	if got, exp := c.TSMWillNeed, true; got != exp {
    37  		t.Errorf("unexpected tsm-madv-willneed:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
    38  	}
    39  }
    40  
    41  func TestConfig_Validate_Error(t *testing.T) {
    42  	c := tsdb.NewConfig()
    43  	if err := c.Validate(); err == nil || err.Error() != "Data.Dir must be specified" {
    44  		t.Errorf("unexpected error: %s", err)
    45  	}
    46  
    47  	c.Dir = "/var/lib/influxdb/data"
    48  	if err := c.Validate(); err == nil || err.Error() != "Data.WALDir must be specified" {
    49  		t.Errorf("unexpected error: %s", err)
    50  	}
    51  
    52  	c.WALDir = "/var/lib/influxdb/wal"
    53  	c.Engine = "fake1"
    54  	if err := c.Validate(); err == nil || err.Error() != "unrecognized engine fake1" {
    55  		t.Errorf("unexpected error: %s", err)
    56  	}
    57  
    58  	c.Engine = "tsm1"
    59  	c.Index = "foo"
    60  	if err := c.Validate(); err == nil || err.Error() != "unrecognized index foo" {
    61  		t.Errorf("unexpected error: %s", err)
    62  	}
    63  
    64  	c.Index = tsdb.TSI1IndexName
    65  	if err := c.Validate(); err != nil {
    66  		t.Error(err)
    67  	}
    68  
    69  	c.SeriesIDSetCacheSize = -1
    70  	if err := c.Validate(); err == nil || err.Error() != "series-id-set-cache-size must be non-negative" {
    71  		t.Errorf("unexpected error: %s", err)
    72  	}
    73  }
    74  
    75  func TestConfig_ByteSizes(t *testing.T) {
    76  	// Parse configuration.
    77  	c := tsdb.NewConfig()
    78  	if _, err := toml.Decode(`
    79  dir = "/var/lib/influxdb/data"
    80  wal-dir = "/var/lib/influxdb/wal"
    81  wal-fsync-delay = "10s"
    82  cache-max-memory-size = 5368709120
    83  cache-snapshot-memory-size = 104857600
    84  `, &c); err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	if err := c.Validate(); err != nil {
    89  		t.Errorf("unexpected validate error: %s", err)
    90  	}
    91  
    92  	if got, exp := c.Dir, "/var/lib/influxdb/data"; got != exp {
    93  		t.Errorf("unexpected dir:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
    94  	}
    95  	if got, exp := c.WALDir, "/var/lib/influxdb/wal"; got != exp {
    96  		t.Errorf("unexpected wal-dir:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
    97  	}
    98  	if got, exp := c.WALFsyncDelay, time.Duration(10*time.Second); time.Duration(got).Nanoseconds() != exp.Nanoseconds() {
    99  		t.Errorf("unexpected wal-fsync-delay:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
   100  	}
   101  	if got, exp := c.CacheMaxMemorySize, uint64(5<<30); uint64(got) != exp {
   102  		t.Errorf("unexpected cache-max-memory-size:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
   103  	}
   104  	if got, exp := c.CacheSnapshotMemorySize, uint64(100<<20); uint64(got) != exp {
   105  		t.Errorf("unexpected cache-snapshot-memory-size:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
   106  	}
   107  }
   108  
   109  func TestConfig_HumanReadableSizes(t *testing.T) {
   110  	// Parse configuration.
   111  	c := tsdb.NewConfig()
   112  	if _, err := toml.Decode(`
   113  dir = "/var/lib/influxdb/data"
   114  wal-dir = "/var/lib/influxdb/wal"
   115  wal-fsync-delay = "10s"
   116  cache-max-memory-size = "5gib"
   117  cache-snapshot-memory-size = "100mib"
   118  `, &c); err != nil {
   119  		t.Fatal(err)
   120  	}
   121  
   122  	if err := c.Validate(); err != nil {
   123  		t.Errorf("unexpected validate error: %s", err)
   124  	}
   125  
   126  	if got, exp := c.Dir, "/var/lib/influxdb/data"; got != exp {
   127  		t.Errorf("unexpected dir:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
   128  	}
   129  	if got, exp := c.WALDir, "/var/lib/influxdb/wal"; got != exp {
   130  		t.Errorf("unexpected wal-dir:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
   131  	}
   132  	if got, exp := c.WALFsyncDelay, time.Duration(10*time.Second); time.Duration(got).Nanoseconds() != exp.Nanoseconds() {
   133  		t.Errorf("unexpected wal-fsync-delay:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
   134  	}
   135  	if got, exp := c.CacheMaxMemorySize, uint64(5<<30); uint64(got) != exp {
   136  		t.Errorf("unexpected cache-max-memory-size:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
   137  	}
   138  	if got, exp := c.CacheSnapshotMemorySize, uint64(100<<20); uint64(got) != exp {
   139  		t.Errorf("unexpected cache-snapshot-memory-size:\n\nexp=%v\n\ngot=%v\n\n", exp, got)
   140  	}
   141  }