1
0
mirror of https://github.com/drone/drone-cli.git synced 2024-11-23 09:21:56 +01:00

added unit tests for most transforms

This commit is contained in:
Brad Rydzewski 2015-03-10 19:10:37 -07:00
parent fdad6f04e5
commit 42db7b236d
2 changed files with 99 additions and 5 deletions

@ -66,6 +66,7 @@ func normalizeBuild(c *common.Config) {
// step has an image and uses a fully-qualified
// image name.
func normalizeImages(c *common.Config) {
c.Setup.Image = imageName(c.Setup.Image)
c.Clone.Image = imageName(c.Clone.Image)
c.Build.Image = imageName(c.Build.Image)
for name, step := range c.Publish {
@ -114,6 +115,7 @@ func rmNotify(c *common.Config) {
// rmPrivileged is a transformer that ensures every
// step is executed in non-privileged mode.
func rmPrivileged(c *common.Config) {
c.Setup.Privileged = false
c.Clone.Privileged = false
c.Build.Privileged = false
for _, step := range c.Publish {
@ -136,6 +138,7 @@ func rmPrivileged(c *common.Config) {
// rmVolumes is a transformer that ensures every
// step is executed without volumes.
func rmVolumes(c *common.Config) {
c.Setup.Volumes = []string{}
c.Clone.Volumes = []string{}
c.Build.Volumes = []string{}
for _, step := range c.Publish {
@ -155,6 +158,7 @@ func rmVolumes(c *common.Config) {
// rmNetwork is a transformer that ensures every
// step is executed with default bridge networking.
func rmNetwork(c *common.Config) {
c.Setup.NetworkMode = ""
c.Clone.NetworkMode = ""
c.Build.NetworkMode = ""
for _, step := range c.Publish {

@ -38,8 +38,31 @@ func Test_Transform(t *testing.T) {
g.Assert(len(c.Build.Config)).Equal(0)
})
g.It("Should normalize images")
g.It("Should normalize docker plugin")
g.It("Should normalize images", func() {
c := &common.Config{}
c.Setup = &common.Step{Image: "foo"}
c.Clone = &common.Step{Image: "bar"}
c.Build = &common.Step{Image: "foo/bar"}
c.Publish = map[string]*common.Step{"google_compute": &common.Step{}}
c.Deploy = map[string]*common.Step{"amazon": &common.Step{}}
c.Notify = map[string]*common.Step{"slack": &common.Step{}}
normalizeImages(c)
g.Assert(c.Setup.Image).Equal("plugins/drone-foo")
g.Assert(c.Clone.Image).Equal("plugins/drone-bar")
g.Assert(c.Build.Image).Equal("foo/bar")
g.Assert(c.Publish["google_compute"].Image).Equal("plugins/drone-google-compute")
g.Assert(c.Deploy["amazon"].Image).Equal("plugins/drone-amazon")
g.Assert(c.Notify["slack"].Image).Equal("plugins/drone-slack")
})
g.It("Should normalize docker plugin", func() {
c := &common.Config{}
c.Publish = map[string]*common.Step{}
c.Publish["docker"] = &common.Step{Image: "plugins/drone-docker"}
normalizeDockerPlugin(c)
g.Assert(c.Publish["docker"].Privileged).Equal(true)
})
g.It("Should remove publish", func() {
c := &common.Config{}
@ -65,9 +88,76 @@ func Test_Transform(t *testing.T) {
g.Assert(len(c.Notify)).Equal(0)
})
g.It("Should remove privileged")
g.It("Should remove volumes")
g.It("Should remove network")
g.It("Should remove privileged flag", func() {
c := &common.Config{}
c.Setup = &common.Step{Privileged: true}
c.Clone = &common.Step{Privileged: true}
c.Build = &common.Step{Privileged: true}
c.Compose = map[string]*common.Step{"postgres": &common.Step{Privileged: true}}
c.Publish = map[string]*common.Step{"google": &common.Step{Privileged: true}}
c.Deploy = map[string]*common.Step{"amazon": &common.Step{Privileged: true}}
c.Notify = map[string]*common.Step{"slack": &common.Step{Privileged: true}}
rmPrivileged(c)
g.Assert(c.Setup.Privileged).Equal(false)
g.Assert(c.Clone.Privileged).Equal(false)
g.Assert(c.Build.Privileged).Equal(false)
g.Assert(c.Compose["postgres"].Privileged).Equal(false)
g.Assert(c.Publish["google"].Privileged).Equal(false)
g.Assert(c.Deploy["amazon"].Privileged).Equal(false)
g.Assert(c.Notify["slack"].Privileged).Equal(false)
})
g.It("Should not remove docker plugin privileged flag", func() {
c := &common.Config{}
c.Setup = &common.Step{}
c.Clone = &common.Step{}
c.Build = &common.Step{}
c.Publish = map[string]*common.Step{}
c.Publish["docker"] = &common.Step{Image: "plugins/drone-docker"}
normalizeDockerPlugin(c)
g.Assert(c.Publish["docker"].Privileged).Equal(true)
})
g.It("Should remove volumes", func() {
c := &common.Config{}
c.Setup = &common.Step{Volumes: []string{"/:/tmp"}}
c.Clone = &common.Step{Volumes: []string{"/:/tmp"}}
c.Build = &common.Step{Volumes: []string{"/:/tmp"}}
c.Compose = map[string]*common.Step{"postgres": &common.Step{Volumes: []string{"/:/tmp"}}}
c.Publish = map[string]*common.Step{"google": &common.Step{Volumes: []string{"/:/tmp"}}}
c.Deploy = map[string]*common.Step{"amazon": &common.Step{Volumes: []string{"/:/tmp"}}}
c.Notify = map[string]*common.Step{"slack": &common.Step{Volumes: []string{"/:/tmp"}}}
rmVolumes(c)
g.Assert(len(c.Setup.Volumes)).Equal(0)
g.Assert(len(c.Clone.Volumes)).Equal(0)
g.Assert(len(c.Build.Volumes)).Equal(0)
g.Assert(len(c.Compose["postgres"].Volumes)).Equal(0)
g.Assert(len(c.Publish["google"].Volumes)).Equal(0)
g.Assert(len(c.Deploy["amazon"].Volumes)).Equal(0)
g.Assert(len(c.Notify["slack"].Volumes)).Equal(0)
})
g.It("Should remove network", func() {
c := &common.Config{}
c.Setup = &common.Step{NetworkMode: "host"}
c.Clone = &common.Step{NetworkMode: "host"}
c.Build = &common.Step{NetworkMode: "host"}
c.Compose = map[string]*common.Step{"postgres": &common.Step{NetworkMode: "host"}}
c.Publish = map[string]*common.Step{"google": &common.Step{NetworkMode: "host"}}
c.Deploy = map[string]*common.Step{"amazon": &common.Step{NetworkMode: "host"}}
c.Notify = map[string]*common.Step{"slack": &common.Step{NetworkMode: "host"}}
rmNetwork(c)
g.Assert(c.Setup.NetworkMode).Equal("")
g.Assert(c.Clone.NetworkMode).Equal("")
g.Assert(c.Build.NetworkMode).Equal("")
g.Assert(c.Compose["postgres"].NetworkMode).Equal("")
g.Assert(c.Publish["google"].NetworkMode).Equal("")
g.Assert(c.Deploy["amazon"].NetworkMode).Equal("")
g.Assert(c.Notify["slack"].NetworkMode).Equal("")
})
g.It("Should return full qualified image name", func() {
g.Assert(imageName("microsoft/azure")).Equal("microsoft/azure")