github.com/danrjohnson/terraform@v0.7.0-rc2.0.20160627135212-d0fc1fa086ff/builtin/providers/docker/resource_docker_container_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 TestAccDockerContainer_basic(t *testing.T) {
    13  	var c dc.Container
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:  func() { testAccPreCheck(t) },
    16  		Providers: testAccProviders,
    17  		Steps: []resource.TestStep{
    18  			resource.TestStep{
    19  				Config: testAccDockerContainerConfig,
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccContainerRunning("docker_container.foo", &c),
    22  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func TestAccDockerContainer_volume(t *testing.T) {
    29  	var c dc.Container
    30  
    31  	testCheck := func(*terraform.State) error {
    32  		if len(c.Mounts) != 2 {
    33  			return fmt.Errorf("Incorrect number of mounts: expected 2, got %d", len(c.Mounts))
    34  		}
    35  
    36  		for _, v := range c.Mounts {
    37  			if v.Name != "testAccDockerContainerVolume_volume" {
    38  				continue
    39  			}
    40  
    41  			if v.Destination != "/tmp/volume" {
    42  				return fmt.Errorf("Bad destination on mount: expected /tmp/volume, got %q", v.Destination)
    43  			}
    44  
    45  			if v.Mode != "rw" {
    46  				return fmt.Errorf("Bad mode on mount: expected rw, got %q", v.Mode)
    47  			}
    48  
    49  			return nil
    50  		}
    51  
    52  		return fmt.Errorf("Mount for testAccDockerContainerVolume_volume not found")
    53  	}
    54  
    55  	resource.Test(t, resource.TestCase{
    56  		PreCheck:  func() { testAccPreCheck(t) },
    57  		Providers: testAccProviders,
    58  		Steps: []resource.TestStep{
    59  			resource.TestStep{
    60  				Config: testAccDockerContainerVolumeConfig,
    61  				Check: resource.ComposeTestCheckFunc(
    62  					testAccContainerRunning("docker_container.foo", &c),
    63  					testCheck,
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func TestAccDockerContainer_customized(t *testing.T) {
    71  	var c dc.Container
    72  
    73  	testCheck := func(*terraform.State) error {
    74  		if len(c.Config.Entrypoint) < 3 ||
    75  			(c.Config.Entrypoint[0] != "/bin/bash" &&
    76  				c.Config.Entrypoint[1] != "-c" &&
    77  				c.Config.Entrypoint[2] != "ping localhost") {
    78  			return fmt.Errorf("Container wrong entrypoint: %s", c.Config.Entrypoint)
    79  		}
    80  
    81  		if c.Config.User != "root:root" {
    82  			return fmt.Errorf("Container wrong user: %s", c.Config.User)
    83  		}
    84  
    85  		if c.HostConfig.RestartPolicy.Name == "on-failure" {
    86  			if c.HostConfig.RestartPolicy.MaximumRetryCount != 5 {
    87  				return fmt.Errorf("Container has wrong restart policy max retry count: %d", c.HostConfig.RestartPolicy.MaximumRetryCount)
    88  			}
    89  		} else {
    90  			return fmt.Errorf("Container has wrong restart policy: %s", c.HostConfig.RestartPolicy.Name)
    91  		}
    92  
    93  		if c.HostConfig.Memory != (512 * 1024 * 1024) {
    94  			return fmt.Errorf("Container has wrong memory setting: %d", c.HostConfig.Memory)
    95  		}
    96  
    97  		if c.HostConfig.MemorySwap != (2048 * 1024 * 1024) {
    98  			return fmt.Errorf("Container has wrong memory swap setting: %d", c.HostConfig.MemorySwap)
    99  		}
   100  
   101  		if c.HostConfig.CPUShares != 32 {
   102  			return fmt.Errorf("Container has wrong cpu shares setting: %d", c.HostConfig.CPUShares)
   103  		}
   104  
   105  		if c.Config.Labels["env"] != "prod" || c.Config.Labels["role"] != "test" {
   106  			return fmt.Errorf("Container does not have the correct labels")
   107  		}
   108  
   109  		if c.HostConfig.LogConfig.Type != "json-file" {
   110  			return fmt.Errorf("Container does not have the correct log config: %s", c.HostConfig.LogConfig.Type)
   111  		}
   112  
   113  		if c.HostConfig.LogConfig.Config["max-size"] != "10m" {
   114  			return fmt.Errorf("Container does not have the correct max-size log option: %v", c.HostConfig.LogConfig.Config["max-size"])
   115  		}
   116  
   117  		if c.HostConfig.LogConfig.Config["max-file"] != "20" {
   118  			return fmt.Errorf("Container does not have the correct max-file log option: %v", c.HostConfig.LogConfig.Config["max-file"])
   119  		}
   120  
   121  		if len(c.HostConfig.ExtraHosts) != 2 {
   122  			return fmt.Errorf("Container does not have correct number of extra host entries, got %d", len(c.HostConfig.ExtraHosts))
   123  		}
   124  
   125  		if c.HostConfig.ExtraHosts[0] != "testhost2:10.0.2.0" {
   126  			return fmt.Errorf("Container has incorrect extra host string: %q", c.HostConfig.ExtraHosts[0])
   127  		}
   128  
   129  		if c.HostConfig.ExtraHosts[1] != "testhost:10.0.1.0" {
   130  			return fmt.Errorf("Container has incorrect extra host string: %q", c.HostConfig.ExtraHosts[1])
   131  		}
   132  
   133  		return nil
   134  	}
   135  
   136  	resource.Test(t, resource.TestCase{
   137  		PreCheck:  func() { testAccPreCheck(t) },
   138  		Providers: testAccProviders,
   139  		Steps: []resource.TestStep{
   140  			resource.TestStep{
   141  				Config: testAccDockerContainerCustomizedConfig,
   142  				Check: resource.ComposeTestCheckFunc(
   143  					testAccContainerRunning("docker_container.foo", &c),
   144  					testCheck,
   145  				),
   146  			},
   147  		},
   148  	})
   149  }
   150  
   151  func testAccContainerRunning(n string, container *dc.Container) resource.TestCheckFunc {
   152  	return func(s *terraform.State) error {
   153  		rs, ok := s.RootModule().Resources[n]
   154  		if !ok {
   155  			return fmt.Errorf("Not found: %s", n)
   156  		}
   157  
   158  		if rs.Primary.ID == "" {
   159  			return fmt.Errorf("No ID is set")
   160  		}
   161  
   162  		client := testAccProvider.Meta().(*dc.Client)
   163  		containers, err := client.ListContainers(dc.ListContainersOptions{})
   164  		if err != nil {
   165  			return err
   166  		}
   167  
   168  		for _, c := range containers {
   169  			if c.ID == rs.Primary.ID {
   170  				inspected, err := client.InspectContainer(c.ID)
   171  				if err != nil {
   172  					return fmt.Errorf("Container could not be inspected: %s", err)
   173  				}
   174  				*container = *inspected
   175  				return nil
   176  			}
   177  		}
   178  
   179  		return fmt.Errorf("Container not found: %s", rs.Primary.ID)
   180  	}
   181  }
   182  
   183  const testAccDockerContainerConfig = `
   184  resource "docker_image" "foo" {
   185  	name = "nginx:latest"
   186  }
   187  
   188  resource "docker_container" "foo" {
   189  	name = "tf-test"
   190  	image = "${docker_image.foo.latest}"
   191  }
   192  `
   193  
   194  const testAccDockerContainerVolumeConfig = `
   195  resource "docker_image" "foo" {
   196  	name = "nginx:latest"
   197  }
   198  
   199  resource "docker_volume" "foo" {
   200      name = "testAccDockerContainerVolume_volume"
   201  }
   202  
   203  resource "docker_container" "foo" {
   204  	name = "tf-test"
   205  	image = "${docker_image.foo.latest}"
   206  
   207      volumes {
   208          volume_name = "${docker_volume.foo.name}"
   209          container_path = "/tmp/volume"
   210          read_only = false
   211      }
   212  }
   213  `
   214  
   215  const testAccDockerContainerCustomizedConfig = `
   216  resource "docker_image" "foo" {
   217  	name = "nginx:latest"
   218  }
   219  
   220  resource "docker_container" "foo" {
   221  	name = "tf-test"
   222  	image = "${docker_image.foo.latest}"
   223  	entrypoint = ["/bin/bash", "-c", "ping localhost"]
   224  	user = "root:root"
   225  	restart = "on-failure"
   226  	max_retry_count = 5
   227  	memory = 512
   228  	memory_swap = 2048
   229  	cpu_shares = 32
   230  	labels {
   231  		env = "prod"
   232  		role = "test"
   233  	}
   234  	log_driver = "json-file"
   235  	log_opts = {
   236  		max-size = "10m"
   237  		max-file = 20
   238  	}
   239  	network_mode = "bridge"
   240  
   241  	host {
   242  		host = "testhost"
   243  		ip = "10.0.1.0"
   244  	}
   245  
   246  	host {
   247  		host = "testhost2"
   248  		ip = "10.0.2.0"
   249  	}
   250  }
   251  `