github.com/zenyuk/edward@v1.8.17/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/yext/edward/common" 12 "github.com/yext/edward/services" 13 "github.com/yext/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 outServiceMap map[string]*services.ServiceConfig 140 outGroupMap map[string]*services.ServiceGroupConfig 141 outErr error 142 }{ 143 { 144 name: "Config with imports", 145 inFile: "test1.json", 146 outServiceMap: map[string]*services.ServiceConfig{ 147 "service1": &service1, 148 "service2": &service2, 149 "service3": &service3, 150 }, 151 outGroupMap: map[string]*services.ServiceGroupConfig{ 152 "group1": &group1, 153 "group2": &group2, 154 "group3": &group3, 155 }, 156 outErr: nil, 157 }, 158 { 159 name: "Config with imports with imports", 160 inFile: "recursiveimport.json", 161 outServiceMap: map[string]*services.ServiceConfig{ 162 "service1": &service1, 163 "service2": &service2, 164 }, 165 outGroupMap: map[string]*services.ServiceGroupConfig{ 166 "group2": &group2, 167 }, 168 outErr: nil, 169 }, 170 { 171 name: "Config missing imports", 172 inFile: "test2.json", 173 outErr: errors.New("open imports2/import2.json: no such file or directory"), 174 }, 175 { 176 name: "Duplicated import", 177 inFile: "test3.json", 178 outErr: errors.New("Duplicate name or alias: service2"), 179 }, 180 { 181 name: "Duplicated service", 182 inFile: "test4.json", 183 outErr: errors.New("Duplicate name or alias: service1"), 184 }, 185 { 186 name: "Duplicated group", 187 inFile: "test5.json", 188 outErr: errors.New("Duplicate name or alias: group"), 189 }, 190 { 191 name: "Group and service clash", 192 inFile: "test6.json", 193 outErr: errors.New("Duplicate name or alias: group"), 194 }, 195 { 196 name: "Service alias clash", 197 inFile: "test7.json", 198 outErr: errors.New("Duplicate name or alias: service1"), 199 }, 200 { 201 name: "Group alias clashes", 202 inFile: "test8.json", 203 outErr: errors.New("Duplicate name or alias: service1, service3"), 204 }, 205 { 206 name: "Valid aliases", 207 inFile: "test9.json", 208 outServiceMap: map[string]*services.ServiceConfig{ 209 "service1": &service1alias, 210 }, 211 outGroupMap: map[string]*services.ServiceGroupConfig{ 212 "group1": &group1alias, 213 }, 214 }, 215 { 216 name: "Invalid json", 217 inFile: "bad.json", 218 outErr: errors.New("could not parse config file (line 7, char 9): invalid character ':' after array element"), 219 }, 220 } 221 222 func TestLoadConfigWithImports(t *testing.T) { 223 err := os.Chdir("testdata") 224 if err != nil { 225 t.Errorf("%v", err) 226 return 227 } 228 for _, test := range fileBasedTests { 229 cfg, err := LoadConfig(test.inFile, "") 230 validateTestResults(cfg, err, test.inFile, test.outServiceMap, test.outGroupMap, test.outErr, test.name, t) 231 } 232 } 233 234 func validateTestResults(cfg Config, err error, file string, expectedServices map[string]*services.ServiceConfig, expectedGroups map[string]*services.ServiceGroupConfig, expectedErr error, name string, t *testing.T) { 235 for _, s := range expectedServices { 236 s.ConfigFile, _ = filepath.Abs(file) 237 } 238 must.BeEqual(t, expectedServices, cfg.ServiceMap, name+": services did not match.") 239 must.BeEqual(t, expectedGroups, cfg.GroupMap, name+": groups did not match.") 240 241 must.BeEqualErrors(t, expectedErr, err, name+": Errors did not match.") 242 }