Пример #1
0
func testClone_lg(t *testing.T, repoPath string, opt *vcsclient.CloneInfo, wantCloneErrStr string, wantCloneErrHTTPStatus int) {
	storageDir, err := ioutil.TempDir("", "vcsstore-test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(storageDir)

	conf := &vcsstore.Config{
		StorageDir: storageDir,
		Log:        log.New(os.Stderr, "", 0),
		DebugLog:   log.New(os.Stderr, "", log.LstdFlags),
	}

	h := NewHandler(vcsstore.NewService(conf), NewGitTransporter(conf), nil)
	h.Log = log.New(os.Stderr, "", 0)
	h.Debug = true

	srv := httptest.NewServer(h)
	defer srv.Close()

	baseURL, err := url.Parse(srv.URL)
	if err != nil {
		t.Fatal(err)
	}
	c := vcsclient.New(baseURL, nil)
	repo, err := c.Repository(repoPath)
	if err != nil {
		t.Fatal(err)
	}

	if repo, ok := repo.(vcsclient.RepositoryCloneUpdater); ok {
		// Clones the first time.
		err := repo.CloneOrUpdate(opt)
		checkErr(t, err, wantCloneErrStr, wantCloneErrHTTPStatus)

		// Updates the second time.
		err = repo.CloneOrUpdate(opt)
		checkErr(t, err, wantCloneErrStr, wantCloneErrHTTPStatus)
	} else {
		t.Fatalf("Remote cloning is not implemented for %T.", repo)
	}
}
Пример #2
0
func serveCmd(args []string) {
	fs := flag.NewFlagSet("serve", flag.ExitOnError)
	debug := fs.Bool("d", false, "debug mode (don't use on publicly available servers)")
	bindAddr := fs.String("http", ":"+defaultPort, "HTTP listen address")
	tlsCert := fs.String("tls.cert", "", "TLS certificate file (if set, server uses TLS)")
	tlsKey := fs.String("tls.key", "", "TLS key file (if set, server uses TLS)")
	basicAuth := fs.String("http.basicauth", "", "if set to 'user:passwd', require HTTP Basic Auth")
	cache := fs.String("cache", "", "HTTP cache (either 'mem' or 'disk:/path/to/cache/dir')")
	fs.Usage = func() {
		fmt.Fprintln(os.Stderr, `usage: vcsstore serve [options]

Starts an HTTP server that serves information about VCS repositories.

The options are:
`)
		fs.PrintDefaults()
		os.Exit(1)
	}
	fs.Parse(args)

	if fs.NArg() != 0 {
		fs.Usage()
	}

	err := os.MkdirAll(*storageDir, 0700)
	if err != nil {
		log.Fatalf("Error creating directory %q: %s.", *storageDir, err)
	}

	var logw io.Writer
	if *verbose {
		logw = os.Stderr
	} else {
		logw = ioutil.Discard
	}

	conf := &vcsstore.Config{
		StorageDir: *storageDir,
		Log:        log.New(logw, "vcsstore: ", log.LstdFlags),
	}
	if *debug {
		conf.DebugLog = log.New(logw, "vcsstore DEBUG: ", log.LstdFlags)
	}

	vh := server.NewHandler(vcsstore.NewService(conf), server.NewGitTransporter(conf), nil)
	vh.Log = log.New(logw, "server: ", log.LstdFlags)
	vh.Debug = *debug

	var h http.Handler
	if *basicAuth != "" {
		parts := strings.SplitN(*basicAuth, ":", 2)
		if len(parts) != 2 {
			log.Fatalf("Basic auth must be specified as 'user:passwd'.")
		}
		user, passwd := parts[0], parts[1]
		if user == "" || passwd == "" {
			log.Fatalf("Basic auth user and passwd must both be nonempty.")
		}
		log.Printf("Requiring HTTP Basic auth")
		h = newBasicAuthHandler(user, passwd, vh)
	} else {
		h = vh
	}
	h = cacheHandler(*cache, h)
	http.Handle("/", handlers.CombinedLoggingHandler(os.Stderr, h))

	if *tlsCert != "" || *tlsKey != "" {
		fmt.Fprintf(os.Stderr, "Starting HTTPS server on %s (cert %s, key %s)\n", *bindAddr, *tlsCert, *tlsKey)
		log.Fatal(http.ListenAndServeTLS(*bindAddr, *tlsCert, *tlsKey, nil))
	} else {
		fmt.Fprintf(os.Stderr, "Starting HTTP server on %s\n", *bindAddr)
		log.Fatal(http.ListenAndServe(*bindAddr, nil))
	}
}
Пример #3
0
func TestCrossRepoDiff_git_git_lg(t *testing.T) {
	t.Parallel()

	storageDir, err := ioutil.TempDir("", "vcsstore-test")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(storageDir)

	conf := &vcsstore.Config{
		StorageDir: storageDir,
		Log:        log.New(os.Stderr, "", 0),
		DebugLog:   log.New(os.Stderr, "", log.LstdFlags),
	}

	h := NewHandler(vcsstore.NewService(conf), NewGitTransporter(conf), nil)
	h.Log = log.New(os.Stderr, "", 0)
	h.Debug = true

	srv := httptest.NewServer(h)
	defer srv.Close()

	baseURL, err := url.Parse(srv.URL)
	if err != nil {
		t.Fatal(err)
	}
	c := vcsclient.New(baseURL, nil)

	baseRepo := openAndCloneRepo(t, c, "github.com/sgtest/vcsstore-cross-repo-diff-test",
		&vcsclient.CloneInfo{VCS: "git", CloneURL: "https://github.com/sgtest/vcsstore-cross-repo-diff-test.git"})
	headRepo := openAndCloneRepo(t, c, "github.com/sqs/vcsstore-cross-repo-diff-test",
		&vcsclient.CloneInfo{VCS: "git", CloneURL: "https://github.com/sqs/vcsstore-cross-repo-diff-test.git"})

	const (
		baseCommit = "e7b2d6b444232fb1174fdd7561c25e94b0f62b60"
		headCommit = "843e1b8483f1542eeab08990b528608f5b318960"
	)
	want := &vcs.Diff{Raw: `diff --git f f
index 78981922613b2afb6025042ff6bd878ac1994e85..422c2b7ab3b3c668038da977e4e93a5fc623169c 100644
--- f
+++ f
@@ -1 +1,2 @@
 a
+b
`}

	// Run this a lot to ferret out concurrency issues.
	var n int
	envNumThreads := os.Getenv("TEST_CROSSREPO_DIFF_NUM_THREADS")
	if envNumThreads == "" {
		n = 5000
	} else {
		n, _ = strconv.Atoi(envNumThreads)
	}
	var wg sync.WaitGroup
	for i := 0; i < n; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			diff, err := baseRepo.(vcs.CrossRepoDiffer).CrossRepoDiff(baseCommit, headRepo, headCommit, nil)
			if err != nil {
				t.Fatal(err)
			}
			if !reflect.DeepEqual(diff, want) {
				t.Errorf("CrossRepoDiff: got %v, want %v", diff, want)
			}
		}()
	}
	wg.Wait()
}