Пример #1
0
func TestHooksHandlerNoRepoManager(t *testing.T) {
	dir, err := ziptools.Unzip("test-data/buildscripts-repo.zip")
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		os.RemoveAll(dir)
	}()

	req, err := http.NewRequest("POST", "http://example.com/hooks/xxx", nil)
	if err != nil {
		os.RemoveAll(dir)
		log.Fatal(err)
	}

	w := httptest.NewRecorder()

	mockDecap := MockBuilder{}
	HooksHandler("file://"+dir, "master", &mockDecap)(w, req, []httprouter.Param{
		httprouter.Param{Key: "repomanager", Value: "nosuchmanager"},
	},
	)

	if w.Code != 400 {
		os.RemoveAll(dir)
		t.Fatalf("Want 400 but got %d\n", w.Code)
	}
}
Пример #2
0
func TestHooksHandlerBuildScripts(t *testing.T) {
	dir, err := ziptools.Unzip("test-data/buildscripts-repo.zip")
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		os.RemoveAll(dir)
	}()

	req, err := http.NewRequest("POST", "http://example.com/hooks/xxx", bytes.NewBufferString(""))
	if err != nil {
		os.RemoveAll(dir)
		log.Fatal(err)
	}

	w := httptest.NewRecorder()

	projectSetChan = make(chan map[string]v1.Project, 1)

	mockDecap := MockBuilder{}
	HooksHandler("file://"+dir, "master", &mockDecap)(w, req, []httprouter.Param{
		httprouter.Param{Key: "repomanager", Value: "buildscripts"},
	},
	)

	if w.Code != 200 {
		os.RemoveAll(dir)
		t.Fatalf("Want 200 but got %d\n", w.Code)
	}
}
Пример #3
0
func TestProject(t *testing.T) {
	dir, err := ziptools.Unzip("test-data/buildscripts-repo.zip")
	if err != nil {
		t.Fatal(err)
	}

	projects, err := assembleProjects("file://"+dir, "master")
	os.RemoveAll(dir)

	if err != nil {
		t.Fatal(err)
	}

	projectGetChan = make(chan map[string]v1.Project, 1)
	projectGetChan <- projects
	if _, present := projectByTeamName("ae6rt", "dynamodb-lab"); !present {
		t.Fatalf("Expecting to find ae6rt/dynamodb-lab project but did not\n")
	}

	projectGetChan = make(chan map[string]v1.Project, 1)
	projectGetChan <- projects
	if _, present := projectByTeamName("nope", "nope"); present {
		t.Fatalf("Not expecting to find nope/nope project but did \n")
	}
}
Пример #4
0
func TestAssembleProjects(t *testing.T) {
	dir, err := ziptools.Unzip("test-data/buildscripts-repo.zip")
	if err != nil {
		t.Fatal(err)
	}
	proj, err := assembleProjects("file://"+dir, "master")
	os.RemoveAll(dir)

	if err != nil {
		t.Fatal(err)
	}

	if len(proj) != 1 {
		t.Fatalf("Want 1 but got %d\n", len(proj))
	}

	foundIt := false
	for _, v := range proj {
		if v.Team == "ae6rt" && v.ProjectName == "dynamodb-lab" {
			foundIt = true
			if v.Descriptor.RepoManager != "github" {
				t.Fatalf("Want github but got %s\n", v.Descriptor.RepoManager)
			}
			if v.Descriptor.RepoURL != "https://github.com/ae6rt/dynamodb-lab.git" {
				t.Fatalf("Want https://github.com/ae6rt/dynamodb-lab.git but got %s\n", v.Descriptor.RepoManager)
			}
			break
		}
	}
	if !foundIt {
		t.Fatalf("Want a project ae6rt/dynamodb-lab but did not find one\n")
	}
}
Пример #5
0
func TestHooksHandlerGithub(t *testing.T) {
	dir, err := ziptools.Unzip("test-data/buildscripts-repo.zip")
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		os.RemoveAll(dir)
	}()

	req, err := http.NewRequest("POST}", "http://example.com/hooks/xxx", bytes.NewBufferString(`
{
  "ref": "refs/heads/master",
  "repository": {
    "id": 35129377,
    "name": "dynamodb-lab",
    "full_name": "ae6rt/dynamodb-lab",
    "owner": {
      "name": "ae6rt",
      "email": "*****@*****.**"
    }
  }
}
`,
	))
	req.Header.Set("X-Github-Event", "push")

	if err != nil {
		os.RemoveAll(dir)
		log.Fatal(err)
	}

	w := httptest.NewRecorder()

	mockDecap := MockBuilder{}
	HooksHandler("file://"+dir, "master", &mockDecap)(w, req, []httprouter.Param{
		httprouter.Param{Key: "repomanager", Value: "github"},
	},
	)

	if w.Code != 200 {
		os.RemoveAll(dir)
		t.Fatalf("Want 200 but got %d\n", w.Code)
	}

	// Wait for goroutine to run. Yuck.
	time.Sleep(1000 * time.Millisecond)

	if mockDecap.event.Team() != "ae6rt" {
		t.Fatalf("Want ae6rt but got %s\n", mockDecap.event.Team())
	}
	if mockDecap.event.Project() != "dynamodb-lab" {
		t.Fatalf("Want dynamodb-lab but got %s\n", mockDecap.event.Project())
	}
}
Пример #6
0
func TestHooksHandlerGithubNoEventTypeHeader(t *testing.T) {
	dir, err := ziptools.Unzip("test-data/buildscripts-repo.zip")
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		os.RemoveAll(dir)
	}()

	req, err := http.NewRequest("POST}", "http://example.com/hooks/xxx", bytes.NewBufferString(`
{
  "ref": "refs/heads/master",
  "repository": {
    "id": 35129377,
    "name": "dynamodb-lab",
    "full_name": "ae6rt/dynamodb-lab",
    "owner": {
      "name": "ae6rt",
      "email": "*****@*****.**"
    }
  }
}
`,
	))

	if err != nil {
		os.RemoveAll(dir)
		log.Fatal(err)
	}

	w := httptest.NewRecorder()

	mockDecap := MockBuilder{}
	HooksHandler("file://"+dir, "master", &mockDecap)(w, req, []httprouter.Param{
		httprouter.Param{Key: "repomanager", Value: "github"},
	},
	)

	if w.Code != 400 {
		os.RemoveAll(dir)
		t.Fatalf("Want 400 but got %d\n", w.Code)
	}
}
Пример #7
0
func TestFindProjectDescriptorsByRegex(t *testing.T) {
	dir, err := ziptools.Unzip("test-data/buildscripts-repo.zip")
	if err != nil {
		t.Fatal(err)
	}

	files, err := filesByRegex(dir, projectDescriptorRegex)
	if err != nil {
		os.RemoveAll(dir)
		t.Fatal(err)
	}
	if len(files) != 1 {
		os.RemoveAll(dir)
		t.Fatalf("Want 1 but got %d\n", len(files))
	}
	for _, v := range files {
		if _, err := ioutil.ReadFile(v); err != nil {
			t.Fatal(err)
		}
	}
	os.RemoveAll(dir)
}
Пример #8
0
func TestReadSidecars(t *testing.T) {
	dir, err := ziptools.Unzip("test-data/buildscripts-repo.zip")
	if err != nil {
		t.Fatal(err)
	}

	files, err := filesByRegex(dir, sideCarRegex)
	if err != nil {
		os.RemoveAll(dir)
		t.Fatal(err)
	}
	if len(files) != 3 {
		os.RemoveAll(dir)
		t.Fatalf("Want 3 but got %d\n", len(files))
	}

	arr := readSidecars(files)
	if len(arr) != 3 {
		os.RemoveAll(dir)
		t.Fatalf("Want 3 but got %d\n", len(files))
	}

	os.RemoveAll(dir)
}