github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/cmd/lhsm-plugin-posix/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/intel-hpdd/lemur/cmd/lhsm-plugin-posix/posix" 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 TestPosixLoadConfig(t *testing.T) { 21 var cfg posixConfig 22 cfgFile, cleanup := testhelpers.TempCopy(t, "./test-fixtures/lhsm-plugin-posix.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 := &posixConfig{ 31 NumThreads: 42, 32 Archives: posix.ArchiveSet{ 33 &posix.ArchiveConfig{ 34 Name: "1", 35 ID: 1, 36 Root: "/tmp/archives/1", 37 }, 38 }, 39 } 40 41 if !reflect.DeepEqual(loaded, expected) { 42 t.Fatalf("\nexpected: \n\n%#v\ngot: \n\n%#v\n\n", expected, loaded) 43 } 44 } 45 46 func TestPosixInsecureConfig(t *testing.T) { 47 var cfg posixConfig 48 cfgFile, cleanup := testhelpers.TempCopy(t, "./test-fixtures/lhsm-plugin-posix.test", 0666) 49 defer cleanup() 50 51 err := dmplugin.LoadConfig(cfgFile, &cfg) 52 if err == nil { 53 t.Fatal("Used insecure file, expecteed error") 54 } 55 t.Log(err) 56 // verify err is the correct error 57 } 58 59 func TestPosixMergedConfig(t *testing.T) { 60 os.Setenv(config.AgentConnEnvVar, "foo://bar:1234") 61 os.Setenv(config.PluginMountpointEnvVar, "/foo/bar/baz") 62 63 tmpDir, dirCleanup := testhelpers.TempDir(t) 64 defer dirCleanup() 65 66 testhelpers.CopyFile(t, 67 path.Join("./test-fixtures", path.Base(os.Args[0])), 68 path.Join(tmpDir, path.Base(os.Args[0])), 69 0600) 70 os.Setenv(config.ConfigDirEnvVar, tmpDir) 71 72 plugin, err := dmplugin.New(path.Base(os.Args[0]), func(path string) (fsroot.Client, error) { 73 return fsroot.Test(path), nil 74 }) 75 if err != nil { 76 t.Fatalf("err: %s", err) 77 } 78 79 merged, err := getMergedConfig(plugin) 80 if err != nil { 81 t.Fatalf("err: %s", err) 82 } 83 84 expected := &posixConfig{ 85 NumThreads: 42, 86 Archives: posix.ArchiveSet{ 87 &posix.ArchiveConfig{ 88 Name: "1", 89 ID: 1, 90 Root: "/tmp/archives/1", 91 }, 92 }, 93 Checksums: &posix.ChecksumConfig{}, 94 } 95 96 if !reflect.DeepEqual(merged, expected) { 97 t.Fatalf("\nexpected: \n\n%#v\ngot: \n\n%#v\n\n", expected, merged) 98 } 99 } 100 101 func TestPosixArchiveValidation(t *testing.T) { 102 var cfg posixConfig 103 cfgFile, cleanup := testhelpers.TempCopy(t, "./test-fixtures/lhsm-plugin-posix.test", 0600) 104 defer cleanup() 105 err := dmplugin.LoadConfig(cfgFile, &cfg) 106 loaded := &cfg 107 if err != nil { 108 t.Fatalf("err: %s", err) 109 } 110 111 for _, archive := range loaded.Archives { 112 if err := archive.CheckValid(); err != nil { 113 t.Fatalf("err: %s", err) 114 } 115 } 116 } 117 func TestPosixArchiveValidation2(t *testing.T) { 118 var cfg posixConfig 119 cfgFile, cleanup := testhelpers.TempCopy(t, "./test-fixtures/lhsm-plugin-posix-badarchive", 0600) 120 defer cleanup() 121 err := dmplugin.LoadConfig(cfgFile, &cfg) 122 loaded := &cfg 123 if err != nil { 124 t.Fatalf("err: %s", err) 125 } 126 127 for _, archive := range loaded.Archives { 128 if err := archive.CheckValid(); err == nil { 129 t.Fatalf("expected %s to fail validation", archive) 130 } 131 } 132 } 133 134 func TestPosixChecksumConfig(t *testing.T) { 135 var cfg posixConfig 136 cfgFile, cleanup := testhelpers.TempCopy(t, "./test-fixtures/lhsm-plugin-posix.checksums", 0600) 137 defer cleanup() 138 err := dmplugin.LoadConfig(cfgFile, &cfg) 139 loaded := &cfg 140 if err != nil { 141 t.Fatalf("err: %s", err) 142 } 143 144 checksumConfigs := map[int]*posix.ChecksumConfig{ 145 0: &posix.ChecksumConfig{ 146 Disabled: true, 147 DisableCompareOnRestore: false, 148 }, 149 1: &posix.ChecksumConfig{ 150 Disabled: false, 151 DisableCompareOnRestore: false, 152 }, 153 2: &posix.ChecksumConfig{ 154 Disabled: false, 155 DisableCompareOnRestore: true, 156 }, 157 } 158 159 expected := &posixConfig{ 160 Archives: posix.ArchiveSet{ 161 &posix.ArchiveConfig{ 162 Name: "1", 163 ID: 1, 164 Root: "/tmp/archives/1", 165 Checksums: checksumConfigs[1], 166 }, 167 &posix.ArchiveConfig{ 168 Name: "2", 169 ID: 2, 170 Root: "/tmp/archives/2", 171 Checksums: checksumConfigs[2], 172 }, 173 &posix.ArchiveConfig{ 174 Name: "3", 175 ID: 3, 176 Root: "/tmp/archives/3", 177 Checksums: nil, 178 }, 179 }, 180 Checksums: checksumConfigs[0], // global 181 } 182 183 // First, ensure that the config was loaded as expected 184 if !reflect.DeepEqual(loaded, expected) { 185 t.Fatalf("\nexpected: \n\n%s\ngot: \n\n%s\n\n", expected, loaded) 186 } 187 188 posix.DefaultChecksums = *loaded.Checksums 189 // Next, ensure that the archive backends are configured correctly 190 var tests = []struct { 191 archiveID uint32 192 expectedNum int 193 }{ 194 {1, 1}, 195 {2, 2}, 196 {3, 0}, // should have the global config 197 } 198 199 getExpectedChecksum := func(id uint32) *posix.ChecksumConfig { 200 for _, tc := range tests { 201 if tc.archiveID == id { 202 return checksumConfigs[tc.expectedNum] 203 } 204 } 205 return nil 206 } 207 208 for _, a := range loaded.Archives { 209 mover, err := posix.NewMover(a) 210 if err != nil { 211 t.Fatalf("err: %s", err) 212 } 213 got := mover.ChecksumConfig() 214 expected := getExpectedChecksum(uint32(a.ID)) 215 if !reflect.DeepEqual(expected, got) { 216 t.Fatalf("\nexpected: \n\n%#v\ngot: \n\n%#v\n\n", expected, got) 217 } 218 } 219 220 }