Exemplo n.º 1
0
func TestRunningK8sDataDaemonsets(t *testing.T) {
	c := getK8sClientForDaemonSets(t)
	deisK8sResources := NewResourceInterfaceNamespaced(c, namespace)
	runningK8sData := NewRunningK8sData(deisK8sResources)
	daemonsets, err := runningK8sData.DaemonSets()
	assert.NoErr(t, err)
	assert.True(t, len(daemonsets) == 2, "daemonsets response slice was not the expected length")
	assert.True(t, daemonsets[0].Data != daemonsets[1].Data, "daemonsets should not be identical")
}
Exemplo n.º 2
0
func TestRunningK8sDataNodes(t *testing.T) {
	c := getK8sClientForNodes(t)
	deisK8sResources := NewResourceInterfaceNamespaced(c, namespace)
	runningK8sData := NewRunningK8sData(deisK8sResources)
	nodes, err := runningK8sData.Nodes()
	assert.NoErr(t, err)
	assert.True(t, len(nodes) == 3, "nodes response slice was not the expected length")
	assert.True(t, nodes[0].Data != nodes[1].Data, "nodes should not be identical")
}
Exemplo n.º 3
0
func TestRunningK8sDataPods(t *testing.T) {
	c := getK8sClientForPods(t)
	deisK8sResources := NewResourceInterfaceNamespaced(c, namespace)
	runningK8sData := NewRunningK8sData(deisK8sResources)
	pods, err := runningK8sData.Pods()
	assert.NoErr(t, err)
	assert.True(t, len(pods) == 2, "pods response slice was not the expected length")
	assert.True(t, pods[0].Data != pods[1].Data, "pods should not be identical")
}
Exemplo n.º 4
0
func TestRunningK8sDataReplicationControllers(t *testing.T) {
	c := getK8sClientForReplicationControllers(t)
	deisK8sResources := NewResourceInterfaceNamespaced(c, namespace)
	runningK8sData := NewRunningK8sData(deisK8sResources)
	rcs, err := runningK8sData.ReplicationControllers()
	assert.NoErr(t, err)
	assert.True(t, len(rcs) == 2, "rc response slice was not the expected length")
	assert.True(t, rcs[0].Data != rcs[1].Data, "rcs should not be identical")
}
Exemplo n.º 5
0
func TestRunningK8sDataServices(t *testing.T) {
	c := getK8sClientForServices(t)
	//_, _ = c.Setup(t).Services(namespace).List(api.ListOptions{})
	deisK8sResources := NewResourceInterfaceNamespaced(c, namespace)
	runningK8sData := NewRunningK8sData(deisK8sResources)
	services, err := runningK8sData.Services()
	assert.NoErr(t, err)
	assert.True(t, len(services) == 2, "services response slice was not the expected length")
	assert.True(t, services[0].Data != services[1].Data, "services should not be identical")
}
Exemplo n.º 6
0
func TestDoPeriodic(t *testing.T) {
	interval := time.Duration(3000) * time.Millisecond
	p := &testPeriodic{t: t, err: nil, freq: interval}
	closeCh1 := DoPeriodic([]Periodic{p})
	time.Sleep(interval / 2) // wait a little while for the goroutine to call the job once
	assert.True(t, p.i == 1, "the periodic wasn't called once")
	time.Sleep(interval)
	assert.True(t, p.i == 2, "the periodic wasn't called twice")
	time.Sleep(interval)
	assert.True(t, p.i == 3, "the periodic wasn't called thrice")
	close(closeCh1)
}
Exemplo n.º 7
0
func TestWaitForObjectMissing(t *testing.T) {
	statter := &FakeObjectStatter{
		Fn: func(context.Context, string) (storagedriver.FileInfo, error) {
			return storagedriver.FileInfoInternal{FileInfoFields: storagedriver.FileInfoFields{}}, storagedriver.PathNotFoundError{Path: objPath}
		},
	}
	err := WaitForObject(statter, objPath, 1*time.Millisecond, 2*time.Millisecond)
	assert.True(t, err != nil, "no error received when there should have been")
	// it should make 1 call immediately, then calls at 1ms and 2ms
	assert.True(
		t,
		len(statter.Calls) >= 1,
		"the statter was not called, but should have been called at least once",
	)
}
Exemplo n.º 8
0
func TestParse(t *testing.T) {
	//TODO: use gogenerate to generate valid html templates
	//http://godoc.org/github.com/arschles/gogenerate

	tmpl := `
    <html>
      <head>
        <title>hello {{.name}}</title>
      </head>
      <body>
        {{.greeting}}
      </body>
    </html>
  `
	fileName := "mytmpl.tmpl"
	tmplBytes := []byte(tmpl)
	expectedErr := errors.New("template not found")
	assetFunc := createValidAssetFunc(fileName, tmplBytes, expectedErr)

	tmpl1, err1 := New("test", assetFunc).Parse(fileName)
	assert.NoErr(t, err1)
	assert.False(t, tmpl1 == nil, "tmpl1 was nil when it should not have been")
	tmpl2, err2 := New("test1", assetFunc).Parse(fileName + fileName)
	assert.Err(t, err2, expectedErr)
	assert.True(t, tmpl2 == nil, "tmpl2 was not nil when it should have been")

	//TODO: check actual template output
	name := "Aaron"
	tmplData := map[string]string{
		"name": name,
	}
	buf1 := bytes.NewBuffer([]byte{})
	executeErr1 := tmpl1.Execute(buf1, tmplData)
	stdTmpl, stdTmplParseErr := template.New("referenceTest").Parse(tmpl)
	assert.NoErr(t, stdTmplParseErr)
	buf2 := bytes.NewBuffer([]byte{})
	executeErr2 := stdTmpl.Execute(buf2, tmplData)

	assert.NoErr(t, executeErr1)
	assert.NoErr(t, executeErr2)

	bytes1 := buf1.Bytes()
	bytes2 := buf2.Bytes()

	assert.True(t,
		string(bytes1) == string(bytes2),
		"actual template output %s is not equal expected %s", string(bytes1), string(bytes2))
}
Exemplo n.º 9
0
func TestCollectEnv(t *testing.T) {
	envMap := collectEnv()
	pwd, ok := envMap["PWD"]
	assert.True(t, ok, "'PWD' not found in the env")
	wd, err := os.Getwd()
	assert.NoErr(t, err)
	assert.Equal(t, pwd, wd, "working dir")
}
Exemplo n.º 10
0
func TestGitPktLine(t *testing.T) {
	b := new(bytes.Buffer)
	str := "hello world"
	err := gitPktLine(b, str)
	assert.NoErr(t, err)
	outStr := string(b.Bytes())
	assert.True(t, len(outStr) > 4, "output string <= 4 chars")
	assert.Equal(t, outStr[:4], fmt.Sprintf("%04x", len(str)+4), "hex prefix")
	assert.Equal(t, outStr[4:], str, "remainder of string")
}
Exemplo n.º 11
0
func TestGetProcFileFromServerFailure(t *testing.T) {
	expectedErr := errors.New("test error")
	getter := &storage.FakeObjectGetter{
		Fn: func(context.Context, string) ([]byte, error) {
			return []byte("web: example-go"), expectedErr
		},
	}

	_, err := getProcFile(getter, "", objKey, buildTypeProcfile)
	assert.Err(t, err, fmt.Errorf("error in reading %s (%s)", objKey, expectedErr))
	assert.True(t, err != nil, "no error received when there should have been")
}
Exemplo n.º 12
0
func TestSuccessfulRoundTrip(t *testing.T) {
	mut := new(sync.Mutex)
	lockID := NewLockID()
	router := mux.NewRouter()
	registerLockHandler(router, mut, lockID)
	registerUnlockHandler(router, mut, lockID)

	w := httptest.NewRecorder()
	r, err := http.NewRequest("POST", "/lock", bytes.NewReader(nil))
	assert.NoErr(t, err)
	router.ServeHTTP(w, r)
	assert.Equal(t, w.Code, http.StatusOK, "response code")
	idStr := string(w.Body.Bytes())
	assert.True(t, len(idStr) > 0, "returned lock ID was empty")
	assert.True(t, lockID.Equals(idStr), fmt.Sprintf("internal lock ID (%s) was not equal returned lock ID (%s)", lockID.id, idStr))

	w = httptest.NewRecorder()
	r, err = http.NewRequest("DELETE", "/lock/"+idStr, bytes.NewReader(nil))
	assert.NoErr(t, err)
	router.ServeHTTP(w, r)
	assert.Equal(t, w.Code, http.StatusOK, "response code")
}
Exemplo n.º 13
0
func TestObjectExistsSuccess(t *testing.T) {
	objInfo := storagedriver.FileInfoInternal{storagedriver.FileInfoFields{Path: objPath, Size: 1234}}
	statter := &FakeObjectStatter{
		Fn: func(context.Context, string) (storagedriver.FileInfo, error) {
			return objInfo, nil
		},
	}
	exists, err := ObjectExists(statter, objPath)
	assert.NoErr(t, err)
	assert.True(t, exists, "object not found when it should be present")
	assert.Equal(t, len(statter.Calls), 1, "number of StatObject calls")
	assert.Equal(t, statter.Calls[0].Path, objPath, "object key")
}
Exemplo n.º 14
0
// TestConcurrentPushSameRepo tests many concurrent pushes, each to the same repo
func TestConcurrentPushSameRepo(t *testing.T) {
	const testingServerAddr = "127.0.0.1:2245"
	key, err := sshTestingHostKey()
	assert.NoErr(t, err)

	cfg, err := serverConfigure()
	assert.NoErr(t, err)
	cfg.AddHostKey(key)

	c := NewCircuit()
	pushLock := NewInMemoryRepositoryLock(500 * time.Millisecond)
	runServer(cfg, c, pushLock, testingServerAddr, 2*time.Second, t)

	// Give server time to initialize.
	time.Sleep(200 * time.Millisecond)

	assert.Equal(t, c.State(), ClosedState, "circuit state")

	// Connect to the server and issue env var set. This should return true.
	client, err := ssh.Dial("tcp", testingServerAddr, clientConfig())
	assert.NoErr(t, err)

	const numPushers = 4
	outCh := make(chan *sshSessionOutput, numPushers)
	for i := 0; i < numPushers; i++ {
		go func() {
			sess, newSessErr := client.NewSession()
			assert.NoErr(t, newSessErr)
			defer sess.Close()
			out, outErr := sess.Output("git-upload-pack /demo.git")
			outCh <- &sshSessionOutput{outStr: string(out), err: outErr}
		}()
	}

	// ensure at least 1 output was successful
	foundOK := false
	to := 1 * time.Second
	for i := 0; i < numPushers; i++ {
		select {
		case sessOut := <-outCh:
			if sessOut.outStr == "OK" {
				foundOK = true
			}
		case <-time.After(to):
			t.Fatalf("didn't receive an output within %s", to)
		}
	}
	assert.True(t, foundOK, "no SSH requests were successful")
}
Exemplo n.º 15
0
func TestMultipleSameRepoLocks(t *testing.T) {
	var wg sync.WaitGroup
	const repo = "repo1"
	const numTries = 100
	lck := NewInMemoryRepositoryLock()
	assert.NoErr(t, lck.Lock(repo, 0*time.Second))
	for i := 0; i < numTries; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			assert.True(t, lck.Lock(repo, 0*time.Second) != nil, "lock of already locked repo should return error")
		}()
	}
	assert.NoErr(t, waitWithTimeout(&wg, 1*time.Second))
	assert.NoErr(t, lck.Unlock(repo, 0*time.Second))
	for i := 0; i < numTries; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			assert.True(t, lck.Unlock(repo, 0*time.Second) != nil, "unlock of already unlocked repo should return error")
		}()
	}
	assert.NoErr(t, waitWithTimeout(&wg, 1*time.Second))
}
Exemplo n.º 16
0
func TestGetFiles(t *testing.T) {
	dir, err := tests.DataDir()
	assert.NoErr(t, err)
	files, err := getFiles(dir)
	assert.NoErr(t, err)
	expected := tests.ExpectedDataSlice()
	set := map[string]struct{}{}
	for _, file := range files {
		set[file] = struct{}{}
	}
	for _, ex := range expected {
		_, ok := set[ex]
		assert.True(t, ok, "file %s was not found", ex)
	}
}
Exemplo n.º 17
0
func TestGetProcFileFromRepoFailure(t *testing.T) {
	tmpDir, err := ioutil.TempDir("", "tmpdir")
	if err != nil {
		t.Fatalf("error creating temp directory (%s)", err)
	}
	data := []byte("web= example-go")
	if err := ioutil.WriteFile(tmpDir+"/Procfile", data, 0644); err != nil {
		t.Fatalf("error creating %s/Procfile (%s)", tmpDir, err)
	}
	defer func() {
		if err := os.RemoveAll(tmpDir); err != nil {
			t.Fatalf("failed to remove Procfile from %s (%s)", tmpDir, err)
		}
	}()
	getter := &storage.FakeObjectGetter{}
	_, err = getProcFile(getter, tmpDir, objKey, buildTypeProcfile)

	assert.True(t, err != nil, "no error received when there should have been")
}
Exemplo n.º 18
0
func TestBooleans(t *testing.T) {
	assert.True(t, true, "boolean true")
	assert.False(t, false, "boolean false")
}
Exemplo n.º 19
0
func TestGetBuilderKeyError(t *testing.T) {
	_, err := GetBuilderKey()
	assert.True(t, err != nil, "no error received when there should have been")
}
Exemplo n.º 20
0
func TestNewWithInvalidBuilderKeyPath(t *testing.T) {
	host := "127.0.0.1"
	port := "80"
	_, err := New(host, port)
	assert.True(t, err != nil, "no error received when there should have been")
}
Exemplo n.º 21
0
func TestDirHasGitSuffix(t *testing.T) {
	assert.True(t, dirHasGitSuffix("a.git"), "'a.git' reported no git suffix")
	assert.False(t, dirHasGitSuffix("abc"), "'a' reported git suffix")
}