github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/db/migration/auto_cert_test.go (about)

     1  package migration_test
     2  
     3  import (
     4  	"database/sql"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("Add Cert Cache Table", func() {
    11  
    12  	const postMigrationVersion = 1557237784
    13  	const preMigrationVersion = 1556724983
    14  
    15  	var (
    16  		db *sql.DB
    17  	)
    18  
    19  	Context("Up", func() {
    20  		It("successfully creates table cert_cache", func() {
    21  			db = postgresRunner.OpenDBAtVersion(postMigrationVersion)
    22  			writeAutoCert(db, "magic-domain", "iamcert", "iamnonce")
    23  			cert, err := readAutoCert(db, "magic-domain")
    24  			Expect(err).ToNot(HaveOccurred())
    25  			Expect(cert).To(Equal("iamcert"))
    26  			db.Close()
    27  		})
    28  	})
    29  
    30  	Context("Down", func() {
    31  		It("successfully drops table cert_cache", func() {
    32  			db = postgresRunner.OpenDBAtVersion(postMigrationVersion)
    33  			writeAutoCert(db, "magic-domain", "iamcert", "iamnonce")
    34  			db.Close()
    35  
    36  			db = postgresRunner.OpenDBAtVersion(preMigrationVersion)
    37  			_, err := readAutoCert(db, "magic-domain")
    38  			Expect(err).To(HaveOccurred())
    39  			db.Close()
    40  		})
    41  	})
    42  
    43  })
    44  
    45  func readAutoCert(dbConn *sql.DB, domain string) (string, error) {
    46  	var cert []byte
    47  	err := dbConn.QueryRow("SELECT cert FROM cert_cache WHERE domain = $1", domain).Scan(&cert)
    48  	return string(cert), err
    49  }
    50  
    51  func writeAutoCert(dbConn *sql.DB, domain, cert, nonce string) {
    52  	dbConn.Exec("INSERT INTO cert_cache(domain, cert, nonce) VALUES($1, $2, $3)", domain, cert, nonce)
    53  }