Skip to content

Commit

Permalink
Add initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lubronzhan committed Aug 25, 2023
1 parent 350ce98 commit 9347369
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 77 deletions.
180 changes: 103 additions & 77 deletions pkg/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package session
import (
"bytes"
"context"
"fmt"
"io"
"testing"
"time"
Expand Down Expand Up @@ -48,39 +47,54 @@ func TestGetSession(t *testing.T) {
}
defer simr.Destroy()

params := NewParams().
WithServer(simr.ServerURL().Host).
WithUserInfo(simr.Username(), simr.Password()).WithDatacenter("*")

// Get first session
s, err := GetOrCreate(context.Background(), params)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(s).ToNot(BeNil())
assertSessionCountEqualTo(g, simr, 1)

// Get session key
sessionInfo, err := s.SessionManager.UserSession(context.Background())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(sessionInfo).ToNot(BeNil())
firstSession := sessionInfo.Key

// remove session expect no session
g.Expect(s.TagManager.Logout(context.Background())).To(Succeed())
g.Expect(simr.Run(fmt.Sprintf("session.rm %s", firstSession))).To(Succeed())
assertSessionCountEqualTo(g, simr, 0)

// request sesion again should be a new and different session
s, err = GetOrCreate(context.Background(), params)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(s).ToNot(BeNil())

// Get session info, session key should be different from
// last session
sessionInfo, err = s.SessionManager.UserSession(context.Background())
g.Expect(sessionInfo).ToNot(BeNil())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(sessionInfo.Key).ToNot(BeEquivalentTo(firstSession))
assertSessionCountEqualTo(g, simr, 1)
paramsArr := []*Params{
NewParams().
WithServer(simr.ServerURL().Host).
WithUserInfo(simr.Username(), simr.Password()).WithDatacenter("*"),
NewParams().
WithServer(simr.ServerURL().Host).
WithDatacenter("*").
WithUserKey(simr.UserKey()).
WithUserCertificate((simr.UserCert())),
}
for i := range paramsArr {
params := paramsArr[i]
// Get first session
s, err := GetOrCreate(context.Background(), params)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(s).ToNot(BeNil())
assertSessionCountEqualTo(g, simr, 1)

// Get session key
// sessionInfo, err := s.SessionManager.UserSession(context.Background())
// g.Expect(err).ToNot(HaveOccurred())
// g.Expect(sessionInfo).ToNot(BeNil())
// firstSession := sessionInfo.Key

// remove session expect no session
// g.Expect(s.TagManager.Logout(context.Background())).To(Succeed())
// g.Expect(simr.Run(fmt.Sprintf("session.rm %s", firstSession))).To(Succeed())
// assertSessionCountEqualTo(g, simr, 0)

// // request sesion again should be a new and different session
// s, err = GetOrCreate(context.Background(), params)
// g.Expect(err).ToNot(HaveOccurred())
// g.Expect(s).ToNot(BeNil())

// // Get session info, session key should be different from
// // last session
// sessionInfo, err = s.SessionManager.UserSession(context.Background())
// g.Expect(sessionInfo).ToNot(BeNil())
// g.Expect(err).ToNot(HaveOccurred())
// g.Expect(sessionInfo.Key).ToNot(BeEquivalentTo(firstSession))
// assertSessionCountEqualTo(g, simr, 1)

// // remove session expect no session
// g.Expect(s.TagManager.Logout(context.Background())).To(Succeed())
// g.Expect(simr.Run(fmt.Sprintf("session.rm %s", sessionInfo.Key))).To(Succeed())
// assertSessionCountEqualTo(g, simr, 0)

Check failure on line 96 in pkg/session/session_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}
}

func sessionCount(stdout io.Reader) (int, error) {
Expand Down Expand Up @@ -126,47 +140,59 @@ func TestGetSessionWithKeepAlive(t *testing.T) {
}
defer simr.Destroy()

params := NewParams().
WithServer(simr.ServerURL().Host).
WithUserInfo(simr.Username(), simr.Password()).
WithDatacenter("*")

// Get first Session
s, err := GetOrCreate(context.Background(), params)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(s).ToNot(BeNil())
assertSessionCountEqualTo(g, simr, 1)

// Get session key
sessionInfo, err := s.SessionManager.UserSession(context.Background())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(sessionInfo).ToNot(BeNil())
firstSession := sessionInfo.Key

// Get the session again
// as keep alive is enabled and session is
// not expired we must get the same cached session
s, err = GetOrCreate(context.Background(), params)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(s).ToNot(BeNil())
sessionInfo, err = s.SessionManager.UserSession(context.Background())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(sessionInfo).ToNot(BeNil())
g.Expect(sessionInfo.Key).To(BeEquivalentTo(firstSession))
assertSessionCountEqualTo(g, simr, 1)

// Try to remove vim session
g.Expect(s.Logout(context.Background())).To(Succeed())

// after logging out old session must be deleted,
// we must get a new different session
// total session count must remain 1
s, err = GetOrCreate(context.Background(), params)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(s).ToNot(BeNil())
sessionInfo, err = s.SessionManager.UserSession(context.Background())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(sessionInfo).ToNot(BeNil())
g.Expect(sessionInfo.Key).ToNot(BeEquivalentTo(firstSession))
assertSessionCountEqualTo(g, simr, 1)
paramsArr := []*Params{
NewParams().
WithServer(simr.ServerURL().Host).
WithUserInfo(simr.Username(), simr.Password()).WithDatacenter("*"),
NewParams().
WithServer(simr.ServerURL().Host).
WithDatacenter("*").
WithUserKey(simr.UserKey()).
WithUserCertificate((simr.UserCert())),
}
for i := range paramsArr {
params := paramsArr[i]

// Get first Session
s, err := GetOrCreate(context.Background(), params)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(s).ToNot(BeNil())
assertSessionCountEqualTo(g, simr, 1)

// Get session key
sessionInfo, err := s.SessionManager.UserSession(context.Background())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(sessionInfo).ToNot(BeNil())
firstSession := sessionInfo.Key

// Get the session again
// as keep alive is enabled and session is
// not expired we must get the same cached session
s, err = GetOrCreate(context.Background(), params)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(s).ToNot(BeNil())
sessionInfo, err = s.SessionManager.UserSession(context.Background())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(sessionInfo).ToNot(BeNil())
g.Expect(sessionInfo.Key).To(BeEquivalentTo(firstSession))
assertSessionCountEqualTo(g, simr, 1)

// Try to remove vim session
g.Expect(s.Logout(context.Background())).To(Succeed())

// after logging out old session must be deleted,
// we must get a new different session
// total session count must remain 1
s, err = GetOrCreate(context.Background(), params)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(s).ToNot(BeNil())
sessionInfo, err = s.SessionManager.UserSession(context.Background())
g.Expect(err).ToNot(HaveOccurred())
g.Expect(sessionInfo).ToNot(BeNil())
g.Expect(sessionInfo.Key).ToNot(BeEquivalentTo(firstSession))
assertSessionCountEqualTo(g, simr, 1)

// Try to remove vim session
g.Expect(s.Logout(context.Background())).To(Succeed())
}
}
34 changes: 34 additions & 0 deletions test/helpers/vcsim/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,37 @@ func (s Simulator) Password() string {
pwd, _ := s.server.URL.User.Password()
return pwd
}

func (s Simulator) UserKey() string {
return localhostKey
}

func (s Simulator) UserCert() string {
return localhostCert
}

// localhostCert was generated from crypto/tls/generate_cert.go with the following command:
//
// go run generate_cert.go --rsa-bits 512 --host 127.0.0.1,::1,example.com --ca --start-date "Jan 1 00:00:00 1970" --duration=1000000h
var localhostCert = `-----BEGIN CERTIFICATE-----
MIIBjzCCATmgAwIBAgIRAKpi2WmTcFrVjxrl5n5YDUEwDQYJKoZIhvcNAQELBQAw
EjEQMA4GA1UEChMHQWNtZSBDbzAgFw03MDAxMDEwMDAwMDBaGA8yMDg0MDEyOTE2
MDAwMFowEjEQMA4GA1UEChMHQWNtZSBDbzBcMA0GCSqGSIb3DQEBAQUAA0sAMEgC
QQC9fEbRszP3t14Gr4oahV7zFObBI4TfA5i7YnlMXeLinb7MnvT4bkfOJzE6zktn
59zP7UiHs3l4YOuqrjiwM413AgMBAAGjaDBmMA4GA1UdDwEB/wQEAwICpDATBgNV
HSUEDDAKBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MC4GA1UdEQQnMCWCC2V4
YW1wbGUuY29thwR/AAABhxAAAAAAAAAAAAAAAAAAAAABMA0GCSqGSIb3DQEBCwUA
A0EAUsVE6KMnza/ZbodLlyeMzdo7EM/5nb5ywyOxgIOCf0OOLHsPS9ueGLQX9HEG
//yjTXuhNcUugExIjM/AIwAZPQ==
-----END CERTIFICATE-----`

// localhostKey is the private key for localhostCert.
var localhostKey = `-----BEGIN RSA PRIVATE KEY-----
MIIBOwIBAAJBAL18RtGzM/e3XgavihqFXvMU5sEjhN8DmLtieUxd4uKdvsye9Phu
R84nMTrOS2fn3M/tSIezeXhg66quOLAzjXcCAwEAAQJBAKcRxH9wuglYLBdI/0OT
BLzfWPZCEw1vZmMR2FF1Fm8nkNOVDPleeVGTWoOEcYYlQbpTmkGSxJ6ya+hqRi6x
goECIQDx3+X49fwpL6B5qpJIJMyZBSCuMhH4B7JevhGGFENi3wIhAMiNJN5Q3UkL
IuSvv03kaPR5XVQ99/UeEetUgGvBcABpAiBJSBzVITIVCGkGc7d+RCf49KTCIklv
bGWObufAR8Ni4QIgWpILjW8dkGg8GOUZ0zaNA6Nvt6TIv2UWGJ4v5PoV98kCIQDx
rIiZs5QbKdycsv9gQJzwQAogC8o04X3Zz3dsoX+h4A==
-----END RSA PRIVATE KEY-----`

0 comments on commit 9347369

Please sign in to comment.