github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/cmd/lhsmd/agent/config_test.go (about)

     1  // Copyright (c) 2018 DDN. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package agent
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"path"
    11  	"reflect"
    12  	"runtime"
    13  	"testing"
    14  
    15  	"github.com/intel-hpdd/lemur/cmd/lhsmd/config"
    16  	"github.com/intel-hpdd/go-lustre/fs/spec"
    17  )
    18  
    19  func TestConfiguredPlugins(t *testing.T) {
    20  	loaded, err := LoadConfig("./test-fixtures/plugin-config")
    21  	if err != nil {
    22  		t.Fatalf("err: %s", err)
    23  	}
    24  
    25  	expected := []*PluginConfig{
    26  		{
    27  			Name:             "lhsm-plugin-posix",
    28  			BinPath:          config.DefaultPluginDir + "/lhsm-plugin-posix",
    29  			AgentConnection:  "",
    30  			ClientMount:      "/mnt/lhsmd/lhsm-plugin-posix",
    31  			RestartOnFailure: true,
    32  		},
    33  		{
    34  			Name:             "lhsm-plugin-s3",
    35  			BinPath:          config.DefaultPluginDir + "/lhsm-plugin-s3",
    36  			AgentConnection:  "",
    37  			ClientMount:      "/mnt/lhsmd/lhsm-plugin-s3",
    38  			RestartOnFailure: true,
    39  		},
    40  		{
    41  			Name:             "lhsm-plugin-noop",
    42  			BinPath:          config.DefaultPluginDir + "/lhsm-plugin-noop",
    43  			AgentConnection:  "",
    44  			ClientMount:      "/mnt/lhsmd/lhsm-plugin-noop",
    45  			RestartOnFailure: true,
    46  		},
    47  	}
    48  	t.Skip("TODO: Fix test to deal with unix socket")
    49  	got := loaded.Plugins()
    50  	if !reflect.DeepEqual(got, expected) {
    51  		t.Fatalf("\nexpected:\n%s\ngot:\n%s\n", expected, got)
    52  	}
    53  }
    54  
    55  func TestLoadConfig(t *testing.T) {
    56  	loaded, err := LoadConfig("./test-fixtures/good-config")
    57  	if err != nil {
    58  		t.Fatalf("err: %s", err)
    59  	}
    60  
    61  	expectedDevice, err := spec.ClientDeviceFromString("10.211.55.37@tcp0:/testFs")
    62  	if err != nil {
    63  		t.Fatalf("err: %s", err)
    64  	}
    65  	expected := &Config{
    66  		MountRoot:    "/mnt/lhsmd",
    67  		ClientDevice: expectedDevice,
    68  		ClientMountOptions: []string{
    69  			"user_xattr",
    70  		},
    71  		Processes: runtime.NumCPU(),
    72  		InfluxDB: &influxConfig{
    73  			URL: "http://172.17.0.4:8086",
    74  			DB:  "lhsmd",
    75  		},
    76  		EnabledPlugins: []string{
    77  			"lhsm-plugin-posix",
    78  		},
    79  		Snapshots: &snapshotConfig{
    80  			Enabled: false,
    81  		},
    82  		PluginDir: "/go/bin",
    83  		Transport: &transportConfig{
    84  			Type:      "grpc",
    85  			SocketDir: "/tmp",
    86  		},
    87  	}
    88  
    89  	if !reflect.DeepEqual(loaded, expected) {
    90  		t.Fatalf("\nexpected:\n%s\ngot:\n%s\n", expected, loaded)
    91  	}
    92  }
    93  
    94  func TestMergedConfig(t *testing.T) {
    95  	defCfg := NewConfig()
    96  	loaded, err := LoadConfig("./test-fixtures/merge-config")
    97  	if err != nil {
    98  		t.Fatalf("err: %s", err)
    99  	}
   100  	got := defCfg.Merge(loaded)
   101  
   102  	expectedDevice, err := spec.ClientDeviceFromString("10.211.55.37@tcp0:/testFs")
   103  	if err != nil {
   104  		t.Fatalf("err: %s", err)
   105  	}
   106  	expected := &Config{
   107  		MountRoot:    "/mnt/lhsmd",
   108  		ClientDevice: expectedDevice,
   109  		ClientMountOptions: []string{
   110  			"user_xattr",
   111  		},
   112  		Processes: runtime.NumCPU(),
   113  		InfluxDB: &influxConfig{
   114  			URL: "http://172.17.0.4:8086",
   115  			DB:  "lhsmd",
   116  		},
   117  		EnabledPlugins: []string{
   118  			"lhsm-plugin-posix",
   119  		},
   120  		PluginDir: "/go/bin",
   121  		Snapshots: &snapshotConfig{
   122  			Enabled: false,
   123  		},
   124  		Transport: &transportConfig{
   125  			Type:      "grpc",
   126  			SocketDir: "/var/run/lhsmd",
   127  		},
   128  	}
   129  
   130  	if !reflect.DeepEqual(got, expected) {
   131  		t.Fatalf("\nexpected:\n%s\ngot:\n%s\n", expected, got)
   132  	}
   133  }
   134  
   135  func TestJsonConfig(t *testing.T) {
   136  	cfg, err := LoadConfig("./test-fixtures/json-config")
   137  
   138  	if err != nil {
   139  		t.Fatalf("Error from LoadConfig(): %s", err)
   140  	}
   141  
   142  	if cfg.ClientDevice == nil {
   143  		t.Fatal("ClientDevice should not be nil")
   144  	}
   145  }
   146  
   147  func TestConfigSaveLoad(t *testing.T) {
   148  	startCfg := DefaultConfig()
   149  	cd, err := spec.ClientDeviceFromString("1.2.3.4@tcp:/foo")
   150  	if err != nil {
   151  		t.Fatal(err)
   152  	}
   153  	startCfg.ClientDevice = cd
   154  
   155  	td, err := ioutil.TempDir("", "agent-config-test")
   156  	if err != nil {
   157  		t.Fatal(err)
   158  	}
   159  	defer os.RemoveAll(td)
   160  
   161  	cfgFile := path.Join(td, "cfg")
   162  	if err = ioutil.WriteFile(cfgFile, []byte(startCfg.String()), 0644); err != nil {
   163  		t.Fatal(err)
   164  	}
   165  
   166  	loaded, err := LoadConfig(cfgFile)
   167  	if err != nil {
   168  		t.Fatal(err)
   169  	}
   170  
   171  	if startCfg.String() != loaded.String() {
   172  		t.Fatalf("start cfg != loaded\nstart:\n%s\nloaded:\n%s\n", startCfg, loaded)
   173  	}
   174  }