|
| 1 | +package template_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/AliceO2Group/Control/apricot/local" |
| 5 | + . "github.com/onsi/ginkgo/v2" |
| 6 | + . "github.com/onsi/gomega" |
| 7 | + |
| 8 | + "github.com/AliceO2Group/Control/configuration/template" |
| 9 | +) |
| 10 | + |
| 11 | +var _ = Describe("Template function stack", func() { |
| 12 | + |
| 13 | + Describe("utility functions", func() { |
| 14 | + var utilFuncMap map[string]interface{} |
| 15 | + |
| 16 | + BeforeEach(func() { |
| 17 | + varStack := map[string]string{ |
| 18 | + "test_var": "test_value", |
| 19 | + "prefix_var": "prefixed_value", |
| 20 | + "fallback_var": "fallback_value", |
| 21 | + "prefixed_none_fallback_var": "none", |
| 22 | + } |
| 23 | + utilFuncMap = template.MakeUtilFuncMap(varStack) |
| 24 | + }) |
| 25 | + |
| 26 | + Context("strings functions", func() { |
| 27 | + It("should validate inputs and convert string to int", func() { |
| 28 | + atoiFunc := utilFuncMap["Atoi"].(func(string) int) |
| 29 | + |
| 30 | + result := atoiFunc("123") |
| 31 | + Expect(result).To(Equal(123)) |
| 32 | + // we only produce an error log if unexpected input appears |
| 33 | + result = atoiFunc("abc") |
| 34 | + Expect(result).To(Equal(0)) |
| 35 | + result = atoiFunc("") |
| 36 | + Expect(result).To(Equal(0)) |
| 37 | + }) |
| 38 | + |
| 39 | + It("should validate inputs and convert int to string", func() { |
| 40 | + itoaFunc := utilFuncMap["Itoa"].(func(int) string) |
| 41 | + |
| 42 | + Expect(itoaFunc(123)).To(Equal("123")) |
| 43 | + Expect(itoaFunc(0)).To(Equal("0")) |
| 44 | + Expect(itoaFunc(-456)).To(Equal("-456")) |
| 45 | + }) |
| 46 | + |
| 47 | + It("should trim quotes", func() { |
| 48 | + trimQuotesFunc := utilFuncMap["TrimQuotes"].(func(string) string) |
| 49 | + |
| 50 | + Expect(trimQuotesFunc("\"test\"")).To(Equal("test")) |
| 51 | + // Fixme: should it support also single quotes? |
| 52 | + //Expect(trimQuotesFunc("'test'")).To(Equal("test")) |
| 53 | + |
| 54 | + Expect(trimQuotesFunc("test")).To(Equal("test")) |
| 55 | + Expect(trimQuotesFunc("")).To(Equal("")) |
| 56 | + Expect(trimQuotesFunc("\"test")).To(Equal("test")) |
| 57 | + Expect(trimQuotesFunc("test\"")).To(Equal("test")) |
| 58 | + }) |
| 59 | + |
| 60 | + It("should trim spaces", func() { |
| 61 | + trimSpaceFunc := utilFuncMap["TrimSpace"].(func(string) string) |
| 62 | + |
| 63 | + Expect(trimSpaceFunc(" test ")).To(Equal("test")) |
| 64 | + Expect(trimSpaceFunc("test")).To(Equal("test")) |
| 65 | + Expect(trimSpaceFunc("test test ")).To(Equal("test test")) |
| 66 | + }) |
| 67 | + |
| 68 | + It("should convert to upper case", func() { |
| 69 | + toUpperFunc := utilFuncMap["ToUpper"].(func(string) string) |
| 70 | + Expect(toUpperFunc("test")).To(Equal("TEST")) |
| 71 | + Expect(toUpperFunc("Test")).To(Equal("TEST")) |
| 72 | + Expect(toUpperFunc("1")).To(Equal("1")) |
| 73 | + }) |
| 74 | + |
| 75 | + It("should convert to lower case", func() { |
| 76 | + toLowerFunc := utilFuncMap["ToLower"].(func(string) string) |
| 77 | + Expect(toLowerFunc("TEST")).To(Equal("test")) |
| 78 | + Expect(toLowerFunc("Test")).To(Equal("test")) |
| 79 | + Expect(toLowerFunc("1")).To(Equal("1")) |
| 80 | + }) |
| 81 | + |
| 82 | + It("should check if a string is truthy", func() { |
| 83 | + isTruthyFunc := utilFuncMap["IsTruthy"].(func(string) bool) |
| 84 | + |
| 85 | + Expect(isTruthyFunc("true")).To(BeTrue()) |
| 86 | + Expect(isTruthyFunc("TRUE")).To(BeTrue()) |
| 87 | + Expect(isTruthyFunc("yes")).To(BeTrue()) |
| 88 | + Expect(isTruthyFunc("y")).To(BeTrue()) |
| 89 | + Expect(isTruthyFunc("1")).To(BeTrue()) |
| 90 | + Expect(isTruthyFunc("on")).To(BeTrue()) |
| 91 | + Expect(isTruthyFunc("ok")).To(BeTrue()) |
| 92 | + |
| 93 | + Expect(isTruthyFunc("false")).To(BeFalse()) |
| 94 | + Expect(isTruthyFunc("")).To(BeFalse()) |
| 95 | + Expect(isTruthyFunc("0")).To(BeFalse()) |
| 96 | + Expect(isTruthyFunc("truthy")).To(BeFalse()) |
| 97 | + }) |
| 98 | + |
| 99 | + It("should check if a string is falsy", func() { |
| 100 | + isFalsyFunc := utilFuncMap["IsFalsy"].(func(string) bool) |
| 101 | + Expect(isFalsyFunc("false")).To(BeTrue()) |
| 102 | + Expect(isFalsyFunc("FALSE")).To(BeTrue()) |
| 103 | + Expect(isFalsyFunc("no")).To(BeTrue()) |
| 104 | + Expect(isFalsyFunc("n")).To(BeTrue()) |
| 105 | + Expect(isFalsyFunc("0")).To(BeTrue()) |
| 106 | + Expect(isFalsyFunc("off")).To(BeTrue()) |
| 107 | + Expect(isFalsyFunc("none")).To(BeTrue()) |
| 108 | + Expect(isFalsyFunc("")).To(BeTrue()) |
| 109 | + |
| 110 | + Expect(isFalsyFunc("true")).To(BeFalse()) |
| 111 | + Expect(isFalsyFunc("1")).To(BeFalse()) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + Context("json functions", func() { |
| 116 | + It("should unmarshal JSON", func() { |
| 117 | + unmarshalFunc := utilFuncMap["json"].(map[string]interface{})["Unmarshal"].(func(string) interface{}) |
| 118 | + result := unmarshalFunc(`{"key": "value"}`) |
| 119 | + Expect(result).To(HaveKeyWithValue("key", "value")) |
| 120 | + }) |
| 121 | + |
| 122 | + It("should marshal to JSON", func() { |
| 123 | + marshalFunc := utilFuncMap["json"].(map[string]interface{})["Marshal"].(func(interface{}) string) |
| 124 | + input := map[string]string{"key": "value"} |
| 125 | + result := marshalFunc(input) |
| 126 | + Expect(result).To(MatchJSON(`{"key": "value"}`)) |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + Context("uid functions", func() { |
| 131 | + It("should generate a new UID", func() { |
| 132 | + newFunc := utilFuncMap["uid"].(map[string]interface{})["New"].(func() string) |
| 133 | + uid1 := newFunc() |
| 134 | + uid2 := newFunc() |
| 135 | + Expect(uid1).NotTo(Equal(uid2)) |
| 136 | + }) |
| 137 | + }) |
| 138 | + |
| 139 | + Context("util functions", func() { |
| 140 | + It("should handle prefixed override", func() { |
| 141 | + prefixedOverrideFunc := utilFuncMap["util"].(map[string]interface{})["PrefixedOverride"].(func(string, string) string) |
| 142 | + Expect(prefixedOverrideFunc("var", "prefix")).To(Equal("prefixed_value")) |
| 143 | + Expect(prefixedOverrideFunc("fallback_var", "nonexistent_prefix")).To(Equal("fallback_value")) |
| 144 | + Expect(prefixedOverrideFunc("fallback_var", "prefixed_none")).To(Equal("fallback_value")) |
| 145 | + Expect(prefixedOverrideFunc("nonexistent_fallback_var", "prefix")).To(Equal("")) |
| 146 | + }) |
| 147 | + |
| 148 | + It("should handle nullable strings", func() { |
| 149 | + nullableFunc := utilFuncMap["util"].(map[string]interface{})["Nullable"].(func(*string) string) |
| 150 | + var nilString *string |
| 151 | + Expect(nullableFunc(nilString)).To(Equal("")) |
| 152 | + |
| 153 | + nonNilString := "test" |
| 154 | + Expect(nullableFunc(&nonNilString)).To(Equal("test")) |
| 155 | + }) |
| 156 | + |
| 157 | + It("should check if suffix is in range", func() { |
| 158 | + suffixInRangeFunc := utilFuncMap["util"].(map[string]interface{})["SuffixInRange"].(func(string, string, string, string) string) |
| 159 | + Expect(suffixInRangeFunc("test5", "test", "1", "10")).To(Equal("true")) |
| 160 | + Expect(suffixInRangeFunc("test15", "test", "1", "10")).To(Equal("false")) |
| 161 | + // no prefix |
| 162 | + Expect(suffixInRangeFunc("other5", "test", "1", "10")).To(Equal("false")) |
| 163 | + // no suffix |
| 164 | + Expect(suffixInRangeFunc("test", "test", "1", "10")).To(Equal("false")) |
| 165 | + // range arguments are not numbers |
| 166 | + Expect(suffixInRangeFunc("test", "test", "a", "10")).To(Equal("false")) |
| 167 | + Expect(suffixInRangeFunc("test", "test", "1", "b")).To(Equal("false")) |
| 168 | + }) |
| 169 | + }) |
| 170 | + }) |
| 171 | + |
| 172 | + Describe("inventory functions", func() { |
| 173 | + var ( |
| 174 | + svc *local.Service |
| 175 | + configAccessObject map[string]interface{} |
| 176 | + err error |
| 177 | + ) |
| 178 | + BeforeEach(func() { |
| 179 | + svc, err = local.NewService("file://" + *tmpDir + "/" + serviceConfigFile) |
| 180 | + Expect(err).NotTo(HaveOccurred()) |
| 181 | + |
| 182 | + varStack := map[string]string{} |
| 183 | + configAccessObject = template.MakeConfigAccessObject(svc, varStack) |
| 184 | + }) |
| 185 | + |
| 186 | + It("should get detector for host", func() { |
| 187 | + getDetectorFunc := configAccessObject["inventory"].(map[string]interface{})["DetectorForHost"].(func(string) string) |
| 188 | + Expect(getDetectorFunc("flp001")).To(Equal("ABC")) |
| 189 | + Expect(getDetectorFunc("NOPE")).To(ContainSubstring("error")) |
| 190 | + }) |
| 191 | + It("should get detectors for a list of hosts", func() { |
| 192 | + getDetectorsFunc := configAccessObject["inventory"].(map[string]interface{})["DetectorsForHosts"].(func(string) string) |
| 193 | + Expect(getDetectorsFunc("[ \"flp001\", \"flp002\" ]")).To(Equal("[\"ABC\",\"DEF\"]")) |
| 194 | + Expect(getDetectorsFunc("[ \"flp001\", \"NOPE\" ]")).To(ContainSubstring("error")) |
| 195 | + Expect(getDetectorsFunc("[ \"NOPE\" ]")).To(ContainSubstring("error")) |
| 196 | + Expect(getDetectorsFunc("flp001")).To(ContainSubstring("error")) |
| 197 | + }) |
| 198 | + It("should get CRU cards for host", func() { |
| 199 | + getCruCardsFunc := configAccessObject["inventory"].(map[string]interface{})["CRUCardsForHost"].(func(string) string) |
| 200 | + Expect(getCruCardsFunc("flp001")).To(Equal("[\"0228\",\"0229\"]")) |
| 201 | + Expect(getCruCardsFunc("NOPE")).To(ContainSubstring("error")) |
| 202 | + }) |
| 203 | + It("should get endpoints for CRU card", func() { |
| 204 | + getEndpointsFunc := configAccessObject["inventory"].(map[string]interface{})["EndpointsForCRUCard"].(func(string, string) string) |
| 205 | + Expect(getEndpointsFunc("flp001", "0228")).To(Equal("0 1")) |
| 206 | + // fixme: probably incorrect behaviour, but I don't want to risk breaking something |
| 207 | + Expect(getEndpointsFunc("flp001", "NOPE")).To(BeEmpty()) |
| 208 | + }) |
| 209 | + }) |
| 210 | +}) |
0 commit comments