Example #1
0
func TestFetchPageHTTP(t *testing.T) {

	testOne := func(tt FetchTest) {
		ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(tt.responseCode)
			w.Write([]byte(tt.html))
		}))
		defer ts.Close()

		baseURL, _ := url.Parse(ts.URL)
		p := newEagerPage(baseURL)
		links := FetchPageHTTP(p)

		if tt.error != "" {
			assert.Error(t, p.Error(), "there should be a page fetch error")
		} else {
			assert.NoError(t, p.Error(), "there should be no page error")

			assert.Condition(t, func() bool {
				if len(tt.links) != len(links) {
					return false
				}
				for i, l := range links {
					uu, _ := baseURL.Parse(tt.links[i])
					if *(*url.URL)(l) != *uu {
						return false
					}
				}
				return true
			}, "all links should be resolved relative to base URL")

			assert.Condition(t, func() bool {
				if len(tt.assets) != len(p.Assets()) {
					return false
				}
				for i, a := range p.Assets() {
					uu, _ := baseURL.Parse(tt.assets[i])
					if *(*url.URL)(a) != *uu {
						return false
					}
				}
				return true
			}, "all assets should be resolved relative to the base URL")
		}
	}

	for _, tt := range fetchTests {
		testOne(tt)
	}
}
Example #2
0
// Tests creating a new chain
func TestNew(t *testing.T) {
	c1 := func(h Handler) Handler {
		return nil
	}

	c2 := func(h Handler) Handler {
		return nil
	}

	slice := []Constructor{c1, c2}
	chain := New(slice...)
	assert.Condition(t, funcsEqual(chain.constructors[0], slice[0]))
	assert.Condition(t, funcsEqual(chain.constructors[1], slice[1]))
}
Example #3
0
func TestEmpireTemplate_Large(t *testing.T) {
	labels := make(map[string]string)
	env := make(map[string]string)
	for i := 0; i < 100; i++ {
		env[fmt.Sprintf("ENV_VAR_%d", i)] = fmt.Sprintf("value%d", i)
	}
	app := &scheduler.App{
		ID:      "",
		Release: "v1",
		Name:    "bigappwithlotsofprocesses",
		Env:     env,
		Labels:  labels,
	}

	for i := 0; i < 60; i++ {
		app.Processes = append(app.Processes, &scheduler.Process{
			Type:    fmt.Sprintf("%d", i),
			Command: []string{"./bin/web"},
		})
	}

	tmpl := newTemplate()
	buf := new(bytes.Buffer)

	data := &TemplateData{app, nil}
	err := tmpl.Execute(buf, data)
	t.Logf("Template size: %d bytes", buf.Len())
	assert.NoError(t, err)
	assert.Condition(t, func() bool {
		return buf.Len() < MaxTemplateSize
	}, fmt.Sprintf("template must be smaller than %d, was %d", MaxTemplateSize, buf.Len()))
}
Example #4
0
func TestHoard_ExpirationSetting(t *testing.T) {

	h := Make(Expires().AfterSeconds(1))

	result := h.Get("key2", func() (interface{}, *Expiration) {
		expiration := Expires().AfterSecondsIdle(10).AfterSeconds(10).OnCondition(func() bool {
			return true
		})
		return "second", expiration
	})

	assert.Equal(t, result, "second")
	assert.NotEqual(t, 0, h.cache["key2"].expiration.idle)
	assert.NotEqual(t, 0, h.cache["key2"].expiration.duration)
	assert.Condition(t, func() bool {
		return h.cache["key2"].expiration.condition != nil
	})
	assert.Condition(t, func() bool {
		return !h.cache["key2"].expiration.absolute.IsZero()
	})

}
Example #5
0
func TestShared_ExpirationSetting(t *testing.T) {

	result := Get("key2", func() (interface{}, *Expiration) {
		expiration := new(Expiration)
		expiration.AfterSecondsIdle(10)
		expiration.AfterSeconds(10)
		expiration.OnCondition(func() bool {
			return true
		})
		return "second", expiration
	})

	assert.Equal(t, result, "second")
	assert.NotEqual(t, 0, Shared().cache["key2"].expiration.idle)
	assert.NotEqual(t, 0, Shared().cache["key2"].expiration.duration)
	assert.Condition(t, func() bool {
		return Shared().cache["key2"].expiration.condition != nil
	})
	assert.Condition(t, func() bool {
		return !Shared().cache["key2"].expiration.absolute.IsZero()
	})

}
Example #6
0
func TestReset(t *testing.T) {

	var bitMask uint64 = 1<<0 | 1<<1 | 1<<2 | 1<<3

	b := MakeBits(bitMask)

	if assert.NotNil(t, b) {

		b.Reset()
		assert.Condition(t, func() bool {
			return b.bits == 0
		})
	}

}
Example #7
0
func TestHoard_Make(t *testing.T) {

	h := Make(ExpiresNever)

	if assert.NotNil(t, h) {
		assert.Equal(t, h.defaultExpiration, ExpiresNever)
	}

	h = Make(Expires().AfterSeconds(3))
	if assert.NotNil(t, h) {
		assert.Condition(t, func() bool {
			return h.defaultExpiration.duration == 3*time.Second
		})
	}

}
Example #8
0
// TestGetJobs
func TestGetJobs(t *testing.T) {
	var (
		// d *OVTest
		c *ICSPClient
	)
	if os.Getenv("ONEVIEW_TEST_ACCEPTANCE") == "true" {
		_, c = getTestDriverA()
		if c == nil {
			t.Fatalf("Failed to execute getTestDriver() ")
		}
		data, err := c.GetJobs()
		assert.NoError(t, err, "GetJobs threw error -> %s, %+v\n", err, data)
		if data.Total > 0 {
			log.Debugf("data -> %+v", data.Members[0])
			assert.Condition(t, func() bool { return len(data.Members[0].URI) > 0 }, "has no uri content")
			data, err := c.GetJob(ODSUri{URI: data.Members[0].URI})
			assert.NoError(t, err, "GetJob threw error -> %s, %+v\n", err, data)
		}
	} else {
		_, c = getTestDriverU()
		data, err := c.GetJobs()
		assert.Error(t, err, fmt.Sprintf("ALL ok, no error, caught as expected: %s,%+v\n", err, data))
	}
}
Example #9
0
// Condition uses a Comparison to assert a complex condition.
func Condition(t TestingT, comp assert.Comparison, msgAndArgs ...interface{}) {
	if !assert.Condition(t, comp, msgAndArgs...) {
		t.FailNow()
	}
}