Ejemplo n.º 1
0
func TestHappyScheduler(t *testing.T) {
	var totalJobs = 3

	// Start the scheduler
	sch := NewScheduler()

	// Enqueue all the mock jobs
	mockJobs := jobGenerator(totalJobs)
	for i := 0; i < totalJobs; i++ {
		sch.Enqueue(mockJobs[i])
	}

	assert.Equal(t, sch.Size(), 3)

	// Dequeue all the mock jobs
	cont := 0
	dueJobC := make(chan *Job, 10)
	stopC := make(chan bool)
	sch.Notify(dueJobC, stopC)

	for job := range dueJobC {
		assert.Equal(t, job.Id, strconv.Itoa(cont), "Received the wrong job")
		cont += 1
		if cont == totalJobs {
			stopC <- true
		}
	}

	assert.Equal(t, sch.Size(), 0)
}
Ejemplo n.º 2
0
func TestHappyProcessChain(t *testing.T) {
	OnRequest1Fn := func(ctx context.ContextSpec) (*http.Response, error) {
		return nil, nil
	}
	OnResponse1Fn := func(ctx context.ContextSpec) (*http.Response, error) {
		return nil, nil
	}

	OnRequest2Fn := func(ctx context.ContextSpec) (*http.Response, error) {
		return httputils.NewTextResponse(nil, 200, "happy life"), nil
	}
	OnResponse2Fn := func(ctx context.ContextSpec) (*http.Response, error) {
		return nil, nil
	}

	modMock1 := modules.NewWithParams("mod_mock_1", OnRequest1Fn, OnResponse1Fn)
	modMock2 := modules.NewWithParams("mod_mock_2", OnRequest2Fn, OnResponse2Fn)

	chain := New()
	chain.AppendModule(modMock1)
	chain.AppendModule(modMock2)

	assert.Equal(t, len(chain.GetModules()), 2)

	resp, err := chain.Process(context.Empty())

	assert.Nil(t, err)
	assert.Equal(t, resp.StatusCode, 200)
}
Ejemplo n.º 3
0
// docker run golang go version
func TestRunContainer(t *testing.T) {
	var payload = []byte(`{"AttachStdin": "false",
		"AttachStout": "true",
		"AttachStderr": "true",
		"Tty": "false",
		"OpenStdin": "false",
		"StdinOnce": "false",
		"Cmd":["/bin/bash", "-c", "go", "version"],
		"Image": "golang",
		"DisableNetwork": "false"
	}`)

	mockReq, _ := http.NewRequest("POST", "/containers/create?name=golang", bytes.NewBuffer(payload))

	ctx := context.Empty()
	ctx.SetHttpRequest(mockReq)

	modDocker := NewDocker(defaultParams())
	resp, err := modDocker.ProcessRequest(ctx)
	assert.Nil(t, err)

	assert.Equal(t, resp.StatusCode, 200)
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Printf("Docker Run output\n============\n %s\n", body)
}
Ejemplo n.º 4
0
func TestEvalRequest(t *testing.T) {
	mockreq := getMockRequest()
	ctx := Empty()
	ctx.SetHttpRequest(mockreq)

	url := ctx.Eval("$request.method")
	assert.Equal(t, url, "GET")

	ver := ctx.Eval("$request.queryparam.version")
	assert.Equal(t, ver, "2.0")

	unknown := ctx.Eval("$request.doesNotExist")
	assert.Equal(t, unknown, "<$request.doesNotExist> ??")

	fullReq := ctx.Eval("$request")
	fmt.Printf("%s\n", fullReq)
}
Ejemplo n.º 5
0
func TestEncDecJob(t *testing.T) {
	job := NewJob("test", "script.sh", false, -1, 3, cronsExp["every_minute"])
	bytes, err := job.Bytes()

	assert.NoError(t, err)

	job2, err2 := NewFromBytes(bytes)
	assert.NoError(t, err2)

	assert.Equal(t, job, job2, "job before and after serialization is not the same")
}
Ejemplo n.º 6
0
func TestGetImages(t *testing.T) {
	// Prepare the context
	mockReq, _ := http.NewRequest("GET", "/images/json", nil)
	ctx := context.Empty()
	ctx.SetHttpRequest(mockReq)

	modDocker := NewDocker(defaultParams())
	resp, err := modDocker.ProcessRequest(ctx)

	assert.Nil(t, err)
	assert.Equal(t, resp.StatusCode, 200)
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Printf("Docker Images\n============\n %s\n", body)
}
Ejemplo n.º 7
0
func TestHappyRead(t *testing.T) {
	// register the modules
	modprobe.Install("mod_mock", NewModMock)

	input := []byte(`
	{"id": "getping",
     "method": "GET",
     "enabled": true,
     "pattern": "/ping/:hola",
     "chain": [
  	    {"ref": "mod_mock", 
  	     "params": {
  	     	"optiona1": "a1", 
  	     	"optiona2": "a2"}},
	    {"ref": "mod_mock", 
	     "params": {
	     	"optiona1": "b1"}
	     }
	     ]
	}`)
	p, err := newProxyFromJson(input)
	assert.Nil(t, err)
	assert.Equal(t, p.GetId(), "getping")
	assert.Equal(t, p.GetMethod(), "GET")
	assert.Equal(t, p.IsEnabled(), true)
	assert.Equal(t, p.GetPattern(), "/ping/:hola")
	assert.Equal(t, len(p.GetChain().GetModules()), 2)

	modType := reflect.TypeOf(p.GetChain().GetModules()[0])

	fmt.Printf("All: %+v\n", modType)
	fmt.Printf("Name: %s\n", modType.Name())
	fmt.Printf("Pkg: %s\n", modType.PkgPath())

	fmt.Printf("Kind: %s\n", modType.Kind())
}
Ejemplo n.º 8
0
func TestLoadConfigFile(t *testing.T) {
	hclBend := NewHclBackend("/Users/javi/dev/go/src/github.com/kapalhq/envoy/fixtures")
	apisFound := 0

	notifyC := make(chan proxy.ApiProxySpec, 10)
	stopC := make(chan bool, 1)
	hclBend.WatchProxyChanges(notifyC, stopC)

	go func() {
		for proxy := range notifyC {
			if proxy != nil {
				apisFound++
			}
		}
	}()

	time.Sleep(1)
	assert.Equal(t, apisFound, 2)
}
Ejemplo n.º 9
0
func TestHappyProxy(t *testing.T) {
	proxy := New("testProxy", "GET", "/ping", true, getMockChain())
	resp, err := proxy.RoundTrip(context.Empty())
	assert.Nil(t, err)
	assert.Equal(t, resp.StatusCode, 200)
}