github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/integration/lifecycle/rootfs_test.go (about)

     1  package lifecycle_test
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"os"
     8  
     9  	"github.com/cloudfoundry-incubator/garden"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/types"
    13  )
    14  
    15  var dockerRegistryRootFSPath = os.Getenv("GARDEN_DOCKER_REGISTRY_TEST_ROOTFS")
    16  
    17  var _ = Describe("Rootfs container create parameter", func() {
    18  	var container garden.Container
    19  	var args []string
    20  
    21  	BeforeEach(func() {
    22  		container = nil
    23  		args = []string{}
    24  	})
    25  
    26  	JustBeforeEach(func() {
    27  		client = startGarden(args...)
    28  	})
    29  
    30  	AfterEach(func() {
    31  		if container != nil {
    32  			Expect(client.Destroy(container.Handle())).To(Succeed())
    33  		}
    34  	})
    35  
    36  	Context("without a default rootfs", func() {
    37  		BeforeEach(func() {
    38  			args = []string{"--rootfs", ""}
    39  		})
    40  
    41  		It("without a rootfs in container spec, the container creation fails", func() {
    42  			var err error
    43  
    44  			container, err = client.Create(garden.ContainerSpec{RootFSPath: ""})
    45  			Ω(err).Should(HaveOccurred())
    46  			Ω(err).Should(MatchError(ContainSubstring(
    47  				"RootFSPath: is a required parameter, since no default rootfs was provided to the server. To provide a default rootfs, use the --rootfs flag on startup.",
    48  			)))
    49  		})
    50  
    51  		It("with a rootfs in container spec, the container is created successfully", func() {
    52  			var err error
    53  
    54  			container, err = client.Create(garden.ContainerSpec{RootFSPath: os.Getenv("GARDEN_TEST_ROOTFS")})
    55  			Ω(err).ShouldNot(HaveOccurred())
    56  		})
    57  	})
    58  
    59  	Context("with a default rootfs", func() {
    60  		It("the container is created successfully", func() {
    61  			var err error
    62  
    63  			container, err = client.Create(garden.ContainerSpec{RootFSPath: ""})
    64  			Expect(err).ToNot(HaveOccurred())
    65  		})
    66  	})
    67  
    68  	Context("with a docker rootfs URI", func() {
    69  		Context("not containing a host", func() {
    70  			It("the container is created successfully", func() {
    71  				var err error
    72  
    73  				container, err = client.Create(garden.ContainerSpec{RootFSPath: "docker:///busybox"})
    74  				Expect(err).ToNot(HaveOccurred())
    75  			})
    76  		})
    77  
    78  		Context("containing a host", func() {
    79  			Context("which is valid", func() {
    80  				It("the container is created successfully", func() {
    81  					var err error
    82  
    83  					container, err = client.Create(garden.ContainerSpec{RootFSPath: "docker://index.docker.io/busybox"})
    84  					Expect(err).ToNot(HaveOccurred())
    85  				})
    86  			})
    87  
    88  			Context("which is invalid", func() {
    89  				It("the container is not created successfully", func() {
    90  					var err error
    91  
    92  					container, err = client.Create(garden.ContainerSpec{RootFSPath: "docker://xindex.docker.io/busybox"})
    93  					Expect(err.Error()).To(MatchRegexp("could not resolve"))
    94  				})
    95  			})
    96  
    97  			Context("which is insecure", func() {
    98  				var dockerRegistry garden.Container
    99  
   100  				dockerRegistryIP := "10.0.0.1"
   101  				dockerRegistryPort := "5001"
   102  
   103  				if dockerRegistryRootFSPath == "" {
   104  					log.Println("GARDEN_DOCKER_REGISTRY_TEST_ROOTFS undefined; skipping")
   105  					return
   106  				}
   107  
   108  				JustBeforeEach(func() {
   109  					dockerRegistry = startDockerRegistry(dockerRegistryIP, dockerRegistryPort)
   110  				})
   111  
   112  				AfterEach(func() {
   113  					if dockerRegistry != nil {
   114  						Expect(client.Destroy(dockerRegistry.Handle())).To(Succeed())
   115  					}
   116  				})
   117  
   118  				Context("when the host is listed in -insecureDockerRegistryList", func() {
   119  					BeforeEach(func() {
   120  						args = []string{
   121  							"-insecureDockerRegistryList", dockerRegistryIP + ":" + dockerRegistryPort,
   122  							"-allowHostAccess=true",
   123  						}
   124  					})
   125  
   126  					It("creates the container successfully ", func() {
   127  						_, err := client.Create(garden.ContainerSpec{
   128  							RootFSPath: fmt.Sprintf("docker://%s:%s/busybox", dockerRegistryIP, dockerRegistryPort),
   129  						})
   130  						Expect(err).ToNot(HaveOccurred())
   131  					})
   132  				})
   133  
   134  				Context("when the host is NOT listed in -insecureDockerRegistryList", func() {
   135  					It("fails, and suggests the -insecureDockerRegistryList flag", func() {
   136  						_, err := client.Create(garden.ContainerSpec{
   137  							RootFSPath: fmt.Sprintf("docker://%s:%s/busybox", dockerRegistryIP, dockerRegistryPort),
   138  						})
   139  
   140  						Expect(err).To(MatchError(ContainSubstring(
   141  							"Registry %s:%s is missing from -insecureDockerRegistryList ([])", dockerRegistryIP, dockerRegistryPort,
   142  						)))
   143  					})
   144  				})
   145  			})
   146  		})
   147  	})
   148  })
   149  
   150  func startDockerRegistry(dockerRegistryIP string, dockerRegistryPort string) garden.Container {
   151  	dockerRegistry, err := client.Create(
   152  		garden.ContainerSpec{
   153  			RootFSPath: dockerRegistryRootFSPath,
   154  			Network:    dockerRegistryIP,
   155  		},
   156  	)
   157  	Expect(err).ToNot(HaveOccurred())
   158  
   159  	_, err = dockerRegistry.Run(garden.ProcessSpec{
   160  		Env: []string{
   161  			"DOCKER_REGISTRY_CONFIG=/docker-registry/config/config_sample.yml",
   162  			fmt.Sprintf("REGISTRY_PORT=%s", dockerRegistryPort),
   163  			"STANDALONE=true",
   164  			"MIRROR_SOURCE=https://registry-1.docker.io",
   165  			"MIRROR_SOURCE_INDEX=https://index.docker.io",
   166  		},
   167  		Path: "docker-registry",
   168  	}, garden.ProcessIO{Stdout: GinkgoWriter, Stderr: GinkgoWriter})
   169  	Expect(err).ToNot(HaveOccurred())
   170  
   171  	Eventually(
   172  		fmt.Sprintf("http://%s:%s/_ping", dockerRegistryIP, dockerRegistryPort),
   173  		"5s",
   174  	).Should(RespondToGETWith(200))
   175  
   176  	return dockerRegistry
   177  }
   178  
   179  type statusMatcher struct {
   180  	expectedStatus int
   181  
   182  	httpError    error
   183  	actualStatus int
   184  }
   185  
   186  func RespondToGETWith(expected int) types.GomegaMatcher {
   187  	return &statusMatcher{expected, nil, 200}
   188  }
   189  
   190  func (m *statusMatcher) Match(actual interface{}) (success bool, err error) {
   191  	response, err := http.Get(fmt.Sprintf("%s", actual))
   192  	if err != nil {
   193  		m.httpError = err
   194  		return false, nil
   195  	}
   196  
   197  	m.httpError = nil
   198  	m.actualStatus = response.StatusCode
   199  	return response.StatusCode == m.expectedStatus, nil
   200  }
   201  
   202  func (m *statusMatcher) FailureMessage(actual interface{}) string {
   203  	if m.httpError != nil {
   204  		return fmt.Sprintf("Expected http request to have status %d but got error: %s", m.expectedStatus, m.httpError.Error())
   205  	}
   206  
   207  	return fmt.Sprintf("Expected http status code to be %d but was %d", m.expectedStatus, m.actualStatus)
   208  }
   209  
   210  func (m *statusMatcher) NegatedFailureMessage(actual interface{}) string {
   211  	if m.httpError != nil {
   212  		return fmt.Sprintf("Expected http request to have status %d, but got error: %s", m.expectedStatus, m.httpError.Error())
   213  	}
   214  
   215  	return fmt.Sprintf("Expected http status code not to be %d", m.expectedStatus)
   216  }