Ejemplo n.º 1
0
func TestGetVersion(t *testing.T) {
	tmp, err := utils.TestDirectory("")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmp)
	eng, err := engine.New(tmp)
	if err != nil {
		t.Fatal(err)
	}
	var called bool
	eng.Register("version", func(job *engine.Job) engine.Status {
		called = true
		v := &engine.Env{}
		v.SetJson("Version", "42.1")
		v.Set("ApiVersion", "1.1.1.1.1")
		v.Set("GoVersion", "2.42")
		v.Set("Os", "Linux")
		v.Set("Arch", "x86_64")
		if _, err := v.WriteTo(job.Stdout); err != nil {
			return job.Error(err)
		}
		return engine.StatusOK
	})

	r := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "/version", nil)
	if err != nil {
		t.Fatal(err)
	}
	// FIXME getting the version should require an actual running Server
	if err := ServeRequest(eng, api.APIVERSION, r, req); err != nil {
		t.Fatal(err)
	}
	if !called {
		t.Fatalf("handler was not called")
	}
	out := engine.NewOutput()
	v, err := out.AddEnv()
	if err != nil {
		t.Fatal(err)
	}
	if _, err := io.Copy(out, r.Body); err != nil {
		t.Fatal(err)
	}
	out.Close()
	expected := "42.1"
	if result := v.Get("Version"); result != expected {
		t.Errorf("Expected version %s, %s found", expected, result)
	}
	expected = "application/json"
	if result := r.HeaderMap.Get("Content-Type"); result != expected {
		t.Errorf("Expected Content-Type %s, %s found", expected, result)
	}
}
Ejemplo n.º 2
0
func tmpEngine(t *testing.T) *engine.Engine {
	tmp, err := utils.TestDirectory("")
	if err != nil {
		t.Fatal(err)
	}
	eng, err := engine.New(tmp)
	if err != nil {
		t.Fatal(err)
	}
	return eng
}
Ejemplo n.º 3
0
func newTestEngine(t *testing.T) *Engine {
	tmp, err := utils.TestDirectory("")
	if err != nil {
		t.Fatal(err)
	}
	eng, err := New(tmp)
	if err != nil {
		t.Fatal(err)
	}
	return eng
}
Ejemplo n.º 4
0
func TestLookupImage(t *testing.T) {
	tmp, err := utils.TestDirectory("")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmp)
	store := mkTestTagStore(tmp, t)
	defer store.graph.driver.Cleanup()

	if img, err := store.LookupImage(testImageName); err != nil {
		t.Fatal(err)
	} else if img == nil {
		t.Errorf("Expected 1 image, none found")
	}
	if img, err := store.LookupImage(testImageName + ":" + DEFAULTTAG); err != nil {
		t.Fatal(err)
	} else if img == nil {
		t.Errorf("Expected 1 image, none found")
	}

	if img, err := store.LookupImage(testImageName + ":" + "fail"); err == nil {
		t.Errorf("Expected error, none found")
	} else if img != nil {
		t.Errorf("Expected 0 image, 1 found")
	}

	if img, err := store.LookupImage("fail:fail"); err == nil {
		t.Errorf("Expected error, none found")
	} else if img != nil {
		t.Errorf("Expected 0 image, 1 found")
	}

	if img, err := store.LookupImage(testImageID); err != nil {
		t.Fatal(err)
	} else if img == nil {
		t.Errorf("Expected 1 image, none found")
	}

	if img, err := store.LookupImage(testImageName + ":" + testImageID); err != nil {
		t.Fatal(err)
	} else if img == nil {
		t.Errorf("Expected 1 image, none found")
	}
}
Ejemplo n.º 5
0
func newTestDirectory(templateDir string) (dir string, err error) {
	return utils.TestDirectory(templateDir)
}
Ejemplo n.º 6
0
func TestGetInfo(t *testing.T) {
	tmp, err := utils.TestDirectory("")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmp)
	eng, err := engine.New(tmp)
	if err != nil {
		t.Fatal(err)
	}

	var called bool
	eng.Register("info", func(job *engine.Job) engine.Status {
		called = true
		v := &engine.Env{}
		v.SetInt("Containers", 1)
		v.SetInt("Images", 42000)
		if _, err := v.WriteTo(job.Stdout); err != nil {
			return job.Error(err)
		}
		return engine.StatusOK
	})

	r := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "/info", nil)
	if err != nil {
		t.Fatal(err)
	}
	// FIXME getting the version should require an actual running Server
	if err := ServeRequest(eng, api.APIVERSION, r, req); err != nil {
		t.Fatal(err)
	}
	if !called {
		t.Fatalf("handler was not called")
	}

	out := engine.NewOutput()
	i, err := out.AddEnv()
	if err != nil {
		t.Fatal(err)
	}
	if _, err := io.Copy(out, r.Body); err != nil {
		t.Fatal(err)
	}
	out.Close()
	{
		expected := 42000
		result := i.GetInt("Images")
		if expected != result {
			t.Fatalf("%#v\n", result)
		}
	}
	{
		expected := 1
		result := i.GetInt("Containers")
		if expected != result {
			t.Fatalf("%#v\n", result)
		}
	}
	{
		expected := "application/json"
		if result := r.HeaderMap.Get("Content-Type"); result != expected {
			t.Fatalf("%#v\n", result)
		}
	}
}