(t *testing.T)
| 252 | } |
| 253 | |
| 254 | func TestResources1(t *testing.T) { |
| 255 | type test struct { // an individual test |
| 256 | name string |
| 257 | res engine.Res // a resource |
| 258 | fail bool |
| 259 | experr error // expected error if fail == true (nil ignores it) |
| 260 | experrstr string // expected error prefix |
| 261 | timeline []Step // TODO: this could be a generator that keeps pushing out steps until it's done! |
| 262 | expect func() error // function to check for expected state |
| 263 | startup func() error // function to run as startup |
| 264 | cleanup func() error // function to run as cleanup |
| 265 | } |
| 266 | |
| 267 | // helpers |
| 268 | // TODO: make a series of helps to orchestrate the resources (eg: edit |
| 269 | // file, wait for event w/ timeout, run command w/ timeout, etc...) |
| 270 | sleep := func(ms uint) Step { |
| 271 | return &manualStep{ |
| 272 | action: func() error { |
| 273 | //nolint:gosec // G115: ms is a test-controlled value |
| 274 | time.Sleep(time.Duration(ms) * time.Millisecond) |
| 275 | return nil |
| 276 | }, |
| 277 | expect: func() error { return nil }, |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | testCases := []test{} |
| 282 | { |
| 283 | r := makeRes("file", "r1") |
| 284 | res := r.(*FileRes) // if this panics, the test will panic |
| 285 | p := "/tmp/whatever" |
| 286 | s := "hello, world\n" |
| 287 | res.Path = p |
| 288 | res.State = FileStateExists |
| 289 | contents := s |
| 290 | res.Content = &contents |
| 291 | |
| 292 | timeline := []Step{ |
| 293 | NewStartupStep(1000 * 60), // startup |
| 294 | NewChangedStep(1000*60, false), // did we do something? |
| 295 | FileExpect(p, s), // check initial state |
| 296 | NewClearChangedStep(1000 * 15), // did we do something? |
| 297 | FileWrite(p, "this is whatever\n"), // change state |
| 298 | NewChangedStep(1000*60, false), // did we do something? |
| 299 | FileExpect(p, s), // check again |
| 300 | sleep(1), // we can sleep too! |
| 301 | } |
| 302 | |
| 303 | testCases = append(testCases, test{ |
| 304 | name: "simple file", |
| 305 | res: res, |
| 306 | fail: false, |
| 307 | timeline: timeline, |
| 308 | expect: func() error { return nil }, |
| 309 | startup: func() error { return nil }, |
| 310 | cleanup: func() error { return os.Remove(p) }, |
| 311 | }) |
nothing calls this directly
no test coverage detected