github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/runtime/engines/config/parser_test.go (about) 1 // Copyright (c) 2019, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package config 7 8 import ( 9 "io/ioutil" 10 "os" 11 "reflect" 12 "testing" 13 ) 14 15 type testConfig struct { 16 BoolYes bool `default:"yes" authorized:"yes,no" directive:"bool_yes"` 17 BoolNo bool `default:"no" authorized:"yes,no" directive:"bool_no"` 18 Uint uint `default:"0" directive:"uint"` 19 Int int `default:"-0" directive:"int"` 20 String string `directive:"string"` 21 StringAuthorized string `authorized:"value1,value2" directive:"string_authorized"` 22 StringSlice []string `directive:"string_slice"` 23 StringSliceDefault []string `default:"value1,value2" directive:"string_slice_default"` 24 } 25 26 func genConfig(content []byte) (string, error) { 27 f, err := ioutil.TempFile("", "parser-") 28 if err != nil { 29 return "", err 30 } 31 defer f.Close() 32 if _, err := f.Write(content); err != nil { 33 return "", err 34 } 35 return f.Name(), nil 36 } 37 38 func TestParser(t *testing.T) { 39 var def testConfig 40 var valid testConfig 41 42 if err := Parser("test_samples/no.conf", &def); err == nil { 43 t.Errorf("unexpected success while opening non existent configuration file") 44 } 45 46 if err := Parser("", &def); err != nil { 47 t.Error(err) 48 } 49 if def.BoolYes != true { 50 t.Errorf("unexpected value for bool_yes: %v", def.BoolYes) 51 } 52 if def.BoolNo != false { 53 t.Errorf("unexpected value for bool_no: %v", def.BoolNo) 54 } 55 if def.Uint != 0 { 56 t.Errorf("unexpected value for uint: %v", def.Uint) 57 } 58 if def.Int != 0 { 59 t.Errorf("unexpected value for int: %v", def.Int) 60 } 61 if def.String != "" { 62 t.Errorf("unexpected value for string: %v", def.String) 63 } 64 if def.StringAuthorized != "" { 65 t.Errorf("unexpected value for string_authorized: %v", def.StringAuthorized) 66 } 67 if !reflect.DeepEqual(def.StringSlice, []string{}) { 68 t.Errorf("unexpected value for string_slice: %v", def.StringSlice) 69 } 70 if !reflect.DeepEqual(def.StringSliceDefault, []string{"value1", "value2"}) { 71 t.Errorf("unexpected value for string_slice_default: %v", def.StringSliceDefault) 72 } 73 74 validConfig := []byte(` 75 bool_yes = no 76 bool_no = yes 77 uint = 1 78 int = -1 79 string = data 80 string_authorized = value2 81 string_slice = value1 82 string_slice = value2 83 string_slice = value3 84 string_slice_default = value3 85 `) 86 87 path, err := genConfig(validConfig) 88 if err != nil { 89 t.Error(err) 90 } 91 defer os.Remove(path) 92 93 if err := Parser(path, &valid); err != nil { 94 t.Error(err) 95 } 96 if valid.BoolYes != false { 97 t.Errorf("unexpected value for bool_yes: %v", valid.BoolYes) 98 } 99 if valid.BoolNo != true { 100 t.Errorf("unexpected value for bool_no: %v", valid.BoolNo) 101 } 102 if valid.Uint != 1 { 103 t.Errorf("unexpected value for uint: %v", valid.Uint) 104 } 105 if valid.Int != -1 { 106 t.Errorf("unexpected value for int: %v", valid.Int) 107 } 108 if valid.String != "data" { 109 t.Errorf("unexpected value for string: %v", valid.String) 110 } 111 if valid.StringAuthorized != "value2" { 112 t.Errorf("unexpected value for string_authorized: %v", valid.StringAuthorized) 113 } 114 if !reflect.DeepEqual(valid.StringSlice, []string{"value1", "value2", "value3"}) { 115 t.Errorf("unexpected value for string_slice: %v", valid.StringSlice) 116 } 117 if !reflect.DeepEqual(valid.StringSliceDefault, []string{"value3"}) { 118 t.Errorf("unexpected value for string_slice_default: %v", valid.StringSliceDefault) 119 } 120 121 for _, s := range []string{ 122 "bool_yes = enable", 123 "bool_no = disable", 124 "uint = -1", 125 "int = string", 126 "string_authorized = value3", 127 } { 128 badConfig := []byte(s) 129 130 path, err = genConfig(badConfig) 131 if err != nil { 132 t.Error(err) 133 } 134 135 if err := Parser(path, &valid); err == nil { 136 t.Errorf("unexpected success while parsing %s", s) 137 } 138 139 os.Remove(path) 140 } 141 }