test_context.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package framework
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. )
  7. type TestContextType struct {
  8. FRPClientPath string
  9. FRPServerPath string
  10. LogLevel string
  11. Debug bool
  12. }
  13. var TestContext TestContextType
  14. // RegisterCommonFlags registers flags common to all e2e test suites.
  15. // The flag set can be flag.CommandLine (if desired) or a custom
  16. // flag set that then gets passed to viperconfig.ViperizeFlags.
  17. //
  18. // The other Register*Flags methods below can be used to add more
  19. // test-specific flags. However, those settings then get added
  20. // regardless whether the test is actually in the test suite.
  21. func RegisterCommonFlags(flags *flag.FlagSet) {
  22. flags.StringVar(&TestContext.FRPClientPath, "frpc-path", "../../bin/frpc", "The frp client binary to use.")
  23. flags.StringVar(&TestContext.FRPServerPath, "frps-path", "../../bin/frps", "The frp server binary to use.")
  24. flags.StringVar(&TestContext.LogLevel, "log-level", "debug", "Log level.")
  25. flags.BoolVar(&TestContext.Debug, "debug", false, "Enable debug mode to print detail info.")
  26. }
  27. func ValidateTestContext(t *TestContextType) error {
  28. if t.FRPClientPath == "" || t.FRPServerPath == "" {
  29. return fmt.Errorf("frpc and frps binary path can't be empty")
  30. }
  31. if _, err := os.Stat(t.FRPClientPath); err != nil {
  32. return fmt.Errorf("load frpc-path error: %v", err)
  33. }
  34. if _, err := os.Stat(t.FRPServerPath); err != nil {
  35. return fmt.Errorf("load frps-path error: %v", err)
  36. }
  37. return nil
  38. }