github.com/mattevans/edward@v1.9.2/config/config_test.go (about) 1 package config 2 3 import ( 4 "errors" 5 "os" 6 "path/filepath" 7 "testing" 8 9 must "github.com/theothertomelliott/must" 10 11 "github.com/mattevans/edward/common" 12 "github.com/mattevans/edward/services" 13 "github.com/mattevans/edward/services/backends/commandline" 14 ) 15 16 func TestMain(m *testing.M) { 17 // Register necessary backends 18 services.RegisterLegacyMarshaler(&commandline.LegacyUnmarshaler{}) 19 services.RegisterBackend(&commandline.Loader{}) 20 21 os.Exit(m.Run()) 22 } 23 24 var service1 = services.ServiceConfig{ 25 Name: "service1", 26 Description: "My Service 1 is magic", 27 Path: common.StringToStringPointer("."), 28 RequiresSudo: true, 29 Backends: []*services.BackendConfig{ 30 { 31 Type: "commandline", 32 Config: &commandline.Backend{ 33 Commands: commandline.ServiceConfigCommands{ 34 Build: "buildCmd", 35 Launch: "launchCmd", 36 Stop: "stopCmd", 37 }, 38 LaunchChecks: &commandline.LaunchChecks{ 39 LogText: "startedProperty", 40 }, 41 }, 42 }, 43 }, 44 } 45 46 var service1alias = services.ServiceConfig{ 47 Name: "service1", 48 Aliases: []string{"service2"}, 49 Path: common.StringToStringPointer("."), 50 RequiresSudo: true, 51 Backends: []*services.BackendConfig{ 52 { 53 Type: "commandline", 54 Config: &commandline.Backend{ 55 Commands: commandline.ServiceConfigCommands{ 56 Build: "buildCmd", 57 Launch: "launchCmd", 58 Stop: "stopCmd", 59 }, 60 LaunchChecks: &commandline.LaunchChecks{ 61 LogText: "startedProperty", 62 }, 63 }, 64 }, 65 }, 66 } 67 68 var group1 = services.ServiceGroupConfig{ 69 Name: "group1", 70 Description: "My wonderfull group 1", 71 Services: []*services.ServiceConfig{&service1}, 72 Groups: []*services.ServiceGroupConfig{}, 73 ChildOrder: []string{"service1"}, 74 } 75 76 var group1alias = services.ServiceGroupConfig{ 77 Name: "group1", 78 Aliases: []string{"group2"}, 79 Services: []*services.ServiceConfig{&service1alias}, 80 Groups: []*services.ServiceGroupConfig{}, 81 ChildOrder: []string{"service1"}, 82 } 83 84 var service2 = services.ServiceConfig{ 85 Name: "service2", 86 Path: common.StringToStringPointer("service2/path"), 87 Backends: []*services.BackendConfig{ 88 { 89 Type: "commandline", 90 Config: &commandline.Backend{ 91 Commands: commandline.ServiceConfigCommands{ 92 Build: "buildCmd2", 93 Launch: "launchCmd2", 94 Stop: "stopCmd2", 95 }, 96 }, 97 }, 98 }, 99 } 100 101 var group2 = services.ServiceGroupConfig{ 102 Name: "group2", 103 Services: []*services.ServiceConfig{&service2}, 104 Groups: []*services.ServiceGroupConfig{}, 105 ChildOrder: []string{"service2"}, 106 } 107 108 var service3 = services.ServiceConfig{ 109 Name: "service3", 110 Path: common.StringToStringPointer("."), 111 RequiresSudo: true, 112 Backends: []*services.BackendConfig{ 113 { 114 Type: "commandline", 115 Config: &commandline.Backend{ 116 Commands: commandline.ServiceConfigCommands{ 117 Build: "buildCmd", 118 Launch: "launchCmd", 119 Stop: "stopCmd", 120 }, 121 LaunchChecks: &commandline.LaunchChecks{ 122 LogText: "startedProperty", 123 }, 124 }, 125 }, 126 }, 127 } 128 129 var group3 = services.ServiceGroupConfig{ 130 Name: "group3", 131 Services: []*services.ServiceConfig{&service3}, 132 Groups: []*services.ServiceGroupConfig{}, 133 ChildOrder: []string{"service3"}, 134 } 135 136 var fileBasedTests = []struct { 137 name string 138 inFile string 139 telemetryScript string 140 outServiceMap map[string]*services.ServiceConfig 141 outGroupMap map[string]*services.ServiceGroupConfig 142 outErr error 143 }{ 144 { 145 name: "Config with imports", 146 inFile: "test1.json", 147 outServiceMap: map[string]*services.ServiceConfig{ 148 "service1": &service1, 149 "service2": &service2, 150 "service3": &service3, 151 }, 152 outGroupMap: map[string]*services.ServiceGroupConfig{ 153 "group1": &group1, 154 "group2": &group2, 155 "group3": &group3, 156 }, 157 outErr: nil, 158 }, 159 { 160 name: "Config with imports with imports", 161 inFile: "recursiveimport.json", 162 outServiceMap: map[string]*services.ServiceConfig{ 163 "service1": &service1, 164 "service2": &service2, 165 }, 166 outGroupMap: map[string]*services.ServiceGroupConfig{ 167 "group2": &group2, 168 }, 169 outErr: nil, 170 }, 171 { 172 name: "Config missing imports", 173 inFile: "test2.json", 174 outErr: errors.New("open imports2/import2.json: no such file or directory"), 175 }, 176 { 177 name: "Duplicated import", 178 inFile: "test3.json", 179 outErr: errors.New("Duplicate name or alias: service2"), 180 }, 181 { 182 name: "Duplicated service", 183 inFile: "test4.json", 184 outErr: errors.New("Duplicate name or alias: service1"), 185 }, 186 { 187 name: "Duplicated group", 188 inFile: "test5.json", 189 outErr: errors.New("Duplicate name or alias: group"), 190 }, 191 { 192 name: "Group and service clash", 193 inFile: "test6.json", 194 outErr: errors.New("Duplicate name or alias: group"), 195 }, 196 { 197 name: "Service alias clash", 198 inFile: "test7.json", 199 outErr: errors.New("Duplicate name or alias: service1"), 200 }, 201 { 202 name: "Group alias clashes", 203 inFile: "test8.json", 204 outErr: errors.New("Duplicate name or alias: service1, service3"), 205 }, 206 { 207 name: "Valid aliases", 208 inFile: "test9.json", 209 outServiceMap: map[string]*services.ServiceConfig{ 210 "service1": &service1alias, 211 }, 212 outGroupMap: map[string]*services.ServiceGroupConfig{ 213 "group1": &group1alias, 214 }, 215 }, 216 { 217 name: "Invalid json", 218 inFile: "bad.json", 219 outErr: errors.New("could not parse config file (line 7, char 9): invalid character ':' after array element"), 220 }, 221 { 222 name: "With telemetry", 223 inFile: "telemetry.json", 224 telemetryScript: "telemetry.sh", 225 outServiceMap: map[string]*services.ServiceConfig{ 226 "service1": &service1alias, 227 }, 228 }, 229 } 230 231 func TestLoadConfigWithImports(t *testing.T) { 232 err := os.Chdir("testdata") 233 if err != nil { 234 t.Errorf("%v", err) 235 return 236 } 237 for _, test := range fileBasedTests { 238 cfg, err := LoadConfig(test.inFile, "") 239 validateTestResults( 240 cfg, 241 err, 242 test.inFile, 243 test.outServiceMap, 244 test.outGroupMap, 245 test.telemetryScript, 246 test.outErr, 247 test.name, 248 t, 249 ) 250 } 251 } 252 253 func validateTestResults( 254 cfg Config, 255 err error, 256 file string, 257 expectedServices map[string]*services.ServiceConfig, 258 expectedGroups map[string]*services.ServiceGroupConfig, 259 telemetryScript string, 260 expectedErr error, 261 name string, 262 t *testing.T, 263 ) { 264 for _, s := range expectedServices { 265 s.ConfigFile, _ = filepath.Abs(file) 266 } 267 must.BeEqual(t, expectedServices, cfg.ServiceMap, name+": services did not match.") 268 must.BeEqual(t, expectedGroups, cfg.GroupMap, name+": groups did not match.") 269 must.BeEqual(t, telemetryScript, cfg.TelemetryScript, name+": telemetry script") 270 271 must.BeEqualErrors(t, expectedErr, err, name+": Errors did not match.") 272 }