Esempio n. 1
0
func TestStatisticGet(t *testing.T) {
	fmt.Println("Testing Statistic API")
	assert := assert.New(t)

	apiTest := newHarborAPI()

	//prepare for test

	var myProCount, pubProCount, totalProCount int32
	result, err := apiTest.StatisticGet(*admin)
	if err != nil {
		t.Error("Error while get statistic information", err.Error())
		t.Log(err)
	} else {
		myProCount = result.MyProjectCount
		pubProCount = result.PublicProjectCount
		totalProCount = result.TotalProjectCount
	}
	//post project
	var project apilib.ProjectReq
	project.ProjectName = "statistic_project"
	project.Public = 1

	//case 2: admin successful login, expect project creation success.
	fmt.Println("case 2: admin successful login, expect project creation success.")
	reply, err := apiTest.ProjectsPost(*admin, project)
	if err != nil {
		t.Error("Error while creat project", err.Error())
		t.Log(err)
	} else {
		assert.Equal(reply, int(201), "Case 2: Project creation status should be 201")
	}

	//get and compare
	result, err = apiTest.StatisticGet(*admin)
	if err != nil {
		t.Error("Error while get statistic information", err.Error())
		t.Log(err)
	} else {
		assert.Equal(myProCount+1, result.MyProjectCount, "MyProjectCount should be equal")
		assert.Equal(int32(2), result.MyRepoCount, "MyRepoCount should be equal")
		assert.Equal(pubProCount+1, result.PublicProjectCount, "PublicProjectCount should be equal")
		assert.Equal(int32(2), result.PublicRepoCount, "PublicRepoCount should be equal")
		assert.Equal(totalProCount+1, result.TotalProjectCount, "TotalProCount should be equal")
		assert.Equal(int32(2), result.TotalRepoCount, "TotalRepoCount should be equal")

	}

	//get the project
	var projects []apilib.Project
	var addProjectID int32
	httpStatusCode, projects, err := apiTest.ProjectsGet(project.ProjectName, 1)
	if err != nil {
		t.Error("Error while search project by proName and isPublic", err.Error())
		t.Log(err)
	} else {
		assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
		addProjectID = projects[0].ProjectId
	}

	//delete the project
	projectID := strconv.Itoa(int(addProjectID))
	httpStatusCode, err = apiTest.ProjectsDelete(*admin, projectID)
	if err != nil {
		t.Error("Error while delete project", err.Error())
		t.Log(err)
	} else {
		assert.Equal(int(200), httpStatusCode, "Case 1: Project creation status should be 200")
		//t.Log(result)
	}

	fmt.Printf("\n")

}
Esempio n. 2
0
func TestLogGet(t *testing.T) {

	fmt.Println("Testing Log API")
	assert := assert.New(t)
	apiTest := newHarborAPI()

	//prepare for test

	var project apilib.ProjectReq
	project.ProjectName = "my_project"
	project.Public = 1
	now := fmt.Sprintf("%v", time.Now().Unix())
	statusCode, result, err := apiTest.LogGet(*admin, "0", now, "1000")
	if err != nil {
		t.Error("Error while get log information", err.Error())
		t.Log(err)
	} else {
		assert.Equal(int(200), statusCode, "Log get should return 200")

	}
	logNum := len(result)
	fmt.Println("result", result)
	//add the project first.
	fmt.Println("add the project first.")
	reply, err := apiTest.ProjectsPost(*admin, project)
	if err != nil {
		t.Error("Error while creat project", err.Error())
		t.Log(err)
	} else {
		assert.Equal(int(201), reply, "Case 2: Project creation status should be 201")
	}
	//case 1: right parameters, expect the right output
	now = fmt.Sprintf("%v", time.Now().Unix())
	statusCode, result, err = apiTest.LogGet(*admin, "0", now, "1000")
	if err != nil {
		t.Error("Error while get log information", err.Error())
		t.Log(err)
	} else {
		assert.Equal(logNum+1, len(result), "lines of logs should be equal")
		num, index := getLog(result)
		if num != 1 {
			assert.Equal(1, num, "add my_project log number should be 1")
		} else {
			assert.Equal("my_project/", result[index].RepoName, "RepoName should be equal")
			assert.Equal("N/A", result[index].RepoTag, "RepoTag should be equal")
			assert.Equal("create", result[index].Operation, "Operation should be equal")
		}
	}
	fmt.Println("log ", result)
	//case 2: wrong format of start_time parameter, expect the wrong output
	statusCode, result, err = apiTest.LogGet(*admin, "ss", now, "3")
	if err != nil {
		t.Error("Error occured while get log information since the format of start_time parameter is not right.", err.Error())
		t.Log(err)
	} else {
		assert.Equal(int(400), statusCode, "Http status code should be 400")
	}

	//case 3: wrong format of end_time parameter, expect the wrong output
	statusCode, result, err = apiTest.LogGet(*admin, "0", "cc", "3")
	if err != nil {
		t.Error("Error occured while get log information since the format of end_time parameter is not right.", err.Error())
		t.Log(err)
	} else {
		assert.Equal(int(400), statusCode, "Http status code should be 400")
	}

	//case 4: wrong format of lines parameter, expect the wrong output
	statusCode, result, err = apiTest.LogGet(*admin, "0", now, "s")
	if err != nil {
		t.Error("Error occured while get log information since the format of lines parameter is not right.", err.Error())
		t.Log(err)
	} else {
		assert.Equal(int(400), statusCode, "Http status code should be 400")
	}

	//case 5: wrong format of lines parameter, expect the wrong output
	statusCode, result, err = apiTest.LogGet(*admin, "0", now, "-5")
	if err != nil {
		t.Error("Error occured while get log information since the format of lines parameter is not right.", err.Error())
		t.Log(err)
	} else {
		assert.Equal(int(400), statusCode, "Http status code should be 400")
	}

	//case 6: all parameters are null, expect the right output
	statusCode, result, err = apiTest.LogGet(*admin, "", "", "")
	if err != nil {
		t.Error("Error while get log information", err.Error())
		t.Log(err)
	} else {
		//default get 10 logs
		if logNum+1 >= 10 {
			logNum = 10
		} else {
			logNum++
		}
		assert.Equal(logNum, len(result), "lines of logs should be equal")
		num, index := getLog(result)
		if num != 1 {
			assert.Equal(1, num, "add my_project log number should be 1")
		} else {
			assert.Equal("my_project/", result[index].RepoName, "RepoName should be equal")
			assert.Equal("N/A", result[index].RepoTag, "RepoTag should be equal")
			assert.Equal("create", result[index].Operation, "Operation should be equal")
		}
	}

	//get the project
	var projects []apilib.Project
	var addProjectID int32
	httpStatusCode, projects, err := apiTest.ProjectsGet(project.ProjectName, 1)
	if err != nil {
		t.Error("Error while search project by proName and isPublic", err.Error())
		t.Log(err)
	} else {
		assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
		addProjectID = projects[0].ProjectId
	}

	//delete the project
	projectID := strconv.Itoa(int(addProjectID))
	httpStatusCode, err = apiTest.ProjectsDelete(*admin, projectID)
	if err != nil {
		t.Error("Error while delete project", err.Error())
		t.Log(err)
	} else {
		assert.Equal(int(200), httpStatusCode, "Case 1: Project creation status should be 200")
		//t.Log(result)
	}

	fmt.Printf("\n")
}