Skip to content

Commit

Permalink
Merge pull request #88 from mysteriumnetwork/github-ci
Browse files Browse the repository at this point in the history
Move CI to github
  • Loading branch information
Andrey M authored May 31, 2023
2 parents 8c397c9 + 5099c5d commit 252d9a2
Show file tree
Hide file tree
Showing 22 changed files with 391 additions and 298 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @soffokl @yeszhanov95 @n10ty
35 changes: 35 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Build and test

on: push

env:
GITHUB_CI: true

jobs:
run-tests:
name: Run unit tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.18
- name: Run unit tests
run: go run mage.go -v test
e2e:
name: Run e2e scenarios
runs-on: ubuntu-latest
steps:
- shell: bash
env:
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
run: |
echo "$DOCKERHUB_PASSWORD" | docker login --username "$DOCKERHUB_USERNAME" --password-stdin || true
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.18
- name: Run e2e scenarios
run: go run mage.go -v e2e

23 changes: 0 additions & 23 deletions .gitlab-ci.yml

This file was deleted.

22 changes: 22 additions & 0 deletions Dockerfile.e2e
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM golang:1.18-alpine AS builder

RUN apk add build-base
WORKDIR /go/src/github.com/mysteriumnetwork/discovery
COPY go.mod go.sum ./
RUN go mod download

# Compile application
ADD . .
RUN go run mage.go -v buildE2e

FROM alpine:3

# Install packages
RUN apk add --no-cache ca-certificates git

# Install application
COPY --from=builder /go/src/github.com/mysteriumnetwork/discovery/build/discovery /usr/bin/discovery

EXPOSE 8080

CMD ["/usr/bin/discovery"]
22 changes: 22 additions & 0 deletions Dockerfile.e2e.pricer
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM golang:1.18-alpine AS builder

RUN apk add build-base
WORKDIR /go/src/github.com/mysteriumnetwork/discovery
COPY go.mod go.sum ./
RUN go mod download

# Compile application
ADD . .
RUN go run mage.go -v buildPricerE2e

FROM alpine:3

# Install packages
RUN apk add --no-cache ca-certificates git

# Install application
COPY --from=builder /go/src/github.com/mysteriumnetwork/discovery/build/pricer /usr/bin/pricer

EXPOSE 8080

CMD ["/usr/bin/pricer"]
6 changes: 5 additions & 1 deletion ci/e2e/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,9 @@ func upDependencies() error {
}

func upApp() error {
return sh.RunV("docker-compose", "-f", e2e.DockerFile, "up", "--build", "-d", "discovery")
err := sh.RunV("docker-compose", "-f", e2e.DockerFile, "up", "--build", "-d", "discovery")
if err != nil {
return err
}
return sh.RunV("docker-compose", "-f", e2e.DockerFile, "up", "--build", "-d", "discopricer")
}
1 change: 0 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func main() {

proposalsAPI := proposal.NewAPI(
proposalService,
proposalRepo,
locationProvider,
cfg.ProposalsCacheTTL,
cfg.ProposalsCacheLimit,
Expand Down
65 changes: 38 additions & 27 deletions e2e/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,29 @@ import (
v3 "github.com/mysteriumnetwork/discovery/proposal/v3"
)

var discoveryAPI = newAPI(DiscoveryAPIurl)
var DiscoveryAPI = newDiscoveryAPI(DiscoveryAPIurl)
var PricerAPI = newPricingAPI(PricerAPIUrl)

func newAPI(basePath string) *api {
return &api{
func newDiscoveryAPI(basePath string) *discoveryAPI {
return &discoveryAPI{
basePath: basePath,
}
}

type api struct {
type discoveryAPI struct {
basePath string
}

func (a *api) LatestPrices() (latestPrices pricing.LatestPrices, err error) {
_, err = sling.New().Base(a.basePath).Get("/api/v3/prices").Receive(&latestPrices, nil)
return latestPrices, err
}

func (a *api) ListFilters(query Query) (proposals []v3.Proposal, err error) {
func (a *discoveryAPI) ListFilters(query Query) (proposals []v3.Proposal, err error) {
_, err = sling.New().Base(a.basePath).Get("/api/v3/proposals").QueryStruct(query).Receive(&proposals, nil)
return proposals, err
}

func (a *api) GetPriceConfig(token string) (config pricing.Config, err error) {
resp, err := sling.New().Base(a.basePath).Add("Authorization", "Bearer "+token).Get("/api/v3/prices/config").Receive(&config, nil)
if resp.StatusCode == 401 {
return config, errors.New(fmt.Sprint(resp.StatusCode))
}
return config, err
}

func (a *api) GetStatus() (status health.StatusResponse, err error) {
func (a *discoveryAPI) GetStatus() (status health.StatusResponse, err error) {
_, err = sling.New().Base(a.basePath).Get("/api/v3/status").Receive(&status, nil)
return status, err
}

func (a *api) UpdatePriceConfig(token string, cfg pricing.Config) (err error) {
resp, err := sling.New().Base(a.basePath).Add("Authorization", "Bearer "+token).BodyJSON(&cfg).Post("/api/v3/prices/config").Receive(nil, nil)
if resp.StatusCode == 401 || resp.StatusCode == 400 {
return errors.New(fmt.Sprint(resp.StatusCode))
}
return err
}

type Query struct {
From string `url:"from"`
ProviderID []string `url:"provider_id"`
Expand All @@ -71,3 +51,34 @@ type Query struct {
IncludeMonitoringFailed bool `url:"include_monitoring_failed"`
NATCompatibility string `url:"nat_compatibility"`
}

func newPricingAPI(basePath string) *pricerAPI {
return &pricerAPI{
basePath: basePath,
}
}

type pricerAPI struct {
basePath string
}

func (a *pricerAPI) LatestPrices() (latestPrices pricing.LatestPrices, err error) {
_, err = sling.New().Base(a.basePath).Get("/api/v3/prices").Receive(&latestPrices, nil)
return latestPrices, err
}

func (a *pricerAPI) GetPriceConfig(token string) (config pricing.Config, err error) {
resp, err := sling.New().Base(a.basePath).Add("Authorization", "Bearer "+token).Get("/api/v3/prices/config").Receive(&config, nil)
if resp.StatusCode == 401 {
return config, errors.New(fmt.Sprint(resp.StatusCode))
}
return config, err
}

func (a *pricerAPI) UpdatePriceConfig(token string, cfg pricing.Config) (err error) {
resp, err := sling.New().Base(a.basePath).Add("Authorization", "Bearer "+token).BodyJSON(&cfg).Post("/api/v3/prices/config").Receive(nil, nil)
if resp.StatusCode == 401 || resp.StatusCode == 400 {
return errors.New(fmt.Sprint(resp.StatusCode))
}
return err
}
7 changes: 4 additions & 3 deletions e2e/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package e2e

import (
"encoding/json"
"fmt"

v3 "github.com/mysteriumnetwork/discovery/proposal/v3"
"github.com/nats-io/nats.go"
"github.com/rs/zerolog/log"
)

var defaultBroker = initBroker()

func initBroker() *Broker {
broker, err := NewBroker(BrokerURL)
fmt.Println(err)
if err != nil {
log.Error().Err(err).Msg("failed to initialize broker")
}
return broker
}

Expand Down
1 change: 1 addition & 0 deletions e2e/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ const (
DockerFile = "e2e/docker-compose.yml"
DiscoveryAPIurl = "http://localhost:8080"
QualityAPIUrl = "http://localhost:8004"
PricerAPIUrl = "http://localhost:8081"
)
27 changes: 25 additions & 2 deletions e2e/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,44 @@ services:
discovery:
build:
context: ..
dockerfile: Dockerfile.e2e
ports:
- 8080:8080
environment:
- PORT=8080
- QUALITY_ORACLE_URL=http://wiremock:8080
- QUALITY_CACHE_TTL=20s
- QUALITY_CACHE_TTL=1s
- PROPOSALS_CACHE_TTL=0s
- BADGER_ADDRESS=http://wiremock:8080
- BROKER_URL=nats://broker
- LOCATION_ADDRESS=http://wiremock:8080
- UNIVERSE_JWT_SECRET=suchsecret
- REDIS_ADDRESS=redis:6379
depends_on:
- db
- broker
- redis

discopricer:
build:
context: ..
dockerfile: Dockerfile.e2e.pricer
depends_on:
- db
- wiremock
- redis
ports:
- 8081:8081
environment:
- MYSTERIUM_LOG_MODE=json
- PORT=8081
- QUALITY_ORACLE_URL=http://wiremock:8004
- QUALITY_CACHE_TTL=20s
- BADGER_ADDRESS=http://wiremock:8004
- SENTINEL_URL=http://wiremock:8004
- BROKER_URL=nats://broker.mysterium.network
- UNIVERSE_JWT_SECRET=suchsecret
- REDIS_ADDRESS=redis:6379
restart: always
redis:
image: "redis:alpine"
ports:
Expand Down
2 changes: 1 addition & 1 deletion e2e/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestHealthcheck(t *testing.T) {
status, err := discoveryAPI.GetStatus()
status, err := DiscoveryAPI.GetStatus()
assert.NoError(t, err)

assert.True(t, status.CacheOK)
Expand Down
14 changes: 7 additions & 7 deletions e2e/price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestMain(m *testing.M) {
}

func Test_LatestPrices(t *testing.T) {
prices, err := discoveryAPI.LatestPrices()
prices, err := PricerAPI.LatestPrices()
assert.NoError(t, err)
assert.NotNil(t, prices.Defaults.Current.Residential.PricePerGiB)
assert.NotNil(t, prices.Defaults.Current.Residential.PricePerHour)
Expand All @@ -46,13 +46,13 @@ func Test_LatestPrices(t *testing.T) {

func Test_GetConfig(t *testing.T) {
t.Run("rejected with bad token", func(t *testing.T) {
_, err := discoveryAPI.GetPriceConfig("tkn")
_, err := PricerAPI.GetPriceConfig("tkn")
assert.Error(t, err)
assert.Equal(t, "401", err.Error())
})

t.Run("fetches with correct token", func(t *testing.T) {
cfg, err := discoveryAPI.GetPriceConfig(validToken)
cfg, err := PricerAPI.GetPriceConfig(validToken)
assert.NoError(t, err)

bts, err := json.Marshal(cfg)
Expand All @@ -64,13 +64,13 @@ func Test_GetConfig(t *testing.T) {

func Test_PostConfig(t *testing.T) {
t.Run("rejected with bad token", func(t *testing.T) {
err := discoveryAPI.UpdatePriceConfig("tkn", pricing.Config{})
err := PricerAPI.UpdatePriceConfig("tkn", pricing.Config{})
assert.Error(t, err)
assert.Equal(t, "401", err.Error())
})

t.Run("rejected with invalid config", func(t *testing.T) {
err := discoveryAPI.UpdatePriceConfig(validToken, pricing.Config{})
err := PricerAPI.UpdatePriceConfig(validToken, pricing.Config{})
assert.Error(t, err)
assert.Equal(t, "400", err.Error())
})
Expand All @@ -81,10 +81,10 @@ func Test_PostConfig(t *testing.T) {
assert.NoError(t, err)
toSend.BasePrices.Other.PricePerGiB = 11

err = discoveryAPI.UpdatePriceConfig(validToken, toSend)
err = PricerAPI.UpdatePriceConfig(validToken, toSend)
assert.NoError(t, err)

cfg, err := discoveryAPI.GetPriceConfig(validToken)
cfg, err := PricerAPI.GetPriceConfig(validToken)
assert.NoError(t, err)
assert.EqualValues(t, toSend, cfg)
})
Expand Down
Loading

0 comments on commit 252d9a2

Please sign in to comment.