network.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package network
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/client"
  7. "gotest.tools/v3/assert"
  8. )
  9. func createNetwork(ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) (string, error) {
  10. config := types.NetworkCreate{}
  11. for _, op := range ops {
  12. op(&config)
  13. }
  14. n, err := client.NetworkCreate(ctx, name, config)
  15. return n.ID, err
  16. }
  17. // Create creates a network with the specified options
  18. func Create(ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) (string, error) {
  19. return createNetwork(ctx, client, name, ops...)
  20. }
  21. // CreateNoError creates a network with the specified options and verifies there were no errors
  22. func CreateNoError(ctx context.Context, t *testing.T, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) string {
  23. t.Helper()
  24. name, err := createNetwork(ctx, client, name, ops...)
  25. assert.NilError(t, err)
  26. return name
  27. }
  28. func RemoveNoError(ctx context.Context, t *testing.T, apiClient client.APIClient, name string) {
  29. t.Helper()
  30. err := apiClient.NetworkRemove(ctx, name)
  31. assert.NilError(t, err)
  32. }