github.com/chenbh/concourse/v6@v6.4.2/atc/integration/integration_suite_test.go (about)

     1  package integration_test
     2  
     3  import (
     4  	"context"
     5  	"crypto/rand"
     6  	"crypto/rsa"
     7  	"fmt"
     8  	"net/http"
     9  	"os"
    10  	"testing"
    11  	"time"
    12  
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  
    16  	"github.com/chenbh/concourse/v6/atc"
    17  	"github.com/chenbh/concourse/v6/atc/atccmd"
    18  	"github.com/chenbh/concourse/v6/atc/postgresrunner"
    19  	"github.com/chenbh/concourse/v6/go-concourse/concourse"
    20  	"github.com/concourse/flag"
    21  	"github.com/jessevdk/go-flags"
    22  	"github.com/tedsuo/ifrit"
    23  	"golang.org/x/oauth2"
    24  )
    25  
    26  var (
    27  	cmd            *atccmd.RunCommand
    28  	postgresRunner postgresrunner.Runner
    29  	dbProcess      ifrit.Process
    30  	atcProcess     ifrit.Process
    31  	atcURL         string
    32  )
    33  
    34  var _ = BeforeEach(func() {
    35  	cmd = &atccmd.RunCommand{}
    36  
    37  	// call parseArgs to populate flag defaults but ignore errors so that we can
    38  	// use the required:"true" field annotation
    39  	//
    40  	// use flags.None so that we don't print errors
    41  	parser := flags.NewParser(cmd, flags.None)
    42  	_, _ = parser.ParseArgs([]string{})
    43  
    44  	cmd.Postgres.User = "postgres"
    45  	cmd.Postgres.Database = "testdb"
    46  	cmd.Postgres.Port = 5433 + uint16(GinkgoParallelNode())
    47  	cmd.Postgres.SSLMode = "disable"
    48  	cmd.Auth.MainTeamFlags.LocalUsers = []string{"test"}
    49  	cmd.Auth.AuthFlags.LocalUsers = map[string]string{
    50  		"test":    "test",
    51  		"v-user":  "v-user",
    52  		"po-user": "po-user",
    53  		"m-user":  "m-user",
    54  		"o-user":  "o-user",
    55  	}
    56  	cmd.Auth.AuthFlags.Clients = map[string]string{
    57  		"client-id": "client-secret",
    58  	}
    59  	cmd.Server.ClientID = "client-id"
    60  	cmd.Server.ClientSecret = "client-secret"
    61  	cmd.Logger.LogLevel = "debug"
    62  	cmd.Logger.SetWriterSink(GinkgoWriter)
    63  	cmd.BindPort = 9090 + uint16(GinkgoParallelNode())
    64  	cmd.DebugBindPort = 0
    65  
    66  	signingKey, err := rsa.GenerateKey(rand.Reader, 1024)
    67  	Expect(err).ToNot(HaveOccurred())
    68  
    69  	cmd.Auth.AuthFlags.SigningKey = &flag.PrivateKey{PrivateKey: signingKey}
    70  
    71  	postgresRunner = postgresrunner.Runner{
    72  		Port: 5433 + GinkgoParallelNode(),
    73  	}
    74  	dbProcess = ifrit.Invoke(postgresRunner)
    75  	postgresRunner.CreateTestDB()
    76  
    77  	// workaround to avoid panic due to registering http handlers multiple times
    78  	http.DefaultServeMux = new(http.ServeMux)
    79  })
    80  
    81  var _ = JustBeforeEach(func() {
    82  	atcURL = fmt.Sprintf("http://localhost:%v", cmd.BindPort)
    83  
    84  	runner, err := cmd.Runner([]string{})
    85  	Expect(err).NotTo(HaveOccurred())
    86  
    87  	atcProcess = ifrit.Invoke(runner)
    88  
    89  	Eventually(func() error {
    90  		_, err := http.Get(atcURL + "/api/v1/info")
    91  		return err
    92  	}, 20*time.Second).ShouldNot(HaveOccurred())
    93  })
    94  
    95  var _ = AfterEach(func() {
    96  	atcProcess.Signal(os.Interrupt)
    97  	err := <-atcProcess.Wait()
    98  	Expect(err).NotTo(HaveOccurred())
    99  
   100  	postgresRunner.DropTestDB()
   101  
   102  	dbProcess.Signal(os.Interrupt)
   103  	err = <-dbProcess.Wait()
   104  	Expect(err).NotTo(HaveOccurred())
   105  })
   106  
   107  func TestIntegration(t *testing.T) {
   108  	RegisterFailHandler(Fail)
   109  	RunSpecs(t, "Integration Suite")
   110  }
   111  
   112  func login(atcURL, username, password string) concourse.Client {
   113  	oauth2Config := oauth2.Config{
   114  		ClientID:     "client-id",
   115  		ClientSecret: "client-secret",
   116  		Endpoint:     oauth2.Endpoint{TokenURL: atcURL + "/sky/issuer/token"},
   117  		Scopes:       []string{"openid", "federated:id"},
   118  	}
   119  
   120  	ctx := context.Background()
   121  	oauthToken, err := oauth2Config.PasswordCredentialsToken(ctx, username, password)
   122  	Expect(err).NotTo(HaveOccurred())
   123  
   124  	tokenSource := oauth2.StaticTokenSource(oauthToken)
   125  	httpClient := oauth2.NewClient(ctx, tokenSource)
   126  
   127  	return concourse.NewClient(atcURL, httpClient, false)
   128  }
   129  
   130  func setupTeam(atcURL string, team atc.Team) {
   131  	ccClient := login(atcURL, "test", "test")
   132  	createdTeam, _, _, _, err := ccClient.Team(team.Name).CreateOrUpdate(team)
   133  
   134  	Expect(err).ToNot(HaveOccurred())
   135  	Expect(createdTeam.Name).To(Equal(team.Name))
   136  	Expect(createdTeam.Auth).To(Equal(team.Auth))
   137  }
   138  
   139  func setupPipeline(atcURL, teamName string, config []byte) {
   140  	ccClient := login(atcURL, "test", "test")
   141  	_, _, _, err := ccClient.Team(teamName).CreateOrUpdatePipelineConfig("pipeline-name", "0", config, false)
   142  	Expect(err).ToNot(HaveOccurred())
   143  }