rewrote ci pipeline to include test enviroment database
This commit is contained in:
parent
30fe38a2e7
commit
fb421591e9
@ -1,11 +1,46 @@
|
||||
backend:
|
||||
image: golang:1.20
|
||||
image: docker:latest
|
||||
|
||||
# Define the GitLab CI/CD pipeline
|
||||
stages:
|
||||
- build
|
||||
- test
|
||||
|
||||
variables:
|
||||
POSTGRES_DB: test
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: ""
|
||||
POSTGRES_HOST_AUTH_METHOD: trust
|
||||
|
||||
# Define the job for building the Go backend application
|
||||
build:
|
||||
stage: build
|
||||
image: golang:1.17.4-alpine3.14
|
||||
script:
|
||||
- cd src/backend/cmd
|
||||
- go test
|
||||
- go mod download
|
||||
- go build -o app
|
||||
|
||||
frontend:
|
||||
image: mcr.microsoft.com/dotnet/sdk:6.0
|
||||
# Define the job for running the tests
|
||||
test:
|
||||
stage: test
|
||||
image: golang:1.17.4-alpine3.14
|
||||
services:
|
||||
- postgres:latest
|
||||
variables:
|
||||
Host: postgres
|
||||
User: $POSTGRES_USER
|
||||
Password: $POSTGRES_PASSWORD
|
||||
Database: $POSTGRES_DB
|
||||
POSTGRES_HOST_AUTH_METHOD: trust
|
||||
before_script:
|
||||
- apk add --no-cache postgresql-client
|
||||
- apk add --no-cache gcc musl-dev
|
||||
- psql -U "$POSTGRES_USER" -h postgres < ./src/init.sql
|
||||
- cd src/backend
|
||||
- go mod download
|
||||
# set env variables
|
||||
- export DB_USER=$POSTGRES_USER
|
||||
- export DB_PASSWORD=$POSTGRES_PASSWORD
|
||||
- export DB_NAME=$POSTGRES_DB
|
||||
script:
|
||||
- cd src/PanoptesFrontend
|
||||
- dotnet test
|
||||
- go test -v ./...
|
||||
|
||||
@ -35,7 +35,7 @@
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
var response = await httpService.GetAsync<Schema>("http://localhost:10000/localhost/schema");
|
||||
var response = await httpService.GetAsync<Schema>("http://localhost:10000/localhost:8080/schema");
|
||||
data = response.components;
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@ -9,22 +10,15 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"gitlab.computing.dcu.ie/murphg62/2023-ca400-murphg62-byrnm257/src/backend/pkg/db"
|
||||
_ "golang.org/x/mod/module"
|
||||
)
|
||||
|
||||
|
||||
|
||||
type Schema struct {
|
||||
Name string `json:"Name"`
|
||||
Type int `json:"Type"`
|
||||
}
|
||||
|
||||
type Container struct {
|
||||
Id string `json:"Id`
|
||||
Name string `json:"Name"`
|
||||
port string `json:"Port"`
|
||||
}
|
||||
|
||||
type Module struct {
|
||||
Name string `json:"Name"`
|
||||
@ -32,27 +26,22 @@ type Module struct {
|
||||
}
|
||||
|
||||
|
||||
|
||||
func homePage(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "Welcome to the home page!")
|
||||
}
|
||||
|
||||
func getSchema(w http.ResponseWriter, r *http.Request){
|
||||
func GetSchema(w http.ResponseWriter, r *http.Request){
|
||||
vars := mux.Vars(r)
|
||||
key := vars["container"]
|
||||
|
||||
|
||||
requestURL := fmt.Sprintf("http://%s:8080/schema", key)
|
||||
requestURL := fmt.Sprintf("http://%s/schema", key)
|
||||
resp, err := http.Get(requestURL)
|
||||
if err != nil {
|
||||
log.Fatal("Request to container failed: %v", err)
|
||||
log.Fatalf("Request to container failed: %v", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to print Body: %v", err)
|
||||
log.Fatalf("Failed to print Body: %v", err)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@ -60,7 +49,7 @@ func getSchema(w http.ResponseWriter, r *http.Request){
|
||||
w.Write(body)
|
||||
}
|
||||
|
||||
func getStats(w http.ResponseWriter, r *http.Request){
|
||||
func GetStats(w http.ResponseWriter, r *http.Request){
|
||||
vars := mux.Vars(r)
|
||||
key := vars["container"]
|
||||
|
||||
@ -68,14 +57,14 @@ func getStats(w http.ResponseWriter, r *http.Request){
|
||||
requestURL := fmt.Sprintf("http://%s:8080/stats", key)
|
||||
resp, err := http.Get(requestURL)
|
||||
if err != nil {
|
||||
log.Fatal("Request to container failed: %v", err)
|
||||
log.Fatalf("Request to container failed: %v", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to print Body: %v", err)
|
||||
log.Fatalf("Failed to print Body: %v", err)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@ -84,8 +73,8 @@ func getStats(w http.ResponseWriter, r *http.Request){
|
||||
}
|
||||
|
||||
|
||||
func getModules(db *sql.DB) http.HandlerFunc{
|
||||
return func(w http.ResponseWriter, r *http.Request){
|
||||
func GetModules(db *sql.DB) http.HandlerFunc{
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" {
|
||||
var Modules []Module
|
||||
rows, err := db.Query("Select name, container_name FROM Module")
|
||||
@ -105,18 +94,16 @@ func getModules(db *sql.DB) http.HandlerFunc{
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(Modules); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleRequests(DB *sql.DB) {
|
||||
func HandleRequests(DB *sql.DB) {
|
||||
myRouter := mux.NewRouter().StrictSlash(true)
|
||||
myRouter.HandleFunc("/", homePage)
|
||||
myRouter.HandleFunc("/{container}/schema", getSchema)
|
||||
myRouter.HandleFunc("/{container}/stats", getStats)
|
||||
myRouter.HandleFunc("/modules", getModules(DB))
|
||||
myRouter.HandleFunc("/api/{container}/schema", GetSchema)
|
||||
myRouter.HandleFunc("/api/{container}/stats", GetStats)
|
||||
myRouter.HandleFunc("/api/modules", GetModules(DB))
|
||||
|
||||
log.Fatal(http.ListenAndServe(":10000", myRouter))
|
||||
|
||||
@ -124,8 +111,20 @@ func handleRequests(DB *sql.DB) {
|
||||
|
||||
|
||||
|
||||
// placeholder, replace with own
|
||||
const (
|
||||
host = "localhost"
|
||||
port = 5432
|
||||
user = "postgres"
|
||||
password = "Gcvy4266"
|
||||
dbname = "Panoptes"
|
||||
)
|
||||
|
||||
func main() {
|
||||
DB := db.ConnectDB()
|
||||
handleRequests(DB)
|
||||
DB, err := db.ConnectDB(host, port, user, password, dbname)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
HandleRequests(DB)
|
||||
db.CloseDB(DB)
|
||||
}
|
||||
@ -3,7 +3,15 @@ module gitlab.computing.dcu.ie/murphg62/2023-ca400-murphg62-byrnm257/src/backend
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0
|
||||
github.com/gorilla/mux v1.8.0
|
||||
github.com/lib/pq v1.10.8
|
||||
github.com/stretchr/testify v1.8.2
|
||||
golang.org/x/mod v0.8.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
@ -1,6 +1,25 @@
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
github.com/lib/pq v1.10.8 h1:3fdt97i/cwSU83+E0hZTC/Xpc9mTZxc6UWSCRcSbxiE=
|
||||
github.com/lib/pq v1.10.8/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@ -6,16 +6,9 @@ import (
|
||||
|
||||
)
|
||||
|
||||
// placeholder, replace with own
|
||||
const (
|
||||
host = "localhost"
|
||||
port = 5432
|
||||
user = "postgres"
|
||||
password = "Gcvy4266"
|
||||
dbname = "Panoptes"
|
||||
)
|
||||
|
||||
func ConnectDB() *sql.DB {
|
||||
|
||||
func ConnectDB(host string, port int, user string, password string, dbname string) (*sql.DB, error) {
|
||||
|
||||
psql := fmt.Sprintf("host=%s port=%d user=%s "+ "password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
|
||||
|
||||
@ -23,14 +16,14 @@ func ConnectDB() *sql.DB {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = db.Ping()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("Successfully connected to db!")
|
||||
return db
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func CloseDB(db *sql.DB){
|
||||
defer db.Close()
|
||||
func CloseDB(db *sql.DB) error{
|
||||
err := db.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
47
src/backend/pkg/db/db_test/db_test.go
Normal file
47
src/backend/pkg/db/db_test/db_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package db_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"gitlab.computing.dcu.ie/murphg62/2023-ca400-murphg62-byrnm257/src/backend/pkg/db"
|
||||
"os"
|
||||
)
|
||||
|
||||
func TestDbConnect(t *testing.T){
|
||||
|
||||
host := "postgres"
|
||||
port := 5432
|
||||
user := os.Getenv("POSTGRES_USER")
|
||||
password := os.Getenv("POSTGRES_PASSWORD")
|
||||
dbname := os.Getenv("POSTGRES_DB")
|
||||
|
||||
|
||||
DB, err := db.ConnectDB(host, port, user, password, dbname);
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Failed to connect to database: %v", err)
|
||||
}
|
||||
|
||||
err = DB.Ping()
|
||||
if err != nil {
|
||||
t.Errorf("Failed to ping the database: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDbDisconnect(t *testing.T){
|
||||
|
||||
host := "postgres"
|
||||
port := 5432
|
||||
user := os.Getenv("POSTGRES_USER")
|
||||
password := os.Getenv("POSTGRES_PASSWORD")
|
||||
dbname := os.Getenv("POSTGRES_DB")
|
||||
|
||||
DB, err := db.ConnectDB(host, port, user, password, dbname);
|
||||
if err!= nil {
|
||||
t.Errorf("Failed to connect to database: %v", err)
|
||||
}
|
||||
|
||||
err = db.CloseDB(DB)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to disconnect from the database: %v", err)
|
||||
}
|
||||
}
|
||||
11
src/init.sql
Normal file
11
src/init.sql
Normal file
@ -0,0 +1,11 @@
|
||||
CREATE TABLE module (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
container_name TEXT NOT NULL
|
||||
);
|
||||
|
||||
|
||||
INSERT INTO
|
||||
module (name, container_name)
|
||||
VALUES
|
||||
('Module A', 'Container A') ('Module B', 'Container B') ('Module C', 'Container C')
|
||||
Loading…
x
Reference in New Issue
Block a user