Skip to content

Commit

Permalink
Add extended test for MongoDB.
Browse files Browse the repository at this point in the history
Also refactored mysql/postgresql a bit by extracting structs and similar
pieces of code.
  • Loading branch information
php-coder committed Jan 26, 2016
1 parent 2245e96 commit c431122
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 221 deletions.
71 changes: 71 additions & 0 deletions test/extended/images/mongodb_ephemeral.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package images

import (
"fmt"

g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"

exutil "github.com/openshift/origin/test/extended/util"
"github.com/openshift/origin/test/extended/util/db"
"time"
)

var _ = g.Describe("images: mongodb: ephemeral template", func() {
defer g.GinkgoRecover()

templatePath := exutil.FixturePath("..", "..", "examples", "db-templates", "mongodb-ephemeral-template.json")
oc := exutil.NewCLI("mongodb-create", exutil.KubeConfigPath()).Verbose()

g.Describe("creating from a template", func() {
g.It(fmt.Sprintf("should process and create the %q template", templatePath), func() {

g.By("creating a new app")
o.Expect(oc.Run("new-app").Args("-f", templatePath).Execute()).Should(o.Succeed())

g.By("expecting the mongodb service get endpoints")
o.Expect(oc.KubeFramework().WaitForAnEndpoint("mongodb")).Should(o.Succeed())

g.By("expecting the mongodb pod is running")
podNames, err := exutil.WaitForPods(
oc.KubeREST().Pods(oc.Namespace()),
exutil.ParseLabelsOrDie("name=mongodb"),
exutil.CheckPodIsRunningFn,
1,
1*time.Minute,
)
o.Expect(err).ShouldNot(o.HaveOccurred())
o.Expect(podNames).Should(o.HaveLen(1))

g.By("expecting the mongodb service is answering for ping")
mongo := db.NewMongoDB(podNames[0], "")

for times := 0; times < 10; times++ {
ok, err := mongo.IsReady(oc)
if ok {
break
}

if times == 10 {
o.Expect(err).ShouldNot(o.HaveOccurred())
o.Expect(ok).Should(o.BeTrue())
break
}

time.Sleep(1 * time.Second)
}

g.By("expecting that we can insert a new record")
result, err := mongo.Query(oc, `db.foo.save({ "status": "passed" })`)
o.Expect(err).ShouldNot(o.HaveOccurred())
o.Expect(result).Should(o.ContainSubstring(`WriteResult({ "nInserted" : 1 })`))

g.By("expecting that we can read a record")
findCmd := "printjson(db.foo.find({}, {_id: 0}).toArray())" // don't include _id field to output because it changes every time
result, err = mongo.Query(oc, findCmd)
o.Expect(err).ShouldNot(o.HaveOccurred())
o.Expect(result).Should(o.ContainSubstring(`{ "status" : "passed" }`))
})
})

})
13 changes: 7 additions & 6 deletions test/extended/images/mysql_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
o "github.com/onsi/gomega"

exutil "github.com/openshift/origin/test/extended/util"
"github.com/openshift/origin/test/extended/util/db"
testutil "github.com/openshift/origin/test/util"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
)
Expand All @@ -32,18 +33,18 @@ func CreateMySQLReplicationHelpers(c kclient.PodInterface, masterDeployment, sla
o.Expect(err).NotTo(o.HaveOccurred())

// Create MySQL helper for master
master := exutil.NewMysql(masterPod, "")
master := db.NewMysql(masterPod, "")

// Create MySQL helpers for slaves
slaves := make([]exutil.Database, len(slavePods))
for i := range slavePods {
slave := exutil.NewMysql(slavePods[i], masterPod)
slave := db.NewMysql(slavePods[i], masterPod)
slaves[i] = slave
}

helperNames, err := exutil.WaitForPods(c, exutil.ParseLabelsOrDie(fmt.Sprintf("deployment=%s", helperDeployment)), exutil.CheckPodIsRunningFn, 1, 1*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
helper := exutil.NewMysql(helperNames[0], masterPod)
helper := db.NewMysql(helperNames[0], masterPod)

return master, slaves, helper
}
Expand Down Expand Up @@ -114,20 +115,20 @@ func replicationTestFactory(oc *exutil.CLI, template string) func() {
g.By("after master is restarted by changing the Deployment Config")
err = oc.Run("env").Args("dc", "mysql-master", "MYSQL_ROOT_PASSWORD=newpass").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), master.GetPodName(), 1*time.Minute)
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), master.PodName(), 1*time.Minute)
master, _, _ = assertReplicationIsWorking("mysql-master-2", "mysql-slave-1", 1)

g.By("after master is restarted by deleting the pod")
err = oc.Run("delete").Args("pod", "-l", "deployment=mysql-master-2").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), master.GetPodName(), 1*time.Minute)
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), master.PodName(), 1*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
_, slaves, _ := assertReplicationIsWorking("mysql-master-2", "mysql-slave-1", 1)

g.By("after slave is restarted by deleting the pod")
err = oc.Run("delete").Args("pod", "-l", "deployment=mysql-slave-1").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), slaves[0].GetPodName(), 1*time.Minute)
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), slaves[0].PodName(), 1*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
assertReplicationIsWorking("mysql-master-2", "mysql-slave-1", 1)

Expand Down
13 changes: 7 additions & 6 deletions test/extended/images/postgresql_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
o "github.com/onsi/gomega"

exutil "github.com/openshift/origin/test/extended/util"
"github.com/openshift/origin/test/extended/util/db"
testutil "github.com/openshift/origin/test/util"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
)
Expand Down Expand Up @@ -45,18 +46,18 @@ func CreatePostgreSQLReplicationHelpers(c kclient.PodInterface, masterDeployment
o.Expect(err).NotTo(o.HaveOccurred())

// Create PostgreSQL helper for master
master := exutil.NewPostgreSQL(masterPod, "")
master := db.NewPostgreSQL(masterPod, "")

// Create PostgreSQL helpers for slaves
slaves := make([]exutil.Database, len(slavePods))
for i := range slavePods {
slave := exutil.NewPostgreSQL(slavePods[i], masterPod)
slave := db.NewPostgreSQL(slavePods[i], masterPod)
slaves[i] = slave
}

helperNames, err := exutil.WaitForPods(c, exutil.ParseLabelsOrDie(fmt.Sprintf("deployment=%s", helperDeployment)), exutil.CheckPodIsRunningFn, 1, 1*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
helper := exutil.NewPostgreSQL(helperNames[0], masterPod)
helper := db.NewPostgreSQL(helperNames[0], masterPod)

return master, slaves, helper
}
Expand Down Expand Up @@ -125,20 +126,20 @@ func PostgreSQLReplicationTestFactory(oc *exutil.CLI, image string) func() {
g.By("after master is restarted by changing the Deployment Config")
err = oc.Run("env").Args("dc", "postgresql-master", "POSTGRESQL_ADMIN_PASSWORD=newpass").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), master.GetPodName(), 1*time.Minute)
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), master.PodName(), 1*time.Minute)
master, _, _ = assertReplicationIsWorking("postgresql-master-2", "postgresql-slave-1", 1)

g.By("after master is restarted by deleting the pod")
err = oc.Run("delete").Args("pod", "-l", "deployment=postgresql-master-2").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), master.GetPodName(), 1*time.Minute)
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), master.PodName(), 1*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
_, slaves, _ := assertReplicationIsWorking("postgresql-master-2", "postgresql-slave-1", 1)

g.By("after slave is restarted by deleting the pod")
err = oc.Run("delete").Args("pod", "-l", "deployment=postgresql-slave-1").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), slaves[0].GetPodName(), 1*time.Minute)
err = exutil.WaitUntilPodIsGone(oc.KubeREST().Pods(oc.Namespace()), slaves[0].PodName(), 1*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
assertReplicationIsWorking("postgresql-master-2", "postgresql-slave-1", 1)

Expand Down
62 changes: 62 additions & 0 deletions test/extended/util/db/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package db

import (
"fmt"
"os/exec"
"strings"

"github.com/openshift/origin/test/extended/util"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
)

// PodConfig holds configuration for a pod.
type PodConfig struct {
Container string
Env map[string]string
}

func getPodConfig(c kclient.PodInterface, podName string) (conf *PodConfig, err error) {
pod, err := c.Get(podName)
if err != nil {
return nil, err
}
env := make(map[string]string)
for _, container := range pod.Spec.Containers {
for _, e := range container.Env {
env[e.Name] = e.Value
}
}
return &PodConfig{pod.Spec.Containers[0].Name, env}, nil
}

func firstContainerName(c kclient.PodInterface, podName string) (string, error) {
pod, err := c.Get(podName)
if err != nil {
return "", err
}

return pod.Spec.Containers[0].Name, nil
}

func isReady(oc *util.CLI, podName string, pingCommand, expectedOutput string) (bool, error) {
out, err := executeShellCommand(oc, podName, pingCommand)
ok := strings.Contains(out, expectedOutput)
if !ok {
err = fmt.Errorf("Expected output: %q but actual: %q", expectedOutput, out)
}
return ok, err
}

func executeShellCommand(oc *util.CLI, podName string, command string) (string, error) {
out, err := oc.Run("exec").Args(podName, "--", "bash", "-c", command).Output()
if err != nil {
switch err.(type) {
case *exec.ExitError:
return "", nil
default:
return "", err
}
}

return out, nil
}
59 changes: 59 additions & 0 deletions test/extended/util/db/mongodb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package db

import (
"errors"
"fmt"

"github.com/openshift/origin/test/extended/util"
)

// MongoDB is a MongoDB helper for executing commands.
type MongoDB struct {
podName string
masterPodName string
}

// NewMongoDB creates a new util.Database instance.
func NewMongoDB(podName, masterPodName string) util.Database {
if masterPodName == "" {
masterPodName = podName
}
return &MongoDB{
podName: podName,
masterPodName: masterPodName,
}
}

// PodName implements Database.
func (m MongoDB) PodName() string {
return m.podName
}

// IsReady pings the MongoDB server.
func (m MongoDB) IsReady(oc *util.CLI) (bool, error) {
return isReady(
oc,
m.podName,
`mongo --quiet --eval '{"ping", 1}'`,
"1",
)
}

// Query executes a query as an ordinary user and returns the result.
func (m MongoDB) Query(oc *util.CLI, query string) (string, error) {
return executeShellCommand(
oc,
m.podName,
fmt.Sprintf(`mongo --quiet "$MONGODB_DATABASE" --username "$MONGODB_USER" --password "$MONGODB_PASSWORD" --eval '%s'`, query),
)
}

// QueryPrivileged queries the database as a privileged user.
func (m MongoDB) QueryPrivileged(oc *util.CLI, query string) (string, error) {
return "", errors.New("not implemented")
}

// TestRemoteLogin tests wheather it is possible to remote login to hostAddress.
func (m MongoDB) TestRemoteLogin(oc *util.CLI, hostAddress string) error {
return errors.New("not implemented")
}
Loading

0 comments on commit c431122

Please sign in to comment.