Пример #1
0
func connectPG() {
	var db *sqlx.DB
	c, err := dockertest.ConnectToPostgreSQL(15, time.Second, func(url string) bool {
		var err error
		db, err = sqlx.Open("postgres", url)
		if err != nil {
			log.Printf("Got error in postgres connector: %s", err)
			return false
		}
		return db.Ping() == nil
	})

	if err != nil {
		log.Fatalf("Could not connect to database: %s", err)
	}

	containers = append(containers, c)
	s := NewSQLManager(db, nil)

	if err = s.CreateSchemas(); err != nil {
		log.Fatalf("Could not create postgres schema: %v", err)
	}

	managers["postgres"] = s
}
Пример #2
0
func TestMain(m *testing.M) {
	if c, err := dockertest.ConnectToPostgreSQL(15, time.Second, func(url string) bool {
		db, err := sql.Open("postgres", url)
		if err != nil {
			log.Printf("Could not connect to database because %s", err)
			return false
		} else if err := db.Ping(); err != nil {
			log.Printf("Could not ping database because %s", err)
			return false
		}

		// Database is now available, let's continue!
		os.Setenv("DATABASE_URL", url)
		if env.Getenv("DATABASE_URL", "") != url {
			log.Fatalf("Could not set DATABASE_URL environment variable: %s != %s", env.Getenv("DATABASE_URL", ""), url)
			return false
		}
		return true
	}); err != nil {
		log.Fatalf("Could not connect to database: %s", err)
	} else {
		defer c.KillRemove()
	}
	os.Exit(m.Run())
}
Пример #3
0
func TestMain(m *testing.M) {
	var db *sqlx.DB
	var err error
	var c dockertest.ContainerID
	if c, err = dockertest.ConnectToPostgreSQL(15, time.Second, func(url string) bool {
		var err error
		db, err = sqlx.Open("postgres", url)
		if err != nil {
			return false
		}
		return db.Ping() == nil
	}); err != nil {
		log.Fatalf("Could not connect to database: %s", err)
	}

	s = &PostgresStore{DB: db}
	if err := s.CreateSchemas(); err != nil {
		log.Fatalf("Could not set up schemas: %v", err)
	}
	if err := s.CreateSchemas(); err != nil {
		log.Fatalf("Schema did fail on second time: %v", err)
	}

	result := m.Run()
	c.KillRemove()
	os.Exit(result)
}
Пример #4
0
func prepareTestContainer(t *testing.T, s logical.Storage, b logical.Backend) (cid dockertest.ContainerID, retURL string) {
	if os.Getenv("PG_URL") != "" {
		return "", os.Getenv("PG_URL")
	}

	// Without this the checks for whether the container has started seem to
	// never actually pass. There's really no reason to expose the test
	// containers, so don't.
	dockertest.BindDockerToLocalhost = "yep"

	testImagePull.Do(func() {
		dockertest.Pull("postgres")
	})

	cid, connErr := dockertest.ConnectToPostgreSQL(60, 500*time.Millisecond, func(connURL string) bool {
		// This will cause a validation to run
		resp, err := b.HandleRequest(&logical.Request{
			Storage:   s,
			Operation: logical.UpdateOperation,
			Path:      "config/connection",
			Data: map[string]interface{}{
				"connection_url": connURL,
			},
		})
		if err != nil || (resp != nil && resp.IsError()) {
			// It's likely not up and running yet, so return false and try again
			return false
		}
		if resp == nil {
			t.Fatal("expected warning")
		}

		retURL = connURL
		return true
	})

	if connErr != nil {
		t.Fatalf("could not connect to database: %v", connErr)
	}

	return
}