github.com/unirita/cuto@v0.9.8-0.20160830082821-aa6652f877b7/message/request_test.go (about) 1 package message 2 3 import ( 4 "os" 5 "testing" 6 ) 7 8 func TestRequest_実行命令メッセージをパースできる(t *testing.T) { 9 message := `{ 10 "type":"request", 11 "version":"1.2.3", 12 "nid":1234, 13 "jid":"job1", 14 "path":"C:\\work\\test.bat", 15 "param":"test", 16 "env":"testenv", 17 "workspace":"C:\\work", 18 "warnrc":10, 19 "warnstr":"warn", 20 "errrc":20, 21 "errstr":"err", 22 "timeout":60 23 }` 24 25 var req Request 26 err := req.ParseJSON(message) 27 if err != nil { 28 t.Fatalf("想定外のエラーが発生しました: %s", err) 29 } 30 31 if req.Version != "1.2.3" { 32 t.Errorf("取得したversionの値が違います: %s", req.Version) 33 } 34 if req.NID != 1234 { 35 t.Errorf("取得したnidの値が違います: %d", req.NID) 36 } 37 if req.JID != `job1` { 38 t.Errorf("取得したjidの値が違います: %s", req.JID) 39 } 40 if req.Path != `C:\work\test.bat` { 41 t.Errorf("取得したpathの値が違います: %s", req.Path) 42 } 43 if req.Param != `test` { 44 t.Errorf("取得したparamの値が違います: %s", req.Param) 45 } 46 if req.Env != `testenv` { 47 t.Errorf("取得したenvの値が違います: %s", req.Env) 48 } 49 if req.Workspace != `C:\work` { 50 t.Errorf("取得したWorkspaceの値が違います: %s", req.Workspace) 51 } 52 if req.WarnRC != 10 { 53 t.Errorf("取得したwarnrcの値が違います: %d", req.WarnRC) 54 } 55 if req.WarnStr != `warn` { 56 t.Errorf("取得したwarnstrの値が違います: %s", req.WarnStr) 57 } 58 if req.ErrRC != 20 { 59 t.Errorf("取得したerrrcの値が違います: %d", req.ErrRC) 60 } 61 if req.ErrStr != `err` { 62 t.Errorf("取得したerrstrの値が違います: %s", req.ErrStr) 63 } 64 if req.Timeout != 60 { 65 t.Errorf("取得したtimeoutの値が違います: %d", req.Timeout) 66 } 67 } 68 69 func TestRequest_JSON文字列としてパースできない場合はエラーとする(t *testing.T) { 70 message := ` 71 "type":"request", 72 "version":"1.2.3", 73 "nid":1234, 74 "jid":"job1", 75 "path":"C:\\work\\test.bat", 76 "param":"test", 77 "env":"testenv", 78 "workspace":"C:\\work", 79 "warnrc":10, 80 "warnstr":"warn", 81 "errrc":20, 82 "errstr":"err", 83 "timeout":60 84 }` 85 86 var req Request 87 err := req.ParseJSON(message) 88 if err == nil { 89 t.Error("発生すべきエラーが発生しませんでした。") 90 } 91 } 92 93 func TestRequest_メッセージタイプが違う場合はエラーとする(t *testing.T) { 94 message := `{ 95 "type":"somthingelse", 96 "version":"1.2.3", 97 "nid":1234, 98 "jid":"job1", 99 "path":"C:\\work\\test.bat", 100 "param":"test", 101 "env":"testenv", 102 "workspace":"C:\\work", 103 "warnrc":10, 104 "warnstr":"warn", 105 "errrc":20, 106 "errstr":"err", 107 "timeout":60 108 }` 109 110 var req Request 111 err := req.ParseJSON(message) 112 if err == nil { 113 t.Error("発生すべきエラーが発生しませんでした。") 114 } 115 } 116 117 func TestRequest_プロパティ値からJSONメッセージを生成できる(t *testing.T) { 118 MasterVersion = "1.2.3" 119 120 var req Request 121 req.NID = 1234 122 req.JID = `job1` 123 req.Path = `C:\work\test.bat` 124 req.Param = `test` 125 req.Env = `testenv` 126 req.Workspace = `C:\work` 127 req.WarnRC = 10 128 req.WarnStr = `warn` 129 req.ErrRC = 20 130 req.ErrStr = `err` 131 req.Timeout = 60 132 133 msg, err := req.GenerateJSON() 134 if err != nil { 135 t.Fatalf("想定外のエラーが発生しました: %s", err) 136 } 137 138 expect := `{"type":"request","version":"1.2.3","nid":1234,"jid":"job1","path":"C:\\work\\test.bat","param":"test","env":"testenv","workspace":"C:\\work","warnrc":10,"warnstr":"warn","errrc":20,"errstr":"err","timeout":60}` 139 if msg != expect { 140 t.Error("生成されたJSONメッセージが想定値と違います") 141 t.Logf("生成値: %s", msg) 142 t.Logf("想定値: %s", expect) 143 } 144 } 145 146 func TestExpandMasterVars_メッセージ内の変数を展開出来る(t *testing.T) { 147 AddSysValue("JOBNET", "ID", "123456") 148 os.Setenv("TEST", "testenv") 149 res := new(Response) 150 res.RC = 1 151 AddJobValue("test", res) 152 153 req := new(Request) 154 req.Path = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 155 req.Param = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 156 req.Env = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 157 req.Workspace = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 158 159 err := req.ExpandMasterVars() 160 if err != nil { 161 t.Fatalf("想定外のエラーが発生した[%s]", err) 162 } 163 164 expected := `$MSJOBNET:ID$ testenv $MJtest:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 165 if req.Path != expected { 166 t.Errorf("変数展開後のPathの値が想定と違っている。") 167 t.Logf("想定値:%s", expected) 168 t.Logf("実績値:%s", req.Path) 169 } 170 if req.Workspace != expected { 171 t.Errorf("変数展開後のWorkspaceの値が想定と違っている。") 172 t.Logf("想定値:%s", expected) 173 t.Logf("実績値:%s", req.Workspace) 174 } 175 expected = `123456 testenv 1 $SSROOT$ $SETEST$ $SJtest:RC$` 176 if req.Param != expected { 177 t.Errorf("変数展開後のParamの値が想定と違っている。") 178 t.Logf("想定値:%s", expected) 179 t.Logf("実績値:%s", req.Param) 180 } 181 if req.Env != expected { 182 t.Errorf("変数展開後のEnvの値が想定と違っている。") 183 t.Logf("想定値:%s", expected) 184 t.Logf("実績値:%s", req.Env) 185 } 186 } 187 188 func TestExpandMasterVars_エラー発生時は元の値を維持する(t *testing.T) { 189 AddSysValue("JOBNET", "ID", "123456") 190 os.Setenv("TEST", "testenv") 191 res := new(Response) 192 res.RC = 1 193 AddJobValue("test", res) 194 195 req := new(Request) 196 req.Path = `$MSJOBNET:ID$ $METEST$ $MJtest1:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 197 req.Param = `$MSJOBNET:ID$ $METEST$ $MJtest1:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 198 req.Env = `$MSJOBNET:ID$ $METEST$ $MJtest1:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 199 req.Workspace = `$MSJOBNET:ID$ $METEST$ $MJtest1:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 200 201 err := req.ExpandMasterVars() 202 if err == nil { 203 t.Fatal("エラーが発生していない。") 204 } 205 206 expected := `$MSJOBNET:ID$ $METEST$ $MJtest1:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 207 if req.Path != expected { 208 t.Errorf("変数展開後のPathの値が想定と違っている。") 209 t.Logf("想定値:%s", expected) 210 t.Logf("実績値:%s", req.Path) 211 } 212 if req.Workspace != expected { 213 t.Errorf("変数展開後のWorkspaceの値が想定と違っている。") 214 t.Logf("想定値:%s", expected) 215 t.Logf("実績値:%s", req.Workspace) 216 } 217 if req.Param != expected { 218 t.Errorf("変数展開後のParamの値が想定と違っている。") 219 t.Logf("想定値:%s", expected) 220 t.Logf("実績値:%s", req.Param) 221 } 222 if req.Env != expected { 223 t.Errorf("変数展開後のEnvの値が想定と違っている。") 224 t.Logf("想定値:%s", expected) 225 t.Logf("実績値:%s", req.Env) 226 } 227 } 228 229 func TestExpandServantVars_メッセージ内の変数を展開出来る(t *testing.T) { 230 AddSysValue("ROOT", "", `C:\cute`) 231 os.Setenv("TEST", "testenv") 232 res := new(Response) 233 res.RC = 1 234 AddJobValue("test", res) 235 236 req := new(Request) 237 req.Path = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 238 req.Param = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 239 req.Env = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 240 req.Workspace = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSROOT$ $SETEST$ $SJtest:RC$` 241 242 err := req.ExpandServantVars() 243 if err != nil { 244 t.Fatalf("想定外のエラーが発生した[%s]", err) 245 } 246 247 expected := `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ C:\cute testenv $SJtest:RC$` 248 if req.Path != expected { 249 t.Errorf("変数展開後のPathの値が想定と違っている。") 250 t.Logf("想定値:%s", expected) 251 t.Logf("実績値:%s", req.Path) 252 } 253 if req.Workspace != expected { 254 t.Errorf("変数展開後のWorkspaceの値が想定と違っている。") 255 t.Logf("想定値:%s", expected) 256 t.Logf("実績値:%s", req.Workspace) 257 } 258 if req.Param != expected { 259 t.Errorf("変数展開後のParamの値が想定と違っている。") 260 t.Logf("想定値:%s", expected) 261 t.Logf("実績値:%s", req.Param) 262 } 263 if req.Env != expected { 264 t.Errorf("変数展開後のEnvの値が想定と違っている。") 265 t.Logf("想定値:%s", expected) 266 t.Logf("実績値:%s", req.Env) 267 } 268 } 269 270 func TestExpandServantVars_メッセージ内の時刻変数を展開出来る(t *testing.T) { 271 AddSysValue("ROOT", "", `C:\cute`) 272 os.Setenv("TEST", "testenv") 273 res := new(Response) 274 res.RC = 1 275 AddJobValue("test", res) 276 277 req := new(Request) 278 req.Path = `$ST20150730123456.789$` 279 req.Param = `$ST20150730123456.789$` 280 req.Env = `$ST20150730123456.789$` 281 req.Workspace = `$ST20150730123456.789$` 282 283 err := req.ExpandServantVars() 284 if err != nil { 285 t.Fatalf("想定外のエラーが発生した[%s]", err) 286 } 287 288 expected := `$ST20150730123456.789$` 289 if req.Path != expected { 290 t.Errorf("変数展開後のPathの値が想定と違っている。") 291 t.Logf("想定値:%s", expected) 292 t.Logf("実績値:%s", req.Path) 293 } 294 if req.Workspace != expected { 295 t.Errorf("変数展開後のWorkspaceの値が想定と違っている。") 296 t.Logf("想定値:%s", expected) 297 t.Logf("実績値:%s", req.Workspace) 298 } 299 expected = `2015-07-30 21:34:56.789` 300 if req.Param != expected { 301 t.Errorf("変数展開後のParamの値が想定と違っている。") 302 t.Logf("想定値:%s", expected) 303 t.Logf("実績値:%s", req.Param) 304 } 305 if req.Env != expected { 306 t.Errorf("変数展開後のEnvの値が想定と違っている。") 307 t.Logf("想定値:%s", expected) 308 t.Logf("実績値:%s", req.Env) 309 } 310 } 311 312 func TestExpandServantVars_エラー発生時は元の値を維持する(t *testing.T) { 313 AddSysValue("ROOT", "", `C:\cute`) 314 os.Setenv("TEST", "testenv") 315 res := new(Response) 316 res.RC = 1 317 AddJobValue("test", res) 318 319 req := new(Request) 320 req.Path = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSNOEXST$ $SETEST$ $SJtest:RC$` 321 req.Param = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSNOEXST$ $SETEST$ $SJtest:RC$` 322 req.Env = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSNOEXST$ $SETEST$ $SJtest:RC$` 323 req.Workspace = `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSNOEXST$ $SETEST$ $SJtest:RC$` 324 325 err := req.ExpandServantVars() 326 if err == nil { 327 t.Fatal("エラーが発生していない。") 328 } 329 330 expected := `$MSJOBNET:ID$ $METEST$ $MJtest:RC$ $SSNOEXST$ $SETEST$ $SJtest:RC$` 331 if req.Path != expected { 332 t.Errorf("変数展開後のPathの値が想定と違っている。") 333 t.Logf("想定値:%s", expected) 334 t.Logf("実績値:%s", req.Path) 335 } 336 if req.Workspace != expected { 337 t.Errorf("変数展開後のWorkspaceの値が想定と違っている。") 338 t.Logf("想定値:%s", expected) 339 t.Logf("実績値:%s", req.Workspace) 340 } 341 if req.Param != expected { 342 t.Errorf("変数展開後のParamの値が想定と違っている。") 343 t.Logf("想定値:%s", expected) 344 t.Logf("実績値:%s", req.Param) 345 } 346 if req.Env != expected { 347 t.Errorf("変数展開後のEnvの値が想定と違っている。") 348 t.Logf("想定値:%s", expected) 349 t.Logf("実績値:%s", req.Env) 350 } 351 }