|
| 1 | +package containers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/testcontainers/testcontainers-go" |
| 7 | + "github.com/testcontainers/testcontainers-go/wait" |
| 8 | + "math/rand" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +type ExposedPort string |
| 14 | + |
| 15 | +const ( |
| 16 | + Remote ExposedPort = "remote" |
| 17 | + InProcess ExposedPort = "in-process" |
| 18 | + Launchpad ExposedPort = "launchpad" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + flagdPortRemote = "8013" |
| 23 | + flagdPortInProcess = "8015" |
| 24 | + flagdPortLaunchpad = "8016" |
| 25 | +) |
| 26 | + |
| 27 | +type flagdConfig struct { |
| 28 | + version string |
| 29 | +} |
| 30 | + |
| 31 | +type FlagdContainer struct { |
| 32 | + testcontainers.Container |
| 33 | + portRemote int |
| 34 | + portInProcess int |
| 35 | + portLaunchpad int |
| 36 | +} |
| 37 | + |
| 38 | +func (fc *FlagdContainer) GetPort(port ExposedPort) int { |
| 39 | + switch port { |
| 40 | + case Remote: |
| 41 | + return fc.portRemote |
| 42 | + case InProcess: |
| 43 | + return fc.portInProcess |
| 44 | + case Launchpad: |
| 45 | + return fc.portLaunchpad |
| 46 | + } |
| 47 | + return fc.portRemote |
| 48 | +} |
| 49 | + |
| 50 | +func NewFlagd(ctx context.Context) (*FlagdContainer, error) { |
| 51 | + version, err := readTestbedVersion() |
| 52 | + |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + c := &flagdConfig{ |
| 58 | + version: fmt.Sprintf("v%v", version), |
| 59 | + } |
| 60 | + |
| 61 | + return setupContainer(ctx, c) |
| 62 | +} |
| 63 | + |
| 64 | +func setupContainer(ctx context.Context, cfg *flagdConfig) (*FlagdContainer, error) { |
| 65 | + registry := "ghcr.io/open-feature" |
| 66 | + imgName := "flagd-testbed" |
| 67 | + |
| 68 | + fullImgName := registry + "/" + imgName + ":" + cfg.version |
| 69 | + |
| 70 | + req := testcontainers.ContainerRequest{ |
| 71 | + Image: fullImgName, |
| 72 | + Name: fmt.Sprintf("%s-%d", imgName, rand.Int()), |
| 73 | + ExposedPorts: []string{ |
| 74 | + flagdPortRemote + "/tcp", |
| 75 | + flagdPortInProcess + "/tcp", |
| 76 | + flagdPortLaunchpad + "/tcp", |
| 77 | + }, |
| 78 | + WaitingFor: wait.ForExposedPort(), |
| 79 | + Privileged: false, |
| 80 | + } |
| 81 | + |
| 82 | + c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 83 | + ContainerRequest: req, |
| 84 | + Started: true, |
| 85 | + Reuse: true, |
| 86 | + }) |
| 87 | + |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + mappedPortRemote, errRemote := c.MappedPort(ctx, flagdPortRemote) |
| 93 | + mappedPortInProcess, errInProcess := c.MappedPort(ctx, flagdPortInProcess) |
| 94 | + mappedPortLaunchpad, errLaunchpad := c.MappedPort(ctx, flagdPortLaunchpad) |
| 95 | + if errRemote != nil || errInProcess != nil || errLaunchpad != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + return &FlagdContainer{ |
| 100 | + Container: c, |
| 101 | + portRemote: mappedPortRemote.Int(), |
| 102 | + portInProcess: mappedPortInProcess.Int(), |
| 103 | + portLaunchpad: mappedPortLaunchpad.Int(), |
| 104 | + }, nil |
| 105 | +} |
| 106 | + |
| 107 | +func readTestbedVersion() (string, error) { |
| 108 | + wd, _ := os.Getwd() |
| 109 | + fileName := "../flagd-testbed/version.txt" |
| 110 | + |
| 111 | + content, err := os.ReadFile(fmt.Sprintf("%s/%s", wd, fileName)) |
| 112 | + if err != nil { |
| 113 | + fmt.Printf("Failed to read file: %s", fileName) |
| 114 | + return "", err |
| 115 | + } |
| 116 | + |
| 117 | + return strings.TrimSuffix(string(content), "\n"), nil |
| 118 | +} |
0 commit comments