예제 #1
0
파일: spec_test.go 프로젝트: tnaoto/drone
func TestSpec(t *testing.T) {
	g := goblin.Goblin(t)

	g.Describe("Spec file", func() {

		g.Describe("when looking up a container", func() {

			spec := Spec{}
			spec.Containers = append(spec.Containers, &Container{
				Name: "golang",
			})

			g.It("should find and return the container", func() {
				c, err := spec.lookupContainer("golang")
				g.Assert(err == nil).IsTrue("error should be nil")
				g.Assert(c).Equal(spec.Containers[0])
			})

			g.It("should return an error when not found", func() {
				c, err := spec.lookupContainer("node")
				g.Assert(err == nil).IsFalse("should return error")
				g.Assert(c == nil).IsTrue("should return nil container")
			})

		})
	})
}
예제 #2
0
func TestCache(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("Cache", func() {

		var c *gin.Context
		g.BeforeEach(func() {
			c = new(gin.Context)
			ToContext(c, Default())
		})

		g.It("Should set and get an item", func() {
			Set(c, "foo", "bar")
			v, e := Get(c, "foo")
			g.Assert(v).Equal("bar")
			g.Assert(e == nil).IsTrue()
		})

		g.It("Should return nil when item not found", func() {
			v, e := Get(c, "foo")
			g.Assert(v == nil).IsTrue()
			g.Assert(e == nil).IsFalse()
		})
	})
}
예제 #3
0
파일: shell_test.go 프로젝트: tnaoto/drone
func Test_shell(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("shell containers", func() {

		g.It("should ignore plugin steps", func() {
			root := parse.NewRootNode()
			c := root.NewPluginNode()
			c.Container = runner.Container{}
			ops := NewShellOp(Linux_adm64)
			ops.VisitContainer(c)

			g.Assert(len(c.Container.Entrypoint)).Equal(0)
			g.Assert(len(c.Container.Command)).Equal(0)
			g.Assert(c.Container.Environment["DRONE_SCRIPT"]).Equal("")
		})

		g.It("should set entrypoint, command and environment variables", func() {
			root := parse.NewRootNode()
			root.Base = "/go"
			root.Path = "/go/src/github.com/octocat/hello-world"

			c := root.NewShellNode()
			c.Commands = []string{"go build"}
			ops := NewShellOp(Linux_adm64)
			ops.VisitContainer(c)

			g.Assert(c.Container.Entrypoint).Equal([]string{"/bin/sh", "-c"})
			g.Assert(c.Container.Command).Equal([]string{"echo $DRONE_SCRIPT | base64 -d | /bin/sh -e"})
			g.Assert(c.Container.Environment["DRONE_SCRIPT"] != "").IsTrue()
		})
	})
}
예제 #4
0
func TestBlobstore(t *testing.T) {
	db := datasql.MustConnect("sqlite3", ":memory:")
	bs := New(db)

	g := goblin.Goblin(t)
	g.Describe("Blobstore", func() {

		// before each test be sure to purge the blob
		// table data from the database.
		g.Before(func() {
			db.Exec("DELETE FROM blobs")
		})

		g.It("Should Put a Blob", func() {
			err := bs.Put("foo", []byte("bar"))
			g.Assert(err == nil).IsTrue()
		})

		g.It("Should Put a Blob reader", func() {
			var buf bytes.Buffer
			buf.Write([]byte("bar"))
			err := bs.PutReader("foo", &buf)
			g.Assert(err == nil).IsTrue()
		})

		g.It("Should Overwrite a Blob", func() {
			bs.Put("foo", []byte("bar"))
			bs.Put("foo", []byte("baz"))
			blob, err := bs.Get("foo")
			g.Assert(err == nil).IsTrue()
			g.Assert(string(blob)).Equal("baz")
		})

		g.It("Should Get a Blob", func() {
			bs.Put("foo", []byte("bar"))
			blob, err := bs.Get("foo")
			g.Assert(err == nil).IsTrue()
			g.Assert(string(blob)).Equal("bar")
		})

		g.It("Should Get a Blob reader", func() {
			bs.Put("foo", []byte("bar"))
			r, _ := bs.GetReader("foo")
			blob, _ := ioutil.ReadAll(r)
			g.Assert(string(blob)).Equal("bar")
		})

		g.It("Should Del a Blob", func() {
			bs.Put("foo", []byte("bar"))
			err := bs.Del("foo")
			g.Assert(err == nil).IsTrue()
		})

		g.It("Should create a Context", func() {
			c := NewContext(context.Background(), db)
			b := blobstore.FromContext(c).(*Blobstore)
			g.Assert(b.DB).Equal(db)
		})
	})
}
예제 #5
0
파일: user_test.go 프로젝트: jonbodner/lgtm
func TestUsers(t *testing.T) {
	gin.SetMode(gin.TestMode)
	logrus.SetOutput(ioutil.Discard)

	g := goblin.Goblin(t)

	g.Describe("User endpoint", func() {
		g.It("Should return the authenticated user", func() {

			e := gin.New()
			e.NoRoute(GetUser)
			e.Use(func(c *gin.Context) {
				c.Set("user", fakeUser)
			})

			w := httptest.NewRecorder()
			r, _ := http.NewRequest("GET", "/", nil)
			e.ServeHTTP(w, r)

			want, _ := json.Marshal(fakeUser)
			got := strings.TrimSpace(w.Body.String())
			g.Assert(got).Equal(string(want))
			g.Assert(w.Code).Equal(200)
		})
	})
}
예제 #6
0
func Test_Matrix(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("Calculate matrix", func() {

		axis, _ := Parse(fakeMatrix)

		g.It("Should calculate permutations", func() {
			g.Assert(len(axis)).Equal(24)
		})

		g.It("Should not duplicate permutations", func() {
			set := map[string]bool{}
			for _, perm := range axis {
				set[perm.String()] = true
			}
			g.Assert(len(set)).Equal(24)
		})

		g.It("Should return nil if no matrix", func() {
			axis, err := Parse("")
			g.Assert(err == nil).IsTrue()
			g.Assert(axis == nil).IsTrue()
		})
	})
}
예제 #7
0
func Test_RealClient(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("Real Dart Client", func() {
		// These tests will use the LIVE Dart URLs
		// for integration testing purposes.
		//
		// Note that an outage or network connectivity
		// issues could result in false positives.
		c := NewClientDefault()

		g.It("Should Get a Version", func() {
			sdk, err := c.GetSDK()
			g.Assert(err == nil).IsTrue()
			g.Assert(sdk.Version == "").IsFalse()
			g.Assert(sdk.Revision == "").IsFalse()
		})

		g.It("Should Get a Package", func() {
			pkg, err := c.GetPackage("angular")
			g.Assert(err == nil).IsTrue()
			g.Assert(pkg.Name).Equal("angular")
			g.Assert(pkg.Latest != nil).IsTrue()
		})

		g.It("Should Get a Recent Package List", func() {
			pkgs, err := c.GetPackageRecent()
			g.Assert(err == nil).IsTrue()
			g.Assert(len(pkgs)).Equal(100)
		})
	})
}
예제 #8
0
func TestParseForwardedHeadersProto(t *testing.T) {
	g := goblin.Goblin(t)

	g.Describe("Parse proto Forwarded Headers", func() {
		g.It("Should parse a normal proto Forwarded header", func() {
			parsedHeader := parseHeader(mockRequest, "Forwarded", "proto")
			g.Assert("https" == parsedHeader[0]).IsTrue()
		})
		g.It("Should parse a normal for Forwarded header", func() {
			parsedHeader := parseHeader(mockRequest, "Forwarded", "for")
			g.Assert(reflect.DeepEqual([]string{"110.0.2.2", "\"[::1]\"", "10.2.3.4"}, parsedHeader)).IsTrue()
		})
		g.It("Should parse a normal host Forwarded header", func() {
			parsedHeader := parseHeader(mockRequest, "Forwarded", "host")
			g.Assert("example.com" == parsedHeader[0]).IsTrue()
		})
		g.It("Should parse a normal by Forwarded header", func() {
			parsedHeader := parseHeader(mockRequest, "Forwarded", "by")
			g.Assert("127.0.0.1" == parsedHeader[0]).IsTrue()
		})
		g.It("Should not crash if a wrongly formed Forwarder header is sent", func() {
			parsedHeader := parseHeader(wronglyFormedRequest, "Forwarded", "by")
			g.Assert(len(parsedHeader) == 0).IsTrue()
		})
	})
}
예제 #9
0
func TestParallelNode(t *testing.T) {
	g := goblin.Goblin(t)

	g.Describe("ParallelNode", func() {
		g.It("should append nodes", func() {
			node := NewRunNode()

			parallel0 := NewParallelNode()
			parallel1 := parallel0.Append(node)
			g.Assert(parallel0.Type().String()).Equal(NodeParallel)
			g.Assert(parallel0.Body[0]).Equal(node)
			g.Assert(parallel0).Equal(parallel1)
		})

		g.It("should fail validation when invalid type", func() {
			node := ParallelNode{}
			err := node.Validate()
			g.Assert(err == nil).IsFalse()
			g.Assert(err.Error()).Equal("Parallel Node uses an invalid type")
		})

		g.It("should fail validation when empty body", func() {
			node := NewParallelNode()
			err := node.Validate()
			g.Assert(err == nil).IsFalse()
			g.Assert(err.Error()).Equal("Parallel Node body is empty")
		})

		g.It("should pass validation", func() {
			node := NewParallelNode().Append(NewRunNode())
			g.Assert(node.Validate() == nil).IsTrue()
		})
	})
}
예제 #10
0
func TestBuildNode(t *testing.T) {
	g := goblin.Goblin(t)

	g.Describe("Build", func() {
		g.Describe("given a yaml file", func() {

			g.It("should unmarshal", func() {
				in := []byte(".")
				out := build{}
				err := yaml.Unmarshal(in, &out)
				if err != nil {
					g.Fail(err)
				}
				g.Assert(out.Context).Equal(".")
			})

			g.It("should unmarshal shorthand", func() {
				in := []byte("{ context: ., dockerfile: Dockerfile }")
				out := build{}
				err := yaml.Unmarshal(in, &out)
				if err != nil {
					g.Fail(err)
				}
				g.Assert(out.Context).Equal(".")
				g.Assert(out.Dockerfile).Equal("Dockerfile")
			})
		})
	})
}
예제 #11
0
func Test_Inject(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("Inject params", func() {

		g.It("Should replace vars with $$", func() {
			s := "echo $$FOO $BAR"
			m := map[string]string{}
			m["FOO"] = "BAZ"
			g.Assert("echo BAZ $BAR").Equal(Inject(s, m))
		})

		g.It("Should not replace vars with single $", func() {
			s := "echo $FOO $BAR"
			m := map[string]string{}
			m["FOO"] = "BAZ"
			g.Assert(s).Equal(Inject(s, m))
		})

		g.It("Should not replace vars in nil map", func() {
			s := "echo $$FOO $BAR"
			g.Assert(s).Equal(Inject(s, nil))
		})
	})
}
예제 #12
0
func TestRecoverNode(t *testing.T) {
	g := goblin.Goblin(t)

	g.Describe("RecoverNode", func() {
		g.It("should set body", func() {
			node0 := NewRunNode()

			recover0 := NewRecoverNode()
			recover1 := recover0.SetBody(node0)
			g.Assert(recover0.Type().String()).Equal(NodeRecover)
			g.Assert(recover0.Body).Equal(node0)
			g.Assert(recover0).Equal(recover1)
		})

		g.It("should fail validation when invalid type", func() {
			recover0 := RecoverNode{}
			err := recover0.Validate()
			g.Assert(err == nil).IsFalse()
			g.Assert(err.Error()).Equal("Recover Node uses an invalid type")
		})

		g.It("should fail validation when empty body", func() {
			recover0 := NewRecoverNode()
			err := recover0.Validate()
			g.Assert(err == nil).IsFalse()
			g.Assert(err.Error()).Equal("Recover Node body is empty")
		})

		g.It("should pass validation", func() {
			recover0 := NewRecoverNode()
			recover0.SetBody(NewRunNode())
			g.Assert(recover0.Validate() == nil).IsTrue()
		})
	})
}
예제 #13
0
파일: args_test.go 프로젝트: tnaoto/drone
func Test_args(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("plugins arguments", func() {

		g.It("should ignore non-plugin containers", func() {
			root := parse.NewRootNode()
			c := root.NewShellNode()
			c.Container = runner.Container{}
			c.Vargs = map[string]interface{}{
				"depth": 50,
			}

			ops := NewArgsOp()
			ops.VisitContainer(c)

			g.Assert(c.Container.Environment["PLUGIN_DEPTH"]).Equal("")
		})

		g.It("should include args as environment variable", func() {
			root := parse.NewRootNode()
			c := root.NewPluginNode()
			c.Container = runner.Container{}
			c.Vargs = map[string]interface{}{
				"depth": 50,
			}

			ops := NewArgsOp()
			ops.VisitContainer(c)

			g.Assert(c.Container.Environment["PLUGIN_DEPTH"]).Equal("50")
		})
	})

}
예제 #14
0
func Test_normalize(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("normalizing", func() {

		g.Describe("images", func() {

			g.It("should append tag if empty", func() {
				c := newConfig(&yaml.Container{
					Image: "golang",
				})

				ImageTag(c)
				g.Assert(c.Pipeline[0].Image).Equal("golang:latest")
			})

			g.It("should not override existing tag", func() {
				c := newConfig(&yaml.Container{
					Image: "golang:1.5",
				})

				ImageTag(c)
				g.Assert(c.Pipeline[0].Image).Equal("golang:1.5")
			})
		})
	})
}
예제 #15
0
파일: pull_test.go 프로젝트: tnaoto/drone
func Test_pull(t *testing.T) {
	root := parse.NewRootNode()

	g := goblin.Goblin(t)
	g.Describe("pull image", func() {

		g.It("should be enabled for plugins", func() {
			c := root.NewPluginNode()
			c.Container = runner.Container{}
			op := NewPullOp(true)

			op.VisitContainer(c)
			g.Assert(c.Container.Pull).IsTrue()
		})

		g.It("should be disabled for plugins", func() {
			c := root.NewPluginNode()
			c.Container = runner.Container{}
			op := NewPullOp(false)

			op.VisitContainer(c)
			g.Assert(c.Container.Pull).IsFalse()
		})

		g.It("should be disabled for non-plugins", func() {
			c := root.NewShellNode()
			c.Container = runner.Container{}
			op := NewPullOp(true)

			op.VisitContainer(c)
			g.Assert(c.Container.Pull).IsFalse()
		})
	})
}
예제 #16
0
func TestSubstitution(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("Parameter Substitution", func() {

		g.It("Should substitute simple parameters", func() {
			before := "echo ${GREETING} WORLD"
			after := "echo HELLO WORLD"
			g.Assert(substitute(before, "GREETING", "HELLO")).Equal(after)
		})

		g.It("Should substitute quoted parameters", func() {
			before := "echo \"${GREETING}\" WORLD"
			after := "echo \"HELLO\" WORLD"
			g.Assert(substituteQ(before, "GREETING", "HELLO")).Equal(after)
		})

		g.It("Should substitute parameters and trim prefix", func() {
			before := "echo ${GREETING##asdf} WORLD"
			after := "echo HELLO WORLD"
			g.Assert(substitutePrefix(before, "GREETING", "asdfHELLO")).Equal(after)
		})

		g.It("Should substitute parameters and trim suffix", func() {
			before := "echo ${GREETING%%asdf} WORLD"
			after := "echo HELLO WORLD"
			g.Assert(substituteSuffix(before, "GREETING", "HELLOasdf")).Equal(after)
		})

		g.It("Should substitute parameters without using the default", func() {
			before := "echo ${GREETING=HOLA} WORLD"
			after := "echo HELLO WORLD"
			g.Assert(substituteDefault(before, "GREETING", "HELLO")).Equal(after)
		})

		g.It("Should substitute parameters using the a default", func() {
			before := "echo ${GREETING=HOLA} WORLD"
			after := "echo HOLA WORLD"
			g.Assert(substituteDefault(before, "GREETING", "")).Equal(after)
		})

		g.It("Should substitute parameters with replacement", func() {
			before := "echo ${GREETING/HE/A} MONDE"
			after := "echo ALLO MONDE"
			g.Assert(substituteReplace(before, "GREETING", "HELLO")).Equal(after)
		})

		g.It("Should substitute parameters with left substr", func() {
			before := "echo ${FOO:4} IS COOL"
			after := "echo THIS IS COOL"
			g.Assert(substituteLeft(before, "FOO", "THIS IS A REALLY LONG STRING")).Equal(after)
		})

		g.It("Should substitute parameters with substr", func() {
			before := "echo ${FOO:8:5} IS COOL"
			after := "echo DRONE IS COOL"
			g.Assert(substituteSubstr(before, "FOO", "THIS IS DRONE CI")).Equal(after)
		})
	})
}
예제 #17
0
func TestContainer(t *testing.T) {
	g := goblin.Goblin(t)

	g.Describe("Container validation", func() {

		g.It("fails with an invalid name", func() {
			c := Container{
				Image: "golang:1.5",
			}
			err := c.Validate()
			g.Assert(err != nil).IsTrue()
			g.Assert(err.Error()).Equal("Missing container name")
		})

		g.It("fails with an invalid image", func() {
			c := Container{
				Name: "container_0",
			}
			err := c.Validate()
			g.Assert(err != nil).IsTrue()
			g.Assert(err.Error()).Equal("Missing container image")
		})

		g.It("passes with valid attributes", func() {
			c := Container{
				Name:  "container_0",
				Image: "golang:1.5",
			}
			g.Assert(c.Validate() == nil).IsTrue()
		})
	})
}
예제 #18
0
func test_Stash(t *testing.T) {
	g := goblin.Goblin(t)
	g.Describe("Stash Plugin", func() {

		g.It("Should return correct kind", func() {
			var s = &Stash{URL: "https://stash.atlassian.com"}
			g.Assert(s.GetKind()).Equal(model.RemoteStash)
		})

		g.It("Should parse hostname", func() {
			var s = &Stash{URL: "https://stash.atlassian.com"}
			g.Assert(s.GetHost()).Equal("stash.atlassian.com")
		})

		g.It("Should authorize user")

		g.It("Should get repos")

		g.It("Should get script/file")

		g.It("Should activate the repo")

		g.It("Should parse commit hook")
	})
}
예제 #19
0
파일: repo_test.go 프로젝트: Zhomart/drone
func TestSetPerm(t *testing.T) {
	g := goblin.Goblin(t)
	g.Describe("SetPerm", func() {
		g.BeforeEach(func() {
			os.Unsetenv("PUBLIC_MODE")
		})
		g.It("Should set pull to false (private repo, user not logged in)", func() {
			c := gin.Context{}
			c.Set("repo", &model.Repo{
				IsPrivate: true,
			})
			SetPerm()(&c)
			v, ok := c.Get("perm")
			g.Assert(ok).IsTrue("perm was not set")
			p, ok := v.(*model.Perm)
			g.Assert(ok).IsTrue("perm was the wrong type")
			g.Assert(p.Pull).IsFalse("pull should be false")
		})
		g.It("Should set pull to true (private repo, user not logged in, public mode)", func() {
			os.Setenv("PUBLIC_MODE", "true")
			c := gin.Context{}
			c.Set("repo", &model.Repo{
				IsPrivate: true,
			})
			SetPerm()(&c)
			v, ok := c.Get("perm")
			g.Assert(ok).IsTrue("perm was not set")
			p, ok := v.(*model.Perm)
			g.Assert(ok).IsTrue("perm was the wrong type")
			g.Assert(p.Pull).IsTrue("pull should be true")
		})
	})
}
예제 #20
0
파일: map_test.go 프로젝트: Ablu/drone
func TestMapEqualSlice(t *testing.T) {
	g := goblin.Goblin(t)

	g.Describe("Yaml map equal slice", func() {

		g.It("should unmarshal a map", func() {
			in := []byte("foo: bar")
			out := MapEqualSlice{}
			err := yaml.Unmarshal(in, &out)
			if err != nil {
				g.Fail(err)
			}
			g.Assert(len(out.Map())).Equal(1)
			g.Assert(out.Map()["foo"]).Equal("bar")
		})

		g.It("should unmarshal a map equal slice", func() {
			in := []byte("[ foo=bar ]")
			out := MapEqualSlice{}
			err := yaml.Unmarshal(in, &out)
			if err != nil {
				g.Fail(err)
			}
			g.Assert(len(out.parts)).Equal(1)
			g.Assert(out.parts["foo"]).Equal("bar")
		})

		g.It("should throw error when invalid map equal slice", func() {
			in := []byte("foo") // string value should fail parse
			out := MapEqualSlice{}
			err := yaml.Unmarshal(in, &out)
			g.Assert(err != nil).IsTrue("expects error")
		})
	})
}
예제 #21
0
파일: config_test.go 프로젝트: Ablu/drone
func TestParse(t *testing.T) {
	g := goblin.Goblin(t)

	g.Describe("Parser", func() {
		g.Describe("Given a yaml file", func() {

			g.It("Should unmarshal a string", func() {
				out, err := ParseString(sampleYaml)
				if err != nil {
					g.Fail(err)
				}
				g.Assert(out.Image).Equal("hello-world")
				g.Assert(out.Workspace.Base).Equal("/go")
				g.Assert(out.Workspace.Path).Equal("src/github.com/octocat/hello-world")
				g.Assert(out.Build.Context).Equal(".")
				g.Assert(out.Build.Dockerfile).Equal("Dockerfile")
				g.Assert(out.Volumes[0].Name).Equal("custom")
				g.Assert(out.Volumes[0].Driver).Equal("blockbridge")
				g.Assert(out.Networks[0].Name).Equal("custom")
				g.Assert(out.Networks[0].Driver).Equal("overlay")
				g.Assert(out.Services[0].Name).Equal("database")
				g.Assert(out.Services[0].Image).Equal("mysql")
				g.Assert(out.Pipeline[0].Name).Equal("test")
				g.Assert(out.Pipeline[0].Image).Equal("golang")
				g.Assert(out.Pipeline[0].Commands).Equal([]string{"go install", "go test"})
				g.Assert(out.Pipeline[1].Name).Equal("build")
				g.Assert(out.Pipeline[1].Image).Equal("golang")
				g.Assert(out.Pipeline[1].Commands).Equal([]string{"go build"})
				g.Assert(out.Pipeline[2].Name).Equal("notify")
				g.Assert(out.Pipeline[2].Image).Equal("slack")
			})
		})
	})
}
예제 #22
0
func TestSecret(t *testing.T) {
	g := goblin.Goblin(t)
	g.Describe("SecretReplacer", func() {
		g.It("Should conceal secret", func() {
			secrets := []*model.Secret{
				{
					Name:    "SECRET",
					Value:   "secret_value",
					Conceal: true,
				},
			}
			r := NewSecretReplacer(secrets)
			g.Assert(r.Replace(testString)).Equal("This is SECRET: *****")
		})

		g.It("Should not conceal secret", func() {
			secrets := []*model.Secret{
				{
					Name:    "SECRET",
					Value:   "secret_value",
					Conceal: false,
				},
			}
			r := NewSecretReplacer(secrets)
			g.Assert(r.Replace(testString)).Equal(testString)
		})
	})
}
예제 #23
0
func Test(t *testing.T) {
	g := goblin.Goblin(t)

	g.Describe("Version#Check", func() {
		g.It("Doesn't panic", func() {
			result := NewerVersionAvailable()
			// Always on the latest version.
			g.Assert(result).Equal(false)
		})
	})

	g.Describe("Version#NewestVersion", func() {
		g.It("Is the same as the Version", func() {
			result := NewestVersion()
			// Always on the latest version.
			g.Assert(result).Equal(Version)
		})
	})

	g.Describe("Version#Update", func() {
		g.It("Returns false when there's no update available", func() {
			result := Update()
			g.Assert(result).Equal(false)
		})
	})
}
예제 #24
0
파일: envs_test.go 프로젝트: tnaoto/drone
func Test_env(t *testing.T) {
	root := parse.NewRootNode()

	g := goblin.Goblin(t)
	g.Describe("environment variables", func() {

		g.It("should be copied", func() {
			envs := map[string]string{"CI": "drone"}

			c := root.NewContainerNode()
			c.Container = runner.Container{}
			op := NewEnvOp(envs)

			op.VisitContainer(c)
			g.Assert(c.Container.Environment["CI"]).Equal("drone")
		})

		g.It("should include http proxy variables", func() {
			httpProxy = "foo"
			httpsProxy = "bar"
			noProxy = "baz"

			c := root.NewContainerNode()
			c.Container = runner.Container{}
			op := NewEnvOp(map[string]string{})

			op.VisitContainer(c)
			g.Assert(c.Container.Environment["HTTP_PROXY"]).Equal("foo")
			g.Assert(c.Container.Environment["HTTPS_PROXY"]).Equal("bar")
			g.Assert(c.Container.Environment["NO_PROXY"]).Equal("baz")
		})

	})
}
예제 #25
0
func TestReposCache(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("Repo List Cache", func() {

		var c *gin.Context
		g.BeforeEach(func() {
			c = new(gin.Context)
			cache.ToContext(c, cache.Default())
		})

		g.It("should skip when no user session", func() {
			Perms(c)

			_, ok := c.Get("perm")
			g.Assert(ok).IsFalse()
		})

		g.It("should get repos from cache", func() {
			c.Set("user", fakeUser)
			cache.SetRepos(c, fakeUser, fakeRepos)

			Repos(c)

			repos, ok := c.Get("repos")
			g.Assert(ok).IsTrue()
			g.Assert(repos).Equal(fakeRepos)
		})

	})
}
예제 #26
0
func Test(t *testing.T) {
	g := goblin.Goblin(t)

	g.Describe("#searchUrl", func() {
		g.It("Should build a http link", func() {
			l := searchPage([]string{"how", "do", "i"}, false)
			g.Assert(l.Url).Equal("http://google.com/search?q=site:stackoverflow.com%20how%20do%20i")
			g.Assert(l.Title).Equal("how do i")
		})

		g.It("Should build a https link", func() {
			l := searchPage([]string{"how", "do", "i"}, true)
			g.Assert(l.Url).Equal("https://encrypted.google.com/search?q=site:stackoverflow.com%20how%20do%20i")
			g.Assert(l.Title).Equal("how do i")
		})
	})

	g.Describe("#linkHeading", func() {
		g.It("Should add a border around the link url", func() {
			l := page.Page{"url", "title"}
			heading := linkHeading(l)
			g.Assert(heading).Equal("********\nurl\n********\n\n")
		})
	})

	g.Describe("link#Fetch", func() {
		g.It("Should make a successful http request to get the page", func() {
			l := searchPage([]string{"how", "do", "i"}, false)
			doc, err := l.Fetch()
			g.Assert(err).Equal(nil)
			g.Assert(doc.Find("body").Length()).Equal(1)
		})
	})

	g.Describe("#fetchFormattedResult", func() {
		g.It("Should grab the results from the page", func() {
			l := page.Page{
				"http://stackoverflow.com/questions/11810218/how-to-set-and-get-fields-in-golang-structs?answertab=votes",
				"how to set and get fields in golang structs?",
			}
			result := make(chan string, 1)
			go fetchFormattedResult(l, result)
			resultText := <-result
			g.Assert(strings.Contains(resultText, linkHeading(l))).Equal(true)
			g.Assert(strings.Count(resultText, "\n") > 6).Equal(true)
		})

		g.It("Should not panic when no answers are given for a question", func() {
			l := page.Page{
				"http://stackoverflow.com/questions/21370072/multiple-files-to-download-and-to-rename-using-curl?answertab=votes",
				"Multiple files to download and to rename using curl",
			}
			result := make(chan string, 1)
			go fetchFormattedResult(l, result)
			resultText := <-result
			g.Assert(strings.Contains(resultText, "No answers given"))
		})
	})
}
예제 #27
0
파일: deis_test.go 프로젝트: voxxit/drone
func Test_Deis(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("Deis Deploy", func() {

		g.It("Should set git.config", func() {
			b := new(buildfile.Buildfile)
			h := Deis{
				App:     "drone",
				Deisurl: "deis.yourdomain.com:2222",
			}

			h.Write(b)
			out := b.String()
			g.Assert(strings.Contains(out, CmdRevParse)).Equal(true)
			g.Assert(strings.Contains(out, CmdGlobalUser)).Equal(true)
			g.Assert(strings.Contains(out, CmdGlobalEmail)).Equal(true)
		})

		g.It("Should add remote", func() {
			b := new(buildfile.Buildfile)
			h := Deis{
				App:     "drone",
				Deisurl: "deis.yourdomain.com:2222/",
			}

			h.Write(b)
			out := b.String()
			g.Assert(strings.Contains(out, "\ngit remote add deis ssh://[email protected]:2222/drone.git\n")).Equal(true)
		})

		g.It("Should push to remote", func() {
			b := new(buildfile.Buildfile)
			d := Deis{
				App: "drone",
			}

			d.Write(b)
			out := b.String()
			g.Assert(strings.Contains(out, "\ngit push deis $COMMIT:master\n")).Equal(true)
		})

		g.It("Should force push to remote", func() {
			b := new(buildfile.Buildfile)
			h := Deis{
				Force: true,
				App:   "drone",
			}

			h.Write(b)
			out := b.String()
			g.Assert(strings.Contains(out, "\ngit add -A\n")).Equal(true)
			g.Assert(strings.Contains(out, "\ngit commit -m 'adding build artifacts'\n")).Equal(true)
			g.Assert(strings.Contains(out, "\ngit push deis HEAD:master --force\n")).Equal(true)
		})

	})
}
예제 #28
0
func TestTeams(t *testing.T) {
	gin.SetMode(gin.TestMode)
	logrus.SetOutput(ioutil.Discard)

	g := goblin.Goblin(t)

	g.Describe("Team endpoint", func() {
		g.It("Should return the team list", func() {
			cache := new(cache.Cache)
			cache.On("Get", "teams:octocat").Return(fakeTeams, nil).Once()

			e := gin.New()
			e.NoRoute(GetTeams)
			e.Use(func(c *gin.Context) {
				c.Set("user", fakeUser)
				c.Set("cache", cache)
			})

			w := httptest.NewRecorder()
			r, _ := http.NewRequest("GET", "/", nil)
			e.ServeHTTP(w, r)

			// the user is appended to the team list so we retrieve a full list of
			// accounts to which the user has access.
			teams := append(fakeTeams, &model.Team{
				Login: fakeUser.Login,
			})

			want, _ := json.Marshal(teams)
			got := strings.TrimSpace(w.Body.String())
			g.Assert(got).Equal(string(want))
			g.Assert(w.Code).Equal(200)
		})

		g.It("Should return a 500 error", func() {
			remote := new(remote.Remote)
			cache := new(cache.Cache)
			cache.On("Get", "teams:octocat").Return(nil, fmt.Errorf("Not Found")).Once()
			remote.On("GetTeams", fakeUser).Return(nil, fmt.Errorf("Not Found")).Once()

			e := gin.New()
			e.NoRoute(GetTeams)
			e.Use(func(c *gin.Context) {
				c.Set("user", fakeUser)
				c.Set("cache", cache)
				c.Set("remote", remote)
			})

			w := httptest.NewRecorder()
			r, _ := http.NewRequest("GET", "/", nil)
			e.ServeHTTP(w, r)

			got := strings.TrimSpace(w.Body.String())
			g.Assert(got).Equal("Error getting team list")
			g.Assert(w.Code).Equal(500)
		})
	})
}
예제 #29
0
func Test_normalize(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("normalizing", func() {

		g.Describe("images", func() {

			g.It("should append tag if empty", func() {
				c := newConfig(&yaml.Container{
					Image: "golang",
				})

				ImageTag(c)
				g.Assert(c.Pipeline[0].Image).Equal("golang:latest")
			})

			g.It("should not override existing tag", func() {
				c := newConfig(&yaml.Container{
					Image: "golang:1.5",
				})

				ImageTag(c)
				g.Assert(c.Pipeline[0].Image).Equal("golang:1.5")
			})
		})

		g.Describe("plugins", func() {

			g.It("should prepend namespace", func() {
				c := newConfig(&yaml.Container{
					Image: "slack",
				})

				ImageNamespace(c, "plugins")
				g.Assert(c.Pipeline[0].Image).Equal("plugins/slack")
			})

			g.It("should not override existing namespace", func() {
				c := newConfig(&yaml.Container{
					Image: "index.docker.io/drone/git",
				})

				ImageNamespace(c, "plugins")
				g.Assert(c.Pipeline[0].Image).Equal("index.docker.io/drone/git")
			})

			g.It("should replace underscores with dashes", func() {
				c := newConfig(&yaml.Container{
					Image: "gh_pages",
				})

				ImageName(c)
				g.Assert(c.Pipeline[0].Image).Equal("gh-pages")
			})
		})
	})
}
예제 #30
0
func TestContainerNode(t *testing.T) {
	g := goblin.Goblin(t)

	g.Describe("Containers", func() {
		g.Describe("given a yaml file", func() {

			g.It("should unmarshal", func() {
				in := []byte(sampleContainer)
				out := containerList{}
				err := yaml.Unmarshal(in, &out)
				if err != nil {
					g.Fail(err)
				}
				g.Assert(len(out.containers)).Equal(1)

				c := out.containers[0]
				g.Assert(c.Name).Equal("foo")
				g.Assert(c.Image).Equal("golang")
				g.Assert(c.Build).Equal(".")
				g.Assert(c.Pull).Equal(true)
				g.Assert(c.Privileged).Equal(true)
				g.Assert(c.Entrypoint.parts).Equal([]string{"/bin/sh"})
				g.Assert(c.Command.parts).Equal([]string{"yes"})
				g.Assert(c.Commands.parts).Equal([]string{"whoami"})
				g.Assert(c.ExtraHosts.parts).Equal([]string{"foo.com"})
				g.Assert(c.Volumes.parts).Equal([]string{"/foo:/bar"})
				g.Assert(c.VolumesFrom.parts).Equal([]string{"foo"})
				g.Assert(c.Devices.parts).Equal([]string{"/dev/tty0"})
				g.Assert(c.Network).Equal("bridge")
				g.Assert(c.DNS.parts).Equal([]string{"8.8.8.8"})
				g.Assert(c.MemSwapLimit).Equal(int64(1))
				g.Assert(c.MemLimit).Equal(int64(2))
				g.Assert(c.CPUQuota).Equal(int64(3))
				g.Assert(c.CPUSet).Equal("1,2")
				g.Assert(c.OomKillDisable).Equal(true)
				g.Assert(c.AuthConfig.Username).Equal("octocat")
				g.Assert(c.AuthConfig.Password).Equal("password")
				g.Assert(c.AuthConfig.Email).Equal("*****@*****.**")
				g.Assert(c.Vargs["access_key"]).Equal("970d28f4dd477bc184fbd10b376de753")
				g.Assert(c.Vargs["secret_key"]).Equal("9c5785d3ece6a9cdefa42eb99b58986f9095ff1c")
			})

			g.It("should unmarshal named", func() {
				in := []byte("foo: { name: bar }")
				out := containerList{}
				err := yaml.Unmarshal(in, &out)
				if err != nil {
					g.Fail(err)
				}
				g.Assert(len(out.containers)).Equal(1)
				g.Assert(out.containers[0].Name).Equal("bar")
			})

		})
	})
}