github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/server/camlistored/run_test.go (about)

     1  /*
     2  Copyright 2013 The Camlistore Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"runtime"
    24  	"strings"
    25  	"testing"
    26  	"time"
    27  
    28  	"camlistore.org/pkg/osutil"
    29  )
    30  
    31  func TestStarts(t *testing.T) {
    32  	td, err := ioutil.TempDir("", "camlistored-test")
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	defer os.RemoveAll(td)
    37  
    38  	fakeHome := filepath.Join(td, "fakeHome")
    39  	confDir := filepath.Join(fakeHome, "conf")
    40  
    41  	defer pushEnv("HOME", fakeHome)()
    42  	defer pushEnv("HOMEPATH", fakeHome)()
    43  	defer pushEnv("APPDATA", filepath.Join(fakeHome, "appdata"))()
    44  	defer pushEnv("CAMLI_CONFIG_DIR", confDir)()
    45  
    46  	if _, err := os.Stat(osutil.CamliConfigDir()); !os.IsNotExist(err) {
    47  		t.Fatalf("expected conf dir %q to not exist", osutil.CamliConfigDir())
    48  	}
    49  	if !strings.Contains(osutil.CamliBlobRoot(), td) {
    50  		t.Fatalf("blob root %q should contain the temp dir %q", osutil.CamliBlobRoot(), td)
    51  	}
    52  	if _, err := os.Stat(osutil.CamliBlobRoot()); !os.IsNotExist(err) {
    53  		t.Fatalf("expected blobroot dir %q to not exist", osutil.CamliBlobRoot())
    54  	}
    55  	if fi, err := os.Stat(osutil.UserServerConfigPath()); !os.IsNotExist(err) {
    56  		t.Errorf("expected no server config file; got %v, %v", fi, err)
    57  	}
    58  
    59  	mkdir(t, confDir)
    60  	*flagOpenBrowser = false
    61  	defaultListenAddr = ":0"
    62  
    63  	up := make(chan struct{})
    64  	down := make(chan struct{})
    65  	dead := make(chan int, 1)
    66  	osExit = func(status int) {
    67  		dead <- status
    68  		close(dead)
    69  		runtime.Goexit()
    70  	}
    71  	go Main(up, down)
    72  	select {
    73  	case status := <-dead:
    74  		t.Errorf("os.Exit(%d) before server came up", status)
    75  		return
    76  	case <-up:
    77  		t.Logf("server is up")
    78  	case <-time.After(10 * time.Second):
    79  		t.Fatal("timeout starting server")
    80  	}
    81  
    82  	if _, err := os.Stat(osutil.UserServerConfigPath()); err != nil {
    83  		t.Errorf("expected a server config file; got %v", err)
    84  	}
    85  
    86  	down <- struct{}{}
    87  	<-dead
    88  }
    89  
    90  func pushEnv(k, v string) func() {
    91  	old := os.Getenv(k)
    92  	os.Setenv(k, v)
    93  	return func() {
    94  		os.Setenv(k, old)
    95  	}
    96  }
    97  
    98  func mkdir(t *testing.T, dir string) {
    99  	if err := os.MkdirAll(dir, 0700); err != nil {
   100  		t.Fatal(err)
   101  	}
   102  }