github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/cmd/lhsm-plugin-s3/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 main
     6  
     7  import (
     8  	"os"
     9  	"path"
    10  	"reflect"
    11  	"testing"
    12  
    13  	"github.com/aws/aws-sdk-go/service/s3/s3manager"
    14  	"github.com/intel-hpdd/lemur/cmd/lhsmd/config"
    15  	"github.com/intel-hpdd/lemur/dmplugin"
    16  	"github.com/intel-hpdd/lemur/internal/testhelpers"
    17  	"github.com/intel-hpdd/lemur/pkg/fsroot"
    18  )
    19  
    20  func TestS3LoadConfig(t *testing.T) {
    21  	var cfg s3Config
    22  	cfgFile, cleanup := testhelpers.TempCopy(t, "./test-fixtures/lhsm-plugin-s3.test", 0600)
    23  	defer cleanup()
    24  	err := dmplugin.LoadConfig(cfgFile, &cfg)
    25  	loaded := &cfg
    26  	if err != nil {
    27  		t.Fatalf("err: %s", err)
    28  	}
    29  
    30  	expected := &s3Config{
    31  		Archives: archiveSet{
    32  			&archiveConfig{
    33  				Name:           "2",
    34  				ID:             2,
    35  				Region:         "us-west-1",
    36  				Bucket:         "hpdd-test-bucket",
    37  				Prefix:         "archive-test",
    38  				UploadPartSize: 16,
    39  			},
    40  		},
    41  	}
    42  
    43  	if !reflect.DeepEqual(loaded, expected) {
    44  		t.Fatalf("\nexpected: \n\n%s\ngot: \n\n%s\n\n", expected, loaded)
    45  	}
    46  }
    47  
    48  func TestS3InsecureConfig(t *testing.T) {
    49  	var cfg s3Config
    50  	cfgFile, cleanup := testhelpers.TempCopy(t, "./test-fixtures/lhsm-plugin-s3.test", 0666)
    51  	defer cleanup()
    52  
    53  	err := dmplugin.LoadConfig(cfgFile, &cfg)
    54  	if err == nil {
    55  		t.Fatal("Used insecure file, expecteed error")
    56  	}
    57  	t.Log(err)
    58  	// verify err is the correct error
    59  }
    60  
    61  func TestS3MergedConfig(t *testing.T) {
    62  	os.Setenv(config.AgentConnEnvVar, "foo://bar:1234")
    63  	os.Setenv(config.PluginMountpointEnvVar, "/foo/bar/baz")
    64  
    65  	tmpDir, dirCleanup := testhelpers.TempDir(t)
    66  	defer dirCleanup()
    67  
    68  	testhelpers.CopyFile(t,
    69  		path.Join("./test-fixtures", path.Base(os.Args[0])),
    70  		path.Join(tmpDir, path.Base(os.Args[0])),
    71  		0600)
    72  	os.Setenv(config.ConfigDirEnvVar, tmpDir)
    73  
    74  	plugin, err := dmplugin.New(path.Base(os.Args[0]), func(path string) (fsroot.Client, error) {
    75  		return fsroot.Test(path), nil
    76  	})
    77  	if err != nil {
    78  		t.Fatalf("err: %s", err)
    79  	}
    80  
    81  	merged, err := getMergedConfig(plugin)
    82  	if err != nil {
    83  		t.Fatalf("err: %s", err)
    84  	}
    85  
    86  	expected := &s3Config{
    87  		Region:         "us-east-1",
    88  		UploadPartSize: s3manager.DefaultUploadPartSize,
    89  		Archives: archiveSet{
    90  			&archiveConfig{
    91  				Name:           "2",
    92  				ID:             2,
    93  				Region:         "us-west-1",
    94  				Bucket:         "hpdd-test-bucket",
    95  				Prefix:         "archive-test",
    96  				UploadPartSize: 16,
    97  			},
    98  		},
    99  	}
   100  
   101  	if !reflect.DeepEqual(merged, expected) {
   102  		t.Fatalf("\nexpected: \n\n%s\ngot: \n\n%s\n\n", expected, merged)
   103  	}
   104  }
   105  
   106  func TestArchiveValidation(t *testing.T) {
   107  	cfg := &s3Config{}
   108  	cfgFile, cleanup := testhelpers.TempCopy(t, "./test-fixtures/lhsm-plugin-s3.test", 0600)
   109  	defer cleanup()
   110  	err := dmplugin.LoadConfig(cfgFile, cfg)
   111  	if err != nil {
   112  		t.Fatalf("err: %s", err)
   113  	}
   114  
   115  	for _, archive := range cfg.Archives {
   116  		archive.mergeGlobals(cfg)
   117  		if err = archive.checkValid(); err != nil {
   118  			t.Fatalf("err: %s", err)
   119  		}
   120  	}
   121  
   122  	cfg2 := &s3Config{}
   123  	cfgFile2, cleanup2 := testhelpers.TempCopy(t, "./test-fixtures/lhsm-plugin-s3-badarchive", 0600)
   124  	defer cleanup2()
   125  	err = dmplugin.LoadConfig(cfgFile2, cfg2)
   126  	if err != nil {
   127  		t.Fatalf("err: %s", err)
   128  	}
   129  
   130  	for _, archive := range cfg2.Archives {
   131  		archive.mergeGlobals(cfg)
   132  		if err := archive.checkValid(); err == nil {
   133  			t.Fatalf("expected %s to fail validation", archive)
   134  		}
   135  	}
   136  }