helpers_windows.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package network
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/client"
  8. "gotest.tools/assert/cmp"
  9. )
  10. // IsNetworkAvailable provides a comparison to check if a docker network is available
  11. func IsNetworkAvailable(c client.NetworkAPIClient, name string) cmp.Comparison {
  12. return func() cmp.Result {
  13. networks, err := c.NetworkList(context.Background(), types.NetworkListOptions{})
  14. if err != nil {
  15. return cmp.ResultFromError(err)
  16. }
  17. for _, network := range networks {
  18. if network.Name == name {
  19. return cmp.ResultSuccess
  20. }
  21. }
  22. return cmp.ResultFailure(fmt.Sprintf("could not find network %s", name))
  23. }
  24. }
  25. // IsNetworkNotAvailable provides a comparison to check if a docker network is not available
  26. func IsNetworkNotAvailable(c client.NetworkAPIClient, name string) cmp.Comparison {
  27. return func() cmp.Result {
  28. networks, err := c.NetworkList(context.Background(), types.NetworkListOptions{})
  29. if err != nil {
  30. return cmp.ResultFromError(err)
  31. }
  32. for _, network := range networks {
  33. if network.Name == name {
  34. return cmp.ResultFailure(fmt.Sprintf("network %s is still present", name))
  35. }
  36. }
  37. return cmp.ResultSuccess
  38. }
  39. }
  40. // IsUserNamespace returns whether the user namespace remapping is enabled
  41. func IsUserNamespace() bool {
  42. root := os.Getenv("DOCKER_REMAP_ROOT")
  43. return root != ""
  44. }