예제 #1
0
파일: fetcher_test.go 프로젝트: Kimau/GoCam
func TestHourReport(t *testing.T) {

	timeStart := time.Now().Add(-time.Hour * 2)
	lastFrame := 450 + time.Duration(rand.Intn(100))
	var timCount uint8 = 0

	blkListRand := []computeBlock{}
	blkListRamp := []computeBlock{}

	for t := timeStart; time.Now().After(t); t = t.Add(time.Duration(time.Millisecond * lastFrame)) {
		lastFrame = 450 + time.Duration(rand.Intn(100))

		blkListRand = append(blkListRand, computeBlock{
			stamp: t,
			lum:   uint8(rand.Intn(0xFF)),
		})

		blkListRamp = append(blkListRamp, computeBlock{
			stamp: t,
			lum:   timCount,
		})

		timCount += 1
	}

	saveGIFToFolder("_testReport0.gif", makeLumHourlyImg(blkListRand), 256)
	saveGIFToFolder("_testReport1.gif", makeLumHourlyImg(blkListRamp), 256)

	saveGIFToFolder("_testTimeline0.gif", makeLumTimeline(blkListRand), 256)
	saveGIFToFolder("_testTimeline1.gif", makeLumTimeline(blkListRamp), 256)
}
예제 #2
0
func Test_Tree_Match(t *testing.T) {
	Convey("Match route in tree", t, func() {
		Convey("Match static routes", func() {
			t := NewTree()
			So(t.Add("/", "", nil), ShouldBeFalse)
			So(t.Add("/user", "", nil), ShouldBeFalse)
			So(t.Add("/user/unknwon", "", nil), ShouldBeFalse)
			So(t.Add("/user/unknwon/profile", "", nil), ShouldBeFalse)

			So(t.Add("/", "/", nil), ShouldBeTrue)

			_, _, ok := t.Match("/")
			So(ok, ShouldBeTrue)
			_, _, ok = t.Match("/user")
			So(ok, ShouldBeTrue)
			_, _, ok = t.Match("/user/unknwon")
			So(ok, ShouldBeTrue)
			_, _, ok = t.Match("/user/unknwon/profile")
			So(ok, ShouldBeTrue)

			_, _, ok = t.Match("/404")
			So(ok, ShouldBeFalse)
		})

		Convey("Match optional routes", func() {
			t := NewTree()
			So(t.Add("/?:user", "", nil), ShouldBeFalse)
			So(t.Add("/user/?:name", "", nil), ShouldBeFalse)
			So(t.Add("/user/list/?:page:int", "", nil), ShouldBeFalse)

			_, params, ok := t.Match("/")
			So(ok, ShouldBeTrue)
			So(params[":user"], ShouldBeEmpty)
			_, params, ok = t.Match("/unknwon")
			So(ok, ShouldBeTrue)
			So(params[":user"], ShouldEqual, "unknwon")

			_, params, ok = t.Match("/user")
			So(ok, ShouldBeTrue)
			So(params[":name"], ShouldBeEmpty)
			_, params, ok = t.Match("/user/unknwon")
			So(ok, ShouldBeTrue)
			So(params[":name"], ShouldEqual, "unknwon")

			_, params, ok = t.Match("/user/list/")
			So(ok, ShouldBeTrue)
			So(params[":page"], ShouldBeEmpty)
			_, params, ok = t.Match("/user/list/123")
			So(ok, ShouldBeTrue)
			So(params[":page"], ShouldEqual, "123")
		})

		Convey("Match with regexp", func() {
			t := NewTree()
			So(t.Add("/v1/:year:int/6/23", "", nil), ShouldBeFalse)
			So(t.Add("/v2/2015/:month:int/23", "", nil), ShouldBeFalse)
			So(t.Add("/v3/2015/6/:day:int", "", nil), ShouldBeFalse)

			_, params, ok := t.Match("/v1/2015/6/23")
			So(ok, ShouldBeTrue)
			So(MatchTest("/v1/:year:int/6/23", "/v1/2015/6/23"), ShouldBeTrue)
			So(params[":year"], ShouldEqual, "2015")
			_, _, ok = t.Match("/v1/year/6/23")
			So(ok, ShouldBeFalse)
			So(MatchTest("/v1/:year:int/6/23", "/v1/year/6/23"), ShouldBeFalse)

			_, params, ok = t.Match("/v2/2015/6/23")
			So(ok, ShouldBeTrue)
			So(params[":month"], ShouldEqual, "6")
			_, _, ok = t.Match("/v2/2015/month/23")
			So(ok, ShouldBeFalse)

			_, params, ok = t.Match("/v3/2015/6/23")
			So(ok, ShouldBeTrue)
			So(params[":day"], ShouldEqual, "23")
			_, _, ok = t.Match("/v2/2015/6/day")
			So(ok, ShouldBeFalse)

			So(t.Add("/v1/shop/cms_:id(.+)_:page(.+).html", "", nil), ShouldBeFalse)
			So(t.Add("/v1/:v/cms/aaa_:id(.+)_:page(.+).html", "", nil), ShouldBeFalse)
			So(t.Add("/v1/:v/cms_:id(.+)_:page(.+).html", "", nil), ShouldBeFalse)
			So(t.Add("/v1/:v(.+)_cms/ttt_:id(.+)_:page:string.html", "", nil), ShouldBeFalse)

			_, params, ok = t.Match("/v1/shop/cms_123_1.html")
			So(ok, ShouldBeTrue)
			So(params[":id"], ShouldEqual, "123")
			So(params[":page"], ShouldEqual, "1")

			_, params, ok = t.Match("/v1/2/cms/aaa_124_2.html")
			So(ok, ShouldBeTrue)
			So(params[":v"], ShouldEqual, "2")
			So(params[":id"], ShouldEqual, "124")
			So(params[":page"], ShouldEqual, "2")

			_, params, ok = t.Match("/v1/3/cms_125_3.html")
			So(ok, ShouldBeTrue)
			So(params[":v"], ShouldEqual, "3")
			So(params[":id"], ShouldEqual, "125")
			So(params[":page"], ShouldEqual, "3")

			_, params, ok = t.Match("/v1/4_cms/ttt_126_4.html")
			So(ok, ShouldBeTrue)
			So(params[":v"], ShouldEqual, "4")
			So(params[":id"], ShouldEqual, "126")
			So(params[":page"], ShouldEqual, "4")
		})

		Convey("Match with path and extension", func() {
			t := NewTree()
			So(t.Add("/*.*", "", nil), ShouldBeFalse)
			So(t.Add("/docs/*.*", "", nil), ShouldBeFalse)

			_, params, ok := t.Match("/profile.html")
			So(ok, ShouldBeTrue)
			So(params[":path"], ShouldEqual, "profile")
			So(params[":ext"], ShouldEqual, "html")

			_, params, ok = t.Match("/profile")
			So(ok, ShouldBeTrue)
			So(params[":path"], ShouldEqual, "profile")
			So(params[":ext"], ShouldBeEmpty)

			_, params, ok = t.Match("/docs/framework/manual.html")
			So(ok, ShouldBeTrue)
			So(params[":path"], ShouldEqual, "framework/manual")
			So(params[":ext"], ShouldEqual, "html")

			_, params, ok = t.Match("/docs/framework/manual")
			So(ok, ShouldBeTrue)
			So(params[":path"], ShouldEqual, "framework/manual")
			So(params[":ext"], ShouldBeEmpty)
		})

		Convey("Match all", func() {
			t := NewTree()
			So(t.Add("/*", "", nil), ShouldBeFalse)
			So(t.Add("/*/123", "", nil), ShouldBeFalse)
			So(t.Add("/*/123/*", "", nil), ShouldBeFalse)
			So(t.Add("/*/*/123", "", nil), ShouldBeFalse)

			_, params, ok := t.Match("/1/2/3")
			So(ok, ShouldBeTrue)
			So(params["*0"], ShouldEqual, "1/2/3")

			_, params, ok = t.Match("/4/123")
			So(ok, ShouldBeTrue)
			So(params["*0"], ShouldEqual, "4")

			_, params, ok = t.Match("/5/123/6")
			So(ok, ShouldBeTrue)
			So(params["*0"], ShouldEqual, "5")
			So(params["*1"], ShouldEqual, "6")

			_, params, ok = t.Match("/7/8/123")
			So(ok, ShouldBeTrue)
			So(params["*0"], ShouldEqual, "7")
			So(params["*1"], ShouldEqual, "8")
		})

		Convey("Complex tests", func() {
			t := NewTree()
			So(t.Add("/:username/:reponame/commit/*", "", nil), ShouldBeFalse)

			_, params, ok := t.Match("/unknwon/com/commit/d855b6c9dea98c619925b7b112f3c4e64b17bfa8")
			So(ok, ShouldBeTrue)
			So(params["*"], ShouldEqual, "d855b6c9dea98c619925b7b112f3c4e64b17bfa8")
		})
	})
}
예제 #3
0
파일: driver_test.go 프로젝트: jarpy/beats
func TestDateTime(t *testing.T) {
	afterTime := func(t time.Time, d string) time.Time {
		dur, err := time.ParseDuration(d)
		if err != nil {
			panic(err)
		}
		return t.Add(dur)
	}
	// NOTE: MySQL rounds DATETIME(x) up - but that's not included in the tests
	format := "2006-01-02 15:04:05.999999"
	t0 := time.Time{}
	tstr0 := "0000-00-00 00:00:00.000000"
	testcases := []timeTests{
		{"DATE", format[:10], []timeTest{
			{t: time.Date(2011, 11, 20, 0, 0, 0, 0, time.UTC)},
			{t: t0, s: tstr0[:10]},
		}},
		{"DATETIME", format[:19], []timeTest{
			{t: time.Date(2011, 11, 20, 21, 27, 37, 0, time.UTC)},
			{t: t0, s: tstr0[:19]},
		}},
		{"DATETIME(0)", format[:21], []timeTest{
			{t: time.Date(2011, 11, 20, 21, 27, 37, 0, time.UTC)},
			{t: t0, s: tstr0[:19]},
		}},
		{"DATETIME(1)", format[:21], []timeTest{
			{t: time.Date(2011, 11, 20, 21, 27, 37, 100000000, time.UTC)},
			{t: t0, s: tstr0[:21]},
		}},
		{"DATETIME(6)", format, []timeTest{
			{t: time.Date(2011, 11, 20, 21, 27, 37, 123456000, time.UTC)},
			{t: t0, s: tstr0},
		}},
		{"TIME", format[11:19], []timeTest{
			{t: afterTime(t0, "12345s")},
			{s: "!-12:34:56"},
			{s: "!-838:59:59"},
			{s: "!838:59:59"},
			{t: t0, s: tstr0[11:19]},
		}},
		{"TIME(0)", format[11:19], []timeTest{
			{t: afterTime(t0, "12345s")},
			{s: "!-12:34:56"},
			{s: "!-838:59:59"},
			{s: "!838:59:59"},
			{t: t0, s: tstr0[11:19]},
		}},
		{"TIME(1)", format[11:21], []timeTest{
			{t: afterTime(t0, "12345600ms")},
			{s: "!-12:34:56.7"},
			{s: "!-838:59:58.9"},
			{s: "!838:59:58.9"},
			{t: t0, s: tstr0[11:21]},
		}},
		{"TIME(6)", format[11:], []timeTest{
			{t: afterTime(t0, "1234567890123000ns")},
			{s: "!-12:34:56.789012"},
			{s: "!-838:59:58.999999"},
			{s: "!838:59:58.999999"},
			{t: t0, s: tstr0[11:]},
		}},
	}
	dsns := []string{
		dsn + "&parseTime=true",
		dsn + "&parseTime=false",
	}
	for _, testdsn := range dsns {
		runTests(t, testdsn, func(dbt *DBTest) {
			microsecsSupported := false
			zeroDateSupported := false
			var rows *sql.Rows
			var err error
			rows, err = dbt.db.Query(`SELECT cast("00:00:00.1" as TIME(1)) = "00:00:00.1"`)
			if err == nil {
				rows.Scan(&microsecsSupported)
				rows.Close()
			}
			rows, err = dbt.db.Query(`SELECT cast("0000-00-00" as DATE) = "0000-00-00"`)
			if err == nil {
				rows.Scan(&zeroDateSupported)
				rows.Close()
			}
			for _, setups := range testcases {
				if t := setups.dbtype; !microsecsSupported && t[len(t)-1:] == ")" {
					// skip fractional second tests if unsupported by server
					continue
				}
				for _, setup := range setups.tests {
					allowBinTime := true
					if setup.s == "" {
						// fill time string whereever Go can reliable produce it
						setup.s = setup.t.Format(setups.tlayout)
					} else if setup.s[0] == '!' {
						// skip tests using setup.t as source in queries
						allowBinTime = false
						// fix setup.s - remove the "!"
						setup.s = setup.s[1:]
					}
					if !zeroDateSupported && setup.s == tstr0[:len(setup.s)] {
						// skip disallowed 0000-00-00 date
						continue
					}
					setup.run(dbt, setups.dbtype, setups.tlayout, textString)
					setup.run(dbt, setups.dbtype, setups.tlayout, binaryString)
					if allowBinTime {
						setup.run(dbt, setups.dbtype, setups.tlayout, binaryTime)
					}
				}
			}
		})
	}
}
예제 #4
0
func TestDataSourceCache(t *testing.T) {
	Convey("When caching a datasource proxy", t, func() {
		clearCache()
		ds := DataSource{
			Id:   1,
			Url:  "http://k8s:8001",
			Type: "Kubernetes",
		}

		t1, err := ds.GetHttpTransport()
		So(err, ShouldBeNil)

		t2, err := ds.GetHttpTransport()
		So(err, ShouldBeNil)

		Convey("Should be using the cached proxy", func() {
			So(t2, ShouldEqual, t1)
		})
	})

	Convey("When getting kubernetes datasource proxy", t, func() {
		clearCache()
		setting.SecretKey = "password"

		json := simplejson.New()
		json.Set("tlsAuth", true)
		json.Set("tlsAuthWithCACert", true)

		t := time.Now()
		ds := DataSource{
			Url:     "http://k8s:8001",
			Type:    "Kubernetes",
			Updated: t.Add(-2 * time.Minute),
		}

		transport, err := ds.GetHttpTransport()
		So(err, ShouldBeNil)

		Convey("Should have no cert", func() {
			So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, true)
		})

		ds.JsonData = json
		ds.SecureJsonData = map[string][]byte{
			"tlsCACert":     util.Encrypt([]byte(caCert), "password"),
			"tlsClientCert": util.Encrypt([]byte(clientCert), "password"),
			"tlsClientKey":  util.Encrypt([]byte(clientKey), "password"),
		}
		ds.Updated = t.Add(-1 * time.Minute)

		transport, err = ds.GetHttpTransport()
		So(err, ShouldBeNil)

		Convey("Should add cert", func() {
			So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, false)
			So(len(transport.TLSClientConfig.Certificates), ShouldEqual, 1)
		})

		ds.JsonData = nil
		ds.SecureJsonData = map[string][]byte{}
		ds.Updated = t

		transport, err = ds.GetHttpTransport()
		So(err, ShouldBeNil)

		Convey("Should remove cert", func() {
			So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, true)
			So(len(transport.TLSClientConfig.Certificates), ShouldEqual, 0)
		})
	})
}
예제 #5
0
func TestDataSourceProxy(t *testing.T) {

	Convey("When getting graphite datasource proxy", t, func() {
		clearCache()
		ds := m.DataSource{Url: "htttp://graphite:8080", Type: m.DS_GRAPHITE}
		targetUrl, err := url.Parse(ds.Url)
		proxy := NewReverseProxy(&ds, "/render", targetUrl)
		proxy.Transport, err = DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		transport, ok := proxy.Transport.(*http.Transport)
		So(ok, ShouldBeTrue)
		So(transport.TLSClientConfig.InsecureSkipVerify, ShouldBeTrue)

		requestUrl, _ := url.Parse("http://grafana.com/sub")
		req := http.Request{URL: requestUrl}

		proxy.Director(&req)

		Convey("Can translate request url and path", func() {
			So(req.URL.Host, ShouldEqual, "graphite:8080")
			So(req.URL.Path, ShouldEqual, "/render")
		})
	})

	Convey("When getting influxdb datasource proxy", t, func() {
		clearCache()
		ds := m.DataSource{
			Type:     m.DS_INFLUXDB_08,
			Url:      "http://influxdb:8083",
			Database: "site",
			User:     "******",
			Password: "******",
		}

		targetUrl, _ := url.Parse(ds.Url)
		proxy := NewReverseProxy(&ds, "", targetUrl)

		requestUrl, _ := url.Parse("http://grafana.com/sub")
		req := http.Request{URL: requestUrl}

		proxy.Director(&req)

		Convey("Should add db to url", func() {
			So(req.URL.Path, ShouldEqual, "/db/site/")
		})

		Convey("Should add username and password", func() {
			queryVals := req.URL.Query()
			So(queryVals["u"][0], ShouldEqual, "user")
			So(queryVals["p"][0], ShouldEqual, "password")
		})
	})

	Convey("When caching a datasource proxy", t, func() {
		clearCache()
		ds := m.DataSource{
			Id:   1,
			Url:  "http://k8s:8001",
			Type: "Kubernetes",
		}

		t1, err := DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		t2, err := DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		Convey("Should be using the cached proxy", func() {
			So(t2, ShouldEqual, t1)
		})
	})

	Convey("When getting kubernetes datasource proxy", t, func() {
		clearCache()
		setting.SecretKey = "password"

		json := simplejson.New()
		json.Set("tlsAuth", true)
		json.Set("tlsAuthWithCACert", true)

		t := time.Now()
		ds := m.DataSource{
			Url:     "http://k8s:8001",
			Type:    "Kubernetes",
			Updated: t.Add(-2 * time.Minute),
		}

		transport, err := DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		Convey("Should have no cert", func() {
			So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, true)
		})

		ds.JsonData = json
		ds.SecureJsonData = map[string][]byte{
			"tlsCACert":     util.Encrypt([]byte(caCert), "password"),
			"tlsClientCert": util.Encrypt([]byte(clientCert), "password"),
			"tlsClientKey":  util.Encrypt([]byte(clientKey), "password"),
		}
		ds.Updated = t.Add(-1 * time.Minute)

		transport, err = DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		Convey("Should add cert", func() {
			So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, false)
			So(len(transport.TLSClientConfig.Certificates), ShouldEqual, 1)
		})

		ds.JsonData = nil
		ds.SecureJsonData = map[string][]byte{}
		ds.Updated = t

		transport, err = DataProxyTransport(&ds)
		So(err, ShouldBeNil)

		Convey("Should remove cert", func() {
			So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, true)
			So(len(transport.TLSClientConfig.Certificates), ShouldEqual, 0)
		})
	})

}
예제 #6
0
func TestName(t *testing.T) {

	Convey("Should kill them one.", t, func() {
		hits := NewTargets()
		hits.Add(NewWorker())
		hits.Add(NewWorker())
		hits.Add(NewWorker())
		hits.Add(NewWorker())

		So(len(hits), ShouldEqual, 4)
		err := hits.Close()
		So(err, ShouldBeNil)
	})

	Convey("New kill channels should have a length of 0.", t, func() {
		k := NewKillChannel()
		So(len(k), ShouldEqual, 0)
	})

	Convey("Should kill them one.", t, func() {
		hits := NewTargets()
		t := &Worker{}
		hits.Add(t)

		So(len(hits), ShouldEqual, 1)

		err := hits.Close()
		So(err, ShouldBeNil)
	})

	Convey("New Targets collection should have len of zero", t, func() {
		t := NewTargets()
		w := NewWorker()
		t.Add(w)

		v, ok := t[w.Name()]

		So(len(t), ShouldEqual, 1)
		So(v, ShouldNotBeNil)
		So(ok, ShouldBeTrue)
	})

	Convey("New Targets collection should have len of zero", t, func() {
		t := NewTargets()
		So(len(t), ShouldEqual, 0)
	})

	Convey("New Target via literals", t, func() {
		t := NewTargets()
		t.AddTarget(&Unnamed{})
		So(len(t), ShouldEqual, 1)
		t.Close()
	})

	Convey("New Target or panic should be fine if no error", t, func() {
		t := NewTargets()
		t.AddOrPanic(NewService())
		So(len(t), ShouldEqual, 1)
		t.Close()
	})

	Convey("Should panic when adding a service that failed to instantiate without error", t, func() {
		defer func() {
			panicky := recover()
			err, ok := panicky.(error)
			So(ok, ShouldBeTrue)
			So(err, ShouldNotBeNil)
		}()
		NewTargets().AddOrPanic(NewServiceWithError())
	})
}