Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from Bidaya0/develop-add-unittest
Browse files Browse the repository at this point in the history
Develop add unittest
  • Loading branch information
Bidaya0 committed Aug 24, 2022
2 parents 8dfa7b8 + 924bc5b commit 5cb7114
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 10 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/gotest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Go unittest"

on:
push:
branches: [ "main" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "main" ]

jobs:
unittest-check:
name: unittest-check
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: '>=1.19.0'
- run: |
go test -v ./...
35 changes: 29 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package cmd
import (
"fmt"
"github.com/bidaya0/gbatect/converter"
batecttypes "github.com/bidaya0/gbatect/types"
"github.com/compose-spec/compose-go/loader"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"os"
)

Expand All @@ -25,18 +28,38 @@ var convertCmd = &cobra.Command{
gbatect take the docker-compose.yml and translates it to batect.yml.
`,
Run: func(cmd *cobra.Command, args []string) {
//if len(args) > 0{
// filepath := args[0]
//}

if fromfile != "" {
result := converter.ReadAndConvert(fromfile)
dockercomposefile, err := os.ReadFile(fromfile)
if err != nil {
fmt.Printf("error: %v", err)
}
k1, err := loader.ParseYAML(dockercomposefile)
if err != nil {
fmt.Printf("error: %v", err)
}
tmpk := k1["services"]
tmp3, _ := tmpk.(map[string]interface{})
services, err := converter.LoadServices(tmp3)
containers, err := converter.TransServicesToContainer(services)
var f1 = batecttypes.BatectConfig{
Containers: containers,
}
batectyaml, err := yaml.Marshal(&f1)
if err != nil {
fmt.Printf("error: %v", err)
}

if err != nil {
fmt.Printf("error: %v", err)
}
if tofile != "" {
err := os.WriteFile(tofile, result, 0644)
err := os.WriteFile(tofile, batectyaml, 0644)
if err != nil {
fmt.Printf("%v", err)
}
} else {
fmt.Printf("%v", string(result))
fmt.Printf("%v", string(batectyaml))
}
}
},
Expand Down
9 changes: 5 additions & 4 deletions converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ func TransServicesToContainer(servicesconfigs []composetypes.ServiceConfig) (bat
return containers, nil
}

func ReadAndConvert(sourceFilePath string) []byte {
func ReadAndConvert(sourceFilePath string) ([]byte, error) {
dockercomposefile, err := os.ReadFile(sourceFilePath)
if err != nil {
fmt.Printf("error: %v", err)
return nil, err
}
k1, err := loader.ParseYAML(dockercomposefile)
if err != nil {
fmt.Printf("error: %v", err)
return nil, err
}
tmpk := k1["services"]
tmp3, _ := tmpk.(map[string]interface{})
Expand All @@ -102,7 +103,7 @@ func ReadAndConvert(sourceFilePath string) []byte {
}
batectyaml, err := yaml.Marshal(&f1)
if err != nil {
fmt.Printf("error: %v", err)
return nil, err
}
return batectyaml
return batectyaml, nil
}
89 changes: 89 additions & 0 deletions converter/converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package converter

import (
"github.com/compose-spec/compose-go/loader"
"testing"
)

var dockercomposefile = []byte(`version: '3'
services:
jobaggrv-mysql:
image: mysql:8-debian
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
environment:
MYSQL_ROOT_PASSWORD: jobaggrv
MYSQL_USER: jobaggrv
MYSQL_PASSWORD: jobaggrv
MYSQL_DATABASE: jobaggrv
ports:
- 33060:3306
cap_add:
- ALL
cap_drop:
- NET_ADMIN
- SYS_ADMIN
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 1m30s
timeout: 10s
retries: 3
start_period: 40s
jobaggrv-scrapy:
build:
dockerfile: ./Dockerfile
context: .
target: prod
args:
GIT_COMMIT: cdc3b19
command: echo "I'm running ${COMPOSE_PROJECT_NAME}"
devices:
- "/dev/ttyUSB0:/dev/ttyUSB0"
- "/dev/sda:/dev/xvda:rwm"
init: true
shm_size: 1024kb
working_dir: /opt
jobaggrv-api:
build:
dockerfile: ./Dockerfile
context: .
args:
- GIT_COMMIT=cdc3b19
entrypoint: gunicorn --worker-class gevent -w 4 --worker-connections 1000 -b 0.0.0.0:8000 'jobaggrv_api.app:app'
ports:
- 8000:8000
depends_on:
- jobaggrv-scrapy
logging:
driver: syslog
options:
syslog-address: "tcp://192.168.0.42:123"
`)

func TestLoadServices(t *testing.T) {
k1, _ := loader.ParseYAML(dockercomposefile)
tmpk := k1["services"]
tmp3, _ := tmpk.(map[string]interface{})
_, err := LoadServices(tmp3)
if err != nil {
t.Error("Expected success, got ", err)
}
return
}

func TestTransServicesToContainer(t *testing.T) {
k1, _ := loader.ParseYAML(dockercomposefile)
tmpk := k1["services"]
tmp3, _ := tmpk.(map[string]interface{})
services, err := LoadServices(tmp3)
_, err = TransServicesToContainer(services)
if err != nil {
t.Error("Expected success, got ", err)
}
return
}

0 comments on commit 5cb7114

Please sign in to comment.