Пример #1
0
func (s *GaudiTestSuite) TestExtendsShouldThrowAnErrorWhenTheElementDoesNotExists(c *C) {
	os.RemoveAll("/var/tmp/gaudi/")

	// Create a gomock controller, and arrange for it's finish to be called
	ctrl := gomock.NewController(c)
	defer ctrl.Finish()
	docker.MOCK().SetController(ctrl)

	util.MOCK().DisableMock("IsDir")
	util.MOCK().DisableMock("IsFile")
	util.MOCK().DisableMock("LogError")

	disableLog()

	g := gaudi.Gaudi{}

	c.Assert(func() {
		g.Init(`
applications:
    a:
        type: apache
    b:
        extends: c
`)
	}, PanicMatches, "b extends a non existing application : c")

	enableLog()
}
Пример #2
0
func (s *GaudiTestSuite) TestEnterContainerShouldUseNsEnter(c *C) {
	os.RemoveAll("/var/tmp/gaudi/")

	// Create a gomock controller, and arrange for it's finish to be called
	ctrl := gomock.NewController(c)
	defer ctrl.Finish()

	// Setup the docker mock package
	docker.MOCK().SetController(ctrl)

	// Setup the util mock package
	util.MOCK().SetController(ctrl)

	// Disable the util package mock
	util.MOCK().DisableMock("IsDir")
	util.MOCK().DisableMock("IsFile")

	util.EXPECT().PrintGreen("Retrieving templates ...")

	images := make(map[string]string)
	images["jpetazzo/nsenter"] = "123"

	docker.EXPECT().ImageExists(gomock.Any()).Return(true).Times(1)
	docker.EXPECT().HasDocker().Return(true).Times(1)
	docker.EXPECT().GetImages().Return(images, nil).Times(1)
	docker.EXPECT().Enter(gomock.Any()).Times(1)

	g := gaudi.Gaudi{}
	g.Init(`
applications:
    app:
        type: apache
`)
	g.Enter("app")
}
Пример #3
0
func (s *GaudiTestSuite) TestInitShouldTrowAndErrorOnWrongContent(c *C) {
	g := gaudi.Gaudi{}

	// Disable the util package mock
	util.MOCK().DisableMock("LogError")

	c.Assert(func() { g.Init("<oldFormat>Skrew you, i'm not yml</oldFormat>") }, Panics, "No application or binary to start. Are you missing a 'applications' or 'binaries' field in your configuration ?")
}
Пример #4
0
func (s *GaudiTestSuite) TestCheckRunningContainerShouldUseDockerPs(c *C) {
	os.RemoveAll("/var/tmp/gaudi/templates/")

	// Create a gomock controller, and arrange for it's finish to be called
	ctrl := gomock.NewController(c)
	defer ctrl.Finish()

	// Setup the docker mock package
	docker.MOCK().SetController(ctrl)

	// Setup the util mock package
	util.MOCK().SetController(ctrl)

	// Disable the util package mock
	util.MOCK().DisableMock("IsDir")
	util.MOCK().DisableMock("IsFile")

	psResult := make(map[string]string)
	psResult["gaudi/lb"] = "123"
	psResult["gaudi/front1"] = "124"
	psResult["gaudi/db"] = "125"

	util.EXPECT().PrintGreen("Retrieving templates ...").Times(1)

	docker.EXPECT().ImageExists(gomock.Any()).Return(true).Times(1)
	docker.EXPECT().HasDocker().Return(true).Times(1)
	docker.EXPECT().SnapshotProcesses().Return(psResult, nil)

	docker.EXPECT().Inspect("123").Return([]byte("[{\"ID\": \"123\", \"State\":{\"Running\": true}, \"NetworkSettings\": {\"IPAddress\": \"123.124.125.126\"}}]"), nil)
	docker.EXPECT().Inspect("124").Return([]byte("[{\"ID\": \"123\", \"State\":{\"Running\": true}, \"NetworkSettings\": {\"IPAddress\": \"123.124.125.127\"}}]"), nil)
	docker.EXPECT().Inspect("125").Return([]byte("[{\"ID\": \"123\", \"State\":{\"Running\": true}, \"NetworkSettings\": {\"IPAddress\": \"123.124.125.128\"}}]"), nil)

	util.EXPECT().PrintOrange("Application", "lb", "is running", "(123.124.125.126:)")
	util.EXPECT().PrintOrange("Application", "front1", "is running", "(123.124.125.127:)")
	util.EXPECT().PrintOrange("Application", "db", "is running", "(123.124.125.128:3306)")

	g := gaudi.Gaudi{}
	g.Init(`
applications:
    lb:
        links: [front1]
        type: varnish

    front1:
        type: apache

    db:
        type: mysql
        ports:
            3306: 9000
`)

	g.Check()
}
Пример #5
0
func (s *GaudiTestSuite) TestInitShouldTrowAndErrorOnMalformedYmlContent(c *C) {
	g := gaudi.Gaudi{}

	// Disable the util package mock
	util.MOCK().DisableMock("LogError")

	c.Assert(func() {
		g.Init(`
		applications:
			tabulated:
				type: varnish
`)
	}, PanicMatches, "YAML error: line 1: found character that cannot start any token")
}
Пример #6
0
func (s *GaudiTestSuite) TestStartApplicationShouldCleanAndBuildThem(c *C) {
	os.RemoveAll("/var/tmp/gaudi/templates/")

	// Create a gomock controller, and arrange for it's finish to be called
	ctrl := gomock.NewController(c)
	defer ctrl.Finish()

	// Setup the util mock package
	util.MOCK().SetController(ctrl)

	util.MOCK().DisableMock("IsFile")
	util.MOCK().DisableMock("IsDir")

	// Retrieving templates (1)
	util.EXPECT().PrintGreen(gomock.Any()).Times(1)
	// Killing, Clearing, Building, Starting (3*2)
	util.EXPECT().PrintGreen(gomock.Any(), gomock.Any(), gomock.Any()).Times(6)
	// Started (1*2)
	util.EXPECT().PrintGreen(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(2)

	// Setup the docker mock package
	docker.MOCK().SetController(ctrl)
	docker.EXPECT().ImageExists(gomock.Any()).Return(true).Times(1)
	docker.EXPECT().HasDocker().Return(true).Times(1)
	docker.EXPECT().Kill(gomock.Any()).Return().Times(2)
	docker.EXPECT().Remove(gomock.Any()).Return().Times(2)
	docker.EXPECT().Build(gomock.Any(), gomock.Any()).Return().Times(2)
	docker.EXPECT().Start(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("123").Times(2)
	docker.EXPECT().Inspect(gomock.Any()).Return([]byte("[{\"ID\": \"123\", \"State\":{\"Running\": false}, \"NetworkSettings\": {\"IPAddress\": \"172.17.0.10\"}}]"), nil).Times(2)

	g := gaudi.Gaudi{}
	g.Init(`
applications:
    app:
        type: php-fpm
        links: [db]
    db:
        type: mysql
        ports:
            3306: 9000
`)

	c.Assert(len(g.Applications), Equals, 2)

	g.StartApplications(true)
	c.Assert(g.GetApplication("db").IsRunning(), Equals, true)
	c.Assert(g.GetApplication("app").IsRunning(), Equals, true)
}
Пример #7
0
func (s *GaudiTestSuite) TestExtendsShouldCopyElements(c *C) {
	os.RemoveAll("/var/tmp/gaudi/")

	// Create a gomock controller, and arrange for it's finish to be called
	ctrl := gomock.NewController(c)
	defer ctrl.Finish()
	docker.MOCK().SetController(ctrl)

	util.MOCK().DisableMock("IsDir")
	util.MOCK().DisableMock("IsFile")

	disableLog()

	docker.EXPECT().ImageExists(gomock.Any()).Return(true).Times(1)
	docker.EXPECT().HasDocker().Return(true).Times(1)

	g := gaudi.Gaudi{}

	g.Init(`
applications:
    a:
        type: apache
        before_script: echo hello
    b:
        extends: a
    c:
        extends: a
        before_script: echo ok
    d:
        extends: c
        type: mysql
`)

	c.Check(g.Applications["a"].Type, Equals, "apache")
	c.Check(g.Applications["b"].BeforeScript, Equals, "echo hello")

	c.Check(g.Applications["b"].Type, Equals, "apache")
	c.Check(g.Applications["b"].BeforeScript, Equals, "echo hello")

	c.Check(g.Applications["c"].Type, Equals, "apache")
	c.Check(g.Applications["c"].BeforeScript, Equals, "echo ok")

	c.Check(g.Applications["d"].Type, Equals, "mysql")
	c.Check(g.Applications["d"].BeforeScript, Equals, "echo ok")

	enableLog()
}
Пример #8
0
func (s *GaudiTestSuite) TestUseCustomTemplateShouldUseIt(c *C) {
	os.RemoveAll("/var/tmp/gaudi/")

	// Create a gomock controller, and arrange for it's finish to be called
	ctrl := gomock.NewController(c)
	defer ctrl.Finish()
	docker.MOCK().SetController(ctrl)

	// Setup the util mock package
	util.MOCK().SetController(ctrl)

	util.MOCK().EnableMock("IsDir")
	util.MOCK().EnableMock("IsFile")
	util.MOCK().EnableMock("LogError")

	g := gaudi.Gaudi{}
	g.ApplicationDir = "/vagrant"

	docker.EXPECT().HasDocker().Return(true).Times(1)
	docker.EXPECT().ImageExists(gomock.Any()).Return(true).Times(1)

	util.EXPECT().IsDir("/var/tmp/gaudi/templates/").Return(false)
	util.EXPECT().IsFile("/vagrant/.gaudi/version.txt").Return(false)
	util.EXPECT().PrintGreen("Retrieving templates ...")

	util.EXPECT().IsFile("/vagrant/front/Dockerfile").Return(true).Times(3)
	util.EXPECT().LogError("Application 'custom' is not supported. Check http://gaudi.io/components.html for a list of supported applications.").Times(3)

	g.Init(`
applications:
    app:
        type: custom
        template: ./front/Dockerfile

    app2:
        type: custom
        template: front/Dockerfile

    app3:
        type: custom
        template: /vagrant/front/Dockerfile
`)
}
Пример #9
0
// Apache
func (s *GaudiTestSuite) TestStartApacheShouldStartedItCorrectly(c *C) {
	g := gaudi.Gaudi{}
	g.Init(`
applications:
    front:
        type: apache
        ports:
            80: 80
`)

	c.Assert(len(g.Applications), Equals, 1)
	g.StartApplications(true)

	// Test apache is running
	resp, err := http.Get("http://" + g.GetApplication("front").Ip)
	defer resp.Body.Close()

	c.Check(err, Equals, nil)
	c.Check(resp.StatusCode, Equals, 200)
}
Пример #10
0
func (s *GaudiTestSuite) TestStartBinariesShouldCleanAndBuildThem(c *C) {
	os.RemoveAll("/var/tmp/gaudi/")

	// Create a gomock controller, and arrange for it's finish to be called
	ctrl := gomock.NewController(c)
	defer ctrl.Finish()

	// Setup the docker mock package
	docker.MOCK().SetController(ctrl)

	// Setup the util mock package
	util.MOCK().SetController(ctrl)

	// Disable the util package mock
	util.MOCK().DisableMock("IsDir")
	util.MOCK().DisableMock("IsFile")

	util.EXPECT().PrintGreen("Retrieving templates ...")

	docker.EXPECT().ImageExists(gomock.Any()).Return(true).Times(1)
	docker.EXPECT().HasDocker().Return(true).Times(1)
	docker.EXPECT().ShouldRebuild(gomock.Any()).Return(true).Times(1)

	util.EXPECT().PrintGreen("Building", "gaudi/npm", "...")
	docker.EXPECT().Build(gomock.Any(), gomock.Any()).Times(1)

	util.EXPECT().PrintGreen("Running", "npm", "update", "...")
	docker.EXPECT().Run(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return().Times(1)

	g := gaudi.Gaudi{}
	g.Init(`
binaries:
    npm:
        type: npm
`)

	c.Assert(len(g.Applications), Equals, 0)
	c.Assert(len(g.Binaries), Equals, 1)

	g.Run("npm", []string{"update"})
}
Пример #11
0
func (s *GaudiTestSuite) TestInitShouldCreateApplications(c *C) {
	os.RemoveAll("/var/tmp/gaudi/")

	// Create a gomock controller, and arrange for it's finish to be called
	ctrl := gomock.NewController(c)
	defer ctrl.Finish()
	docker.MOCK().SetController(ctrl)

	// Setup the util mock package
	util.MOCK().SetController(ctrl)

	// Disable the util package mock
	util.MOCK().DisableMock("IsDir")
	util.MOCK().DisableMock("IsFile")

	util.EXPECT().PrintGreen("Retrieving templates ...")

	docker.EXPECT().ImageExists(gomock.Any()).Return(true).Times(1)
	docker.EXPECT().HasDocker().Return(true).Times(1)
	docker.EXPECT().Inspect(gomock.Any()).Return([]byte("[{\"ID\": \"123\", \"State\":{\"Running\": false}, \"NetworkSettings\": {\"IPAddress\": \"\"}}]"), nil)

	g := gaudi.Gaudi{}
	g.Init(`
applications:
    app:
        type: php-fpm
        links: [db]
    db:
        type: mysql
        ports:
            3306: 9000
`)

	c.Assert(len(g.Applications), Equals, 2)
	c.Assert(g.GetApplication("app").Name, Equals, "app")
	c.Assert(g.GetApplication("app").Type, Equals, "php-fpm")
	c.Assert(g.GetApplication("app").Dependencies[0].Name, Equals, "db")
	c.Assert(g.GetApplication("db").GetFirstPort(), Equals, "3306")
	c.Assert(g.GetApplication("db").IsRunning(), Equals, false)
}
Пример #12
0
// Apache + php-fpm
func (s *GaudiTestSuite) TestStartPhpAndApacheShouldStartedThemCorrectly(c *C) {
	err := os.MkdirAll("/tmp/php", 0775)
	ioutil.WriteFile("/tmp/php/ok.php", []byte("<?php echo 'ok';"), 0775)

	g := gaudi.Gaudi{}
	g.Init(`
applications:
    front:
        type: apache
        links: [app]
        ports:
            80: 80
        volumes:
            /tmp/php: /var/www
        custom:
            fastCgi: app

    app:
        type: php-fpm
        ports:
            9000: 9000
        volumes:
            /tmp/php: /var/www
`)

	c.Assert(len(g.Applications), Equals, 2)
	g.StartApplications(true)
	time.Sleep(2 * time.Second)

	// Test apache is running
	resp, err := http.Get("http://" + g.GetApplication("front").Ip + "/ok.php")
	defer resp.Body.Close()

	content, _ := ioutil.ReadAll(resp.Body)

	c.Check(err, Equals, nil)
	c.Check(resp.StatusCode, Equals, 200)
	c.Check(string(content), Equals, "ok")
}
Пример #13
0
func (s *GaudiTestSuite) TestStartApplicationShouldStartThemByOrderOfDependencies(c *C) {
	os.RemoveAll("/var/tmp/gaudi/templates/")

	// Create a gomock controller, and arrange for it's finish to be called
	ctrl := gomock.NewController(c)
	defer ctrl.Finish()

	// Setup the docker mock package
	docker.MOCK().SetController(ctrl)

	// Setup the util mock package
	util.MOCK().SetController(ctrl)

	// Disable the util package mock
	util.MOCK().DisableMock("IsDir")
	util.MOCK().DisableMock("IsFile")
	util.MOCK().EnableMock("PrintGreen")

	// Retrieving templates (1)
	util.EXPECT().PrintGreen(gomock.Any()).Times(1)
	// Killing, Clearing, Building, Starting (3*5)
	util.EXPECT().PrintGreen(gomock.Any(), gomock.Any(), gomock.Any()).Times(15)
	// Started (1*5)
	util.EXPECT().PrintGreen(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(5)

	docker.EXPECT().ImageExists(gomock.Any()).Return(true).Times(1)
	docker.EXPECT().HasDocker().Return(true).Times(1)
	docker.EXPECT().Kill(gomock.Any()).Return().Times(5)
	docker.EXPECT().Remove(gomock.Any()).Return().Times(5)
	docker.EXPECT().Build(gomock.Any(), gomock.Any()).Return().Times(5)

	gomock.InOrder(
		docker.EXPECT().Start("db", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("100"),
		docker.EXPECT().Inspect("100").Return([]byte("[{\"ID\": \"100\", \"State\":{\"Running\": true}, \"NetworkSettings\": {\"IPAddress\": \"172.17.0.10\"}}]"), nil),

		docker.EXPECT().Start("app", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("101"),
		docker.EXPECT().Inspect("101").Return([]byte("[{\"ID\": \"101\", \"State\":{\"Running\": true}, \"NetworkSettings\": {\"IPAddress\": \"172.17.0.10\"}}]"), nil),

		docker.EXPECT().Start("front1", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("102"),

		docker.EXPECT().Start("front2", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("103"),

		docker.EXPECT().Inspect("102").Return([]byte("[{\"ID\": \"102\", \"State\":{\"Running\": true}, \"NetworkSettings\": {\"IPAddress\": \"172.17.0.10\"}}]"), nil),
		docker.EXPECT().Inspect("103").Return([]byte("[{\"ID\": \"103\", \"State\":{\"Running\": true}, \"NetworkSettings\": {\"IPAddress\": \"172.17.0.10\"}}]"), nil),

		docker.EXPECT().Start("lb", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("104"),
		docker.EXPECT().Inspect("104").Return([]byte("[{\"ID\": \"104\", \"State\":{\"Running\": true}, \"NetworkSettings\": {\"IPAddress\": \"172.17.0.10\"}}]"), nil),
	)

	g := gaudi.Gaudi{}
	g.Init(`
applications:
    lb:
        links: [front1, front2]
        type: varnish

    front1:
        links: [app]
        type: apache

    front2:
        links: [app]
        type: apache

    app:
        links: [db]
        type: php-fpm

    db:
      type: mysql
`)

	g.StartApplications(true)
	c.Assert(len(g.Applications), Equals, 5)
}