github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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  `
    66  
    67  func TestAccDockerNetwork_internal(t *testing.T) {
    68  	var n dc.Network
    69  
    70  	resource.Test(t, resource.TestCase{
    71  		PreCheck:  func() { testAccPreCheck(t) },
    72  		Providers: testAccProviders,
    73  		Steps: []resource.TestStep{
    74  			resource.TestStep{
    75  				Config: testAccDockerNetworkInternalConfig,
    76  				Check: resource.ComposeTestCheckFunc(
    77  					testAccNetwork("docker_network.foobar", &n),
    78  					testAccNetworkInternal(&n, true),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  func testAccNetworkInternal(network *dc.Network, internal bool) resource.TestCheckFunc {
    86  	return func(s *terraform.State) error {
    87  		if network.Internal != internal {
    88  			return fmt.Errorf("Bad value for attribute 'internal': %t", network.Internal)
    89  		}
    90  		return nil
    91  	}
    92  }
    93  
    94  const testAccDockerNetworkInternalConfig = `
    95  resource "docker_network" "foobar" {
    96    name = "foobar"
    97    internal = "true"
    98  }
    99  `