helper_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package api_test
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "github.com/porter-dev/porter/cli/cmd/docker"
  7. )
  8. const baseURL string = "http://localhost:10000/api"
  9. func TestMain(m *testing.M) {
  10. err := startPorterServerWithDocker("user", 10000, docker.SQLite)
  11. if err != nil {
  12. fmt.Printf("%v\n", err)
  13. os.Exit(1)
  14. }
  15. code := m.Run()
  16. stopPorterServerWithDocker("user")
  17. os.Exit(code)
  18. }
  19. type db int
  20. const (
  21. pg db = iota
  22. sqlite
  23. )
  24. // Spins up and shuts down the Docker api server with the given options
  25. func startPorterServerWithDocker(processID string, port int, db docker.PorterDB) error {
  26. env := []string{
  27. "ADMIN_INIT=false",
  28. }
  29. startOpts := &docker.PorterStartOpts{
  30. ProcessID: processID,
  31. ServerImageTag: "testing",
  32. ServerPort: port,
  33. DB: db,
  34. KubeconfigPath: "",
  35. SkipKubeconfig: true,
  36. Env: env,
  37. }
  38. _, _, err := docker.StartPorter(startOpts)
  39. if err != nil {
  40. return err
  41. }
  42. return nil
  43. }
  44. func stopPorterServerWithDocker(processID string) error {
  45. agent, err := docker.NewAgentFromEnv()
  46. if err != nil {
  47. return err
  48. }
  49. err = agent.StopPorterContainersWithProcessID(processID, true)
  50. if err != nil {
  51. return err
  52. }
  53. // remove volumes
  54. err = agent.RemoveLocalVolume("porter_sqlite_" + processID)
  55. return nil
  56. }