// This is a series of *functional* tests against a p4d instance running // (launched via "grunt p4d"). package p4go import ( "bytes" "fmt" "io/ioutil" "os" "regexp" "strings" "testing" "text/template" "time" ) func TestInfo(t *testing.T) { SetupObjs(t, func(api ClientApi, user ClientUser, err Error) { api.Run("info", user) if !strings.Contains(user.GetOutputInfo(), "Server version:") { t.Errorf("Output Info does not contain 'Server version:' %s", user.GetOutputInfo()) t.Fail() } }) } func TestClients(t *testing.T) { SetupObjs(t, func(api ClientApi, user ClientUser, err Error) { api.Run("clients", user) if !strings.Contains(user.GetOutputInfo(), "commons_sclient_1") { t.Errorf("OutputInfo does not contain 'commons_sclient_1': %s", user.GetOutputInfo()) t.Fail() } }) } // Creates and reads a client spec func TestClient(t *testing.T) { SetupObjs(t, func(api ClientApi, user ClientUser, err Error) { clientId := fmt.Sprintf("new_client_%d", time.Now().Unix()) cwd, e := os.Getwd() if e != nil { t.Errorf("Getcwd fail %s", e.Error()) t.FailNow() } rootDir := fmt.Sprintf("%s/%s", cwd, "test_client") type MyClient struct { Client string Root string } myClient := MyClient{clientId, rootDir} template, e := template.ParseFiles("data/new_client.txt") if e != nil { t.Errorf("template.ParseFiles fail %s", e.Error()) t.FailNow() } clientBytes := new(bytes.Buffer) template.Execute(clientBytes, myClient) user.SetInputData(clientBytes.String()) api.SetArgv([]string{"-i"}) api.Run("client", user) if !strings.Contains(user.GetOutputInfo(), "saved") { t.Errorf("Missing output does not contain %s: %s", "saved", user.GetOutputInfo()) t.Fail() } user.Clear() api.SetArgv([]string{"-o", clientId}) api.Run("client", user) spec := user.GetOutputInfo() if !strings.Contains(spec, rootDir) { t.Errorf("Spec output does not contain %s: %s", rootDir, spec) t.Fail() } if !strings.Contains(spec, "//depot/new_client_area/...") { t.Errorf("Spec output does not contain %s: %s", "//depot/new_client_area/...", spec) t.Fail() } }) } // Executes sync and makes sure that a few known files are there func TestSync(t *testing.T) { InitClient(t, func(api ClientApi, user ClientUser, err Error) { api.SetArgv([]string{"-f"}) api.Run("sync", user) cwd, e := os.Getwd() if e != nil { t.Errorf("Getcwd fail %s", e.Error()) t.FailNow() } _, e = os.Open(fmt.Sprintf("%s/work/testing/foo/file1.txt", cwd)) if e != nil { t.Errorf("Did not locate file foo/file1.txt") t.Fail() } }) } func TestEdit(t *testing.T) { InitClient(t, func(api ClientApi, user ClientUser, err Error) { cwd, e := os.Getwd() if e != nil { t.Errorf("Getcwd fail %s", e.Error()) t.FailNow() } file1 := fmt.Sprintf("%s/work/testing/foo/file1.txt", cwd) api.SetArgv([]string{file1}) api.Run("edit", user) ioutil.WriteFile(file1, []byte("new content"), os.FileMode(0777)) user.Clear() api.SetArgv([]string{"-d", "test edit"}) api.Run("submit", user) }) } func TestAdd(t *testing.T) { InitClient(t, func(api ClientApi, user ClientUser, err Error) { cwd, e := os.Getwd() if e != nil { t.Errorf("Getcwd fail %s", e.Error()) t.FailNow() } newFile := fmt.Sprintf("%s/work/testing/foo/add.txt", cwd) ioutil.WriteFile(newFile, []byte("some test content\n"), os.FileMode(0755)) api.SetArgv([]string{newFile}) api.Run("add", user) user.Clear() api.SetArgv([]string{"-d", "test add"}) api.Run("submit", user) }) } func TestDelete(t *testing.T) { InitClient(t, func(api ClientApi, user ClientUser, err Error) { cwd, e := os.Getwd() if e != nil { t.Errorf("Getcwd fail %s", e.Error()) t.FailNow() } bbfile := fmt.Sprintf("%s/work/testing/foo/bbfile.txt", cwd) api.SetArgv([]string{bbfile}) api.Run("delete", user) user.Clear() api.SetArgv([]string{"-d", "test delete"}) api.Run("submit", user) }) } func TestInteg(t *testing.T) { InitClient(t, func(api ClientApi, user ClientUser, err Error) { cwd, e := os.Getwd() if e != nil { t.Errorf("Getcwd fail %s", e.Error()) t.FailNow() } fromFile1 := fmt.Sprintf("%s/work/testing/foo/file1.txt", cwd) toFile1 := fmt.Sprintf("%s/work/testing/foo/loc/file1.txt", cwd) api.SetArgv([]string{fromFile1, toFile1}) api.Run("integrate", user) user.Clear() api.SetArgv([]string{"-d", "test integ"}) api.Run("submit", user) }) } func TestResolveInteg(t *testing.T) { InitClient(t, func(api ClientApi, user ClientUser, err Error) { cwd, e := os.Getwd() if e != nil { t.Errorf("Getcwd fail %s", e.Error()) t.FailNow() } fromFile1 := fmt.Sprintf("%s/work/testing/foo/file1.txt", cwd) toFile1 := fmt.Sprintf("%s/work/testing/foo/loc/file1.txt", cwd) api.SetArgv([]string{fromFile1}) api.Run("edit", user) ioutil.WriteFile(fromFile1, []byte("new altered content\nline 2\n"), os.FileMode(0777)) user.Clear() api.SetArgv([]string{"-d", "edit before integ"}) api.Run("submit", user) user.Clear() api.SetArgv([]string{fromFile1, toFile1}) api.Run("integrate", user) user.Clear() api.SetArgv([]string{"-am"}) api.Run("resolve", user) user.Clear() api.SetArgv([]string{"-d", "test integ"}) api.Run("submit", user) }) } // Creates a connection to the p4d instance as the admin user, along with the // client user instance for testing. func SetupObjs(t *testing.T, exec func(api ClientApi, user ClientUser, err Error)) { var api = NewClientApi() defer api.Delete() var user = NewClientUser() defer user.Delete() var err = NewError() defer err.Delete() api.SetHost("localhost") api.SetPort("1666") api.SetUser("commonsadmin") api.SetPassword("commonsadmin") api.SetProg("mergeit") api.Init(err) if err.Test() != 0 { sb := NewStrBuf() err.Fmt(sb) fmt.Fprintf(os.Stderr, "info status: %d '%s'\n", err.Test(), sb.Text()) t.Fail() } exec(api, user, err) api.Final(err) if err.Test() != 0 { sb := NewStrBuf() err.Fmt(sb) fmt.Fprintf(os.Stderr, "final status: %d '%s'\n", err.Test(), sb.Text()) t.Fail() } } // Preps the client "testing" to a local folder (if it hasn't been already set up) // We will reuse this client setup in subsequent tests. func InitClient(t *testing.T, exec func(api ClientApi, user ClientUser, err Error)) { var api = NewClientApi() defer api.Delete() var user = NewClientUser() defer user.Delete() var err = NewError() defer err.Delete() api.SetHost("localhost") api.SetPort("1666") api.SetUser("commonsadmin") api.SetPassword("commonsadmin") api.SetProg("mergeit") api.SetClient("testing") api.Init(err) if err.Test() != 0 { sb := NewStrBuf() err.Fmt(sb) fmt.Fprintf(os.Stderr, "info status: %d '%s'\n", err.Test(), sb.Text()) t.Fail() } api.SetArgv([]string{"-o", "testing"}) api.Run("client", user) spec := user.GetOutputInfo() cwd, e := os.Getwd() if e != nil { t.Errorf("Getcwd fail %s", e.Error()) t.FailNow() } newRoot := fmt.Sprintf("Root: %s/%s\n", cwd, "work/testing") specRegex, e := regexp.Compile("Root:\\s+.*\\n") if e != nil { t.Errorf("regexp fail %s", e.Error()) t.FailNow() } spec2B := specRegex.ReplaceAll([]byte(spec), []byte(newRoot)) spec2Buf := bytes.NewBuffer(spec2B) user.Clear() api.SetArgv([]string{"-i"}) user.SetInputData(spec2Buf.String()) api.Run("client", user) user.Clear() exec(api, user, err) api.Final(err) if err.Test() != 0 { sb := NewStrBuf() err.Fmt(sb) fmt.Fprintf(os.Stderr, "final status: %d '%s'\n", err.Test(), sb.Text()) t.Fail() } }