github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/docker/resource_docker_network_test.go (about)

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	dc "github.com/fsouza/go-dockerclient"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccDockerNetwork_basic(t *testing.T) {
    13  	var n dc.Network
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:  func() { testAccPreCheck(t) },
    17  		Providers: testAccProviders,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccDockerNetworkConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccNetwork("docker_network.foo", &n),
    23  				),
    24  			},
    25  		},
    26  	})
    27  }
    28  
    29  func testAccNetwork(n string, network *dc.Network) resource.TestCheckFunc {
    30  	return func(s *terraform.State) error {
    31  		rs, ok := s.RootModule().Resources[n]
    32  		if !ok {
    33  			return fmt.Errorf("Not found: %s", n)
    34  		}
    35  
    36  		if rs.Primary.ID == "" {
    37  			return fmt.Errorf("No ID is set")
    38  		}
    39  
    40  		client := testAccProvider.Meta().(*dc.Client)
    41  		networks, err := client.ListNetworks()
    42  		if err != nil {
    43  			return err
    44  		}
    45  
    46  		for _, n := range networks {
    47  			if n.ID == rs.Primary.ID {
    48  				inspected, err := client.NetworkInfo(n.ID)
    49  				if err != nil {
    50  					return fmt.Errorf("Network could not be obtained: %s", err)
    51  				}
    52  				*network = *inspected
    53  				return nil
    54  			}
    55  		}
    56  
    57  		return fmt.Errorf("Network not found: %s", rs.Primary.ID)
    58  	}
    59  }
    60  
    61  const testAccDockerNetworkConfig = `
    62  resource "docker_network" "foo" {
    63    name = "bar"
    64  }
    65  `