helpers_windows.go 1.2 KB

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